Lukasz Lukasz - 3 months ago 16
Python Question

TypeError for bar plot with custom date range

I'm attempting to display a dataframe as a bar graph with a custom date range for

xlim
. I'm able to output a graph if I select
kind='line'
but I get the following error message when attempting
kind='bar'
:

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''


the dataframe looks as follows:

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
...


This works:

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


but this doesn't

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


Yet if I remove
xlim
from
kind='bar'
I produce an output. It would be nice to be able to output a bar graph with a custom x range.

Answer

What about an alternative approach - filtering your data before plotting?

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)