Pandas AttributeError: 'DataFrame' 对象没有属性 'Datetime'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52182361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:00:13  来源:igfitidea点击:

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

pythonpandasdataframe

提问by eduliant

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

我正在使用 holt Winter 方法从这里获得帮助。我的数据格式是

 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'

AttributeError: 'DataFrame' 对象没有属性 'Datetime'

回答by desertnaut

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):

如果您的数据确实如所示(带有Rate&列Year),则您引用的列 ( Datetime) 不存在(与链接的博客文章中的数据相反,其中确实有这样的列):

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 Yearinstead:

你应该参考Year

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