如何转换一年中的天数(365)?

Jim*_*mys -3 python datetime date pandas

我有一个数据框,我想要一个函数来转换一年中的天数(365).提前致谢.!!

d_w_data.drop(['kwh',  'time', 'month', 'dewptm', 'hum', 'wspdm', 'cos(hour)', 'sin(hour)'], inplace=True, axis=1)

d_w_data

Out[55]:
                         year   day     index1                aptemp      maptemp      mtempm   tempm      hour_to_rad
2012-04-12 14:00:00     2012    12  2012-04-12 14:00:00     17.574044   14.483582   15.508494   19.510000   3.665191
2012-04-12 15:00:00     2012    12  2012-04-12 15:00:00     17.769016   14.483582   15.508494   19.600000   3.926991
2012-04-12 16:00:00     2012    12  2012-04-12 16:00:00     17.532051   14.483582   15.508494   19.488235   4.188790
2012-04-12 17:00:00     2012    12  2012-04-12 17:00:00     16.975478   14.483582   15.508494   18.690000   4.450590
2012-04-12 18:00:00     2012    12  2012-04-12 18:00:00     16.520319   14.483582   15.508494   17.566667   4.712389
2012-04-12 19:00:00     2012    12  2012-04-12 19:00:00     15.481347   14.483582   15.508494   16.778125   4.974188
2012-04-12 20:00:00     2012    12  2012-04-12 20:00:00     15.361154   14.483582   15.508494   16.275862   5.235988
2012-04-12 21:00:00     2012    12  2012-04-12 21:00:00     15.084252   14.483582   15.508494   15.584211   5.497787
Run Code Online (Sandbox Code Playgroud)

是的数据框是大熊猫,我想转换一年中的天数(1-365).我想做负荷预测.谢谢!

Nun*_*dré 5

如果你解析你的日期,struct_time你将获得一年中的几天作为属性.

>>>import time
>>>time.strptime("18 Jun 15", "%d %b %y")
time.struct_time(tm_year=2015, tm_mon=6, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=169, tm_isdst=-1)
Run Code Online (Sandbox Code Playgroud)

所以:

>>>import time
>>>time.strptime("18 Jun 15", "%d %b %y").tm_yday
169
Run Code Online (Sandbox Code Playgroud)