【发布时间】:2021-05-07 11:44:34
【问题描述】:
我正在寻找在gas day(与天然气交易有关)分组熊猫系列的有效实施。
这包括 CET 时区早上 6 点到第二天早上 6 点之间的所有时间/时间戳。
由于夏令时,一年一次的汽油日有 23 小时和一次 25 小时。
我目前的解决方案工作正常(见下文,to_gas_day 函数),但速度非常慢。任何想法表示赞赏。
import pandas as pd
def to_gas_day(stamp):
"""Take a time stamp and return date according to gas day (from 6 to 6 CET)."""
if stamp.hour < 6:
day = stamp.date() - pd.Timedelta(days=1)
else:
day = stamp.date()
return pd.to_datetime(day)
se = pd.Series(
data = 1.,
index=pd.date_range('2020-10-23','2020-10-27', freq='H', tz='CET')[:-1]
)
# This is expected count of hours around DST date
se.groupby(to_gas_day).count()
Out[107]:
2020-10-22 6
2020-10-23 24
2020-10-24 25
2020-10-25 24
2020-10-26 18
dtype: int64
【问题讨论】:
标签: pandas time-series pandas-groupby