Skip to content

DEPR: downcast keyword in Index.fillna #53956

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 3 commits into from
Jul 6, 2023
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ Other API changes
Deprecations
~~~~~~~~~~~~
- 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`)
Expand Down
14 changes: 13 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/indexes/test_old_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down