jeangelj
jeangelj

Reputation: 4498

python matplotlib plot datetime index

I am trying to create a simple line graph based on a datetime index. But I get an error message.

#standard packages
import numpy as np
import pandas as pd

#visualization
%matplotlib inline
import matplotlib.pylab as plt

#create weekly datetime index
edf = pd.read_csv('C:\Users\j~\raw.csv', parse_dates=[6])
edf2 = edf[['DATESENT','Sales','Traffic']].copy()
edf2['DATESENT']=pd.to_datetime(edf2['DATESENT'],format='%m/%d/%Y')
edf2 = edf2.set_index(pd.DatetimeIndex(edf2['DATESENT']))
edf2.resample('w').sum()
edf2

#output

            SALES
DATESENT     
2014-01-05  476
2014-01-12  67876

Then I try to plot (the simplest line plot possible to see sales by week)

#linegraph
edf3.plot(x='DATESENT',y='Sales')

But I get this error message

KeyError: 'DATESENT'

Upvotes: 1

Views: 1790

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169494

You're getting a KeyError because your 'DATESENT' is the index and NOT a column in edf3. You can do this instead:

#linegraph
edf3.plot(x=edf3.index,y='Sales')

Upvotes: 2

Related Questions