Skip to content

Commit c470fdd

Browse files
author
tp
committed
Improve doc strings, also indexer_at/between/_time
1 parent 1f6907b commit c470fdd

File tree

2 files changed

+86
-45
lines changed

2 files changed

+86
-45
lines changed

pandas/core/generic.py

+68-36
Original file line numberDiff line numberDiff line change
@@ -6703,9 +6703,10 @@ def at_time(self, time, asof=False):
67036703
"""
67046704
Select values at particular time of day (e.g. 9:30AM).
67056705
6706-
Notes
6706+
Raises
67076707
-----
6708-
For this method to work, the index must to be a :class:`DatetimeIndex`
6708+
AttributeError
6709+
If the index is not a :class:`DatetimeIndex`
67096710
67106711
Parameters
67116712
----------
@@ -6717,25 +6718,27 @@ def at_time(self, time, asof=False):
67176718
67186719
Examples
67196720
--------
6720-
>>> i = pd.date_range('2018-04-09', periods=4, freq='4D2min')
6721+
>>> i = pd.date_range('2018-04-09', periods=4, freq='12H')
67216722
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i)
67226723
>>> ts
67236724
A
6724-
date
67256725
2018-04-09 00:00:00 1
6726-
2018-04-13 00:02:00 2
6727-
2018-04-17 00:04:00 3
6728-
2018-04-21 00:06:00 4
6729-
>>> ts.at_time('0:02')
6726+
2018-04-09 12:00:00 2
6727+
2018-04-10 00:00:00 3
6728+
2018-04-10 12:00:00 4
6729+
6730+
>>> ts.at_time('12:00')
67306731
A
6731-
date
6732-
2018-04-13 00:02:00 2
6732+
2018-04-09 12:00:00 2
6733+
2018-04-10 12:00:00 4
67336734
67346735
See Also
67356736
--------
67366737
between_time : Select values between particular times of the day
67376738
first : Select initial periods of time series based on a date offset
67386739
last : Select final periods of time series based on a date offset
6740+
DatetimeIndex.indexer_at_time : Get just the index locations for
6741+
values at particular time of the day
67396742
"""
67406743
try:
67416744
indexer = self.index.indexer_at_time(time, asof=asof)
@@ -6748,9 +6751,13 @@ def between_time(self, start_time, end_time, include_start=True,
67486751
"""
67496752
Select values between particular times of the day (e.g., 9:00-9:30 AM).
67506753
6751-
Notes
6754+
By setting ``start_time`` to be later than ``end_time``,
6755+
you can get the times that are *not* between the two times.
6756+
6757+
Raises
67526758
-----
6753-
For this method to work, the index must to be a :class:`DatetimeIndex`
6759+
AttributeError
6760+
If the index is not a :class:`DatetimeIndex`
67546761
67556762
Parameters
67566763
----------
@@ -6765,24 +6772,35 @@ def between_time(self, start_time, end_time, include_start=True,
67656772
67666773
Examples
67676774
--------
6768-
>>> i = pd.date_range('2018-04-09', periods=4, freq='2min')
6775+
>>> i = pd.date_range('2018-04-09', periods=4, freq='1D20min')
67696776
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i)
67706777
>>> ts
67716778
A
67726779
2018-04-09 00:00:00 1
6773-
2018-04-09 00:02:00 2
6774-
2018-04-09 00:04:00 3
6775-
2018-04-09 00:06:00 4
6776-
>>> ts.between_time('0:02', '0:04')
6780+
2018-04-10 00:20:00 2
6781+
2018-04-11 00:40:00 3
6782+
2018-04-12 01:00:00 4
6783+
6784+
>>> ts.between_time('0:15', '0:45')
67776785
A
6778-
2018-04-09 00:02:00 2
6779-
2018-04-09 00:04:00 3
6786+
2018-04-10 00:20:00 2
6787+
2018-04-11 00:40:00 3
6788+
6789+
You get the times that are *not* between two times by setting
6790+
``start_time`` later than ``end_time``:
6791+
6792+
>>> ts.between_time('0:45', '0:15')
6793+
A
6794+
2018-04-09 00:00:00 1
6795+
2018-04-12 01:00:00 4
67806796
67816797
See Also
67826798
--------
67836799
at_time : Select values at a particular time of the day
67846800
first : Select initial periods of time series based on a date offset
67856801
last : Select final periods of time series based on a date offset
6802+
DatetimeIndex.indexer_between_time : Get just the index locations for
6803+
values between particular times of the day
67866804
"""
67876805
try:
67886806
indexer = self.index.indexer_between_time(
@@ -7036,29 +7054,36 @@ def first(self, offset):
70367054
Convenience method for subsetting initial periods of time series data
70377055
based on a date offset.
70387056
7039-
Notes
7057+
Raises
70407058
-----
7041-
For this method to work, the index must to be a :class:`DatetimeIndex`
7059+
NotImplementedError
7060+
If the index is not a :class:`DatetimeIndex`
70427061
70437062
Parameters
70447063
----------
70457064
offset : string, DateOffset, dateutil.relativedelta
70467065
70477066
Examples
70487067
--------
7049-
>>> i = pd.date_range('2018-04-09', periods=4, freq='D')
7068+
>>> i = pd.date_range('2018-04-09', periods=4, freq='2D')
70507069
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i)
70517070
>>> ts
70527071
A
70537072
2018-04-09 1
7054-
2018-04-10 2
7055-
2018-04-11 3
7056-
2018-04-12 4
7073+
2018-04-11 2
7074+
2018-04-13 3
7075+
2018-04-15 4
7076+
7077+
Get the rows for the first 3 days:
7078+
70577079
>>> ts.first('3D')
70587080
A
70597081
2018-04-09 1
7060-
2018-04-10 2
7061-
2018-04-11 3
7082+
2018-04-11 2
7083+
7084+
Notice the data for 3 first calender days were returned, not the first
7085+
3 days observed in the dataset, and therefore data for 2018-04-13 was
7086+
not returned.
70627087
70637088
Returns
70647089
-------
@@ -7094,29 +7119,36 @@ def last(self, offset):
70947119
Convenience method for subsetting final periods of time series data
70957120
based on a date offset.
70967121
7097-
Notes
7122+
Raises
70987123
-----
7099-
For this method to work, the index must to be a :class:`DatetimeIndex`
7124+
NotImplementedError
7125+
If the index is not a :class:`DatetimeIndex`
71007126
71017127
Parameters
71027128
----------
71037129
offset : string, DateOffset, dateutil.relativedelta
71047130
71057131
Examples
71067132
--------
7107-
>>> i = pd.date_range('2018-04-09', periods=4, freq='D')
7133+
>>> i = pd.date_range('2018-04-09', periods=4, freq='2D')
71087134
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i)
71097135
>>> ts
71107136
A
71117137
2018-04-09 1
7112-
2018-04-10 2
7113-
2018-04-11 3
7114-
2018-04-12 4
7138+
2018-04-11 2
7139+
2018-04-13 3
7140+
2018-04-15 4
7141+
7142+
Get the rows for the last 3 days:
7143+
71157144
>>> ts.last('3D')
71167145
A
7117-
2018-04-10 2
7118-
2018-04-11 3
7119-
2018-04-12 4
7146+
2018-04-13 3
7147+
2018-04-15 4
7148+
7149+
Notice the data for 3 last calender days were returned, not the last
7150+
3 observed days in the dataset. and therefore data for 2018-04-13 was
7151+
not returned.
71207152
71217153
Returns
71227154
-------

