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!
first convert the type and then plot as below:
series_bool.astype(float).plot()
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With