TypeError: '<=' not supported between instances of 'Timestamp' and 'numpy.float64'

Viewed 2082

I am trying to plot using hvplot, and I am getting this:

TypeError: '<=' not supported between instances of 'Timestamp' and 'numpy.float64'

Here is my data:

    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

Below is my code:

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']]
1 Answers

When I take a sample of your data above and try to plot it, it works for me, so there might be something wrong in the way you read your data from excel to pandas. You can try to do df.info() to see what the datatypes of your data look like. Column TimeConv should be datetime64[ns] and column Hospitalizations should be int64 (or float). Could also be a version problem... do you have the latest versions of hvplot etc installed? But my guess is, your data doesn't look right.

In any case, when I run the following, it works and plots your data:

# 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',
)


This code results in the following plot: scatter plot with hvplot

Related