-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
BUG: first("1M") returning two months when first day is last day of month #38331
Conversation
� Conflicts: � doc/source/whatsnew/v1.2.0.rst
pandas/core/generic.py
Outdated
@@ -8426,7 +8426,10 @@ def first(self: FrameOrSeries, offset) -> FrameOrSeries: | |||
return self | |||
|
|||
offset = to_offset(offset) | |||
end_date = end = self.index[0] + offset | |||
if offset._day_opt == "end" and offset.is_on_offset(self.index[0]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use rollforward?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of the if-else? Then no, This would work for offsets like 1M, but not for something like 10 Days. That is also the thing which lead me to putting offset._day_opt == "end"
in. But we could use it like
if offset._day_opt == "end":
end_date = end = offset.rollforward(self.index[0])
else:
end_date = end = self.index[0] + offset
if this would be preferrably
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is an OK solution, but ideally there should be a way to do this with only public methods.
but not for something like 10 Days
can you give an example? (possibly merits a test so i dont try to incorrectly simplify this somewhere down the line) if its only for Tick offsets, special-casing wouldn't be that bad since we already special case them a few lines down
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tick works, thanks.
test_first_subset
in pandas/tests/frame/methods/test_first_and_last.py
covers the behavior and avoids the simplification (as I learned myself a bit earlier :))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
thanks @phofl |
@meeseeksdev backport 1.2.x |
…hen first day is last day of month
@phofl this didn't fix the "2M" case from the issue?
|
Unfortunately you are right. Opened #38446 to adress this. |
…day of month (pandas-dev#38331)" (pandas-dev#38448) This reverts commit 6ecc787.
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff