Skip to content

Backport PR #54983 on branch 2.1.x (BUG: pct_change showing unnecessary FutureWarning) #55040

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
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.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Bug fixes
~~~~~~~~~
- Fixed bug for :class:`ArrowDtype` raising ``NotImplementedError`` for fixed-size list (:issue:`55000`)
- Fixed bug in :meth:`DataFrame.stack` with ``future_stack=True`` and columns a non-:class:`MultiIndex` consisting of tuples (:issue:`54948`)
- Fixed bug in :meth:`Series.pct_change` and :meth:`DataFrame.pct_change` showing unnecessary ``FutureWarning`` (:issue:`54981`)

.. ---------------------------------------------------------------------------
.. _whatsnew_211.other:
Expand Down
24 changes: 15 additions & 9 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11692,15 +11692,21 @@ def pct_change(
stacklevel=find_stack_level(),
)
if fill_method is lib.no_default:
if self.isna().values.any():
warnings.warn(
"The default fill_method='pad' in "
f"{type(self).__name__}.pct_change is deprecated and will be "
"removed in a future version. Call ffill before calling "
"pct_change to retain current behavior and silence this warning.",
FutureWarning,
stacklevel=find_stack_level(),
)
cols = self.items() if self.ndim == 2 else [(None, self)]
for _, col in cols:
mask = col.isna().values
mask = mask[np.argmax(~mask) :]
if mask.any():
warnings.warn(
"The default fill_method='pad' in "
f"{type(self).__name__}.pct_change is deprecated and will be "
"removed in a future version. Call ffill before calling "
"pct_change to retain current behavior and silence this "
"warning.",
FutureWarning,
stacklevel=find_stack_level(),
)
break
fill_method = "pad"
if limit is lib.no_default:
limit = None
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/frame/methods/test_pct_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,21 @@ def test_pct_change_with_duplicated_indices(fill_method):
index=["a", "b"] * 3,
)
tm.assert_frame_equal(result, expected)


def test_pct_change_none_beginning_no_warning():
# GH#54481
df = DataFrame(
[
[1, None],
[2, 1],
[3, 2],
[4, 3],
[5, 4],
]
)
result = df.pct_change()
expected = DataFrame(
{0: [np.nan, 1, 0.5, 1 / 3, 0.25], 1: [np.nan, np.nan, 1, 0.5, 1 / 3]}
)
tm.assert_frame_equal(result, expected)
8 changes: 8 additions & 0 deletions pandas/tests/series/methods/test_pct_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,11 @@ def test_pct_change_with_duplicated_indices(fill_method):

expected = Series([np.nan, np.nan, 1.0, 0.5, 2.0, 1.0], index=["a", "b"] * 3)
tm.assert_series_equal(result, expected)


def test_pct_change_no_warning_na_beginning():
# GH#54981
ser = Series([None, None, 1, 2, 3])
result = ser.pct_change()
expected = Series([np.nan, np.nan, np.nan, 1, 0.5])
tm.assert_series_equal(result, expected)