diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index b7a18da3924c8..29b02c195680f 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -1886,6 +1886,28 @@ def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self): self.assertEqual(str(index.reindex([])[0].tz), 'US/Eastern') self.assertEqual(str(index.reindex(np.array([]))[0].tz), 'US/Eastern') + def test_round(self): + index = pd.DatetimeIndex([ pd.Timestamp('20120827 12:05:00.002'), + pd.Timestamp('20130101 12:05:01'), + pd.Timestamp('20130712 15:10:00'), + pd.Timestamp('20130712 15:10:00.000004') ]) + + result = index.round('1s') + + tm.assert_index_equal(result, pd.DatetimeIndex([ + pd.Timestamp('20120827 12:05:00'), + pd.Timestamp('20130101 12:05:01'), + pd.Timestamp('20130712 15:10:00'), + pd.Timestamp('20130712 15:10:00') ]) ) + + result = index.round('1h') + + tm.assert_index_equal(result, pd.DatetimeIndex([ + pd.Timestamp('20120827 12:00:00'), + pd.Timestamp('20130101 12:00:00'), + pd.Timestamp('20130712 15:00:00'), + pd.Timestamp('20130712 15:00:00') ]) ) + class TestPeriodIndex(Base, tm.TestCase): _holder = PeriodIndex diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py index 202e30cc2eb5e..128fe13a62c4a 100644 --- a/pandas/tseries/index.py +++ b/pandas/tseries/index.py @@ -592,6 +592,11 @@ def _is_dates_only(self): from pandas.core.format import _is_dates_only return _is_dates_only(self.values) + def round(self, freq): + if isinstance(freq, compat.string_types): + freq = to_offset(freq).nanos + return DatetimeIndex(((self.asi8/freq).round()*freq).astype(np.int64)) + @property def _formatter_func(self): from pandas.core.format import _get_format_datetime64