pandas/core/indexes/datetimes.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -2378,15 +2378,23 @@ def tz_localize(self, tz, ambiguous='raise', errors='raise'):
23782378

23792379
def indexer_at_time(self, time, asof=False):
23802380
"""
2381-
Select values at particular time of day (e.g. 9:30AM)
2381+
Returns index locations of index values at particular time of day
2382+
(e.g. 9:30AM).
23822383
23832384
Parameters
23842385
----------
23852386
time : datetime.time or string
2387+
datetime.time or string in appropriate format ("%H:%M", "%H%M",
2388+
"%I:%M%p", "%I%M%p", "%H:%M:%S", "%H%M%S", "%I:%M:%S%p",
2389+
"%I%M%S%p").
23862390
23872391
Returns
23882392
-------
2389-
values_at_time : TimeSeries
2393+
values_at_time : array of integers
2394+
2395+
See Also
2396+
--------
2397+
indexer_between_time, DataFrame.at_time
23902398
"""
23912399
from dateutil.parser import parse
23922400

@@ -2408,24 +2416,25 @@ def indexer_at_time(self, time, asof=False):
24082416
def indexer_between_time(self, start_time, end_time, include_start=True,
24092417
include_end=True):
24102418
"""
2411-
Select values between particular times of day (e.g., 9:00-9:30AM).
2412-
2413-
Return values of the index between two times. If start_time or
2414-
end_time are strings then tseries.tools.to_time is used to convert to
2415-
a time object.
2419+
Return index locations of values between particular times of day
2420+
(e.g., 9:00-9:30AM).
24162421
24172422
Parameters
24182423
----------
24192424
start_time, end_time : datetime.time, str
24202425
datetime.time or string in appropriate format ("%H:%M", "%H%M",
24212426
"%I:%M%p", "%I%M%p", "%H:%M:%S", "%H%M%S", "%I:%M:%S%p",
2422-
"%I%M%S%p")
2427+
"%I%M%S%p").
24232428
include_start : boolean, default True
24242429
include_end : boolean, default True
24252430
24262431
Returns
24272432
-------
2428-
values_between_time : TimeSeries
2433+
values_between_time : array of integers
2434+
2435+
See Also
2436+
--------
2437+
indexer_at_time, DataFrame.between_time
24292438
"""
24302439
start_time = tools.to_time(start_time)
24312440
end_time = tools.to_time(end_time)

0 commit comments

Comments
 (0)