Skip to content

DEPR: unused 'errors' keyword in where, mask #44294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ Other Deprecations
- 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`)
- 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`)
- 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`)
-
- 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`)

.. ---------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10907,7 +10907,7 @@ def where(
inplace=False,
axis=None,
level=None,
errors="raise",
errors=lib.no_default,
try_cast=lib.no_default,
):
return super().where(cond, other, inplace, axis, level, errors, try_cast)
Expand All @@ -10922,7 +10922,7 @@ def mask(
inplace=False,
axis=None,
level=None,
errors="raise",
errors=lib.no_default,
try_cast=lib.no_default,
):
return super().mask(cond, other, inplace, axis, level, errors, try_cast)
Expand Down
18 changes: 14 additions & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8900,14 +8900,22 @@ def _where(
inplace=False,
axis=None,
level=None,
errors="raise",
errors=lib.no_default,
):
"""
Equivalent to public method `where`, except that `other` is not
applied as a function even if callable. Used in __setitem__.
"""
inplace = validate_bool_kwarg(inplace, "inplace")

if errors is not lib.no_default:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason not to use the deprecate_kwarg decorator?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think with the decorator the warning message would be about _where instead of where/mask

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that would need to use the decorator on all 4 of the public methods instead of just once here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either way works, yah

warnings.warn(
f"The 'errors' keyword in {type(self).__name__}.where and mask is "
"deprecated and will be removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(),
)

if axis is not None:
axis = self._get_axis_number(axis)

Expand Down Expand Up @@ -9025,7 +9033,6 @@ def _where(
other=other,
cond=cond,
align=align,
errors=errors,
)
result = self._constructor(new_data)
return result.__finalize__(self)
Expand All @@ -9044,7 +9051,7 @@ def where(
inplace=False,
axis=None,
level=None,
errors="raise",
errors=lib.no_default,
try_cast=lib.no_default,
):
"""
Expand Down Expand Up @@ -9077,6 +9084,9 @@ def where(
- 'raise' : allow exceptions to be raised.
- 'ignore' : suppress exceptions. On error return original object.

.. deprecated:: 1.4.0
Previously was silently ignored.

try_cast : bool, default None
Try to cast the result back to the input type (if possible).

Expand Down Expand Up @@ -9197,7 +9207,7 @@ def mask(
inplace=False,
axis=None,
level=None,
errors="raise",
errors=lib.no_default,
try_cast=lib.no_default,
):

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def apply_with_block(self: T, f, align_keys=None, swap_axis=True, **kwargs) -> T

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

def where(self: T, other, cond, align: bool, errors: str) -> T:
def where(self: T, other, cond, align: bool) -> T:
if align:
align_keys = ["other", "cond"]
else:
Expand All @@ -339,7 +339,6 @@ def where(self: T, other, cond, align: bool, errors: str) -> T:
align_keys=align_keys,
other=other,
cond=cond,
errors=errors,
)

# TODO what is this used for?
Expand Down
17 changes: 6 additions & 11 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1144,17 +1144,14 @@ def shift(self, periods: int, axis: int = 0, fill_value: Any = None) -> list[Blo

return [self.make_block(new_values)]

def where(self, other, cond, errors="raise") -> list[Block]:
def where(self, other, cond) -> list[Block]:
"""
evaluate the block; return result block(s) from the result

Parameters
----------
other : a ndarray/object
cond : np.ndarray[bool], SparseArray[bool], or BooleanArray
errors : str, {'raise', 'ignore'}, default 'raise'
- ``raise`` : allow exceptions to be raised
- ``ignore`` : suppress exceptions. On error return original object

Returns
-------
Expand All @@ -1163,7 +1160,6 @@ def where(self, other, cond, errors="raise") -> list[Block]:
assert cond.ndim == self.ndim
assert not isinstance(other, (ABCIndex, ABCSeries, ABCDataFrame))

assert errors in ["raise", "ignore"]
Copy link
Member

@simonjayhawkins simonjayhawkins Nov 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are no longer raising on invalid errors value, but this was already not a correct user facing ValueError/TypeError. so maybe just removing is fine and allowing any value/type for errors is ok?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean remove this line but not do the rest of this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that if we wanted to retain the validation it would now need to be higher up the stack but also probably ok that the errors parameter is no longer validated?

transpose = self.ndim == 2

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

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

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

cond = extract_bool_array(cond)
assert not isinstance(other, (ABCIndex, ABCSeries, ABCDataFrame))
Expand Down Expand Up @@ -1619,7 +1614,7 @@ def where(self, other, cond, errors="raise") -> list[Block]:
# For now at least only support casting e.g.
# Interval[int64]->Interval[float64]
raise
return blk.where(other, cond, errors)
return blk.where(other, cond)
raise

return [self.make_block_same_class(result)]
Expand Down Expand Up @@ -1704,15 +1699,15 @@ def putmask(self, mask, new) -> list[Block]:
arr.T.putmask(mask, new)
return [self]

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

cond = extract_bool_array(cond)

try:
res_values = arr.T._where(cond, other).T
except (ValueError, TypeError):
return Block.where(self, other, cond, errors=errors)
return Block.where(self, other, cond)

nb = self.make_block_same_class(res_values)
return [nb]
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def apply(
out = type(self).from_blocks(result_blocks, self.axes)
return out

def where(self: T, other, cond, align: bool, errors: str) -> T:
def where(self: T, other, cond, align: bool) -> T:
if align:
align_keys = ["other", "cond"]
else:
Expand All @@ -327,7 +327,6 @@ def where(self: T, other, cond, align: bool, errors: str) -> T:
align_keys=align_keys,
other=other,
cond=cond,
errors=errors,
)

def setitem(self: T, indexer, value) -> T:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5461,7 +5461,7 @@ def where(
inplace=False,
axis=None,
level=None,
errors="raise",
errors=lib.no_default,
try_cast=lib.no_default,
):
return super().where(cond, other, inplace, axis, level, errors, try_cast)
Expand All @@ -5476,7 +5476,7 @@ def mask(
inplace=False,
axis=None,
level=None,
errors="raise",
errors=lib.no_default,
try_cast=lib.no_default,
):
return super().mask(cond, other, inplace, axis, level, errors, try_cast)
Expand Down
17 changes: 10 additions & 7 deletions pandas/tests/series/methods/test_fillna.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,18 @@ def test_fillna_consistency(self):
)
tm.assert_series_equal(result, expected)

# where (we ignore the errors=)
result = ser.where(
[True, False], Timestamp("20130101", tz="US/Eastern"), errors="ignore"
)
msg = "The 'errors' keyword in "
with tm.assert_produces_warning(FutureWarning, match=msg):
# where (we ignore the errors=)
result = ser.where(
[True, False], Timestamp("20130101", tz="US/Eastern"), errors="ignore"
)
tm.assert_series_equal(result, expected)

result = ser.where(
[True, False], Timestamp("20130101", tz="US/Eastern"), errors="ignore"
)
with tm.assert_produces_warning(FutureWarning, match=msg):
result = ser.where(
[True, False], Timestamp("20130101", tz="US/Eastern"), errors="ignore"
)
tm.assert_series_equal(result, expected)

# with a non-datetime
Expand Down