Skip to content

Commit

Permalink
Fix df case
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl committed Sep 5, 2023
1 parent 50c453b commit b976afd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
25 changes: 14 additions & 11 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11709,17 +11709,20 @@ def pct_change(
stacklevel=find_stack_level(),
)
if fill_method is lib.no_default:
mask = self.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(),
)
for _, col in self.items():
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)

0 comments on commit b976afd

Please sign in to comment.