我有一个数据帧,我想要一个函数来转换以一年中的几天(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
是的,数据帧是熊猫,我想以一年中的几天(1-365)转换天数。我想做一个负载预测。谢谢!!
如果您将日期解析为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)
所以:
>>>import time
>>>time.strptime("18 Jun 15", "%d %b %y").tm_yday
169
你不必离开熊猫来做这件事。
dti = pd.date_range('05-01-1990', '05-31-1990')
dti.dayofyear
返回
array([121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133,
134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146,
147, 148, 149, 150, 151], dtype=int32)