令我惊讶的是,当我只想要当年第一周的数据时,我的简单布尔条件却产生了完整的一年结果。
My code:
# Some sample data
df1 = pd.DataFrame([1596., 1537., 1482., 1960., 1879., 1824.],index=['2007-01-01 00:00:00', '2007-01-01 01:00:00',
'2007-01-01 02:00:00', '2007-12-31 21:00:00',
'2007-12-31 22:00:00', '2007-12-31 23:00:00'])
df1.index = pd.to_datetime(df1.index,format = '%Y-%m-%d %H:%M:%S')
# Consider and plot only the 2007 year and first week result
year_plot = 2007
year_data = df1[(df1.index.year==year_plot)&(df1.index.isocalendar().week==1)]
print(year_data)
DAYTON_MW
Datetime
2007-01-01 00:00:00 1596.0
2007-01-01 01:00:00 1537.0
2007-01-01 02:00:00 1482.0
2007-01-01 03:00:00 1422.0
2007-01-01 04:00:00 1402.0
... ...
2007-12-31 19:00:00 2110.0
2007-12-31 20:00:00 2033.0
2007-12-31 21:00:00 1960.0
2007-12-31 22:00:00 1879.0
2007-12-31 23:00:00 1824.0
192 rows × 1 columns
year_data.plot(figsize=(15, 5), title='Week Of Data')
plt.show()
我需要你的帮助才能知道问题出在哪里。
Update: 问题已经找到了。同时@J_H也发现了这个问题。我很惊讶为什么它会这样,它把 2007 年的最后几天当作第一周。
Result:根据接受的答案,解决方案是
df1[(df1.index.isocalendar().year==year_plot)&(df1.index.isocalendar().week==1)]\
.plot(figsize=(15, 5), title='Week Of Data')# plt.savefig('oneweek.png')
plt.show()