Python pandas 自定义商务日
Python pandas CustomBusinessDay
我试图在 pandas 中过滤一些时间序列数据,但没有任何运气。非常感谢任何关于我做错的提示。首先,我试图只过滤 7 月份的数据2013 然后再次过滤数据以获取数据集样本的每小时平均值。
最终我想做的是使用 CustomBusinessDay 函数过滤工作日或周一、周二、周三等个别工作日的数据,如上所述。
我被 CustomBusinessDay 的代码注释掉了
import pandas as pd
import numpy as np
from pandas.tseries.offsets import CustomBusinessDay
time = pd.date_range('6/28/2013', periods=2000, freq='45min')
data = pd.Series(np.random.randint(100, size=2000), index=time)
print(data)
##weekmask = 'Mon Tue Wed Thu Fri'
df = data.truncate(before='7/1/2013', after='7/31/2013')
df = df.groupby(df.index.hour).mean()
print(df)
##df = CustomBusinessDay(weekmask=weekmask)
##df = pd.bdate_range(start=None, end=None, periods=None, freq='B')
##
##print(df)
Ultimately what I am wanting to do is filter the data as described
above in addition for Weekdays, OR individual weekdays Mondays,
Tuesdays, Wednesday, etc. with the CustomBusinessDay function.
您是否考虑过使用 DatetimeIndex.dayofweek
?
The day of the week with Monday=0, Sunday=6
# Exclude Sundays
data.loc[data.index.dayofweek != 6]
# Weekends only
data.loc[data.index.dayofweek.isin([5, 6])]
# Weekdays only
data.loc[~data.index.dayofweek.isin([5, 6])]
此外,我认为 df = df.groupby(df.index.hour).mean()
的替代方法是:
df.resample('H').mean()
我试图在 pandas 中过滤一些时间序列数据,但没有任何运气。非常感谢任何关于我做错的提示。首先,我试图只过滤 7 月份的数据2013 然后再次过滤数据以获取数据集样本的每小时平均值。
最终我想做的是使用 CustomBusinessDay 函数过滤工作日或周一、周二、周三等个别工作日的数据,如上所述。
我被 CustomBusinessDay 的代码注释掉了
import pandas as pd
import numpy as np
from pandas.tseries.offsets import CustomBusinessDay
time = pd.date_range('6/28/2013', periods=2000, freq='45min')
data = pd.Series(np.random.randint(100, size=2000), index=time)
print(data)
##weekmask = 'Mon Tue Wed Thu Fri'
df = data.truncate(before='7/1/2013', after='7/31/2013')
df = df.groupby(df.index.hour).mean()
print(df)
##df = CustomBusinessDay(weekmask=weekmask)
##df = pd.bdate_range(start=None, end=None, periods=None, freq='B')
##
##print(df)
Ultimately what I am wanting to do is filter the data as described above in addition for Weekdays, OR individual weekdays Mondays, Tuesdays, Wednesday, etc. with the CustomBusinessDay function.
您是否考虑过使用 DatetimeIndex.dayofweek
?
The day of the week with Monday=0, Sunday=6
# Exclude Sundays
data.loc[data.index.dayofweek != 6]
# Weekends only
data.loc[data.index.dayofweek.isin([5, 6])]
# Weekdays only
data.loc[~data.index.dayofweek.isin([5, 6])]
此外,我认为 df = df.groupby(df.index.hour).mean()
的替代方法是:
df.resample('H').mean()