Skip to content

Commit 8b0773f

Browse files
committed
DEPR: deprecate raise_on_error in .where/.mask in favor of errors=
closes #14968
1 parent 2310faa commit 8b0773f

File tree

12 files changed

+114
-59
lines changed

12 files changed

+114
-59
lines changed

Diff for: doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ Deprecations
575575
- :func:`DataFrame.as_blocks` is deprecated, as this is exposing the internal implementation (:issue:`17302`)
576576
- ``pd.TimeGrouper`` is deprecated in favor of :class:`pandas.Grouper` (:issue:`16747`)
577577
- ``cdate_range`` has been deprecated in favor of :func:`bdate_range`, which has gained ``weekmask`` and ``holidays`` parameters for building custom frequency date ranges. See the :ref:`documentation <timeseries.custom-freq-ranges>` for more details (:issue:`17596`)
578+
- ``raise_on_error`` parameter to :func:`Series.where`, :func:`Series.mask`, :func:`DataFrame.where`, :func:`DataFrame.mask` is deprecated, in favor of ``errors=`` (:issue:`14968`)
578579

579580
.. _whatsnew_0210.deprecations.argmin_min:
580581

Diff for: pandas/core/computation/expressions.py

+22-21
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def set_numexpr_threads(n=None):
5656
ne.set_num_threads(n)
5757

5858

59-
def _evaluate_standard(op, op_str, a, b, raise_on_error=True, **eval_kwargs):
59+
def _evaluate_standard(op, op_str, a, b, errors='raise', **eval_kwargs):
6060
""" standard evaluation """
6161
if _TEST_MODE:
6262
_store_test_result(False)
@@ -89,7 +89,7 @@ def _can_use_numexpr(op, op_str, a, b, dtype_check):
8989
return False
9090

9191

