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
(儒略日)列添加datetime列?我正在尝试使用: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
关于python - 从年份和朱利安日开始在 Pandas 中创建日期时间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38067348/