Skip to content

Commit 0e4e1c2

Browse files
author
Albert Villanova del Moral
committed
Add skipna to pct_change (pandas-dev#25006)
1 parent 2448e52 commit 0e4e1c2

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

pandas/core/generic.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -9948,11 +9948,24 @@ def _check_percentile(self, q):
99489948
"""
99499949

99509950
@Appender(_shared_docs['pct_change'] % _shared_doc_kwargs)
9951-
def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None,
9952-
**kwargs):
9951+
def pct_change(self, periods=1, fill_method=None, limit=None, freq=None,
9952+
skipna=False, **kwargs):
9953+
if skipna and fill_method is not None:
9954+
raise ValueError("cannot pass both skipna and fill_method")
9955+
elif skipna and limit is not None:
9956+
raise ValueError("cannot pass both skipna and limit")
9957+
if skipna and self._typ == 'dataframe':
9958+
return self.apply(
9959+
lambda s: s.pct_change(periods=periods,
9960+
fill_method=fill_method, limit=limit,
9961+
freq=freq, skipna=skipna, **kwargs)
9962+
)
99539963
# TODO: Not sure if above is correct - need someone to confirm.
99549964
axis = self._get_axis_number(kwargs.pop('axis', self._stat_axis_name))
9955-
if fill_method is None:
9965+
if skipna:
9966+
# mask = self.isna()
9967+
data = self.dropna()
9968+
elif fill_method is None:
99569969
data = self
99579970
else:
99589971
data = self.fillna(method=fill_method, limit=limit, axis=axis)
@@ -9963,6 +9976,8 @@ def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None,
99639976
if freq is None:
99649977
mask = isna(com.values_from_object(data))
99659978
np.putmask(rs.values, mask, np.nan)
9979+
if skipna:
9980+
rs = rs.reindex_like(self)
99669981
return rs
99679982

99689983
def _agg_by_level(self, name, axis=0, level=0, skipna=True, **kwargs):

0 commit comments

Comments
 (0)