92-
def _evaluate_numexpr(op, op_str, a, b, raise_on_error=False, truediv=True,
92+
def _evaluate_numexpr(op, op_str, a, b, errors='ignore', truediv=True,
9393
reversed=False, **eval_kwargs):
9494
result = None
9595

@@ -112,24 +112,24 @@ def _evaluate_numexpr(op, op_str, a, b, raise_on_error=False, truediv=True,
112112
if 'unknown type object' in str(detail):
113113
pass
114114
except Exception as detail:
115-
if raise_on_error:
115+
if errors == 'raise':
116116
raise
117117

118118
if _TEST_MODE:
119119
_store_test_result(result is not None)
120120

121121
if result is None:
122-
result = _evaluate_standard(op, op_str, a, b, raise_on_error)
122+
result = _evaluate_standard(op, op_str, a, b, errors=errors)
123123

124124
return result
125125

126126

127-
def _where_standard(cond, a, b, raise_on_error=True):
127+
def _where_standard(cond, a, b, errors='raise'):
128128
return np.where(_values_from_object(cond), _values_from_object(a),
129129
_values_from_object(b))
130130

131131

132-
def _where_numexpr(cond, a, b, raise_on_error=False):
132+
def _where_numexpr(cond, a, b, errors='ignore'):
133133
result = None
134134

135135
if _can_use_numexpr(None, 'where', a, b, 'where'):
@@ -147,11 +147,11 @@ def _where_numexpr(cond, a, b, raise_on_error=False):
147147
if 'unknown type object' in str(detail):
148148
pass
149149
except Exception as detail:
150-
if raise_on_error:
150+
if errors == 'raise':
151151
raise TypeError(str(detail))
152152

153153
if result is None:
154-
result = _where_standard(cond, a, b, raise_on_error)
154+
result = _where_standard(cond, a, b, errors=errors)
155155

156156
return result
157157

@@ -189,7 +189,7 @@ def _bool_arith_check(op_str, a, b, not_allowed=frozenset(('/', '//', '**')),
189189
return True
190190

191191

192-
def evaluate(op, op_str, a, b, raise_on_error=False, use_numexpr=True,
192+
def evaluate(op, op_str, a, b, errors='ignore', use_numexpr=True,
193193
**eval_kwargs):
194194
""" evaluate and return the expression of the op on a and b
195195
@@ -200,19 +200,19 @@ def evaluate(op, op_str, a, b, raise_on_error=False, use_numexpr=True,
200200
op_str: the string version of the op
201201
a : left operand
202202
b : right operand
203-
raise_on_error : pass the error to the higher level if indicated
204-
(default is False), otherwise evaluate the op with and
205-
return the results
203+
errors : str, {'raise', 'ignore'}, default 'ignore'
204+
'raise' : pass the error to the higher level
205+
'ignore' : evaluate the op with and return the results
206206
use_numexpr : whether to try to use numexpr (default True)
207207
"""
208+
assert errors in ['raise', 'ignore']
208209
use_numexpr = use_numexpr and _bool_arith_check(op_str, a, b)
209210
if use_numexpr:
210-
return _evaluate(op, op_str, a, b, raise_on_error=raise_on_error,
211-
**eval_kwargs)
212-
return _evaluate_standard(op, op_str, a, b, raise_on_error=raise_on_error)
211+
return _evaluate(op, op_str, a, b, errors=errors, **eval_kwargs)
212+
return _evaluate_standard(op, op_str, a, b, errors=errors)
213213

214214

215-
def where(cond, a, b, raise_on_error=False, use_numexpr=True):
215+
def where(cond, a, b, errors='ignore', use_numexpr=True):
216216
""" evaluate the where condition cond on a and b
217217
218218
Parameters
@@ -221,15 +221,16 @@ def where(cond, a, b, raise_on_error=False, use_numexpr=True):
221221
cond : a boolean array
222222
a : return if cond is True
223223
b : return if cond is False
224-
raise_on_error : pass the error to the higher level if indicated
225-
(default is False), otherwise evaluate the op with and
226-
return the results
224+
errors : str, {'raise', 'ignore'}, default 'ignore'
225+
'raise' : pass the error to the higher level
226+
'ignore' : evaluate the op with and return the results
227227
use_numexpr : whether to try to use numexpr (default True)
228228
"""
229229

230+
assert errors in ['raise', 'ignore']
230231
if use_numexpr:
231-
return _where(cond, a, b, raise_on_error=raise_on_error)
232-
return _where_standard(cond, a, b, raise_on_error=raise_on_error)
232+
return _where(cond, a, b, errors=errors)
233+
return _where_standard(cond, a, b, errors=errors)
233234

234235

235236
def set_test_mode(v=True):

Diff for: pandas/core/frame.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -3832,9 +3832,9 @@ def _combine_match_columns(self, other, func, level=None,
38323832
try_cast=try_cast)
38333833
return self._constructor(new_data)
38343834

3835-
def _combine_const(self, other, func, raise_on_error=True, try_cast=True):
3835+
def _combine_const(self, other, func, errors='raise', try_cast=True):
38363836
new_data = self._data.eval(func=func, other=other,
3837-
raise_on_error=raise_on_error,
3837+
errors=errors,
38383838
try_cast=try_cast)
38393839
return self._constructor(new_data)
38403840

@@ -4006,7 +4006,7 @@ def combiner(x, y, needs_i8_conversion=False):
40064006
mask = isna(x_values)
40074007

40084008
return expressions.where(mask, y_values, x_values,
4009-
raise_on_error=True)
4009+
errors='raise')
40104010

40114011
return self.combine(other, combiner, overwrite=False)
40124012

@@ -4062,7 +4062,7 @@ def update(self, other, join='left', overwrite=True, filter_func=None,
40624062
continue
40634063

40644064
self[col] = expressions.where(mask, this, that,
4065-
raise_on_error=True)
4065+
errors='raise')
40664066

40674067
# ----------------------------------------------------------------------
40684068
# Misc methods

Diff for: pandas/core/generic.py

+33-7
Original file line numberDiff line numberDiff line change
@@ -5748,7 +5748,7 @@ def _align_series(self, other, join='outer', axis=None, level=None,
57485748
return left.__finalize__(self), right.__finalize__(other)
57495749

57505750
def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
5751-
try_cast=False, raise_on_error=True):
5751+
errors='raise', try_cast=False):
57525752
"""
57535753
Equivalent to public method `where`, except that `other` is not
57545754
applied as a function even if callable. Used in __setitem__.
@@ -5877,7 +5877,7 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
58775877

58785878
else:
58795879
new_data = self._data.where(other=other, cond=cond, align=align,
5880-
raise_on_error=raise_on_error,
5880+
errors=errors,
58815881
try_cast=try_cast, axis=block_axis,
58825882
transpose=self._AXIS_REVERSED)
58835883

@@ -5914,12 +5914,18 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
59145914
Whether to perform the operation in place on the data
59155915
axis : alignment axis if needed, default None
59165916
level : alignment level if needed, default None
5917+
errors : str, {'raise', 'ignore'}, default 'raise'
5918+
- ``raise`` : allow exceptions to be raised
5919+
- ``ignore`` : suppress exceptions. On error return original object
5920+
59175921
try_cast : boolean, default False
59185922
try to cast the result back to the input type (if possible),
59195923
raise_on_error : boolean, default True
59205924
Whether to raise on invalid data types (e.g. trying to where on
59215925
strings)
59225926
5927+
.. deprecated:: 0.21.0
5928+
59235929
Returns
59245930
-------
59255931
wh : same type as caller
@@ -5995,24 +6001,44 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
59956001
cond_rev="False", name='where',
59966002
name_other='mask'))
59976003
def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
5998-
try_cast=False, raise_on_error=True):
6004+
errors='raise', try_cast=False, raise_on_error=None):
6005+
6006+
if raise_on_error is not None:
6007+
warnings.warn(
6008+
"raise_on_error is deprecated in favor of errors='raise'",
6009+
FutureWarning, stacklevel=2)
6010+
6011+
if raise_on_error:
6012+
errors = 'raise'
6013+
else:
6014+
errors = 'ignore'
59996015

60006016
other = com._apply_if_callable(other, self)
6001-
return self._where(cond, other, inplace, axis, level, try_cast,
6002-
raise_on_error)
6017+
return self._where(cond, other, inplace, axis, level,
6018+
errors=errors, try_cast=try_cast)
60036019

60046020
@Appender(_shared_docs['where'] % dict(_shared_doc_kwargs, cond="False",
60056021
cond_rev="True", name='mask',
60066022
name_other='where'))
60076023
def mask(self, cond, other=np.nan, inplace=False, axis=None, level=None,
6008-
try_cast=False, raise_on_error=True):
6024+
errors='raise', try_cast=False, raise_on_error=None):
6025+
6026+
if raise_on_error is not None:
6027+
warnings.warn(
6028+
"raise_on_error is deprecated in favor of errors='raise'",
6029+
FutureWarning, stacklevel=2)
6030+
6031+
if raise_on_error:
6032+
errors = 'raise'
6033+
else:
6034+
errors = 'ignore'
60096035

60106036
inplace = validate_bool_kwarg(inplace, 'inplace')
60116037
cond = com._apply_if_callable(cond, self)
60126038

60136039
return self.where(~cond, other=other, inplace=inplace, axis=axis,
60146040
level=level, try_cast=try_cast,
6015-
raise_on_error=raise_on_error)
6041+
errors=errors)
60166042

60176043
_shared_docs['shift'] = ("""
60186044
Shift index by desired number of periods with an optional time freq

Diff for: pandas/core/internals.py

+31-16
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,20 @@ def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
531531
**kwargs)
532532

533533
def _astype(self, dtype, copy=False, errors='raise', values=None,
534-
klass=None, mgr=None, raise_on_error=False, **kwargs):
534+
klass=None, mgr=None, **kwargs):
535535
"""
536-
Coerce to the new type (if copy=True, return a new copy)
537-
raise on an except if raise == True
536+
Coerce to the new type
537+
538+
dtype : str, dtype convertible
539+
copy : boolean, default False
540+
copy if indicated
541+
errors : str, {'raise', 'ignore'}, default 'ignore'
542+
- ``raise`` : allow exceptions to be raised
543+
- ``ignore`` : suppress exceptions. On error return original object
544+
545+
values :
546+
klass :
547+
mgr :
538548
"""
539549
errors_legal_values = ('raise', 'ignore')
540550

