Convert Fractional Day Of Year To Pandas Datetime
Solution 1:
Although the accepted answer is correct in the conversion of DOY to Datetime, there is a slight mistake that has been overlooked.
Midnight of January 1 for any year is DOY 1.0. As you proceed with fractional DOY time, Jan 1 12:00 is DOY 1.5, Jan 2 00:00 is DOY 2.0, etc...
If you add the DOY time to a base offset date, as suggested in other answers, the resulting time is offset forward by one day. For example, pd.to_datetime('2011-01-01') + pd.to_timedelta(df.DOY, unit='D')
, with a DOY series that starts with 1.0, results in a starting date of '2011-01-02' which is incorrect. This is a result of the convention that DOY time starts with 1 instead of 0. See here for more info.
Therefore, the correct answer (producing correct Datetime results) is:
df.DOY = pd.to_datetime('2011-1-1') + pd.to_timedelta(gps.DOY, unit='D') - pd.Timedelta(days=1)
Solution 2:
One way I can think to do this is to convert your column to TimeDelta
and then add it to the base offset (2011/1/1).
df.DOY=pd.to_datetime('2011-1-1')+pd.to_timedelta(df.DOY,unit='D')print(df.DOY)02011-07-20 17:59:57.14880012011-07-20 19:00:00.02880022011-07-20 20:00:02.90880032011-07-20 20:59:57.14880042011-07-20 22:00:00.02880052011-07-20 23:00:02.90880062011-07-20 23:59:57.14880072011-07-21 01:00:00.028800...3491 2012-09-19 04:00:00.0288003492 2012-09-19 08:00:02.9088003493 2012-09-19 11:59:57.1488003494 2012-09-19 16:00:00.0288003495 2012-09-19 20:00:02.9088003496 2012-09-19 23:59:57.1488003497 2012-09-20 04:00:00.0288003498 2012-09-20 08:00:02.908800Name:DOY,dtype:datetime64[ns]
Another method would be to call pd.to_datetime
with the origin
parameter set, as agtoever shows in their answer.
Solution 3:
Just use to_datetime
with the appropiate parameters (read the manual):
>>> pandas.to_datetime([0,0.1,200,400,800], unit='D', origin=pandas.Timestamp('01-01-2011'))
DatetimeIndex(['2011-01-01 00:00:00', '2011-01-01 02:24:00', '2011-07-20 00:00:00', '2012-02-05 00:00:00', '2013-03-11 00:00:00'], dtype='datetime64[ns]', freq=None)
Post a Comment for "Convert Fractional Day Of Year To Pandas Datetime"