ad_name adl_name year JD
0 united_states_of_america colorado 2000 1
1 united_states_of_america colorado 2000 2
2 united_states_of_america colorado 2000 3
3 united_states_of_america colorado 2000 4
4 united_states_of_america colorado 2000 5
如何使用 year
和 JD
(儒略日)列添加日期时间列?我试图使用:
pd.to_datetime(df, format='%Y_%d')
,但这不起作用
您需要添加到转换后的year
列JD
to_timedelta
:
df['date'] = pd.to_datetime(df.year, format='%Y') + pd.to_timedelta(df.JD - 1, unit='d')
print (df)
ad_name adl_name year JD date
0 united_states_of_america colorado 2000 1 2000-01-01
1 united_states_of_america colorado 2000 2 2000-01-02
2 united_states_of_america colorado 2000 3 2000-01-03
3 united_states_of_america colorado 2000 4 2000-01-04
4 united_states_of_america colorado 2000 5 2000-01-05
包含 JD=32
和 JD=366
的示例:
print (df)
ad_name adl_name year JD
0 united_states_of_america colorado 2000 32
1 united_states_of_america colorado 2000 366
df['date'] = pd.to_datetime(df.year, format='%Y') + pd.to_timedelta(df.JD - 1, unit='d')
print (df)
ad_name adl_name year JD date
0 united_states_of_america colorado 2000 32 2000-02-01
1 united_states_of_america colorado 2000 366 2000-12-31
我是一名优秀的程序员,十分优秀!