Skip to content

Commit e2320f1

Browse files
authored
DEPR: unused 'errors' keyword in where, mask (#44294)
1 parent 28c7f76 commit e2320f1

File tree

8 files changed

+37
-31
lines changed

8 files changed

+37
-31
lines changed

doc/source/whatsnew/v1.4.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ Other Deprecations
398398
- Deprecated silent dropping of columns that raised a ``TypeError``, ``DataError``, and some cases of ``ValueError`` in :meth:`Series.aggregate`, :meth:`DataFrame.aggregate`, :meth:`Series.groupby.aggregate`, and :meth:`DataFrame.groupby.aggregate` when used with a list (:issue:`43740`)
399399
- Deprecated casting behavior when setting timezone-aware value(s) into a timezone-aware :class:`Series` or :class:`DataFrame` column when the timezones do not match. Previously this cast to object dtype. In a future version, the values being inserted will be converted to the series or column's existing timezone (:issue:`37605`)
400400
- Deprecated casting behavior when passing an item with mismatched-timezone to :meth:`DatetimeIndex.insert`, :meth:`DatetimeIndex.putmask`, :meth:`DatetimeIndex.where` :meth:`DatetimeIndex.fillna`, :meth:`Series.mask`, :meth:`Series.where`, :meth:`Series.fillna`, :meth:`Series.shift`, :meth:`Series.replace`, :meth:`Series.reindex` (and :class:`DataFrame` column analogues). In the past this has cast to object dtype. In a future version, these will cast the passed item to the index or series's timezone (:issue:`37605`)
401-
-
401+
- Deprecated the 'errors' keyword argument in :meth:`Series.where`, :meth:`DataFrame.where`, :meth:`Series.mask`, and meth:`DataFrame.mask`; in a future version the argument will be removed (:issue:`44294`)
402402

403403
.. ---------------------------------------------------------------------------
404404

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10907,7 +10907,7 @@ def where(
1090710907
inplace=False,
1090810908
axis=None,
1090910909
level=None,
10910-
errors="raise",
10910+
errors=lib.no_default,
1091110911
try_cast=lib.no_default,
1091210912
):
1091310913
return super().where(cond, other, inplace, axis, level, errors, try_cast)
@@ -10922,7 +10922,7 @@ def mask(
1092210922
inplace=False,
1092310923
axis=None,
1092410924
level=None,
10925-
errors="raise",
10925+
errors=lib.no_default,
1092610926
try_cast=lib.no_default,
1092710927
):
1092810928
return super().mask(cond, other, inplace, axis, level, errors, try_cast)

pandas/core/generic.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -8900,14 +8900,22 @@ def _where(
89008900
inplace=False,
89018901
axis=None,
89028902
level=None,
8903-
errors="raise",
8903+
errors=lib.no_default,
89048904
):
89058905
"""
89068906
Equivalent to public method `where`, except that `other` is not
89078907
applied as a function even if callable. Used in __setitem__.
89088908
"""
89098909
inplace = validate_bool_kwarg(inplace, "inplace")
89108910

8911+
if errors is not lib.no_default:
8912+
warnings.warn(
8913+
f"The 'errors' keyword in {type(self).__name__}.where and mask is "
8914+
"deprecated and will be removed in a future version.",
8915+
FutureWarning,
8916+
stacklevel=find_stack_level(),
8917+
)
8918+
89118919
if axis is not None:
89128920
axis = self._get_axis_number(axis)
89138921

@@ -9025,7 +9033,6 @@ def _where(
90259033
other=other,
90269034
cond=cond,
90279035
align=align,
9028-
errors=errors,
90299036
)
90309037
result = self._constructor(new_data)
90319038
return result.__finalize__(self)
@@ -9044,7 +9051,7 @@ def where(
90449051
inplace=False,
90459052
axis=None,
90469053
level=None,
9047-
errors="raise",
9054+
errors=lib.no_default,
90489055
try_cast=lib.no_default,
90499056
):
90509057
"""
@@ -9077,6 +9084,9 @@ def where(
90779084
- 'raise' : allow exceptions to be raised.
90789085
- 'ignore' : suppress exceptions. On error return original object.
90799086
9087+
.. deprecated:: 1.4.0
9088+
Previously was silently ignored.
9089+
90809090
try_cast : bool, default None
90819091
Try to cast the result back to the input type (if possible).
90829092
@@ -9197,7 +9207,7 @@ def mask(
91979207
inplace=False,
91989208
axis=None,
91999209
level=None,
9200-
errors="raise",
9210+
errors=lib.no_default,
92019211
try_cast=lib.no_default,
92029212
):
92039213

pandas/core/internals/array_manager.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def apply_with_block(self: T, f, align_keys=None, swap_axis=True, **kwargs) -> T
327327

328328
return type(self)(result_arrays, self._axes)
329329

330-
def where(self: T, other, cond, align: bool, errors: str) -> T:
330+
def where(self: T, other, cond, align: bool) -> T:
331331
if align:
332332
align_keys = ["other", "cond"]
333333
else:
@@ -339,7 +339,6 @@ def where(self: T, other, cond, align: bool, errors: str) -> T:
339339
align_keys=align_keys,
340340
other=other,
341341
cond=cond,
342-
errors=errors,
343342
)
344343

345344
# TODO what is this used for?

pandas/core/internals/blocks.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -1144,17 +1144,14 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> list[Blo
11441144

11451145
return [self.make_block(new_values)]
11461146

1147-
def where(self, other, cond, errors="raise") -> list[Block]:
1147+
def where(self, other, cond) -> list[Block]:
11481148
"""
11491149
evaluate the block; return result block(s) from the result
11501150
11511151
Parameters
11521152
----------
11531153
other : a ndarray/object
11541154
cond : np.ndarray[bool], SparseArray[bool], or BooleanArray
1155-
errors : str, {'raise', 'ignore'}, default 'raise'
1156-
- ``raise`` : allow exceptions to be raised
1157-
- ``ignore`` : suppress exceptions. On error return original object
11581155
11591156
Returns
11601157
-------
@@ -1163,7 +1160,6 @@ def where(self, other, cond, errors="raise") -> list[Block]:
11631160
assert cond.ndim == self.ndim
11641161
assert not isinstance(other, (ABCIndex, ABCSeries, ABCDataFrame))
11651162

1166-
assert errors in ["raise", "ignore"]
11671163
transpose = self.ndim == 2
11681164

11691165
values = self.values
@@ -1185,9 +1181,8 @@ def where(self, other, cond, errors="raise") -> list[Block]:
11851181
# or if we are a single block (ndim == 1)
11861182
if not self._can_hold_element(other):
11871183
# we cannot coerce, return a compat dtype
1188-
# we are explicitly ignoring errors
11891184
block = self.coerce_to_target_dtype(other)
1190-
blocks = block.where(orig_other, cond, errors=errors)
1185+
blocks = block.where(orig_other, cond)
11911186
return self._maybe_downcast(blocks, "infer")
11921187

11931188
# error: Argument 1 to "setitem_datetimelike_compat" has incompatible type
@@ -1586,7 +1581,7 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> list[Blo
15861581
new_values = self.values.shift(periods=periods, fill_value=fill_value)
15871582
return [self.make_block_same_class(new_values)]
15881583

1589-
def where(self, other, cond, errors="raise") -> list[Block]:
1584+
def where(self, other, cond) -> list[Block]:
15901585

15911586
cond = extract_bool_array(cond)
15921587
assert not isinstance(other, (ABCIndex, ABCSeries, ABCDataFrame))
@@ -1619,7 +1614,7 @@ def where(self, other, cond, errors="raise") -> list[Block]:
16191614
# For now at least only support casting e.g.
16201615
# Interval[int64]->Interval[float64]
16211616
raise
1622-
return blk.where(other, cond, errors)
1617+
return blk.where(other, cond)
16231618
raise
16241619

16251620
return [self.make_block_same_class(result)]
@@ -1704,15 +1699,15 @@ def putmask(self, mask, new) -> list[Block]:
17041699
arr.T.putmask(mask, new)
17051700
return [self]
17061701

1707-
def where(self, other, cond, errors="raise") -> list[Block]:
1702+
def where(self, other, cond) -> list[Block]:
17081703
arr = self.values
17091704

17101705
cond = extract_bool_array(cond)
17111706

17121707
try:
17131708
res_values = arr.T._where(cond, other).T
17141709
except (ValueError, TypeError):
1715-
return Block.where(self, other, cond, errors=errors)
1710+
return Block.where(self, other, cond)
17161711

17171712
nb = self.make_block_same_class(res_values)
17181713
return [nb]

pandas/core/internals/managers.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def apply(
315315
out = type(self).from_blocks(result_blocks, self.axes)
316316
return out
317317

318-
def where(self: T, other, cond, align: bool, errors: str) -> T:
318+
def where(self: T, other, cond, align: bool) -> T:
319319
if align:
320320
align_keys = ["other", "cond"]
321321
else:
@@ -327,7 +327,6 @@ def where(self: T, other, cond, align: bool, errors: str) -> T:
327327
align_keys=align_keys,
328328
other=other,
329329
cond=cond,
330-
errors=errors,
331330
)
332331

333332
def setitem(self: T, indexer, value) -> T:

pandas/core/series.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5461,7 +5461,7 @@ def where(
54615461
inplace=False,
54625462
axis=None,
54635463
level=None,
5464-
errors="raise",
5464+
errors=lib.no_default,
54655465
try_cast=lib.no_default,
54665466
):
54675467
return super().where(cond, other, inplace, axis, level, errors, try_cast)
@@ -5476,7 +5476,7 @@ def mask(
54765476
inplace=False,
54775477
axis=None,
54785478
level=None,
5479-
errors="raise",
5479+
errors=lib.no_default,
54805480
try_cast=lib.no_default,
54815481
):
54825482
return super().mask(cond, other, inplace, axis, level, errors, try_cast)

pandas/tests/series/methods/test_fillna.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,18 @@ def test_fillna_consistency(self):
147147
)
148148
tm.assert_series_equal(result, expected)
149149

150-
# where (we ignore the errors=)
151-
result = ser.where(
152-
[True, False], Timestamp("20130101", tz="US/Eastern"), errors="ignore"
153-
)
150+
msg = "The 'errors' keyword in "
151+
with tm.assert_produces_warning(FutureWarning, match=msg):
152+
# where (we ignore the errors=)
153+
result = ser.where(
154+
[True, False], Timestamp("20130101", tz="US/Eastern"), errors="ignore"
155+
)
154156
tm.assert_series_equal(result, expected)
155157

156-
result = ser.where(
157-
[True, False], Timestamp("20130101", tz="US/Eastern"), errors="ignore"
158-
)
158+
with tm.assert_produces_warning(FutureWarning, match=msg):
159+
result = ser.where(
160+
[True, False], Timestamp("20130101", tz="US/Eastern"), errors="ignore"
161+
)
159162
tm.assert_series_equal(result, expected)
160163

161164
# with a non-datetime

0 commit comments

Comments
 (0)