@@ -1239,16 +1249,18 @@ def shift(self, periods, axis=0, mgr=None):
12391249

12401250
return [self.make_block(new_values, fastpath=True)]
12411251

1242-
def eval(self, func, other, raise_on_error=True, try_cast=False, mgr=None):
1252+
def eval(self, func, other, errors='raise', try_cast=False, mgr=None):
12431253
"""
12441254
evaluate the block; return result block from the result
12451255
12461256
Parameters
12471257
----------
12481258
func : how to combine self, other
12491259
other : a ndarray/object
1250-
raise_on_error : if True, raise when I can't perform the function,
1251-
False by default (and just return the data that we had coming in)
1260+
errors : str, {'raise', 'ignore'}, default 'raise'
1261+
- ``raise`` : allow exceptions to be raised
1262+
- ``ignore`` : suppress exceptions. On error return original object
1263+
12521264
try_cast : try casting the results to the input type
12531265
12541266
Returns
@@ -1286,7 +1298,7 @@ def eval(self, func, other, raise_on_error=True, try_cast=False, mgr=None):
12861298
except TypeError:
12871299
block = self.coerce_to_target_dtype(orig_other)
12881300
return block.eval(func, orig_other,
1289-
raise_on_error=raise_on_error,
1301+
errors=errors,
12901302
try_cast=try_cast, mgr=mgr)
12911303

12921304
# get the result, may need to transpose the other
@@ -1328,7 +1340,7 @@ def get_result(other):
13281340
# error handler if we have an issue operating with the function
13291341
def handle_error():
13301342

1331-
if raise_on_error:
1343+
if errors == 'raise':
13321344
# The 'detail' variable is defined in outer scope.
13331345
raise TypeError('Could not operate %s with block values %s' %
13341346
(repr(other), str(detail))) # noqa
@@ -1374,7 +1386,7 @@ def handle_error():
13741386
result = _block_shape(result, ndim=self.ndim)
13751387
return [self.make_block(result, fastpath=True, )]
13761388

1377-
def where(self, other, cond, align=True, raise_on_error=True,
1389+
def where(self, other, cond, align=True, errors='raise',
13781390
try_cast=False, axis=0, transpose=False, mgr=None):
13791391
"""
13801392
evaluate the block; return result block(s) from the result
@@ -1384,8 +1396,10 @@ def where(self, other, cond, align=True, raise_on_error=True,
13841396
other : a ndarray/object
13851397
cond : the condition to respect
13861398
align : boolean, perform alignment on other/cond
1387-
raise_on_error : if True, raise when I can't perform the function,
1388-
False by default (and just return the data that we had coming in)
1399+
errors : str, {'raise', 'ignore'}, default 'raise'
1400+
- ``raise`` : allow exceptions to be raised
1401+
- ``ignore`` : suppress exceptions. On error return original object
1402+
13891403
axis : int
13901404
transpose : boolean
13911405
Set to True if self is stored with axes reversed
@@ -1395,6 +1409,7 @@ def where(self, other, cond, align=True, raise_on_error=True,
13951409
a new block(s), the result of the func
13961410
"""
13971411
import pandas.core.computation.expressions as expressions
1412+
assert errors in ['raise', 'ignore']
13981413

13991414
values = self.values
14001415
orig_other = other
@@ -1427,9 +1442,9 @@ def func(cond, values, other):
14271442

14281443
try:
14291444
return self._try_coerce_result(expressions.where(
1430-
cond, values, other, raise_on_error=True))
1445+
cond, values, other, errors='raise'))
14311446
except Exception as detail:
1432-
if raise_on_error:
1447+
if errors == 'raise':
14331448
raise TypeError('Could not operate [%s] with block values '
14341449
'[%s]' % (repr(other), str(detail)))
14351450
else:
@@ -1445,10 +1460,10 @@ def func(cond, values, other):
14451460
except TypeError:
14461461

14471462
# we cannot coerce, return a compat dtype
1448-
# we are explicity ignoring raise_on_error here
1463+
# we are explicity ignoring errors
14491464
block = self.coerce_to_target_dtype(other)
14501465
blocks = block.where(orig_other, cond, align=align,
1451-
raise_on_error=raise_on_error,
1466+
errors=errors,
14521467
try_cast=try_cast, axis=axis,
14531468
transpose=transpose)
14541469
return self._maybe_downcast(blocks, 'infer')
@@ -2736,7 +2751,7 @@ def sp_index(self):
27362751
def kind(self):
27372752
return self.values.kind
27382753

2739-
def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
2754+
def _astype(self, dtype, copy=False, errors='raise', values=None,
27402755
klass=None, mgr=None, **kwargs):
27412756
if values is None:
27422757
values = self.values

0 commit comments

Comments
 (0)