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

心已入冬 提交于 2019-12-11 12:06:09

问题


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:

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:

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

An error is thrown:

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..


回答1:


You can use datetime accessor and extract month

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


来源:https://stackoverflow.com/questions/54639490/attributeerror-int64index-object-has-no-attribute-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!