2

I am working with holt winter method taking help from here. My data format is

 Year       Rate
0  2013  34.700000
1  2013  34.666667
2  2013  34.600000
3  2014  35.300000
4  2014  34.180000

Below is my code

import pandas as pd 

#Importing data

df = pd.read_csv('/home/rajnish.kumar/eclipse-workspace/ShivShakti/Result/weeklyDatarateyearonly/part-00000-971f46d7-a97d-4a7e-be41-dc840c2d0618-c000.csv')

df.Timestamp = pd.to_datetime(df.Datetime,format='%Y') 

But I am getting this error:

AttributeError: 'DataFrame' object has no attribute 'Datetime'

1
  • 2
    Presumably you don't have a column called Datetime. This is why I prefer not to use the . accessor syntax for column names Commented Sep 5, 2018 at 10:04

2 Answers 2

5

If your data is indeed as shown (with columns Rate & Year), you are referencing a column (Datetime) that does not exist (in contrast with the data in the linked blog post, where there is indeed such a column):

import pandas as pd
data = {'Year':[2013, 2013, 2013, 2014, 2014], 'Rate':[34.7, 34.6,34.6,35.3,34.18]}
df = pd.DataFrame(data, columns=["Year", "Rate"])
df.Timestamp = pd.to_datetime(df.Datetime,format='%Y') 
# AttributeError: 'DataFrame' object has no attribute 'Datetime'

You should reference Year instead:

df['Timestamp'] = pd.to_datetime(df['Year'],format='%Y') 
df
# result:
   Year   Rate  Timestamp
0  2013  34.70 2013-01-01
1  2013  34.60 2013-01-01
2  2013  34.60 2013-01-01
3  2014  35.30 2014-01-01
4  2014  34.18 2014-01-01
Sign up to request clarification or add additional context in comments.

2 Comments

I'd still prefer df['Year'] over df.Year. There's just so many methods in Pandas for it to be confusing on the surface, let alone AttributeErrors on top
@roganjosh not a pandas expert, so I'll take your word (edited to df['Year']) ;)
0

You could use something like this:

import pandas as pd
data = {'Year':[2013, 2013, 2013, 2014, 2014], 'Rate':[34.7, 34.6,34.6,35.3,34.18]}
df = pd.DataFrame(data, columns=["Year", "Rate"])
df.Timestamp = pd.to_datetime(df.Year) 

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.