-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: add Raises, Examples and See Also sections to methods at_time/between_time/first/last #20725
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6761,13 +6761,42 @@ def at_time(self, time, asof=False): | |
""" | ||
Select values at particular time of day (e.g. 9:30AM). | ||
|
||
Raises | ||
------ | ||
TypeError | ||
If the index is not a :class:`DatetimeIndex` | ||
|
||
Parameters | ||
---------- | ||
time : datetime.time or string | ||
|
||
Returns | ||
------- | ||
values_at_time : type of caller | ||
|
||
Examples | ||
-------- | ||
>>> i = pd.date_range('2018-04-09', periods=4, freq='12H') | ||
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i) | ||
>>> ts | ||
A | ||
2018-04-09 00:00:00 1 | ||
2018-04-09 12:00:00 2 | ||
2018-04-10 00:00:00 3 | ||
2018-04-10 12:00:00 4 | ||
|
||
>>> ts.at_time('12:00') | ||
A | ||
2018-04-09 12:00:00 2 | ||
2018-04-10 12:00:00 4 | ||
|
||
See Also | ||
-------- | ||
between_time : Select values between particular times of the day | ||
first : Select initial periods of time series based on a date offset | ||
last : Select final periods of time series based on a date offset | ||
DatetimeIndex.indexer_at_time : Get just the index locations for | ||
values at particular time of the day | ||
""" | ||
try: | ||
indexer = self.index.indexer_at_time(time, asof=asof) | ||
|
@@ -6780,6 +6809,14 @@ def between_time(self, start_time, end_time, include_start=True, | |
""" | ||
Select values between particular times of the day (e.g., 9:00-9:30 AM). | ||
|
||
By setting ``start_time`` to be later than ``end_time``, | ||
you can get the times that are *not* between the two times. | ||
|
||
Raises | ||
------ | ||
TypeError | ||
If the index is not a :class:`DatetimeIndex` | ||
|
||
Parameters | ||
---------- | ||
start_time : datetime.time or string | ||
|
@@ -6790,6 +6827,38 @@ def between_time(self, start_time, end_time, include_start=True, | |
Returns | ||
------- | ||
values_between_time : type of caller | ||
|
||
Examples | ||
-------- | ||
>>> i = pd.date_range('2018-04-09', periods=4, freq='1D20min') | ||
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i) | ||
>>> ts | ||
A | ||
2018-04-09 00:00:00 1 | ||
2018-04-10 00:20:00 2 | ||
2018-04-11 00:40:00 3 | ||
2018-04-12 01:00:00 4 | ||
|
||
>>> ts.between_time('0:15', '0:45') | ||
A | ||
2018-04-10 00:20:00 2 | ||
2018-04-11 00:40:00 3 | ||
|
||
You get the times that are *not* between two times by setting | ||
``start_time`` later than ``end_time``: | ||
|
||
>>> ts.between_time('0:45', '0:15') | ||
A | ||
2018-04-09 00:00:00 1 | ||
2018-04-12 01:00:00 4 | ||
|
||
See Also | ||
-------- | ||
at_time : Select values at a particular time of the day | ||
first : Select initial periods of time series based on a date offset | ||
last : Select final periods of time series based on a date offset | ||
DatetimeIndex.indexer_between_time : Get just the index locations for | ||
values between particular times of the day | ||
""" | ||
try: | ||
indexer = self.index.indexer_between_time( | ||
|
@@ -7043,22 +7112,50 @@ def first(self, offset): | |
Convenience method for subsetting initial periods of time series data | ||
based on a date offset. | ||
|
||
Raises | ||
------ | ||
TypeError | ||
If the index is not a :class:`DatetimeIndex` | ||
|
||
Parameters | ||
---------- | ||
offset : string, DateOffset, dateutil.relativedelta | ||
|
||
Examples | ||
-------- | ||
ts.first('10D') -> First 10 days | ||
>>> i = pd.date_range('2018-04-09', periods=4, freq='2D') | ||
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i) | ||
>>> ts | ||
A | ||
2018-04-09 1 | ||
2018-04-11 2 | ||
2018-04-13 3 | ||
2018-04-15 4 | ||
|
||
Get the rows for the first 3 days: | ||
|
||
>>> ts.first('3D') | ||
A | ||
2018-04-09 1 | ||
2018-04-11 2 | ||
|
||
Notice the data for 3 first calender days were returned, not the first | ||
3 days observed in the dataset, and therefore data for 2018-04-13 was | ||
not returned. | ||
|
||
Returns | ||
------- | ||
subset : type of caller | ||
|
||
See Also | ||
-------- | ||
last : Select final periods of time series based on a date offset | ||
at_time : Select values at a particular time of the day | ||
between_time : Select values between particular times of the day | ||
""" | ||
from pandas.tseries.frequencies import to_offset | ||
if not isinstance(self.index, DatetimeIndex): | ||
raise NotImplementedError("'first' only supports a DatetimeIndex " | ||
"index") | ||
raise TypeError("'first' only supports a DatetimeIndex index") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add some tests that assert the TypeError? (was this not tested before)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They were not there beforehand, so I've added. A question: There exists test for Series.first/last, but not DataFrame.first/last. Should i copy the relevant tests from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure that would be great (do a search for them as well, it maybe that they are elsewhere, but they should be in |
||
|
||
if len(self.index) == 0: | ||
return self | ||
|
@@ -7079,22 +7176,50 @@ def last(self, offset): | |
Convenience method for subsetting final periods of time series data | ||
based on a date offset. | ||
|
||
Raises | ||
------ | ||
TypeError | ||
If the index is not a :class:`DatetimeIndex` | ||
|
||
Parameters | ||
---------- | ||
offset : string, DateOffset, dateutil.relativedelta | ||
|
||
Examples | ||
-------- | ||
ts.last('5M') -> Last 5 months | ||
>>> i = pd.date_range('2018-04-09', periods=4, freq='2D') | ||
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i) | ||
>>> ts | ||
A | ||
2018-04-09 1 | ||
2018-04-11 2 | ||
2018-04-13 3 | ||
2018-04-15 4 | ||
|
||
Get the rows for the last 3 days: | ||
|
||
>>> ts.last('3D') | ||
A | ||
2018-04-13 3 | ||
2018-04-15 4 | ||
|
||
Notice the data for 3 last calender days were returned, not the last | ||
3 observed days in the dataset, and therefore data for 2018-04-11 was | ||
not returned. | ||
|
||
Returns | ||
------- | ||
subset : type of caller | ||
|
||
See Also | ||
-------- | ||
first : Select initial periods of time series based on a date offset | ||
at_time : Select values at a particular time of the day | ||
between_time : Select values between particular times of the day | ||
""" | ||
from pandas.tseries.frequencies import to_offset | ||
if not isinstance(self.index, DatetimeIndex): | ||
raise NotImplementedError("'last' only supports a DatetimeIndex " | ||
"index") | ||
raise TypeError("'last' only supports a DatetimeIndex index") | ||
|
||
if len(self.index) == 0: | ||
return self | ||
|
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.
put this in a Raises section instead