Skip to content

Commit 8235074

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

File tree

3 files changed

+96
-53
lines changed

3 files changed

+96
-53
lines changed

doc/source/whatsnew/v0.23.0.txt

+4
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,10 @@ Datetimelike API Changes
810810
- :class:`CacheableOffset` and :class:`WeekDay` are no longer available in the ``pandas.tseries.offsets`` module (:issue:`17830`)
811811
- ``pandas.tseries.frequencies.get_freq_group()`` and ``pandas.tseries.frequencies.DAYS`` are removed from the public API (:issue:`18034`)
812812
- :func:`Series.truncate` and :func:`DataFrame.truncate` will raise a ``ValueError`` if the index is not sorted instead of an unhelpful ``KeyError`` (:issue:`17935`)
813+
- :attr:`Series.at_time` and :attr:`DataFrame.at_time` will now raise a ``TypeError``
814+
rather than ``NotImplementedError`` when index is not a :class:`DatetimeIndex`` (:issue:`20725`).
815+
- :attr:`Series.between_time` and :attr:`DateFrame.between_time` will now raise
816+
a ``TypeError`` rather than ``NotImplementedError`` when index is not a :class:`DatetimeIndex`` (:issue:`20725`).
813817
- Restricted ``DateOffset`` keyword arguments. Previously, ``DateOffset`` subclasses allowed arbitrary keyword arguments which could lead to unexpected behavior. Now, only valid arguments will be accepted. (:issue:`17176`, :issue:`18226`).
814818
- :func:`pandas.merge` provides a more informative error message when trying to merge on timezone-aware and timezone-naive columns (:issue:`15800`)
815819
- For :class:`DatetimeIndex` and :class:`TimedeltaIndex` with ``freq=None``, addition or subtraction of integer-dtyped array or ``Index`` will raise ``NullFrequencyError`` instead of ``TypeError`` (:issue:`19895`)

pandas/core/generic.py

