Skip to content

Commit d482d3e

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

File tree

3 files changed

+90
-49
lines changed

3 files changed

+90
-49
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``.
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``.
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

+68-40
Original file line numberDiff line numberDiff line change
@@ -6704,9 +6704,10 @@ def at_time(self, time, asof=False):
67046704
"""
67056705
Select values at particular time of day (e.g. 9:30AM).
67066706
6707-
Notes
6707+
Raises
67086708
-----
6709-
For this method to work, the index must to be a :class:`DatetimeIndex`
6709+
TypeError
6710+
If the index is not a :class:`DatetimeIndex`
67106711
67116712
Parameters
67126713
----------
@@ -6718,25 +6719,27 @@ def at_time(self, time, asof=False):
67186719
67196720
Examples
67206721
--------
6721-
>>> i = pd.date_range('2018-04-09', periods=4, freq='4D2min')
6722+
>>> i = pd.date_range('2018-04-09', periods=4, freq='12H')
67226723
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i)
67236724
>>> ts
67246725
A
6725-
date
67266726
2018-04-09 00:00:00 1
6727-
2018-04-13 00:02:00 2
6728-
2018-04-17 00:04:00 3
6729-
2018-04-21 00:06:00 4
6730-
>>> ts.at_time('0:02')
6727+
2018-04-09 12:00:00 2
6728+
2018-04-10 00:00:00 3
6729+
2018-04-10 12:00:00 4
6730+
6731+
>>> ts.at_time('12:00')
67316732
A
6732-
date
6733-
2018-04-13 00:02:00 2
6733+
2018-04-09 12:00:00 2
6734+
2018-04-10 12:00:00 4
67346735
67356736
See Also
67366737
--------
67376738
between_time : Select values between particular times of the day
67386739
first : Select initial periods of time series based on a date offset
67396740
last : Select final periods of time series based on a date offset
6741+
DatetimeIndex.indexer_at_time : Get just the index locations for
6742+
values at particular time of the day
67406743
"""
67416744
try:
67426745
indexer = self.index.indexer_at_time(time, asof=asof)
@@ -6749,9 +6752,13 @@ def between_time(self, start_time, end_time, include_start=True,
67496752
"""
67506753
Select values between particular times of the day (e.g., 9:00-9:30 AM).
67516754
6752-
Notes
6755+
By setting ``start_time`` to be later than ``end_time``,
6756+
you can get the times that are *not* between the two times.
6757+
6758+
Raises
67536759
-----
6754-
For this method to work, the index must to be a :class:`DatetimeIndex`
6760+
TypeError
6761+
If the index is not a :class:`DatetimeIndex`
67556762
67566763
Parameters
67576764
----------
@@ -6766,24 +6773,35 @@ def between_time(self, start_time, end_time, include_start=True,
67666773
67676774
Examples
67686775
--------
6769-
>>> i = pd.date_range('2018-04-09', periods=4, freq='2min')
6776+
>>> i = pd.date_range('2018-04-09', periods=4, freq='1D20min')
67706777
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i)
67716778
>>> ts
67726779
A
67736780
2018-04-09 00:00:00 1
6774-
2018-04-09 00:02:00 2
6775-
2018-04-09 00:04:00 3
6776-
2018-04-09 00:06:00 4
6777-
>>> ts.between_time('0:02', '0:04')
6781+
2018-04-10 00:20:00 2
6782+
2018-04-11 00:40:00 3
6783+
2018-04-12 01:00:00 4
6784+
6785+
>>> ts.between_time('0:15', '0:45')
6786+
A
6787+
2018-04-10 00:20:00 2
6788+
2018-04-11 00:40:00 3
6789+
6790+
You get the times that are *not* between two times by setting
6791+
``start_time`` later than ``end_time``:
6792+
6793+
>>> ts.between_time('0:45', '0:15')
67786794
A
6779-
2018-04-09 00:02:00 2
6780-
2018-04-09 00:04:00 3
6795+
2018-04-09 00:00:00 1
6796+
2018-04-12 01:00:00 4
67816797
67826798
See Also
67836799
--------
67846800
at_time : Select values at a particular time of the day
67856801
first : Select initial periods of time series based on a date offset
67866802
last : Select final periods of time series based on a date offset
6803+
DatetimeIndex.indexer_between_time : Get just the index locations for
6804+
values between particular times of the day
67876805
"""
67886806
try:
67896807
indexer = self.index.indexer_between_time(
@@ -7037,29 +7055,35 @@ def first(self, offset):
70377055
Convenience method for subsetting initial periods of time series data
70387056
based on a date offset.
70397057
7040-
Notes
7058+
Raises
70417059
-----
7042-
For this method to work, the index must to be a :class:`DatetimeIndex`
7060+
TypeError
7061+
If the index is not a :class:`DatetimeIndex`
70437062
70447063
Parameters
70457064
----------
70467065
offset : string, DateOffset, dateutil.relativedelta
70477066
70487067
Examples
70497068
--------
7050-
>>> i = pd.date_range('2018-04-09', periods=4, freq='D')
7069+
>>> i = pd.date_range('2018-04-09', periods=4, freq='2D')
70517070
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i)
70527071
>>> ts
70537072
A
70547073
2018-04-09 1
7055-
2018-04-10 2
7056-
2018-04-11 3
7057-
2018-04-12 4
7074+
2018-04-11 2
7075+
2018-04-13 3
7076+
2018-04-15 4
7077+
7078+
Get the rows for the first 3 days:
7079+
70587080
>>> ts.first('3D')
70597081
A
70607082
2018-04-09 1
7061-
2018-04-10 2
7062-
2018-04-11 3
7083+
2018-04-11 2
7084+
7085+
Notice the data for 3 first calender days were returned, not the first
7086+
3 days observed, and therefore data for 2018-04-13 was not returned.
70637087
70647088
Returns
70657089
-------
@@ -7073,8 +7097,7 @@ def first(self, offset):
70737097
"""
70747098
from pandas.tseries.frequencies import to_offset
70757099
if not isinstance(self.index, DatetimeIndex):
7076-
raise NotImplementedError("'first' only supports a DatetimeIndex "
7077-
"index")
7100+
raise TypeError("'first' only supports a DatetimeIndex index")
70787101

70797102
if len(self.index) == 0:
70807103
return self
@@ -7095,29 +7118,35 @@ def last(self, offset):
70957118
Convenience method for subsetting final periods of time series data
70967119
based on a date offset.
70977120
7098-
Notes
7121+
Raises
70997122
-----
7100-
For this method to work, the index must to be a :class:`DatetimeIndex`
7123+
TypeError
7124+
If the index is not a :class:`DatetimeIndex`
71017125
71027126
Parameters
71037127
----------
71047128
offset : string, DateOffset, dateutil.relativedelta
71057129
71067130
Examples
71077131
--------
7108-
>>> i = pd.date_range('2018-04-09', periods=4, freq='D')
7132+
>>> i = pd.date_range('2018-04-09', periods=4, freq='2D')
71097133
>>> ts = pd.DataFrame({'A': [1,2,3,4]}, index=i)
71107134
>>> ts
71117135
A
71127136
2018-04-09 1
7113-
2018-04-10 2
7114-
2018-04-11 3
7115-
2018-04-12 4
7137+
2018-04-11 2
7138+
2018-04-13 3
7139+
2018-04-15 4
7140+
7141+
Get the rows for the last 3 days:
7142+
71167143
>>> ts.last('3D')
71177144
A
7118-
2018-04-10 2
7119-
2018-04-11 3
7120-
2018-04-12 4
7145+
2018-04-13 3
7146+
2018-04-15 4
7147+
7148+
Notice the data for 3 last calender days were returned, not the last
7149+
3 observed days, and therefore data for 2018-04-11 was not returned.
71217150
71227151
Returns
71237152
-------
@@ -7131,8 +7160,7 @@ def last(self, offset):
71317160
"""
71327161
from pandas.tseries.frequencies import to_offset
71337162
if not isinstance(self.index, DatetimeIndex):
7134-
raise NotImplementedError("'last' only supports a DatetimeIndex "
7135-
"index")
7163+
raise TypeError("'last' only supports a DatetimeIndex index")
71367164

71377165
if len(self.index) == 0:
71387166
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)