Repository: pandas-dev/pandas

Issue:
BUG: resample with TimedeltaIndex, fenceposts are off
brought up in #12925
some examples in #15167 

Resampling fence posts are off when upsampling < 1 Day.

```
In [5]: df = DataFrame(data=[[1, 3], [-5, 10], [0, 0]],index=pd.timedelta_range('00:00:00','00:10:00',freq='5T'))

In [6]: df.resample('1T').asfreq()
Out[6]: 
            0     1
00:00:00  1.0   3.0
00:01:00  NaN   NaN
00:02:00  NaN   NaN
00:03:00  NaN   NaN
00:04:00  NaN   NaN
00:05:00 -5.0  10.0
00:06:00  NaN   NaN
00:07:00  NaN   NaN
00:08:00  NaN   NaN
00:09:00  NaN   NaN

In [8]: df.resample('1T').mean()
Out[8]: 
            0     1
00:00:00  1.0   3.0
00:01:00  NaN   NaN
00:02:00  NaN   NaN
00:03:00  NaN   NaN
00:04:00  NaN   NaN
00:05:00 -5.0  10.0
00:06:00  NaN   NaN
00:07:00  NaN   NaN
00:08:00  NaN   NaN
00:09:00  NaN   NaN
00:10:00  0.0   0.0
```



Discussion:
Isn't this the same as https://github.com/pydata/pandas/issues/12926, which you fixed already?

very similar, but this test case fails as my original soln didn't work.

Same issue appears when downsampling:

```
In [6]: from pandas import *
In [7]: rng = timedelta_range(start='1d', periods=10, freq='d')
In [8]: df = DataFrame({'value':range(10)}, index=rng)
In [9]: df.resample('2D').count()
Out[9]: 
         value
1 days       2
3 days       2
5 days       2
7 days       2
9 days       2
11 days      0

In [10]: df.resample('2D').asfreq()
Out[10]: 
        value
1 days      0
3 days      2
5 days      4
7 days      6
9 days      8
```


Resolving patch:
diff --git a/pandas/core/resample.py b/pandas/core/resample.py
--- a/pandas/core/resample.py
+++ b/pandas/core/resample.py
@@ -963,7 +963,10 @@ def _downsample(self, how, **kwargs):
         return self._wrap_result(result)
 
     def _adjust_binner_for_upsample(self, binner):
-        """ adjust our binner when upsampling """
+        """
+        Adjust our binner when upsampling.
+        The range of a new index should not be outside specified range
+        """
         if self.closed == 'right':
             binner = binner[1:]
         else:
@@ -1156,17 +1159,11 @@ def _get_binner_for_time(self):
         return self.groupby._get_time_delta_bins(self.ax)
 
     def _adjust_binner_for_upsample(self, binner):
-        """ adjust our binner when upsampling """
-        ax = self.ax
-
-        if is_subperiod(ax.freq, self.freq):
-            # We are actually downsampling
-            # but are in the asfreq path
-            # GH 12926
-            if self.closed == 'right':
-                binner = binner[1:]
-            else:
-                binner = binner[:-1]
+        """
+        Adjust our binner when upsampling.
+        The range of a new index is allowed to be greater than original range
+        so we don't need to change the length of a binner, GH 13022
+        """
         return binner
 
 


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
@@ -26,7 +26,6 @@
 from pandas.compat import range, lrange, zip, OrderedDict
 from pandas.errors import UnsupportedFunctionCall
 import pandas.tseries.offsets as offsets
-from pandas.tseries.frequencies import to_offset
 from pandas.tseries.offsets import Minute, BDay
 
 from pandas.core.groupby.groupby import DataError
@@ -626,12 +625,7 @@ def test_asfreq(self, series_and_frame, freq):
         obj = series_and_frame
 
         result = obj.resample(freq).asfreq()
-        if freq == '2D':
-            new_index = obj.index.take(np.arange(0, len(obj.index), 2))
-            new_index.freq = to_offset('2D')
-        else:
-            new_index = self.create_index(obj.index[0], obj.index[-1],
-                                          freq=freq)
+        new_index = self.create_index(obj.index[0], obj.index[-1], freq=freq)
         expected = obj.reindex(new_index)
         assert_almost_equal(result, expected)
 
@@ -2932,6 +2926,17 @@ def test_resample_with_nat(self):
                                                    freq='1S'))
         assert_frame_equal(result, expected)
 
+    def test_resample_as_freq_with_subperiod(self):
+        # GH 13022
+        index = timedelta_range('00:00:00', '00:10:00', freq='5T')
+        df = DataFrame(data={'value': [1, 5, 10]}, index=index)
+        result = df.resample('2T').asfreq()
+        expected_data = {'value': [1, np.nan, np.nan, np.nan, np.nan, 10]}
+        expected = DataFrame(data=expected_data,
+                             index=timedelta_range('00:00:00',
+                                                   '00:10:00', freq='2T'))
+        tm.assert_frame_equal(result, expected)
+
 
 class TestResamplerGrouper(object):