我想创建pandas.Period
一个自定义时间段,例如一段时间starting_time = pd.Timestamp('2024-01-01 09:15:00')
和 ending_time = pd.Timestamp('2024-01-05 08:17:00')
。
实现此目的的一种方法是先获取pandas.Timedelta
然后创建pandas.Period
。
import pandas as pd
# Define start and end times
starting_time = pd.Timestamp('2024-01-01 09:15:00')
ending_time = pd.Timestamp('2024-01-05 08:17:00')
# Calculate the duration (period) between the two timestamps
period_duration = ending_time - starting_time
period_duration_in_minutes = (period_duration.total_seconds()) //60
freq_str = f"{period_duration_in_minutes}min"
period = pd.Period(starting_time, freq = freq_str)
print(period.start_time)
print(period.end_time)
但我需要一种直接的方法,像这样(我知道这行不通)-
period = pd.Period(start_time = starting_time, end_time=ending_time)