Skip to content

Commit 753ea2e

Browse files
authored
DEPR: enforce try_cast argument deprecation in mask/where (#49193)
1 parent b4ff385 commit 753ea2e

File tree

6 files changed

+1
-75
lines changed

6 files changed

+1
-75
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ Removal of prior version deprecations/changes
146146
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147147
- Removed ``keep_tz`` argument in :meth:`DatetimeIndex.to_series` (:issue:`29731`)
148148
- Remove arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`)
149+
- Removed argument ``try_cast`` from :meth:`DataFrame.mask`, :meth:`DataFrame.where`, :meth:`Series.mask` and :meth:`Series.where` (:issue:`38836`)
149150
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
150151
- Removed the ``numeric_only`` keyword from :meth:`Categorical.min` and :meth:`Categorical.max` in favor of ``skipna`` (:issue:`48821`)
151152
- Removed :func:`is_extension_type` in favor of :func:`is_extension_array_dtype` (:issue:`29457`)

pandas/core/frame.py

-10
Original file line numberDiff line numberDiff line change
@@ -11892,7 +11892,6 @@ def where(
1189211892
axis: Axis | None = ...,
1189311893
level: Level = ...,
1189411894
errors: IgnoreRaise | lib.NoDefault = ...,
11895-
try_cast: bool | lib.NoDefault = ...,
1189611895
) -> DataFrame:
1189711896
...
1189811897

@@ -11906,7 +11905,6 @@ def where(
1190611905
axis: Axis | None = ...,
1190711906
level: Level = ...,
1190811907
errors: IgnoreRaise | lib.NoDefault = ...,
11909-
try_cast: bool | lib.NoDefault = ...,
1191011908
) -> None:
1191111909
...
1191211910

@@ -11920,7 +11918,6 @@ def where(
1192011918
axis: Axis | None = ...,
1192111919
level: Level = ...,
1192211920
errors: IgnoreRaise | lib.NoDefault = ...,
11923-
try_cast: bool | lib.NoDefault = ...,
1192411921
) -> DataFrame | None:
1192511922
...
1192611923

@@ -11937,15 +11934,13 @@ def where( # type: ignore[override]
1193711934
axis: Axis | None = None,
1193811935
level: Level = None,
1193911936
errors: IgnoreRaise | lib.NoDefault = "raise",
11940-
try_cast: bool | lib.NoDefault = lib.no_default,
1194111937
) -> DataFrame | None:
1194211938
return super().where(
1194311939
cond,
1194411940
other,
1194511941
inplace=inplace,
1194611942
axis=axis,
1194711943
level=level,
11948-
try_cast=try_cast,
1194911944
)
1195011945

1195111946
@overload
@@ -11958,7 +11953,6 @@ def mask(
1195811953
axis: Axis | None = ...,
1195911954
level: Level = ...,
1196011955
errors: IgnoreRaise | lib.NoDefault = ...,
11961-
try_cast: bool | lib.NoDefault = ...,
1196211956
) -> DataFrame:
1196311957
...
1196411958

@@ -11972,7 +11966,6 @@ def mask(
1197211966
axis: Axis | None = ...,
1197311967
level: Level = ...,
1197411968
errors: IgnoreRaise | lib.NoDefault = ...,
11975-
try_cast: bool | lib.NoDefault = ...,
1197611969
) -> None:
1197711970
...
1197811971

@@ -11986,7 +11979,6 @@ def mask(
1198611979
axis: Axis | None = ...,
1198711980
level: Level = ...,
1198811981
errors: IgnoreRaise | lib.NoDefault = ...,
11989-
try_cast: bool | lib.NoDefault = ...,
1199011982
) -> DataFrame | None:
1199111983
...
1199211984

@@ -12003,15 +11995,13 @@ def mask( # type: ignore[override]
1200311995
axis: Axis | None = None,
1200411996
level: Level = None,
1200511997
errors: IgnoreRaise | lib.NoDefault = "raise",
12006-
try_cast: bool | lib.NoDefault = lib.no_default,
1200711998
) -> DataFrame | None:
1200811999
return super().mask(
1200912000
cond,
1201012001
other,
1201112002
inplace=inplace,
1201212003
axis=axis,
1201312004
level=level,
12014-
try_cast=try_cast,
1201512005
)
1201612006

1201712007

pandas/core/generic.py

-31
Original file line numberDiff line numberDiff line change
@@ -9806,7 +9806,6 @@ def where(
98069806
axis: Axis | None = ...,
98079807
level: Level = ...,
98089808
errors: IgnoreRaise | lib.NoDefault = ...,
9809-
try_cast: bool_t | lib.NoDefault = ...,
98109809
) -> NDFrameT:
98119810
...
98129811

@@ -9820,7 +9819,6 @@ def where(
98209819
axis: Axis | None = ...,
98219820
level: Level = ...,
98229821
errors: IgnoreRaise | lib.NoDefault = ...,
9823-
try_cast: bool_t | lib.NoDefault = ...,
98249822
) -> None:
98259823
...
98269824

@@ -9834,7 +9832,6 @@ def where(
98349832
axis: Axis | None = ...,
98359833
level: Level = ...,
98369834
errors: IgnoreRaise | lib.NoDefault = ...,
9837-
try_cast: bool_t | lib.NoDefault = ...,
98389835
) -> NDFrameT | None:
98399836
...
98409837

@@ -9857,7 +9854,6 @@ def where(
98579854
axis: Axis | None = None,
98589855
level: Level = None,
98599856
errors: IgnoreRaise | lib.NoDefault = "raise",
9860-
try_cast: bool_t | lib.NoDefault = lib.no_default,
98619857
) -> NDFrameT | None:
98629858
"""
98639859
Replace values where the condition is {cond_rev}.
@@ -9896,12 +9892,6 @@ def where(
98969892
.. deprecated:: 1.5.0
98979893
This argument had no effect.
98989894
9899-
try_cast : bool, default None
9900-
Try to cast the result back to the input type (if possible).
9901-
9902-
.. deprecated:: 1.3.0
9903-
Manually cast back if necessary.
9904-
99059895
Returns
99069896
-------
99079897
Same type as caller or None if ``inplace=True``.
@@ -10012,15 +10002,6 @@ def where(
1001210002
4 True True
1001310003
"""
1001410004
other = com.apply_if_callable(other, self)
10015-
10016-
if try_cast is not lib.no_default:
10017-
warnings.warn(
10018-
"try_cast keyword is deprecated and will be removed in a "
10019-
"future version.",
10020-
FutureWarning,
10021-
stacklevel=find_stack_level(),
10022-
)
10023-
1002410005
return self._where(cond, other, inplace, axis, level)
1002510006

1002610007
@overload
@@ -10033,7 +10014,6 @@ def mask(
1003310014
axis: Axis | None = ...,
1003410015
level: Level = ...,
1003510016
errors: IgnoreRaise | lib.NoDefault = ...,
10036-
try_cast: bool_t | lib.NoDefault = ...,
1003710017
) -> NDFrameT:
1003810018
...
1003910019

@@ -10047,7 +10027,6 @@ def mask(
1004710027
axis: Axis | None = ...,
1004810028
level: Level = ...,
1004910029
errors: IgnoreRaise | lib.NoDefault = ...,
10050-
try_cast: bool_t | lib.NoDefault = ...,
1005110030
) -> None:
1005210031
...
1005310032

@@ -10061,7 +10040,6 @@ def mask(
1006110040
axis: Axis | None = ...,
1006210041
level: Level = ...,
1006310042
errors: IgnoreRaise | lib.NoDefault = ...,
10064-
try_cast: bool_t | lib.NoDefault = ...,
1006510043
) -> NDFrameT | None:
1006610044
...
1006710045

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

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

10094-
if try_cast is not lib.no_default:
10095-
warnings.warn(
10096-
"try_cast keyword is deprecated and will be removed in a "
10097-
"future version.",
10098-
FutureWarning,
10099-
stacklevel=find_stack_level(),
10100-
)
10101-
1010210071
# see gh-21891
1010310072
if not hasattr(cond, "__invert__"):
1010410073
cond = np.array(cond)

pandas/core/series.py

-10
Original file line numberDiff line numberDiff line change
@@ -6066,7 +6066,6 @@ def where(
60666066
axis: Axis | None = ...,
60676067
level: Level = ...,
60686068
errors: IgnoreRaise | lib.NoDefault = ...,
6069-
try_cast: bool | lib.NoDefault = ...,
60706069
) -> Series:
60716070
...
60726071

@@ -6080,7 +6079,6 @@ def where(
60806079
axis: Axis | None = ...,
60816080
level: Level = ...,
60826081
errors: IgnoreRaise | lib.NoDefault = ...,
6083-
try_cast: bool | lib.NoDefault = ...,
60846082
) -> None:
60856083
...
60866084

@@ -6094,7 +6092,6 @@ def where(
60946092
axis: Axis | None = ...,
60956093
level: Level = ...,
60966094
errors: IgnoreRaise | lib.NoDefault = ...,
6097-
try_cast: bool | lib.NoDefault = ...,
60986095
) -> Series | None:
60996096
...
61006097

@@ -6111,15 +6108,13 @@ def where( # type: ignore[override]
61116108
axis: Axis | None = None,
61126109
level: Level = None,
61136110
errors: IgnoreRaise | lib.NoDefault = lib.no_default,
6114-
try_cast: bool | lib.NoDefault = lib.no_default,
61156111
) -> Series | None:
61166112
return super().where(
61176113
cond,
61186114
other,
61196115
inplace=inplace,
61206116
axis=axis,
61216117
level=level,
6122-
try_cast=try_cast,
61236118
)
61246119

61256120
@overload
@@ -6132,7 +6127,6 @@ def mask(
61326127
axis: Axis | None = ...,
61336128
level: Level = ...,
61346129
errors: IgnoreRaise | lib.NoDefault = ...,
6135-
try_cast: bool | lib.NoDefault = ...,
61366130
) -> Series:
61376131
...
61386132

@@ -6146,7 +6140,6 @@ def mask(
61466140
axis: Axis | None = ...,
61476141
level: Level = ...,
61486142
errors: IgnoreRaise | lib.NoDefault = ...,
6149-
try_cast: bool | lib.NoDefault = ...,
61506143
) -> None:
61516144
...
61526145

@@ -6160,7 +6153,6 @@ def mask(
61606153
axis: Axis | None = ...,
61616154
level: Level = ...,
61626155
errors: IgnoreRaise | lib.NoDefault = ...,
6163-
try_cast: bool | lib.NoDefault = ...,
61646156
) -> Series | None:
61656157
...
61666158

@@ -6177,15 +6169,13 @@ def mask( # type: ignore[override]
61776169
axis: Axis | None = None,
61786170
level: Level = None,
61796171
errors: IgnoreRaise | lib.NoDefault = lib.no_default,
6180-
try_cast: bool | lib.NoDefault = lib.no_default,
61816172
) -> Series | None:
61826173
return super().mask(
61836174
cond,
61846175
other,
61856176
inplace=inplace,
61866177
axis=axis,
61876178
level=level,
6188-
try_cast=try_cast,
61896179
)
61906180

61916181
# ----------------------------------------------------------------------

pandas/tests/frame/indexing/test_mask.py

-13
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,6 @@ def test_mask_pos_args_deprecation(self, frame_or_series):
110110
tm.assert_equal(result, expected)
111111

112112

113-
def test_mask_try_cast_deprecated(frame_or_series):
114-
115-
obj = DataFrame(np.random.randn(4, 3))
116-
if frame_or_series is not DataFrame:
117-
obj = obj[0]
118-
119-
mask = obj > 0
120-
121-
with tm.assert_produces_warning(FutureWarning):
122-
# try_cast keyword deprecated
123-
obj.mask(mask, -1, try_cast=True)
124-
125-
126113
def test_mask_stringdtype(frame_or_series):
127114
# GH 40824
128115
obj = DataFrame(

pandas/tests/frame/indexing/test_where.py

-11
Original file line numberDiff line numberDiff line change
@@ -764,17 +764,6 @@ def test_where_datetimelike_noop(self, dtype):
764764
tm.assert_frame_equal(df, expected.astype(object))
765765

766766

767-
def test_where_try_cast_deprecated(frame_or_series):
768-
obj = DataFrame(np.random.randn(4, 3))
769-
obj = tm.get_obj(obj, frame_or_series)
770-
771-
mask = obj > 0
772-
773-
with tm.assert_produces_warning(FutureWarning):
774-
# try_cast keyword deprecated
775-
obj.where(mask, -1, try_cast=False)
776-
777-
778767
def test_where_int_downcasting_deprecated():
779768
# GH#44597
780769
arr = np.arange(6).astype(np.int16).reshape(3, 2)

0 commit comments

Comments
 (0)