![](/img/trans.png)
[英]How to specify what comes first when reading time values in pandas (hour, minute, seconds, day, month, year)?
[英]Pandas: ignore year in index when plotting but keep hour/day/month
我有以下數據系列:
values
Date
2013-01-01 00:00:00 NaN
2013-01-01 01:00:00 0.041702
2013-01-01 02:00:00 0.042505
2013-01-01 03:00:00 0.030535
...
2020-12-30 21:00:00 0.525059
2020-12-30 22:00:00 0.249274
2020-12-30 23:00:00 0.024965
我想要:
2013-01-01 00:00:00
、 2014-01-07 00:00:00
、 2015-01-06 00:00:00
的平均值2015-01-06 00:00:00
等繪制此圖基本上會導致 plot 具有基於大約 365 點估計的單線(忽略閏年和年末數據較少的天數)。 我嘗試按照此處的建議從 pivot 表開始,但失敗了:
df_pv = pd.pivot_table(series.to_frame(), columns=series.index.year)
Exception has occurred: AttributeError 'Series' object has no attribute 'columns'
有任何想法嗎?
就像你寫的那樣,只是一個groupby()
而不是 pivot。
import datetime as dt
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=[10,6])
d = pd.date_range(dt.date(2013,1,1), dt.date(2021,1,1), freq="H")
df = pd.DataFrame({"Date":d,"values":np.random.uniform(0,1,len(d))})
l = df.groupby([df.Date.dt.dayofweek,df.Date.dt.isocalendar().week,df.Date.dt.time]).agg({"values":"mean"}).plot(ax=ax)
l = ax.set_xticklabels(ax.get_xticklabels(), rotation = 90)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.