Pandas Series: between_time() function
Select values between particular times of the day
The between_time() function is used to select values at particular time of day (e.g. 10:30AM).
By setting start_time to be later than end_time, you can get the times that are not between the two times.
Syntax:
Series.between_time(self, start_time, end_time, include_start=True, include_end=True, axis=None)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
start_time | datetime.time or str | Required | |
end_time | For DataFrame, if not None, only use these columns to check for NaNs. | datetime.time or str | Required |
include_start | bool Default Value: True |
Required | |
include_end | bool Default Value: True |
Required | |
axis | {0 or ‘index’, 1 or ‘columns’} Default Value: 0 |
Required |
Returns: Series or DataFrame
Raises: TypeError
If the index is not a DatetimeIndex
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
i = pd.date_range('2019-04-09', periods=4, freq='1D20min')
ts = pd.DataFrame({'P': [2, 3, 4, 5]}, index=i)
ts
Output:
P 2019-04-09 00:00:00 2 2019-04-10 00:20:00 3 2019-04-11 00:40:00 4 2019-04-12 01:00:00 5
Python-Pandas Code:
import numpy as np
import pandas as pd
i = pd.date_range('2019-04-09', periods=4, freq='1D20min')
ts = pd.DataFrame({'P': [2, 3, 4, 5]}, index=i)
ts.between_time('0:15', '0:45')
Output:
P 2019-04-10 00:20:00 3 2019-04-11 00:40:00 4
Example - You get the times that are not between two times by setting start_time later than end_time:
Python-Pandas Code:
import numpy as np
import pandas as pd
i = pd.date_range('2019-04-09', periods=4, freq='1D20min')
ts = pd.DataFrame({'P': [2, 3, 4, 5]}, index=i)
ts.between_time('0:45', '0:15')
Output:
P 2019-04-09 00:00:00 2 2019-04-12 01:00:00 5
Previous: Select all the values in a row at the particular time of the day
Next: Series.dt.dayofweek() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/pandas/series/series-between_time.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics