Skip to content

Commit

Permalink
DEPR: enforce try_cast argument deprecation in mask/where (#49193)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemanley committed Oct 20, 2022
1 parent b4ff385 commit 753ea2e
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 75 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Expand Up @@ -146,6 +146,7 @@ Removal of prior version deprecations/changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Removed ``keep_tz`` argument in :meth:`DatetimeIndex.to_series` (:issue:`29731`)
- Remove arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`)
- Removed argument ``try_cast`` from :meth:`DataFrame.mask`, :meth:`DataFrame.where`, :meth:`Series.mask` and :meth:`Series.where` (:issue:`38836`)
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
- Removed the ``numeric_only`` keyword from :meth:`Categorical.min` and :meth:`Categorical.max` in favor of ``skipna`` (:issue:`48821`)
- Removed :func:`is_extension_type` in favor of :func:`is_extension_array_dtype` (:issue:`29457`)
Expand Down
10 changes: 0 additions & 10 deletions pandas/core/frame.py
Expand Up @@ -11892,7 +11892,6 @@ def where(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool | lib.NoDefault = ...,
) -> DataFrame:
...

Expand All @@ -11906,7 +11905,6 @@ def where(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool | lib.NoDefault = ...,
) -> None:
...

Expand All @@ -11920,7 +11918,6 @@ def where(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool | lib.NoDefault = ...,
) -> DataFrame | None:
...

Expand All @@ -11937,15 +11934,13 @@ def where( # type: ignore[override]
axis: Axis | None = None,
level: Level = None,
errors: IgnoreRaise | lib.NoDefault = "raise",
try_cast: bool | lib.NoDefault = lib.no_default,
) -> DataFrame | None:
return super().where(
cond,
other,
inplace=inplace,
axis=axis,
level=level,
try_cast=try_cast,
)

@overload
Expand All @@ -11958,7 +11953,6 @@ def mask(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool | lib.NoDefault = ...,
) -> DataFrame:
...

Expand All @@ -11972,7 +11966,6 @@ def mask(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool | lib.NoDefault = ...,
) -> None:
...

Expand All @@ -11986,7 +11979,6 @@ def mask(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool | lib.NoDefault = ...,
) -> DataFrame | None:
...

Expand All @@ -12003,15 +11995,13 @@ def mask( # type: ignore[override]
axis: Axis | None = None,
level: Level = None,
errors: IgnoreRaise | lib.NoDefault = "raise",
try_cast: bool | lib.NoDefault = lib.no_default,
) -> DataFrame | None:
return super().mask(
cond,
other,
inplace=inplace,
axis=axis,
level=level,
try_cast=try_cast,
)


Expand Down
31 changes: 0 additions & 31 deletions pandas/core/generic.py
Expand Up @@ -9806,7 +9806,6 @@ def where(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool_t | lib.NoDefault = ...,
) -> NDFrameT:
...

Expand All @@ -9820,7 +9819,6 @@ def where(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool_t | lib.NoDefault = ...,
) -> None:
...

Expand All @@ -9834,7 +9832,6 @@ def where(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool_t | lib.NoDefault = ...,
) -> NDFrameT | None:
...

Expand All @@ -9857,7 +9854,6 @@ def where(
axis: Axis | None = None,
level: Level = None,
errors: IgnoreRaise | lib.NoDefault = "raise",
try_cast: bool_t | lib.NoDefault = lib.no_default,
) -> NDFrameT | None:
"""
Replace values where the condition is {cond_rev}.
Expand Down Expand Up @@ -9896,12 +9892,6 @@ def where(
.. deprecated:: 1.5.0
This argument had no effect.
try_cast : bool, default None
Try to cast the result back to the input type (if possible).
.. deprecated:: 1.3.0
Manually cast back if necessary.
Returns
-------
Same type as caller or None if ``inplace=True``.
Expand Down Expand Up @@ -10012,15 +10002,6 @@ def where(
4 True True
"""
other = com.apply_if_callable(other, self)

if try_cast is not lib.no_default:
warnings.warn(
"try_cast keyword is deprecated and will be removed in a "
"future version.",
FutureWarning,
stacklevel=find_stack_level(),
)

return self._where(cond, other, inplace, axis, level)

@overload
Expand All @@ -10033,7 +10014,6 @@ def mask(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool_t | lib.NoDefault = ...,
) -> NDFrameT:
...

Expand All @@ -10047,7 +10027,6 @@ def mask(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool_t | lib.NoDefault = ...,
) -> None:
...

Expand All @@ -10061,7 +10040,6 @@ def mask(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool_t | lib.NoDefault = ...,
) -> NDFrameT | None:
...

Expand All @@ -10085,20 +10063,11 @@ def mask(
axis: Axis | None = None,
level: Level = None,
errors: IgnoreRaise | lib.NoDefault = "raise",
try_cast: bool_t | lib.NoDefault = lib.no_default,
) -> NDFrameT | None:

inplace = validate_bool_kwarg(inplace, "inplace")
cond = com.apply_if_callable(cond, self)

if try_cast is not lib.no_default:
warnings.warn(
"try_cast keyword is deprecated and will be removed in a "
"future version.",
FutureWarning,
stacklevel=find_stack_level(),
)

# see gh-21891
if not hasattr(cond, "__invert__"):
cond = np.array(cond)
Expand Down
10 changes: 0 additions & 10 deletions pandas/core/series.py
Expand Up @@ -6066,7 +6066,6 @@ def where(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool | lib.NoDefault = ...,
) -> Series:
...

Expand All @@ -6080,7 +6079,6 @@ def where(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool | lib.NoDefault = ...,
) -> None:
...

Expand All @@ -6094,7 +6092,6 @@ def where(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool | lib.NoDefault = ...,
) -> Series | None:
...

Expand All @@ -6111,15 +6108,13 @@ def where( # type: ignore[override]
axis: Axis | None = None,
level: Level = None,
errors: IgnoreRaise | lib.NoDefault = lib.no_default,
try_cast: bool | lib.NoDefault = lib.no_default,
) -> Series | None:
return super().where(
cond,
other,
inplace=inplace,
axis=axis,
level=level,
try_cast=try_cast,
)

@overload
Expand All @@ -6132,7 +6127,6 @@ def mask(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool | lib.NoDefault = ...,
) -> Series:
...

Expand All @@ -6146,7 +6140,6 @@ def mask(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool | lib.NoDefault = ...,
) -> None:
...

Expand All @@ -6160,7 +6153,6 @@ def mask(
axis: Axis | None = ...,
level: Level = ...,
errors: IgnoreRaise | lib.NoDefault = ...,
try_cast: bool | lib.NoDefault = ...,
) -> Series | None:
...

Expand All @@ -6177,15 +6169,13 @@ def mask( # type: ignore[override]
axis: Axis | None = None,
level: Level = None,
errors: IgnoreRaise | lib.NoDefault = lib.no_default,
try_cast: bool | lib.NoDefault = lib.no_default,
) -> Series | None:
return super().mask(
cond,
other,
inplace=inplace,
axis=axis,
level=level,
try_cast=try_cast,
)

# ----------------------------------------------------------------------
Expand Down
13 changes: 0 additions & 13 deletions pandas/tests/frame/indexing/test_mask.py
Expand Up @@ -110,19 +110,6 @@ def test_mask_pos_args_deprecation(self, frame_or_series):
tm.assert_equal(result, expected)


def test_mask_try_cast_deprecated(frame_or_series):

obj = DataFrame(np.random.randn(4, 3))
if frame_or_series is not DataFrame:
obj = obj[0]

mask = obj > 0

with tm.assert_produces_warning(FutureWarning):
# try_cast keyword deprecated
obj.mask(mask, -1, try_cast=True)


def test_mask_stringdtype(frame_or_series):
# GH 40824
obj = DataFrame(
Expand Down
11 changes: 0 additions & 11 deletions pandas/tests/frame/indexing/test_where.py
Expand Up @@ -764,17 +764,6 @@ def test_where_datetimelike_noop(self, dtype):
tm.assert_frame_equal(df, expected.astype(object))


def test_where_try_cast_deprecated(frame_or_series):
obj = DataFrame(np.random.randn(4, 3))
obj = tm.get_obj(obj, frame_or_series)

mask = obj > 0

with tm.assert_produces_warning(FutureWarning):
# try_cast keyword deprecated
obj.where(mask, -1, try_cast=False)


def test_where_int_downcasting_deprecated():
# GH#44597
arr = np.arange(6).astype(np.int16).reshape(3, 2)
Expand Down

0 comments on commit 753ea2e

Please sign in to comment.