diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 0b2b526dfe9e7..488971c13508a 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1516,6 +1516,7 @@ Groupby/Resample/Rolling - Bug in :func:`pandas.core.groupby.GroupBy.first` and :func:`pandas.core.groupby.GroupBy.last` with ``as_index=False`` leading to the loss of timezone information (:issue:`15884`) - Bug in :meth:`DatetimeIndex.resample` when downsampling across a DST boundary (:issue:`8531`) +- Bug in date anchoring for :meth:`DatetimeIndex.resample` with offset :class:`Day` when n > 1 (:issue:`24127`) - Bug where ``ValueError`` is wrongly raised when calling :func:`~pandas.core.groupby.SeriesGroupBy.count` method of a ``SeriesGroupBy`` when the grouping variable only contains NaNs and numpy version < 1.13 (:issue:`21956`). - Multiple bugs in :func:`pandas.core.Rolling.min` with ``closed='left'`` and a diff --git a/pandas/core/resample.py b/pandas/core/resample.py index dc1f94c479a37..6d80d747f21b3 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1587,8 +1587,8 @@ def _get_range_edges(first, last, offset, closed='left', base=0): is_day = isinstance(offset, Day) day_nanos = delta_to_nanoseconds(timedelta(1)) - # #1165 - if (is_day and day_nanos % offset.nanos == 0) or not is_day: + # #1165 and #24127 + if (is_day and not offset.nanos % day_nanos) or not is_day: return _adjust_dates_anchored(first, last, offset, closed=closed, base=base) diff --git a/pandas/tests/groupby/test_timegrouper.py b/pandas/tests/groupby/test_timegrouper.py index 183ccfb5182a2..cb7b419710837 100644 --- a/pandas/tests/groupby/test_timegrouper.py +++ b/pandas/tests/groupby/test_timegrouper.py @@ -43,8 +43,8 @@ def test_groupby_with_timegrouper(self): expected = DataFrame( {'Quantity': 0}, - index=date_range('20130901 13:00:00', - '20131205 13:00:00', freq='5D', + index=date_range('20130901', + '20131205', freq='5D', name='Date', closed='left')) expected.iloc[[0, 6, 18], 0] = np.array([24, 6, 9], dtype='int64') diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index b287eb468cd94..69fb92486d523 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -1463,3 +1463,29 @@ def f(data, add_arg): result = df.groupby("A").resample("D").agg(f, multiplier) expected = df.groupby("A").resample('D').mean().multiply(multiplier) assert_frame_equal(result, expected) + + @pytest.mark.parametrize('k', [1, 2, 3]) + @pytest.mark.parametrize('n1, freq1, n2, freq2', [ + (30, 'S', 0.5, 'Min'), + (60, 'S', 1, 'Min'), + (3600, 'S', 1, 'H'), + (60, 'Min', 1, 'H'), + (21600, 'S', 0.25, 'D'), + (86400, 'S', 1, 'D'), + (43200, 'S', 0.5, 'D'), + (1440, 'Min', 1, 'D'), + (12, 'H', 0.5, 'D'), + (24, 'H', 1, 'D'), + ]) + def test_resample_equivalent_offsets(self, n1, freq1, n2, freq2, k): + # GH 24127 + n1_ = n1 * k + n2_ = n2 * k + s = pd.Series(0, index=pd.date_range('19910905 13:00', + '19911005 07:00', + freq=freq1)) + s = s + range(len(s)) + + result1 = s.resample(str(n1_) + freq1).mean() + result2 = s.resample(str(n2_) + freq2).mean() + assert_series_equal(result1, result2)