From de2dc45ce0f254075b852726c3acc12e5713c6f2 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 30 Jun 2023 17:50:19 -0700 Subject: [PATCH 1/3] DEPR: downcast keyword in Index.fillna --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/core/indexes/base.py | 14 +++++++++++++- pandas/tests/indexes/test_old_base.py | 8 +++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index ebbdbcb0f61f5..c19a5e88b8d49 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -240,6 +240,7 @@ Other API changes Deprecations ~~~~~~~~~~~~ +- Deprecated 'downcast' keyword in :meth:`Index.fillna` (:issue:`??`) - Deprecated 'broadcast_axis' keyword in :meth:`Series.align` and :meth:`DataFrame.align`, upcast before calling ``align`` with ``left = DataFrame({col: left for col in right.columns}, index=right.index)`` (:issue:`51856`) - Deprecated 'fill_method' and 'limit' keywords in :meth:`DataFrame.pct_change`, :meth:`Series.pct_change`, :meth:`DataFrameGroupBy.pct_change`, and :meth:`SeriesGroupBy.pct_change`, explicitly call ``ffill`` or ``bfill`` before calling ``pct_change`` instead (:issue:`53491`) - Deprecated 'method', 'limit', and 'fill_axis' keywords in :meth:`DataFrame.align` and :meth:`Series.align`, explicitly call ``fillna`` on the alignment results instead (:issue:`51856`) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index f03af387151b2..4a49ecb33495d 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2918,7 +2918,7 @@ def notna(self) -> npt.NDArray[np.bool_]: notnull = notna - def fillna(self, value=None, downcast=None): + def fillna(self, value=None, downcast=lib.no_default): """ Fill NA/NaN values with the specified value. @@ -2932,6 +2932,8 @@ def fillna(self, value=None, downcast=None): or the string 'infer' which will try to downcast to an appropriate equal type (e.g. float64 to int64 if possible). + .. deprecated: 2.1.0 + Returns ------- Index @@ -2949,6 +2951,16 @@ def fillna(self, value=None, downcast=None): """ if not is_scalar(value): raise TypeError(f"'value' must be a scalar, passed: {type(value).__name__}") + if downcast is not lib.no_default: + warnings.warn( + f"The 'downcast' keyword in {type(self).__name__}.fillna is " + "deprecated and will be removed in a future version. " + "It was previously silently ignored.", + FutureWarning, + stacklevel=find_stack_level(), + ) + else: + downcast = None if self.hasnans: result = self.putmask(self._isnan, value) diff --git a/pandas/tests/indexes/test_old_base.py b/pandas/tests/indexes/test_old_base.py index ff23c8a8ba5a4..588aa458c8b04 100644 --- a/pandas/tests/indexes/test_old_base.py +++ b/pandas/tests/indexes/test_old_base.py @@ -593,9 +593,11 @@ def test_fillna(self, index): idx = type(index)(values) msg = "does not support 'downcast'" - with pytest.raises(NotImplementedError, match=msg): - # For now at least, we only raise if there are NAs present - idx.fillna(idx[0], downcast="infer") + msg2 = r"The 'downcast' keyword in .*Index\.fillna is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg2): + with pytest.raises(NotImplementedError, match=msg): + # For now at least, we only raise if there are NAs present + idx.fillna(idx[0], downcast="infer") expected = np.array([False] * len(idx), dtype=bool) expected[1] = True From d3218b39802a42dcf1e25c20096fc3e087febd17 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 30 Jun 2023 17:51:59 -0700 Subject: [PATCH 2/3] GH ref --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index c19a5e88b8d49..079047b75a745 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -240,8 +240,8 @@ Other API changes Deprecations ~~~~~~~~~~~~ -- Deprecated 'downcast' keyword in :meth:`Index.fillna` (:issue:`??`) - Deprecated 'broadcast_axis' keyword in :meth:`Series.align` and :meth:`DataFrame.align`, upcast before calling ``align`` with ``left = DataFrame({col: left for col in right.columns}, index=right.index)`` (:issue:`51856`) +- Deprecated 'downcast' keyword in :meth:`Index.fillna` (:issue:`53956`) - Deprecated 'fill_method' and 'limit' keywords in :meth:`DataFrame.pct_change`, :meth:`Series.pct_change`, :meth:`DataFrameGroupBy.pct_change`, and :meth:`SeriesGroupBy.pct_change`, explicitly call ``ffill`` or ``bfill`` before calling ``pct_change`` instead (:issue:`53491`) - Deprecated 'method', 'limit', and 'fill_axis' keywords in :meth:`DataFrame.align` and :meth:`Series.align`, explicitly call ``fillna`` on the alignment results instead (:issue:`51856`) - Deprecated 'quantile' keyword in :meth:`Rolling.quantile` and :meth:`Expanding.quantile`, renamed as 'q' instead (:issue:`52550`) From fc324b937264382b8c5f14e66a97dc399836d8ea Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 1 Jul 2023 09:30:17 -0700 Subject: [PATCH 3/3] docstring fixup --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 4a49ecb33495d..5f19f6d06a194 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2932,7 +2932,7 @@ def fillna(self, value=None, downcast=lib.no_default): or the string 'infer' which will try to downcast to an appropriate equal type (e.g. float64 to int64 if possible). - .. deprecated: 2.1.0 + .. deprecated:: 2.1.0 Returns -------