2

我正在尝试使用 hvplot 进行绘图,我得到了这个:

TypeError:“时间戳”和“numpy.float64”的实例之间不支持“<=”

这是我的数据:

    TimeConv    Hospitalizations
1   2020-04-04  827
2   2020-04-05  1132
3   2020-04-06  1153
4   2020-04-07  1252
5   2020-04-08  1491
... ... ...
71  2020-06-13  2242
72  2020-06-14  2287
73  2020-06-15  2326
74  NaT NaN
75  NaT NaN

下面是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import xlsxwriter
import pandas as pd
from pandas import DataFrame

path = ('Casecountdata.xlsx')
xl = pd.ExcelFile(path)
df1 = xl.parse('Hospitalization by Day')
df2 = df1[['Unnamed: 1','Unnamed: 2']]
df2 = df2.drop(df2.index[0])
df2 = df2.rename(columns={"Unnamed: 1": "Time", "Unnamed: 2": "Hospitalizations"})
df2['TimeConv'] = pd.to_datetime(df2.Time)
df3 = df2[['TimeConv','Hospitalizations']]
4

1 回答 1

1

当我从上面的数据中抽取样本并尝试绘制它时,它对我有用,所以你从 excel 到 pandas 读取数据的方式可能有问题。您可以尝试df.info()查看数据的数据类型是什么样的。列 TimeConv 应为 datetime64[ns],列 Hospitalizations 应为 int64(或浮点数)。也可能是版本问题...您是否安装了最新版本的 hvplot 等?但我的猜测是,您的数据看起来不正确。

无论如何,当我运行以下命令时,它会工作并绘制您的数据:

# import libraries
import pandas as pd
import hvplot.pandas
import holoviews as hv
hv.extension('bokeh')
from io import StringIO  # need this to read your text data

# your sample data
text_data = StringIO("""
    column1    TimeConv    Hospitalizations
    1   2020-04-04  827
    2   2020-04-05  1132
    72  2020-06-14  2287
    73  2020-06-15  2326
    74 NaT NaN
""")

# read text data to dataframe
df = pd.read_csv(text_data, sep="\s+")
df['TimeConv'] = pd.to_datetime(df.TimeConv, yearfirst=True)

# shortly checkout datatypes of your data
df.info()

# create scatter plot of your data
df.hvplot.scatter(
    x='TimeConv',
    y='Hospitalizations',
    width=500,
    title='Showing hospitalizations over time',
)


此代码产生以下图: 带有 hvplot 的散点图

于 2020-06-19T07:46:14.820 回答