Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas boolean Series won't plot

I am new to Python and I am trying to make a plot of a Boolean array. However, I'm getting an error, although others seem to have no problems at all with plotting Boolean arrays.

For numeric arrays, it works as expected. So, the code below works just fine.

import pandas as pd
series_numeric = pd.Series([0,1,0])
series_numeric.plot()

However, for boolean arrays, an error occurs. This is the code:

import pandas as pd
series_bool = pd.Series([False,True,False])
series_bool.plot()

It throws the following error:

TypeError: Empty 'DataFrame': no numeric data to plot

However, this is strange, as the speaker in this video (at 1:45:48) does not seem to have any problems with plotting boolean arrays.

Hope anyone can help!

like image 319
Jordi Avatar asked Aug 07 '18 18:08

Jordi


2 Answers

first convert the type and then plot as below:

series_bool.astype(float).plot()

enter image description here

P.S., in the video you were referring to, the instructor is plotting True/False as well so this looks like a change in the treatment of boolean by Pandas

like image 112
Peybae Avatar answered Oct 11 '22 16:10

Peybae


This is most definitely due to a change in the way pandas/plotting/_core.py handles data. If you go to the source of the current release 0.23.4, the data_types considered numeric are np.number, "datetime", "datetimetz", "timedelta":

    numeric_data = data.select_dtypes(include=[np.number,
                                               "datetime",
                                               "datetimetz",
                                               "timedelta"])

    try:
        is_empty = numeric_data.empty
    except AttributeError:
        is_empty = not len(numeric_data)

    # no empty frames or series allowed
    if is_empty:
        raise TypeError('Empty {0!r}: no numeric data to '
                        'plot'.format(numeric_data.__class__.__name__))

If we go back a couple releases to the source of release 0.20.0, this line reads like this:

numeric_data = data._convert(datetime=True)._get_numeric_data()

This was changed with the release of 0.21.0

like image 42
tobsecret Avatar answered Oct 11 '22 14:10

tobsecret