I have a data frame with dates in string format. I convert them to datetime object using strptime
The y is a float
Then I use plt.plot_date to try to make a scatter plot of the data
but i keep getting float() argument must be a string or a number, not 'datetime.date'
My full code is below:
from datetime import datetime
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
def plot():
data = []
df = pd.read_csv('random_case_number.csv')
for index, row in df.iterrows():
if (row['recipt_number'][: 3] != 'IOE'):
data.append([int(row['recipt_number'][5:]) - 90000000, datetime.strptime(row['date'], '%Y-%m-%d').date()])
dates = mdates.date2num(data[1])
plt.plot_date(dates, data[0])
sample data:
recipt_number,date
IOE0914808418,1998-01-01
MSC2290138517,2021-11-18
LIN2190425860,2021-07-09
LIN2290143717,2022-02-22
WAC2190082175,2021-05-03
IOE9086351290,2022-01-06
IOE0912562803,2021-07-09
IOE9086351290,2022-01-06
SRC2290076110,2022-01-24
IOE0913043187,1322-07-27
Any help is greatly appriciated
CodePudding user response:
Don't iterate over your DataFrame. Try:
df = pd.read_csv('random_case_number.csv')
df["date"] = pd.to_datetime(df["date"])
df["recipt"] = df["recipt_number"].str[5:].astype(int).sub(90000000).where(~df["recipt_number"].str.startswith("IOE"))
ax = df.plot.scatter(x="date",y="recipt",rot=90)