Skip to content

BUG: df.resample('MS', closed='right') incorrectly places bins #55283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,17 @@ def _adjust_bin_edges(
) -> tuple[DatetimeIndex, npt.NDArray[np.int64]]:
# Some hacks for > daily data, see #1471, #1458, #1483

if self.freq != "D" and is_superperiod(self.freq, "D"):
if self.freq.name in ("BM", "ME", "W") or self.freq.name.split("-")[0] in (
"BQ",
"BA",
"Q",
"A",
"W",
):
# If the right end-point is on the last day of the month, roll forwards
# until the last moment of that day. Note that we only do this for offsets
# which correspond to the end of a super-daily period - "month start", for
# example, is excluded.
if self.closed == "right":
# GH 21459, GH 9119: Adjust the bins relative to the wall time
edges_dti = binner.tz_localize(None)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def test_resample_reresample(unit):
s = Series(np.random.default_rng(2).random(len(dti)), dti)
bs = s.resample("B", closed="right", label="right").mean()
result = bs.resample("8H").mean()
assert len(result) == 22
assert len(result) == 25
Comment on lines 661 to +663
Copy link
Member Author

@MarcoGorelli MarcoGorelli Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was wrong to begin with #55282

now it at least mirrors the behaviour of 'MS', which may have empty buckets at the end, but at least doesn't show wrong results #55284

assert isinstance(result.index.freq, offsets.DateOffset)
assert result.index.freq == offsets.Hour(8)

Expand Down