【问题标题】:altair outputs "ufunc 'isinf' not supported for the input types" error, when matplotlib does not当 matplotlib 不支持时,altair 输出“输入类型不支持 ufunc 'isinf'”错误
【发布时间】:2021-02-18 14:05:13
【问题描述】:

我有这样一个 pandas DataFrame:

RangeIndex: 4478 entries, 0 to 4477
Data columns (total 11 columns):
 #   Column                          Non-Null Count  Dtype         
---  ------                          --------------  -----         
 0   timestamp                       4478 non-null   datetime64[ns]
 1   user                            4478 non-null   object        
 2   Active_energy_exported_kWh      2466 non-null   float64       
 3   Active_energy_imported_kWh      4473 non-null   float64       
 4   Reactive_energy_imported_kVARh  3822 non-null   float64       
 5   Reactive_energy_imported_kVARh  3822 non-null   float64       
 6   Active_power_imported_kW        4449 non-null   float64       
 7   Reactive_power_imported_kVAR    4471 non-null   float64       
 8   Apparent_power_imported_kVA     4471 non-null   float64       
 9   Power_factor                    4449 non-null   float64       
 10  Supply_frequency_Hz             4449 non-null   float64       
dtypes: datetime64[ns](1), float64(9), object(1)
memory usage: 385.0+ KB

现在,如果我尝试用altair 以这种方式绘制它:

ttChart = alt.Chart(mlogs).mark_line().encode(
    alt.X('timestamp:T', title=None),
    alt.Y('Supply_frequency_Hz:Q', title=None),
)

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

但是当我这样做时

mlogs.plot(x="timestamp", y="Supply_frequency_Hz")

我可以看到我的期望。

就我所见,类型是正确的,但我看不到发生了什么。

【问题讨论】:

  • 您能否通过上传数据(或重现问题的一小部分数据)创建一个可重现的示例?似乎您的数据中可能有零并已记录它,从而创建了无限值。我不确定为什么它在其中一种情况下不起作用,但需要用数据检查测试。
  • pastebin.com/2KdDuVJQ 在这里。谢谢!
  • 当我使用pd.read_csv() 加载您链接的文件然后运行您的代码时,我没有看到任何错误。
  • 你是对的。当我查看完整的转储(4.5k 行)时,我看到类似这样的内容:2021-02-09 20:32:29,021329fd3488689f3144cd0095308d9114427ad4f7e4a15c52513410f091240879,,,,,,,,, 我认为这是要解决的问题。
  • pastebin.com/wtU7S5ST 这应该有助于重现问题。

标签: python-3.x pandas matplotlib altair


【解决方案1】:

我收到了同样的错误信息。事实证明,我的 DataFrame 中的 2 列具有相同的名称。修复它,解决了问题。

您示例中的两列名为“Reactive_energy_imported_kVARh”。也许这就是错误的原因。

【讨论】: