Repository: pandas-dev/pandas

Issue:
loffset in resample does not work with ffill, bfill, pad
#### Minimal example

```python
df = pd.DataFrame(np.random.rand(49),
                  index=pd.DatetimeIndex(start='2018-04-01',
                                         end='2018-04-03',
                                         freq='1h')
                 )

print(df.head())
print(df.resample('D', loffset=pd.Timedelta('12h')).mean())
print(df.resample('D', loffset=pd.Timedelta('12h')).ffill())

```
#### Problem description

`DataFrame` and `DataFrameGroupBy` both provide a `resample` method, for resampling timeseries data. These methods have an optional argument, `loffset`, which accepts a pandas `Timedelta` instance and is used to adjust the output `DataFrame`'s index, once a suitable aggregation method has been used on the resulting `DatetimeIndexResampler` or `DatetimeIndexResamplerGroupby` object.

This works as expected for, for example, `mean`, but fails in the cases of `ffill`, `bfill` and `pad`. The resample is successful, but the `loffset` argument is ignored.

#### Expected Output

                            0
2018-04-01 00:00:00  0.246580
2018-04-01 01:00:00  0.380124
2018-04-01 02:00:00  0.227830
2018-04-01 03:00:00  0.376657
2018-04-01 04:00:00  0.139719
                            0
2018-04-01 12:00:00  0.434068
2018-04-02 12:00:00  0.471917
2018-04-03 12:00:00  0.395527
                            0
2018-04-01 12:00:00  0.246580
2018-04-02 12:00:00  0.402313
2018-04-03 12:00:00  0.395527


#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 61 Stepping 4, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.22.0
pytest: 3.0.7
pip: 9.0.3
setuptools: 39.0.1
Cython: 0.27.3
numpy: 1.14.2
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.3.1
sphinx: 1.7.2
patsy: 0.4.1
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.1.2
openpyxl: 2.4.7
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.3
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.1.9
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

</details>

loffset in resample does not work with ffill, bfill, pad
#### Minimal example

```python
df = pd.DataFrame(np.random.rand(49),
                  index=pd.DatetimeIndex(start='2018-04-01',
                                         end='2018-04-03',
                                         freq='1h')
                 )

print(df.head())
print(df.resample('D', loffset=pd.Timedelta('12h')).mean())
print(df.resample('D', loffset=pd.Timedelta('12h')).ffill())

```
#### Problem description

`DataFrame` and `DataFrameGroupBy` both provide a `resample` method, for resampling timeseries data. These methods have an optional argument, `loffset`, which accepts a pandas `Timedelta` instance and is used to adjust the output `DataFrame`'s index, once a suitable aggregation method has been used on the resulting `DatetimeIndexResampler` or `DatetimeIndexResamplerGroupby` object.

This works as expected for, for example, `mean`, but fails in the cases of `ffill`, `bfill` and `pad`. The resample is successful, but the `loffset` argument is ignored.

#### Expected Output

                            0
2018-04-01 00:00:00  0.246580
2018-04-01 01:00:00  0.380124
2018-04-01 02:00:00  0.227830
2018-04-01 03:00:00  0.376657
2018-04-01 04:00:00  0.139719
                            0
2018-04-01 12:00:00  0.434068
2018-04-02 12:00:00  0.471917
2018-04-03 12:00:00  0.395527
                            0
2018-04-01 12:00:00  0.246580
2018-04-02 12:00:00  0.402313
2018-04-03 12:00:00  0.395527


#### Output of ``pd.show_versions()``

<details>

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 61 Stepping 4, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.22.0
pytest: 3.0.7
pip: 9.0.3
setuptools: 39.0.1
Cython: 0.27.3
numpy: 1.14.2
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.3.1
sphinx: 1.7.2
patsy: 0.4.1
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.2
feather: None
matplotlib: 2.1.2
openpyxl: 2.4.7
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.3
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.1.9
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

</details>



Discussion:
hmm, guess it doesn. a pull-request would be great to fix! (also doesn't work for ``.asfreq()``)
hmm, guess it doesn. a pull-request would be great to fix! (also doesn't work for ``.asfreq()``)

Resolving patch:
diff --git a/pandas/core/resample.py b/pandas/core/resample.py
--- a/pandas/core/resample.py
+++ b/pandas/core/resample.py
@@ -967,6 +967,7 @@ def _upsample(self, method, limit=None, fill_value=None):
             result = obj.reindex(res_index, method=method,
                                  limit=limit, fill_value=fill_value)
 
+        result = self._apply_loffset(result)
         return self._wrap_result(result)
 
     def _wrap_result(self, result):


Test patch:
diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py
--- a/pandas/tests/test_resample.py
+++ b/pandas/tests/test_resample.py
@@ -1182,6 +1182,19 @@ def test_resample_loffset(self):
         expected = ser.resample('w-sun', loffset=-bday).last()
         assert result.index[0] - bday == expected.index[0]
 
+    def test_resample_loffset_upsample(self):
+        # GH 20744
+        rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min')
+        s = Series(np.random.randn(14), index=rng)
+
+        result = s.resample('5min', closed='right', label='right',
+                            loffset=timedelta(minutes=1)).ffill()
+        idx = date_range('1/1/2000', periods=4, freq='5min')
+        expected = Series([s[0], s[5], s[10], s[-1]],
+                          index=idx + timedelta(minutes=1))
+
+        assert_series_equal(result, expected)
+
     def test_resample_loffset_count(self):
         # GH 12725
         start_time = '1/1/2000 00:00:00'