Skip to content

Commit b7ee829

Browse files
ms7463jreback
authored andcommitted
BUG - anchoring dates for resample with Day(n>1) (#24159)
1 parent 1d18430 commit b7ee829

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,7 @@ Groupby/Resample/Rolling
15241524

15251525
- 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`)
15261526
- Bug in :meth:`DatetimeIndex.resample` when downsampling across a DST boundary (:issue:`8531`)
1527+
- Bug in date anchoring for :meth:`DatetimeIndex.resample` with offset :class:`Day` when n > 1 (:issue:`24127`)
15271528
- Bug where ``ValueError`` is wrongly raised when calling :func:`~pandas.core.groupby.SeriesGroupBy.count` method of a
15281529
``SeriesGroupBy`` when the grouping variable only contains NaNs and numpy version < 1.13 (:issue:`21956`).
15291530
- Multiple bugs in :func:`pandas.core.Rolling.min` with ``closed='left'`` and a

pandas/core/resample.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1587,8 +1587,8 @@ def _get_range_edges(first, last, offset, closed='left', base=0):
15871587
is_day = isinstance(offset, Day)
15881588
day_nanos = delta_to_nanoseconds(timedelta(1))
15891589

1590-
# #1165
1591-
if (is_day and day_nanos % offset.nanos == 0) or not is_day:
1590+
# #1165 and #24127
1591+
if (is_day and not offset.nanos % day_nanos) or not is_day:
15921592
return _adjust_dates_anchored(first, last, offset,
15931593
closed=closed, base=base)
15941594

pandas/tests/groupby/test_timegrouper.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def test_groupby_with_timegrouper(self):
4343

4444
expected = DataFrame(
4545
{'Quantity': 0},
46-
index=date_range('20130901 13:00:00',
47-
'20131205 13:00:00', freq='5D',
46+
index=date_range('20130901',
47+
'20131205', freq='5D',
4848
name='Date', closed='left'))
4949
expected.iloc[[0, 6, 18], 0] = np.array([24, 6, 9], dtype='int64')
5050

pandas/tests/resample/test_datetime_index.py

+26
Original file line numberDiff line numberDiff line change
@@ -1463,3 +1463,29 @@ def f(data, add_arg):
14631463
result = df.groupby("A").resample("D").agg(f, multiplier)
14641464
expected = df.groupby("A").resample('D').mean().multiply(multiplier)
14651465
assert_frame_equal(result, expected)
1466+
1467+
@pytest.mark.parametrize('k', [1, 2, 3])
1468+
@pytest.mark.parametrize('n1, freq1, n2, freq2', [
1469+
(30, 'S', 0.5, 'Min'),
1470+
(60, 'S', 1, 'Min'),
1471+
(3600, 'S', 1, 'H'),
1472+
(60, 'Min', 1, 'H'),
1473+
(21600, 'S', 0.25, 'D'),
1474+
(86400, 'S', 1, 'D'),
1475+
(43200, 'S', 0.5, 'D'),
1476+
(1440, 'Min', 1, 'D'),
1477+
(12, 'H', 0.5, 'D'),
1478+
(24, 'H', 1, 'D'),
1479+
])
1480+
def test_resample_equivalent_offsets(self, n1, freq1, n2, freq2, k):
1481+
# GH 24127
1482+
n1_ = n1 * k
1483+
n2_ = n2 * k
1484+
s = pd.Series(0, index=pd.date_range('19910905 13:00',
1485+
'19911005 07:00',
1486+
freq=freq1))
1487+
s = s + range(len(s))
1488+
1489+
result1 = s.resample(str(n1_) + freq1).mean()
1490+
result2 = s.resample(str(n2_) + freq2).mean()
1491+
assert_series_equal(result1, result2)

0 commit comments

Comments
 (0)