-1

I'm trying to plot a trend line that depends on dates (in the form Sep-14 to Dec-2018) on the same plot as my actual data values.

I've tried using Seaborn:

#dh1018['BILLDATE'] returns a pandas series of strings containing the dates from Sep-14 to Dec-2018.
dh1018=df.loc[107:158,['BILLDATE','Covel']]
dates=dh1018['BILLDATE']

#plotting the actual data
plot(dates, dh1018['Covel'], label='Covel')

#trying to get that trend line
import seaborn as sns
sns.regplot(x=dates, y=dh1018['Covel'], data=dh1018, fit_reg=True)

xlabel('Billdate')
ylabel('Monthly kWh')
title('Monthly kWh of Dining Hall Buildings 2010-2018')
legend(loc='best')
fig_size=rcParams["figure.figsize"]
fig_size[0]=20
fig_size[1]=10
_=plt.xticks(rotation=90) 

In the end, I receive a TypeError basically saying it couldn't convert the dates Sep-14...Dec-18 to numeric. So I guess my question boils down to: how do I convert my date format into a number? All the examples I've found are in neat isoformat.

martineau
  • 119,623
  • 25
  • 170
  • 301
jho
  • 1

1 Answers1

0

Try using pd.to_datetime():

import pandas as pd

dates = pd.to_datetime(dh1018['BILLDATE'])
Nathaniel
  • 3,230
  • 11
  • 18