+74-44
Original file line numberDiff line numberDiff line change
@@ -6770,9 +6770,10 @@ def at_time(self, time, asof=False):
67706770
"""
67716771
Select values at particular time of day (e.g. 9:30AM).
67726772
6773-
Notes
6774-
-----
6775-
For this method to work, the index must to be a :class:`DatetimeIndex`
6773+
Raises
6774+
------
6775+
TypeError
6776+
If the index is not a :class:`DatetimeIndex`
67766777
67776778
Parameters
67786779
----------
@@ -6784,25 +6785,27 @@ def at_time(self, time, asof=False):
67846785
67856786
Examples
67866787
--------
6787-
>>> i = pd.date_range('2018-04-09', periods=4, freq='4D2min')
6788+
>>> i = pd.date_range('2018-04-09', periods=4, freq='12H')
67886789
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i)
67896790
>>> ts
67906791
A
6791-
date
67926792
2018-04-09 00:00:00 1
6793-
2018-04-13 00:02:00 2
6794-
2018-04-17 00:04:00 3
6795-
2018-04-21 00:06:00 4
6796-
>>> ts.at_time('0:02')
6793+
2018-04-09 12:00:00 2
6794+
2018-04-10 00:00:00 3
6795+
2018-04-10 12:00:00 4
6796+
6797+
>>> ts.at_time('12:00')
67976798
A
6798-
date
6799-
2018-04-13 00:02:00 2
6799+
2018-04-09 12:00:00 2
6800+
2018-04-10 12:00:00 4
68006801
68016802
See Also
68026803
--------
68036804
between_time : Select values between particular times of the day
68046805
first : Select initial periods of time series based on a date offset
68056806
last : Select final periods of time series based on a date offset
6807+
DatetimeIndex.indexer_at_time : Get just the index locations for
6808+
values at particular time of the day
68066809
"""
68076810
try:
68086811
indexer = self.index.indexer_at_time(time, asof=asof)
@@ -6815,9 +6818,13 @@ def between_time(self, start_time, end_time, include_start=True,
68156818
"""
68166819
Select values between particular times of the day (e.g., 9:00-9:30 AM).
68176820
6818-
Notes
6819-
-----
6820-
For this method to work, the index must to be a :class:`DatetimeIndex`
6821+
By setting ``start_time`` to be later than ``end_time``,
6822+
you can get the times that are *not* between the two times.
6823+
6824+
Raises
6825+
------
6826+
TypeError
6827+
If the index is not a :class:`DatetimeIndex`
68216828
68226829
Parameters
68236830
----------
@@ -6832,24 +6839,35 @@ def between_time(self, start_time, end_time, include_start=True,
68326839
68336840
Examples
68346841
--------
6835-
>>> i = pd.date_range('2018-04-09', periods=4, freq='2min')
6842+
>>> i = pd.date_range('2018-04-09', periods=4, freq='1D20min')
68366843
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i)
68376844
>>> ts
68386845
A
68396846
2018-04-09 00:00:00 1
6840-
2018-04-09 00:02:00 2
6841-
2018-04-09 00:04:00 3
6842-
2018-04-09 00:06:00 4
6843-
>>> ts.between_time('0:02', '0:04')
6847+
2018-04-10 00:20:00 2
6848+
2018-04-11 00:40:00 3
6849+
2018-04-12 01:00:00 4
6850+
6851+
>>> ts.between_time('0:15', '0:45')
68446852
A
6845-
2018-04-09 00:02:00 2
6846-
2018-04-09 00:04:00 3
6853+
2018-04-10 00:20:00 2
6854+
2018-04-11 00:40:00 3
6855+
6856+
You get the times that are *not* between two times by setting
6857+
``start_time`` later than ``end_time``:
6858+
6859+
>>> ts.between_time('0:45', '0:15')
6860+
A
6861+
2018-04-09 00:00:00 1
6862+
2018-04-12 01:00:00 4
68476863
68486864
See Also
68496865
--------
68506866
at_time : Select values at a particular time of the day
68516867
first : Select initial periods of time series based on a date offset
68526868
last : Select final periods of time series based on a date offset
6869+
DatetimeIndex.indexer_between_time : Get just the index locations for
6870+
values between particular times of the day
68536871
"""
68546872
try:
68556873
indexer = self.index.indexer_between_time(
@@ -7103,29 +7121,36 @@ def first(self, offset):
71037121
Convenience method for subsetting initial periods of time series data
71047122
based on a date offset.
71057123
7106-
Notes
7107-
-----
7108-
For this method to work, the index must to be a :class:`DatetimeIndex`
7124+
Raises
7125+
------
7126+
TypeError
7127+
If the index is not a :class:`DatetimeIndex`
71097128
71107129
Parameters
71117130
----------
71127131
offset : string, DateOffset, dateutil.relativedelta
71137132
71147133
Examples
71157134
--------
7116-
>>> i = pd.date_range('2018-04-09', periods=4, freq='D')
7135+
>>> i = pd.date_range('2018-04-09', periods=4, freq='2D')
71177136
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i)
71187137
>>> ts
71197138
A
71207139
2018-04-09 1
7121-
2018-04-10 2
7122-
2018-04-11 3
7123-
2018-04-12 4
7140+
2018-04-11 2
7141+
2018-04-13 3
7142+
2018-04-15 4
7143+
7144+
Get the rows for the first 3 days:
7145+
71247146
>>> ts.first('3D')
71257147
A
71267148
2018-04-09 1
7127-
2018-04-10 2
7128-
2018-04-11 3
7149+
2018-04-11 2
7150+
7151+
Notice the data for 3 first calender days were returned, not the first
7152+
3 days observed in the dataset, and therefore data for 2018-04-13 was
7153+
not returned.
71297154
71307155
Returns
71317156
-------
@@ -7139,8 +7164,7 @@ def first(self, offset):
71397164
"""
71407165
from pandas.tseries.frequencies import to_offset
71417166
if not isinstance(self.index, DatetimeIndex):
7142-
raise NotImplementedError("'first' only supports a DatetimeIndex "
7143-
"index")
7167+
raise TypeError("'first' only supports a DatetimeIndex index")
71447168

71457169
if len(self.index) == 0:
71467170
return self
@@ -7161,29 +7185,36 @@ def last(self, offset):
71617185
Convenience method for subsetting final periods of time series data
71627186
based on a date offset.
71637187
7164-
Notes
7165-
-----
7166-
For this method to work, the index must to be a :class:`DatetimeIndex`
7188+
Raises
7189+
------
7190+
TypeError
7191+
If the index is not a :class:`DatetimeIndex`
71677192
71687193
Parameters
71697194
----------
71707195
offset : string, DateOffset, dateutil.relativedelta
71717196
71727197
Examples
71737198
--------
7174-
>>> i = pd.date_range('2018-04-09', periods=4, freq='D')
7199+
>>> i = pd.date_range('2018-04-09', periods=4, freq='2D')
71757200
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i)
71767201
>>> ts
71777202
A
71787203
2018-04-09 1
7179-
2018-04-10 2
7180-
2018-04-11 3
7181-
2018-04-12 4
7204+
2018-04-11 2
7205+
2018-04-13 3
7206+
2018-04-15 4
7207+
7208+
Get the rows for the last 3 days:
7209+
71827210
>>> ts.last('3D')
71837211
A
7184-
2018-04-10 2
7185-
2018-04-11 3
7186-
2018-04-12 4
7212+
2018-04-13 3
7213+
2018-04-15 4
7214+
7215+
Notice the data for 3 last calender days were returned, not the last
7216+
3 observed days in the dataset, and therefore data for 2018-04-11 was
7217+
not returned.
71877218
71887219
Returns
71897220
-------
@@ -7197,8 +7228,7 @@ def last(self, offset):
71977228
"""
71987229
from pandas.tseries.frequencies import to_offset
71997230
if not isinstance(self.index, DatetimeIndex):
7200-
raise NotImplementedError("'last' only supports a DatetimeIndex "
7201-
"index")
7231+
raise TypeError("'last' only supports a DatetimeIndex index")
72027232

72037233
if len(self.index) == 0:
72047234
return self

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)