Repository: pandas-dev/pandas

Issue:
Series 'pct_change' with 'fill_method' broken
The `fill_method` argument for `pct_change` does not have any effect on the calculated result. To illustrate:

```python
>>> ser = pd.Series([np.nan, np.nan, 0, 2, 4, 6, np.nan, np.nan])
>>> ser.pct_change()
0         NaN
1         NaN
2         NaN
3         inf
4    1.000000
5    0.500000
6         NaN
7         NaN
dtype: float64

    # Will return same result as above
>>> ser.pct_change(fill_method='pad')
0         NaN
1         NaN
2         NaN
3         inf
4    1.000000
5    0.500000
6         NaN
7         NaN
dtype: float64
```

I would expect rows 6 and 7 above to be 0 as a result of the fill operation. The problem can be traced to the below line of code, where the mask is referencing the original values before any fill logic is applied:

https://github.com/pandas-dev/pandas/blob/a00154dcfe5057cb3fd86653172e74b6893e337d/pandas/core/generic.py#L6893

<details>

INSTALLED VERSIONS
------------------
commit: ce77b79b97d7e827f50ebd0b7ca292fc2d966cc7
python: 3.6.1.final.0
python-bits: 64
OS: Darwin
OS-release: 17.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.0.dev0+364.gce77b79b9.dirty
pytest: None
pip: 9.0.1
setuptools: 27.2.0
Cython: None
numpy: 1.13.1
scipy: 0.19.1
pyarrow: None
xarray: None
IPython: 6.1.0
sphinx: 1.6.5
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.6.0
html5lib: 0.999999999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: 2.7.3.2 (dt dec pq3 ext lo64)
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

</details>


Discussion:

Resolving patch:
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -7488,7 +7488,7 @@ def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None,
                                   **kwargs)) - 1)
         rs = rs.reindex_like(data)
         if freq is None:
-            mask = isna(com._values_from_object(self))
+            mask = isna(com._values_from_object(data))
             np.putmask(rs.values, mask, np.nan)
         return rs
 


Test patch:
diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py
--- a/pandas/tests/frame/test_analytics.py
+++ b/pandas/tests/frame/test_analytics.py
@@ -1941,12 +1941,9 @@ def test_pct_change(self):
         pnl.iat[1, 1] = np.nan
         pnl.iat[2, 3] = 60
 
-        mask = pnl.isnull()
-
         for axis in range(2):
             expected = pnl.ffill(axis=axis) / pnl.ffill(axis=axis).shift(
                 axis=axis) - 1
-            expected[mask] = np.nan
             result = pnl.pct_change(axis=axis, fill_method='pad')
 
             tm.assert_frame_equal(result, expected)
diff --git a/pandas/tests/frame/test_timeseries.py b/pandas/tests/frame/test_timeseries.py
--- a/pandas/tests/frame/test_timeseries.py
+++ b/pandas/tests/frame/test_timeseries.py
@@ -118,7 +118,7 @@ def test_pct_change_shift_over_nas(self):
         df = DataFrame({'a': s, 'b': s})
 
         chg = df.pct_change()
-        expected = Series([np.nan, 0.5, np.nan, 2.5 / 1.5 - 1, .2])
+        expected = Series([np.nan, 0.5, 0., 2.5 / 1.5 - 1, .2])
         edf = DataFrame({'a': expected, 'b': expected})
         assert_frame_equal(chg, edf)
 
diff --git a/pandas/tests/generic/test_generic.py b/pandas/tests/generic/test_generic.py
--- a/pandas/tests/generic/test_generic.py
+++ b/pandas/tests/generic/test_generic.py
@@ -592,6 +592,26 @@ def test_copy_and_deepcopy(self):
                 assert obj_copy is not obj
                 self._compare(obj_copy, obj)
 
+    @pytest.mark.parametrize("periods,fill_method,limit,exp", [
+        (1, "ffill", None, [np.nan, np.nan, np.nan, 1, 1, 1.5, 0, 0]),
+        (1, "ffill", 1, [np.nan, np.nan, np.nan, 1, 1, 1.5, 0, np.nan]),
+        (1, "bfill", None, [np.nan, 0, 0, 1, 1, 1.5, np.nan, np.nan]),
+        (1, "bfill", 1, [np.nan, np.nan, 0, 1, 1, 1.5, np.nan, np.nan]),
+        (-1, "ffill", None, [np.nan, np.nan, -.5, -.5, -.6, 0, 0, np.nan]),
+        (-1, "ffill", 1, [np.nan, np.nan, -.5, -.5, -.6, 0, np.nan, np.nan]),
+        (-1, "bfill", None, [0, 0, -.5, -.5, -.6, np.nan, np.nan, np.nan]),
+        (-1, "bfill", 1, [np.nan, 0, -.5, -.5, -.6, np.nan, np.nan, np.nan])
+    ])
+    def test_pct_change(self, periods, fill_method, limit, exp):
+        vals = [np.nan, np.nan, 1, 2, 4, 10, np.nan, np.nan]
+        obj = self._typ(vals)
+        func = getattr(obj, 'pct_change')
+        res = func(periods=periods, fill_method=fill_method, limit=limit)
+        if type(obj) is DataFrame:
+            tm.assert_frame_equal(res, DataFrame(exp))
+        else:
+            tm.assert_series_equal(res, Series(exp))
+
 
 class TestNDFrame(object):
     # tests that don't fit elsewhere
diff --git a/pandas/tests/generic/test_panel.py b/pandas/tests/generic/test_panel.py
--- a/pandas/tests/generic/test_panel.py
+++ b/pandas/tests/generic/test_panel.py
@@ -45,7 +45,7 @@ def test_to_xarray(self):
           'test_stat_non_defaults_args',
           'test_truncate_out_of_bounds',
           'test_metadata_propagation', 'test_copy_and_deepcopy',
-          'test_sample']:
+          'test_pct_change', 'test_sample']:
 
     def f():
         def tester(self):
diff --git a/pandas/tests/series/test_timeseries.py b/pandas/tests/series/test_timeseries.py
--- a/pandas/tests/series/test_timeseries.py
+++ b/pandas/tests/series/test_timeseries.py
@@ -352,7 +352,7 @@ def test_pct_change_shift_over_nas(self):
         s = Series([1., 1.5, np.nan, 2.5, 3.])
 
         chg = s.pct_change()
-        expected = Series([np.nan, 0.5, np.nan, 2.5 / 1.5 - 1, .2])
+        expected = Series([np.nan, 0.5, 0., 2.5 / 1.5 - 1, .2])
         assert_series_equal(chg, expected)
 
     def test_pct_change_periods_freq(self):