[英]Fill bar plot gaps in matplotlib with pandas resampled time series data
[英]How to plot a resampled pandas series?
我有一個簡單的 dataframe,只有date
列和amount
列:
local_date amount
48 2020-01-01 30.00
464 2020-01-01 1.49
465 2020-01-01 22.45
469 2020-01-01 7.49
472 2020-01-01 19.17
473 2020-01-01 49.37
475 2020-01-01 7.72
481 2020-01-01 59.98
482 2020-01-01 8.20
483 2020-01-01 14.24
數據類型:
local_date datetime64[ns]
amount float64
我想重新采樣它,所以我得到每周的amount
總和,然后是 plot 它。
重采樣工作:
df.set_index('local_date')['amount'].resample('W').sum()
local_date
2020-01-05 339198.67
2020-01-12 570769.94
2020-01-19 556042.39
2020-01-26 564230.50
2020-02-02 569204.69
2020-02-09 606505.21
2020-02-16 620612.11
2020-02-23 618156.03
2020-03-01 645825.50
2020-03-08 688377.73
2020-03-15 892803.67
2020-03-22 783538.04
2020-03-29 856011.93
2020-04-05 243519.71
Freq: W-SUN, Name: amount, dtype: float64
但是,當我向其中添加.plot()
時,出現錯誤:
df.set_index('local_date')['amount'].resample('W').sum().plot()
TypeError: float() argument must be a string or a number, not 'Period'
我確信我以前用這種方法繪制過很多次。 如果 plot 參數不接受句點,是否必須將其轉換為字符串?
我會這樣 go 。
數據
強制日期到日期時間
df['local_date']=pd.to_datetime(df['local_date'])
按日期分組並計算每個日期的金額總和
df.groupby(df.index.date)['amount'].sum().reset_index().plot()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.