Repository: pandas-dev/pandas

Issue:
Resample method doesn't round timestamps with prime numbers frequency
Hi,

I've found this issue when I tried to resample at different frequencies. When the frequency is a prime number, the first timestamp is not rounded and it takes the first timestamp in the file. Do I need an extra parameter to round timestamps?

``` python
csv = r"""Date,Time,Ask,Bid,AskVolume,BidVolume
11-08-2014,00:00:01.093,1.34046,1.34042,1.25,1.69
11-08-2014,00:00:02.159,1.34047,1.34043,4.69,1
11-08-2014,00:00:02.667,1.34046,1.34042,1.32,2.44
11-08-2014,00:00:03.175,1.34046,1.34043,1.32,1
11-08-2014,00:00:07.058,1.34046,1.34043,3.75,1.69
11-08-2014,00:00:07.362,1.34043,1.34041,2.25,1
11-08-2014,00:00:08.324,1.34043,1.34042,1.5,1
11-08-2014,00:00:08.830,1.34045,1.34043,2.44,1
11-08-2014,00:00:08.982,1.34043,1.34041,3,1.69
11-08-2014,00:00:09.815,1.34042,1.34041,1,1
11-08-2014,00:00:10.540,1.34043,1.34041,2.63,1
11-08-2014,00:00:11.061,1.34043,1.3404,7.99,2.25
11-08-2014,00:00:11.617,1.34042,1.34041,1.13,1
11-08-2014,00:00:13.607,1.34043,1.3404,7.5,3.19
11-08-2014,00:00:14.535,1.34043,1.34041,5.25,1
11-08-2014,00:00:15.525,1.34043,1.34041,3.38,1.25
11-08-2014,00:00:17.960,1.34043,1.34041,4.5,1.25
11-08-2014,00:00:20.674,1.34042,1.3404,1.13,2.07
11-08-2014,00:00:21.191,1.34041,1.3404,1,1"""
filename = 'sample.csv'
f = open(filename, 'w')
f.write(csv)
f.close()
df = pd.read_csv(filename, parse_dates={'Timestamp': ['Date', 'Time']}, index_col='Timestamp')
print df.resample('6s', fill_method='bfill')
print df.resample('7s', fill_method='bfill')
print df.resample('11s', fill_method='bfill')
print df.resample('13s', fill_method='bfill')
print df.resample('17s', fill_method='bfill')
```

Thanks,
Paola



Discussion:

Resolving patch:
diff --git a/pandas/tseries/resample.py b/pandas/tseries/resample.py
--- a/pandas/tseries/resample.py
+++ b/pandas/tseries/resample.py
@@ -6,7 +6,7 @@
 from pandas.tseries.frequencies import to_offset, is_subperiod, is_superperiod
 from pandas.tseries.index import DatetimeIndex, date_range
 from pandas.tseries.tdi import TimedeltaIndex
-from pandas.tseries.offsets import DateOffset, Tick, _delta_to_nanoseconds
+from pandas.tseries.offsets import DateOffset, Tick, Day, _delta_to_nanoseconds
 from pandas.tseries.period import PeriodIndex, period_range
 import pandas.tseries.tools as tools
 import pandas.core.common as com
@@ -385,9 +385,11 @@ def _get_range_edges(first, last, offset, closed='left', base=0):
         offset = to_offset(offset)
 
     if isinstance(offset, Tick):
+        is_day = isinstance(offset, Day)
         day_nanos = _delta_to_nanoseconds(timedelta(1))
+
         # #1165
-        if (day_nanos % offset.nanos) == 0:
+        if (is_day and day_nanos % offset.nanos == 0) or not is_day:
             return _adjust_dates_anchored(first, last, offset,
                                           closed=closed, base=base)
 


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
@@ -176,6 +176,54 @@ def test_resample_with_timedeltas(self):
 
         assert_frame_equal(result, expected)
 
+    def test_resample_rounding(self):
+        # GH 8371
+        # odd results when rounding is needed
+
+        data = """date,time,value
+11-08-2014,00:00:01.093,1
+11-08-2014,00:00:02.159,1
+11-08-2014,00:00:02.667,1
+11-08-2014,00:00:03.175,1
+11-08-2014,00:00:07.058,1
+11-08-2014,00:00:07.362,1
+11-08-2014,00:00:08.324,1
+11-08-2014,00:00:08.830,1
+11-08-2014,00:00:08.982,1
+11-08-2014,00:00:09.815,1
+11-08-2014,00:00:10.540,1
+11-08-2014,00:00:11.061,1
+11-08-2014,00:00:11.617,1
+11-08-2014,00:00:13.607,1
+11-08-2014,00:00:14.535,1
+11-08-2014,00:00:15.525,1
+11-08-2014,00:00:17.960,1
+11-08-2014,00:00:20.674,1
+11-08-2014,00:00:21.191,1"""
+
+        from pandas.compat import StringIO
+        df = pd.read_csv(StringIO(data), parse_dates={'timestamp': ['date', 'time']}, index_col='timestamp')
+        df.index.name = None
+        result = df.resample('6s', how='sum')
+        expected = DataFrame({'value' : [4,9,4,2]},index=date_range('2014-11-08',freq='6s',periods=4))
+        assert_frame_equal(result,expected)
+
+        result = df.resample('7s', how='sum')
+        expected = DataFrame({'value' : [4,10,4,1]},index=date_range('2014-11-08',freq='7s',periods=4))
+        assert_frame_equal(result,expected)
+
+        result = df.resample('11s', how='sum')
+        expected = DataFrame({'value' : [11,8]},index=date_range('2014-11-08',freq='11s',periods=2))
+        assert_frame_equal(result,expected)
+
+        result = df.resample('13s', how='sum')
+        expected = DataFrame({'value' : [13,6]},index=date_range('2014-11-08',freq='13s',periods=2))
+        assert_frame_equal(result,expected)
+
+        result = df.resample('17s', how='sum')
+        expected = DataFrame({'value' : [16,3]},index=date_range('2014-11-08',freq='17s',periods=2))
+        assert_frame_equal(result,expected)
+
     def test_resample_basic_from_daily(self):
         # from daily
         dti = DatetimeIndex(