Repository: pandas-dev/pandas

Issue:
rolling_mean with freq='D' returns all NaNs when there is exactly 1 data point per day
related to #3020

``` python
$ python
Python 2.7.4 (default, Apr 23 2013, 12:22:04) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> import pandas as pd
>>> pd.__version__
'0.12.0'
>>> indices = [datetime.datetime(1975, 1, i, 12, 0) for i in range(1, 6)]
>>> series = pd.Series(range(1, 6), index=indices)
>>> series = series.map(lambda x: float(x))  # range() returns ints, so force to float
>>> series = series.sort_index()  # already sorted, but just to be clear
>>> series  # here's what our input series looks like
1975-01-01 12:00:00    1
1975-01-02 12:00:00    2
1975-01-03 12:00:00    3
1975-01-04 12:00:00    4
1975-01-05 12:00:00    5
dtype: float64
>>> pd.rolling_mean(series, window=2, freq='D')  # these results will be wrong
1975-01-01   NaN
1975-01-02   NaN
1975-01-03   NaN
1975-01-04   NaN
1975-01-05   NaN
Freq: D, dtype: float64
>>> better_series = series.append(pd.Series([3.0], index=[datetime.datetime(1975, 1, 3, 6, 0)]))
>>> better_series = better_series.sort_index()
>>> better_series  # here's a revised input with more than one datapoint on one of the days
1975-01-01 12:00:00    1
1975-01-02 12:00:00    2
1975-01-03 06:00:00    3
1975-01-03 12:00:00    3
1975-01-04 12:00:00    4
1975-01-05 12:00:00    5
dtype: float64
>>> pd.rolling_mean(better_series, window=2, freq='D')  # These results will be correct and are what I expected above
1975-01-01    NaN
1975-01-02    1.5
1975-01-03    2.5
1975-01-04    3.5
1975-01-05    4.5
Freq: D, dtype: float64
```



Discussion:
This is an issue with resample itself (the `freq` keyword triggers a resample). With master:

```
In [22]: series.resample('D')
Out[22]:
1975-01-01   NaN
1975-01-02   NaN
1975-01-03   NaN
1975-01-04   NaN
1975-01-05   NaN
Freq: D, dtype: float64
```


Resolving patch:
diff --git a/pandas/tseries/resample.py b/pandas/tseries/resample.py
--- a/pandas/tseries/resample.py
+++ b/pandas/tseries/resample.py
@@ -246,7 +246,9 @@ def _resample_timestamps(self):
 
         # Determine if we're downsampling
         if axlabels.freq is not None or axlabels.inferred_freq is not None:
+
             if len(grouper.binlabels) < len(axlabels) or self.how is not None:
+                # downsample
                 grouped = obj.groupby(grouper, axis=self.axis)
                 result = grouped.aggregate(self._agg_method)
             else:
@@ -259,8 +261,15 @@ def _resample_timestamps(self):
                 else:
                     res_index = binner[:-1]
 
-                result = obj.reindex(res_index, method=self.fill_method,
-                                     limit=self.limit)
+                # if we have the same frequency as our axis, then we are equal sampling
+                # even if how is None
+                if self.fill_method is None and self.limit is None and to_offset(
+                    axlabels.inferred_freq) == self.freq:
+                    result = obj.copy()
+                    result.index = res_index
+                else:
+                    result = obj.reindex(res_index, method=self.fill_method,
+                                         limit=self.limit)
         else:
             # Irregular data, have to use groupby
             grouped = obj.groupby(grouper, axis=self.axis)


Test patch:
diff --git a/pandas/tseries/tests/test_resample.py b/pandas/tseries/tests/test_resample.py
--- a/pandas/tseries/tests/test_resample.py
+++ b/pandas/tseries/tests/test_resample.py
@@ -160,6 +160,32 @@ def test_resample_basic_from_daily(self):
         self.assertEquals(result.irow(5), s['1/9/2005'])
         self.assertEqual(result.index.name, 'index')
 
+    def test_resample_upsampling_picked_but_not_correct(self):
+
+        # Test for issue #3020
+        dates = date_range('01-Jan-2014','05-Jan-2014', freq='D')
+        series = Series(1, index=dates)
+
+        result = series.resample('D')
+        self.assertEquals(result.index[0], dates[0])
+
+        # GH 5955
+        # incorrect deciding to upsample when the axis frequency matches the resample frequency
+
+        import datetime
+        s = Series(np.arange(1.,6),index=[datetime.datetime(1975, 1, i, 12, 0) for i in range(1, 6)])
+        expected = Series(np.arange(1.,6),index=date_range('19750101',periods=5,freq='D'))
+
+        result = s.resample('D',how='count')
+        assert_series_equal(result,Series(1,index=expected.index))
+
+        result1 = s.resample('D',how='sum')
+        result2 = s.resample('D',how='mean')
+        result3 = s.resample('D')
+        assert_series_equal(result1,expected)
+        assert_series_equal(result2,expected)
+        assert_series_equal(result3,expected)
+
     def test_resample_frame_basic(self):
         df = tm.makeTimeDataFrame()
 
@@ -1078,15 +1104,6 @@ def test_all_values_single_bin(self):
         result = s.resample("A", how='mean')
         tm.assert_almost_equal(result[0], s.mean())
 
-    def test_resample_doesnt_truncate(self):
-        # Test for issue #3020
-        import pandas as pd
-        dates = pd.date_range('01-Jan-2014','05-Jan-2014', freq='D')
-        series = Series(1, index=dates)
-
-        result = series.resample('D')
-        self.assertEquals(result.index[0], dates[0])
-
     def test_evenly_divisible_with_no_extra_bins(self):
         # 4076
         # when the frequency is evenly divisible, sometimes extra bins