1

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'

1 Answer 1

2

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.