2016-09-26 73 views
2

我試圖顯示一個數據框作爲具有xlim的自定義日期範圍的條形圖。我能夠輸出的曲線圖,如果我選擇kind='line'但我得到以下錯誤消息試圖kind='bar'時:TypeError用於自定義日期範圍的條形圖

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' 

數據幀如下所示:

df1 = 
    Date   Quantity 
0 2010-01-01 1 
1 2010-01-02 0 
2 2010-01-03 0 
3 2010-01-04 2 
4 2010-01-05 3 
5 2010-01-06 1 
6 2010-01-07 0 
7 2010-01-08 1 
8 2010-01-09 1 
9 2010-01-10 2 
10 2010-01-11 0 
11 2010-01-12 5 
12 2010-01-13 2 
13 2010-01-14 1 
14 2010-01-15 2 
... 

這工作:

df1.plot(x='Date', y='Quantity', kind='line', grid=False, legend=False, 
     xlim=['2010-01-01', '2010-01-10'], figsize=(40, 16)) 

但這不是

df1.plot(x='Date', y='Quantity', kind='bar', grid=False, legend=False, 
     xlim=['2010-01-01', '2010-01-10'], figsize=(40, 16)) 

然而,如果我從kind='bar'刪除xlim我產生一個輸出。能夠輸出具有自定義x範圍的條形圖將是非常好的。

回答

2

怎麼樣的替代方法 - 過濾你的數據之前密謀?

In [10]: df.set_index('Date').ix['2010-01-01' : '2010-01-10'] 
Out[10]: 
      Quantity 
Date 
2010-01-01   1 
2010-01-02   0 
2010-01-03   0 
2010-01-04   2 
2010-01-05   3 
2010-01-06   1 
2010-01-07   0 
2010-01-08   1 
2010-01-09   1 
2010-01-10   2 

In [11]: df.set_index('Date').ix['2010-01-01' : '2010-01-10', 'Quantity'].plot.bar(grid=False, legend=False) 
+0

該解決方案有效,但我必須「刪除」所有其他不相關的列,否則他們將繪製。對於我所需要的,這是我一直在尋找的。 – Lukasz

+1

@Lukasz,你可以這樣做:'df.set_index('Date')。ix ['2010-01-01':'2010-01-10','Quantity']。plot ...'刪除其他列... – MaxU