[FEA] Series and DataFrame between_time
- Dominant language
- C++
- Stars
- 9.8k
- Forks
- 1.1k
- Avg merge
- 3d 6m
- Merged PRs (30d)
- 278
Description
For pandas API compatibility, we can implement [Series](https://pandas.pydata.org/docs/reference/api/pandas.Series.between_time.html) and [DataFrame.between_time](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.between_time.html). `between_time` "select[s] 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."
If the index is not a DatetimeIndex, this method throws a TypeError. DateTimes without time components are considered as if the time component were "0:00:00". Valid `{start, end}_time` must be in the interval [0:00 and 24:00).
The API documentation does not indicate what resolutions are valid or invalid for `{start, end}_time`, but it is documented as only including granularity down to seconds in the [utility function](https://github.com/pandas-dev/pandas/blob/d9488016443ccd81a02321797c491eab7b404863/pandas/core/indexes/datetimes.py#L819-L833):
```
Return index locations of values between particular times of day
(e.g., 9:00-9:30AM).
Parameters
----------
start_time, end_time : datetime.time, str
Time passed either as object (datetime.time) or as string in
appropriate format ("%H:%M", "%H%M", "%I:%M%p", "%I%M%p",
"%H:%M:%S", "%H%M%S", "%I:%M:%S%p","%I%M%S%p").
include_start : bool, default True
include_end : bool, default True
```
```python
import pandas as pd
i = pd.date_range('2018-04-09', periods=4, freq='1D20min')
ts = pd.DataFrame({'A': [1, 2, 3, 4]}, index=i)
print(ts)
print(ts.between_time('0:15', '0:45'))
print(ts.between_time('0:45', '0:15'))
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
A
2018-04-10 00:20:00 2
2018-04-11 00:40:00 3
A
2018-04-09 00:00:00 1
2018-04-12 01:00:00 4
```
```python
i = pd.Series(["2021-01-01", "2021-02-10"], dtype="datetime64[ns]")
s = pd.Series([0,1], index=i)
print(s, "\n")
print(s.between_time("0:00:01", "23:59:59"), "\n")
print(s.between_time("0:00", "0:15"))
2021-01-01 0
2021-02-10 1
dtype: int64
Series([], dtype: int64)
2021-01-01 0
2021-02-10 1
dtype: int64
```
Contributor guide
Assessment
This issue has not been assessed yet.