简体   繁体   中英

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

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'

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

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM