diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 30acc117c237a..aa956e5441c2d 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -145,6 +145,7 @@ Deprecations Removal of prior version deprecations/changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - 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 :func:`is_extension_type` in favor of :func:`is_extension_array_dtype` (:issue:`29457`) - Remove :meth:`DataFrameGroupBy.pad` and :meth:`DataFrameGroupBy.backfill` (:issue:`45076`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6f0f92ac03b3f..396a794d26c64 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -11892,7 +11892,6 @@ def where( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool | lib.NoDefault = ..., ) -> DataFrame: ... @@ -11906,7 +11905,6 @@ def where( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool | lib.NoDefault = ..., ) -> None: ... @@ -11920,7 +11918,6 @@ def where( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool | lib.NoDefault = ..., ) -> DataFrame | None: ... @@ -11937,7 +11934,6 @@ 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, @@ -11945,7 +11941,6 @@ def where( # type: ignore[override] inplace=inplace, axis=axis, level=level, - try_cast=try_cast, ) @overload @@ -11958,7 +11953,6 @@ def mask( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool | lib.NoDefault = ..., ) -> DataFrame: ... @@ -11972,7 +11966,6 @@ def mask( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool | lib.NoDefault = ..., ) -> None: ... @@ -11986,7 +11979,6 @@ def mask( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool | lib.NoDefault = ..., ) -> DataFrame | None: ... @@ -12003,7 +11995,6 @@ 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, @@ -12011,7 +12002,6 @@ def mask( # type: ignore[override] inplace=inplace, axis=axis, level=level, - try_cast=try_cast, ) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 4f3c908817ac7..6403e653f5325 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -9806,7 +9806,6 @@ def where( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool_t | lib.NoDefault = ..., ) -> NDFrameT: ... @@ -9820,7 +9819,6 @@ def where( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool_t | lib.NoDefault = ..., ) -> None: ... @@ -9834,7 +9832,6 @@ def where( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool_t | lib.NoDefault = ..., ) -> NDFrameT | None: ... @@ -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}. @@ -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``. @@ -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 @@ -10033,7 +10014,6 @@ def mask( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool_t | lib.NoDefault = ..., ) -> NDFrameT: ... @@ -10047,7 +10027,6 @@ def mask( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool_t | lib.NoDefault = ..., ) -> None: ... @@ -10061,7 +10040,6 @@ def mask( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool_t | lib.NoDefault = ..., ) -> NDFrameT | None: ... @@ -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) diff --git a/pandas/core/series.py b/pandas/core/series.py index 6620cc1786340..98fc797806d11 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -6066,7 +6066,6 @@ def where( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool | lib.NoDefault = ..., ) -> Series: ... @@ -6080,7 +6079,6 @@ def where( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool | lib.NoDefault = ..., ) -> None: ... @@ -6094,7 +6092,6 @@ def where( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool | lib.NoDefault = ..., ) -> Series | None: ... @@ -6111,7 +6108,6 @@ 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, @@ -6119,7 +6115,6 @@ def where( # type: ignore[override] inplace=inplace, axis=axis, level=level, - try_cast=try_cast, ) @overload @@ -6132,7 +6127,6 @@ def mask( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool | lib.NoDefault = ..., ) -> Series: ... @@ -6146,7 +6140,6 @@ def mask( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool | lib.NoDefault = ..., ) -> None: ... @@ -6160,7 +6153,6 @@ def mask( axis: Axis | None = ..., level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast: bool | lib.NoDefault = ..., ) -> Series | None: ... @@ -6177,7 +6169,6 @@ 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, @@ -6185,7 +6176,6 @@ def mask( # type: ignore[override] inplace=inplace, axis=axis, level=level, - try_cast=try_cast, ) # ---------------------------------------------------------------------- diff --git a/pandas/tests/frame/indexing/test_mask.py b/pandas/tests/frame/indexing/test_mask.py index bd7ffe9a8e770..3a31123da7679 100644 --- a/pandas/tests/frame/indexing/test_mask.py +++ b/pandas/tests/frame/indexing/test_mask.py @@ -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( diff --git a/pandas/tests/frame/indexing/test_where.py b/pandas/tests/frame/indexing/test_where.py index fba8978d2128c..ea559230d1595 100644 --- a/pandas/tests/frame/indexing/test_where.py +++ b/pandas/tests/frame/indexing/test_where.py @@ -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)