-
Notifications
You must be signed in to change notification settings - Fork 915
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
Add first
and last
method to IndexedFrame
#9710
Conversation
Codecov Report
@@ Coverage Diff @@
## branch-22.02 #9710 +/- ##
================================================
- Coverage 10.49% 10.39% -0.10%
================================================
Files 119 119
Lines 20305 20523 +218
================================================
+ Hits 2130 2134 +4
- Misses 18175 18389 +214
Continue to review full report at Codecov.
|
…pes in date_range
return self.copy() | ||
|
||
pd_offset = pd.tseries.frequencies.to_offset(offset) | ||
to_search = op(pd.Timestamp(self._index._column[idx]), pd_offset) |
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.
What does the op
callable do? and what is the operation being done in this line // what does to_search
repr? jw
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.
If first
is called, then op
is add
to compute the cut-off date counting from the first date in the column; if last
, op
is sub
to compute the that from the last date. to_search
is the cut-off date. Feel free to request changes if the naming/logic isn't very readable to your taste.
if ( | ||
idx == 0 | ||
and not isinstance(pd_offset, pd.tseries.offsets.Tick) | ||
and pd_offset.is_on_offset(pd.Timestamp(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.
does is_on_offset
check for datetimes values only at the end
of the offset?
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.
I think pandas DateOffset
can be MonthBegin
or MonthEnd
. Depending on the string to be either MS
or M
.
In [3]: pd.tseries.frequencies.to_offset('M')
Out[3]: <MonthEnd>
In [4]: pd.tseries.frequencies.to_offset('MS')
Out[4]: <MonthBegin>
I believe is_on_offset
checks if the given datetime falls on the given offset. There isn't much documentation about this but it seems like every kind of offset has this function.
Like Nano
would always return true:
https://github.com/pandas-dev/pandas/blob/878a0225c648cb145949f78085a8ff3f902a1c20/pandas/_libs/tslibs/offsets.pyx#L834-L835
and BusinessDay
would check if the weekday of the given date is monday to friday
https://github.com/pandas-dev/pandas/blob/878a0225c648cb145949f78085a8ff3f902a1c20/pandas/_libs/tslibs/offsets.pyx#L1445-L1448
and for MonthEnd
I believe it resorts to the base method:
https://github.com/pandas-dev/pandas/blob/878a0225c648cb145949f78085a8ff3f902a1c20/pandas/_libs/tslibs/offsets.pyx#L650-L659
Note that +
and -
on the MonthEnd
basically sets the date to the end of that month, same apply to other offsets.
first
and last
methodfirst
and last
method to IndexedFrame
@gpucibot merge |
closes #9600
This PR adds
first
andlast
method toindexed_frame
. This method only applies toIndexedFrame
withDatetimeIndex
and gathers the first or last rows within time range specified byoffset
argument.