pandas AttributeError: 'Int64Index' 对象没有属性 'month'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54639490/
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:19:12  来源:igfitidea点击:

AttributeError: 'Int64Index' object has no attribute 'month'

pythonpandascsvdata-science

提问by HenryHub

I have some time series data with three separate colums (Date, Time, kW) that looks like this:

我有一些带有三个单独列(日期、时间、千瓦)的时间序列数据,如下所示:

Date     Time        kW
3/1/2011 12:15:00 AM 171.36
3/1/2011 12:30:00 AM 181.44
3/1/2011 12:45:00 AM 175.68
3/1/2011 1:00:00 AM 180.00
3/1/2011 1:15:00 AM 175.68

And reading the csv file directly from Pandas I can parse the Date & Time:

并直接从 Pandas 读取 csv 文件,我可以解析日期和时间:

df= pd.read_csv('C:\Users\desktop\master.csv', parse_dates=[['Date', 'Time']])

Which appears to work nicely, but the problem is I want to create another data frame in Pandas to represent the numerical value of the month. If I do a:

这似乎工作得很好,但问题是我想在 Pandas 中创建另一个数据框来表示月份的数值。如果我做:

df['month'] = df.index.month

An error is thrown:

抛出一个错误:

AttributeError: 'Int64Index' object has no attribute 'month'

AttributeError: 'Int64Index' object has no attribute 'month'

I am also hoping to create additional dataframes to represent time stampt day, minute, hour... Any tips greatly appreciated..

我也希望创建额外的数据帧来表示时间戳的日期、分钟、小时......任何提示都非常感谢......

采纳答案by Vaishali

You can use datetime accessor and extract month

您可以使用日期时间访问器并提取月份

df['month'] = df['Date_Time'].dt.month