Skip to content

BUG fixing module first() and DateOffset #52487

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

Closed
wants to merge 17 commits into from
Closed
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
Prev Previous commit
Next Next commit
fixing first
  • Loading branch information
DeaMariaLeon committed Apr 20, 2023
commit 621c02862db9740595b90d0911b6984e502cd445
15 changes: 6 additions & 9 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9071,24 +9071,21 @@ def first(self, offset) -> Self:
if len(self.index) == 0:
return self.copy(deep=False)

if isinstance(offset, offsets.DateOffset):
end_date = end = self.index[0] + offset
if end_date in self.index:
end = self.index.searchsorted(end_date, side="left")
return self.iloc[:end]
return self.loc[:end]

offset = to_offset(offset)

if not isinstance(offset, Tick) and offset.is_on_offset(self.index[0]):
if (not isinstance(offset, Tick) and offset.is_on_offset(self.index[0])) and (
type(offset) is not offsets.DateOffset
):
# GH#29623 if first value is end of period, remove offset with n = 1
# before adding the real offset
end_date = end = self.index[0] - offset.base + offset
else:
end_date = end = self.index[0] + offset

# Tick-like, e.g. 3 weeks
if isinstance(offset, Tick) and end_date in self.index:
if (
isinstance(offset, Tick) or (type(offset) is offsets.DateOffset)
) and end_date in self.index:
end = self.index.searchsorted(end_date, side="left")
return self.iloc[:end]

Expand Down