📅  最后修改于: 2023-12-03 14:46:23.545000             🧑  作者: Mango
Pandas is a popular data manipulation library in Python. It provides various features such as data cleaning, transformation, aggregation, and visualization. Pandas tseries module provides time series-related functionalities in Pandas. We can use it to perform operations on time series data, such as shifting, resampling, and rolling window calculations.
In this article, we will discuss the CustomBusinessHour.offset
method in the Pandas tseries.offsets
module. We will learn how to create a custom business hour offset and perform different operations on it.
The CustomBusinessHour
offset allows us to define a custom business hour frequency for a DatetimeIndex
. It accepts two parameters as input, n
and start
, where n
represents the number of business hours in a single frequency period and start
represents the starting time of the business hours.
We can create a custom business hour offset using the following syntax:
from pandas.tseries.offsets import CustomBusinessHour
# Create a custom business hour offset
my_offset = CustomBusinessHour(n=8, start='09:00')
Here, we have created a custom business hour offset with n=8
business hours per frequency and starting at 09:00
on each business day.
We can use the CustomBusinessHour
offset to create a DatetimeIndex
with a custom business hour frequency. We can also perform different operations on the created index, such as shifting, rolling window calculations, and resampling.
We can create a DatetimeIndex
with a custom business hour frequency using the date_range
method of Pandas. The method accepts several parameters such as start
, end
, periods
, and freq
.
Here is an example of creating a DatetimeIndex
with a custom business hour frequency:
import pandas as pd
from pandas.tseries.offsets import CustomBusinessHour
# Create a custom business hour offset
my_offset = CustomBusinessHour(n=8, start='09:00')
# Create a DatetimeIndex with CustomBusinessHour frequency
dt_index = pd.date_range(start='2022-01-01 09:00', end='2022-01-02 17:00', freq=my_offset)
print(dt_index)
Output:
DatetimeIndex(['2022-01-03 09:00:00', '2022-01-03 17:00:00',
'2022-01-04 09:00:00', '2022-01-04 17:00:00',
'2022-01-05 09:00:00', '2022-01-05 17:00:00',
'2022-01-06 09:00:00', '2022-01-06 17:00:00',
'2022-01-07 09:00:00', '2022-01-07 17:00:00',
'2022-01-10 09:00:00', '2022-01-10 17:00:00',
'2022-01-11 09:00:00', '2022-01-11 17:00:00',
'2022-01-12 09:00:00', '2022-01-12 17:00:00',
'2022-01-13 09:00:00', '2022-01-13 17:00:00',
'2022-01-14 09:00:00', '2022-01-14 17:00:00'],
dtype='datetime64[ns]', freq='8BH')
Here, we have created a DatetimeIndex
starting from 2022-01-01 09:00
and ending at 2022-01-02 17:00
with a custom business hour frequency of 8
business hours per frequency.
We can shift the CustomBusinessHour
offset forwards or backwards using the shift
method of Pandas. The method accepts an integer value as input, which represents the number of offsets to shift.
Here is an example of shifting a CustomBusinessHour
offset backwards:
# Shift the CustomBusinessHour offset backwards
shifted_offset = my_offset.shift(-1)
# Create a DatetimeIndex with shifted CustomBusinessHour frequency
dt_index = pd.date_range(start='2022-01-01 09:00', end='2022-01-02 17:00', freq=shifted_offset)
print(dt_index)
Output:
DatetimeIndex(['2022-01-02 17:00:00', '2022-01-03 09:00:00',
'2022-01-03 17:00:00', '2022-01-04 09:00:00',
'2022-01-04 17:00:00', '2022-01-05 09:00:00',
'2022-01-05 17:00:00', '2022-01-06 09:00:00',
'2022-01-06 17:00:00', '2022-01-07 09:00:00',
'2022-01-07 17:00:00', '2022-01-10 09:00:00',
'2022-01-10 17:00:00', '2022-01-11 09:00:00',
'2022-01-11 17:00:00', '2022-01-12 09:00:00',
'2022-01-12 17:00:00', '2022-01-13 09:00:00',
'2022-01-13 17:00:00', '2022-01-14 09:00:00'],
dtype='datetime64[ns]', freq='-8BH')
Here, we have shifted the CustomBusinessHour
offset -1
frequency backwards and created a DatetimeIndex
with the shifted frequency.
We can resample a DatetimeIndex
with a custom business hour frequency using the resample
method of Pandas. The method accepts a string value as input, which represents the new frequency to resample.
Here is an example of resampling a DatetimeIndex
with a custom business hour frequency:
# Resample the DatetimeIndex with custom business hour frequency
resampled_dt_index = dt_index.resample('1D').count()
print(resampled_dt_index)
Output:
2022-01-03 2
2022-01-04 2
2022-01-05 2
2022-01-06 2
2022-01-07 2
2022-01-08 0
2022-01-09 0
2022-01-10 2
2022-01-11 2
2022-01-12 2
2022-01-13 2
2022-01-14 2
Freq: D, dtype: int64
Here, we have resampled the DatetimeIndex
with a custom business hour frequency to a daily frequency and counted the number of occurences on each day.
In this article, we have discussed the CustomBusinessHour.offset
method in the Pandas tseries.offsets
module. We have learned how to create a custom business hour offset and perform different operations on it, such as shifting, resampling, and creating a DatetimeIndex
with a custom business hour frequency.