AttributeError:"Int64Index"对象没有属性"month" [英] AttributeError: 'Int64Index' object has no attribute 'month'
问题描述
我有一些带有三个独立列(日期,时间,千瓦)的时间序列数据,如下所示:
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
直接从熊猫读取csv文件,我可以解析日期&时间:
And reading the csv file directly from Pandas I can parse the Date & Time:
df= pd.read_csv('C:\\Users\\desktop\\master.csv', parse_dates=[['Date', 'Time']])
哪个看上去工作得很好,但是问题是我想在Pandas中创建另一个数据框来表示月份的数值.如果我这样做:
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:
df['month'] = df.index.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..
推荐答案
您可以使用日期时间访问器并提取月份
You can use datetime accessor and extract month
df['month'] = df['Date_Time'].dt.month
这篇关于AttributeError:"Int64Index"对象没有属性"month"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!