简体   繁体   English

熊猫布尔系列不会绘制

[英]Pandas boolean Series won't plot

I am new to Python and I am trying to make a plot of a Boolean array. 我是Python的新手,我正在尝试制作一个布尔数组的图。 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. 然而,这很奇怪,因为这个视频中的发言者(1:45:48)似乎没有绘制布尔数组的任何问题。

Hope anyone can help! 希望有人可以帮忙!

first convert the type and then plot as below: 首先转换类型,然后绘制如下:

series_bool.astype(float).plot()

在此输入图像描述

PS, 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 PS,在你所指的视频中,教练也正在绘制真/假,所以这看起来像是对熊猫布尔治疗的改变

This is most definitely due to a change in the way pandas/plotting/_core.py handles data. 这绝对是由于pandas/plotting/_core.py处理数据的方式发生了变化。 If you go to the source of the current release 0.23.4 , the data_types considered numeric are np.number, "datetime", "datetimetz", "timedelta" : 如果您转到当前版本0.23.4 ,则认为数字的data_types是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: 如果我们返回几个版本到0.20.0版本的源代码 ,这行如下所示:

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

This was changed with the release of 0.21.0 随着0.21.0的发布而改变了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM