将一年中的小数天转换为Pandas Datetime [英] Convert fractional day of year to Pandas Datetime
问题描述
我有一个Pandas DataFrame列,它是一年中的小数日(DOY)。此列显示为:
DOY
0 200.749967
1 200.791667
2 200.833367
3 200.874967
4 200.916667
5 200.958367
6 200.999967
7 201.041667
...
3491 627.166667
3492 627.333367
3493 627.499967
3494 627.666667
3495 627.833367
3496 627.999967
3497 628.166667
3498 628.333367
名称:DOY,长度:3499,dtype:float64
起始年份是2011年,但是DOY数据一直到2012年都在不断增加,而新的DOY数据未重置为零
如何将其转换为格式为'YYYY-MM-DD HH:MM:SS'的Pandas DatetimeIndex?
我可以想到的一种方法是将您的列转换为 TimeDelta
,然后将其添加到基本偏移量(2011/1/1)。
df.DOY = pd.to_datetime('2011-1-1')+ pd.to_timedelta(df.DOY,unit ='D')
print(df.DOY)
0 2011-07-20 17:59 :57.148800
1 2011-07-20 19:00:00.028800
2 2011-07-20 20:00:02.908800
3 2011-07-20 20:59:57.148800
4 2011-07-20 22:00:00.028800
5 2011-07-20 23:00:02.908800
6 2011-07-20 23:59:57.148800
7 2011-07 -21 01:00:00.028800
...
3491 2012-09-19 04:00:00.028800
3492 2012-09-19 08:00:02.908800
3493 2012 -09-19 11:59:57.148800
3494 2012-09-19 16:00:00.028800
3495 2012-09-19 20:00:02.908800
3496 2012-09-19 23 :59:57.148800
3497 2012-09-20 04:00:00.028800
3498 2012-09-20 08:00:02.908800
名称:DOY,dtype:datetime64 [ns]
另一种方法是调用 pd.to_datetime
原始
参数集,如其答案中所示。 / p>
I have a column of a Pandas DataFrame that is fractional day of year (DOY). This column appears as:
DOY
0 200.749967
1 200.791667
2 200.833367
3 200.874967
4 200.916667
5 200.958367
6 200.999967
7 201.041667
...
3491 627.166667
3492 627.333367
3493 627.499967
3494 627.666667
3495 627.833367
3496 627.999967
3497 628.166667
3498 628.333367
Name: DOY, Length: 3499, dtype: float64
The starting year is 2011, however the DOY data continues with increasing values through 2012 without resetting to zero on the new year.
How do I convert this to a Pandas DatetimeIndex with format 'YYYY-MM-DD HH:MM:SS'?
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)
0 2011-07-20 17:59:57.148800
1 2011-07-20 19:00:00.028800
2 2011-07-20 20:00:02.908800
3 2011-07-20 20:59:57.148800
4 2011-07-20 22:00:00.028800
5 2011-07-20 23:00:02.908800
6 2011-07-20 23:59:57.148800
7 2011-07-21 01:00:00.028800
...
3491 2012-09-19 04:00:00.028800
3492 2012-09-19 08:00:02.908800
3493 2012-09-19 11:59:57.148800
3494 2012-09-19 16:00:00.028800
3495 2012-09-19 20:00:02.908800
3496 2012-09-19 23:59:57.148800
3497 2012-09-20 04:00:00.028800
3498 2012-09-20 08:00:02.908800
Name: DOY, dtype: datetime64[ns]
Another method would be to call pd.to_datetime
with the origin
parameter set, as agtoever shows in their answer.
这篇关于将一年中的小数天转换为Pandas Datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!