Skip to content

Commit aaa2760

Browse files
authored
BUG: Series.pct_change with empty Series (#57058)
1 parent 622f31c commit aaa2760

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ including other versions of pandas.
1414
Fixed regressions
1515
~~~~~~~~~~~~~~~~~
1616
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
17+
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
1718

1819
.. ---------------------------------------------------------------------------
1920
.. _whatsnew_221.bug_fixes:

pandas/core/generic.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -12148,19 +12148,20 @@ def pct_change(
1214812148
if limit is lib.no_default:
1214912149
cols = self.items() if self.ndim == 2 else [(None, self)]
1215012150
for _, col in cols:
12151-
mask = col.isna().values
12152-
mask = mask[np.argmax(~mask) :]
12153-
if mask.any():
12154-
warnings.warn(
12155-
"The default fill_method='pad' in "
12156-
f"{type(self).__name__}.pct_change is deprecated and will "
12157-
"be removed in a future version. Either fill in any "
12158-
"non-leading NA values prior to calling pct_change or "
12159-
"specify 'fill_method=None' to not fill NA values.",
12160-
FutureWarning,
12161-
stacklevel=find_stack_level(),
12162-
)
12163-
break
12151+
if len(col) > 0:
12152+
mask = col.isna().values
12153+
mask = mask[np.argmax(~mask) :]
12154+
if mask.any():
12155+
warnings.warn(
12156+
"The default fill_method='pad' in "
12157+
f"{type(self).__name__}.pct_change is deprecated and "
12158+
"will be removed in a future version. Either fill in "
12159+
"any non-leading NA values prior to calling pct_change "
12160+
"or specify 'fill_method=None' to not fill NA values.",
12161+
FutureWarning,
12162+
stacklevel=find_stack_level(),
12163+
)
12164+
break
1216412165
fill_method = "pad"
1216512166
if limit is lib.no_default:
1216612167
limit = None

pandas/tests/series/methods/test_pct_change.py

+8
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,11 @@ def test_pct_change_no_warning_na_beginning():
118118
result = ser.pct_change()
119119
expected = Series([np.nan, np.nan, np.nan, 1, 0.5])
120120
tm.assert_series_equal(result, expected)
121+
122+
123+
def test_pct_change_empty():
124+
# GH 57056
125+
ser = Series([], dtype="float64")
126+
expected = ser.copy()
127+
result = ser.pct_change(periods=0)
128+
tm.assert_series_equal(expected, result)

0 commit comments

Comments
 (0)