0

I have a DataFrame df as below

      Date     Confirmed    Deaths  Recovered
0   2020-01-30    1           0         0
1   2020-01-31    0           0         0
2   2020-02-01    0           0         0
3   2020-02-02    1           0         0
4   2020-02-03    1           0         0

While Trying to plot Date VS Confirmed

df.plot(kind ='line',x='Date',y='Confirmed')

I get the following error

ValueError: x must be a label or position

The Datatype of the columns are

Date         object
Confirmed     int64
Deaths        int64
Recovered     int64
dtype: object
BigBen
  • 46,229
  • 7
  • 24
  • 40
Roheet
  • 5
  • 1
  • 4
  • Maybe because your Date is of type object. try it this way https://stackoverflow.com/a/41815888/2287841 – Octav Feb 11 '21 at 14:43

2 Answers2

1
df['Date']=pd.to_datetime(df['Date'])

Try changing the datatype of the Date column from Object to datetime64[ns] datatype.

PS: Don't forget to import pandas as pd. ↓

import pandas as pd
0

Your code snippet is perfectly fine. I suspect an issue in your instance type of Date column in the dataframe itself.

Try this:

from pandas.core.dtypes.generic import ABCSeries
print(isinstance(df["Date"], ABCSeries))

If this returns False, you can be sure that Date column is not created with a proper instance type in the dataframe. You can try converting your date column to datetime objects using pd.to_datetime()

vivek
  • 26
  • 4