从年和朱利安日在熊猫中创建日期时间

时间:2016-06-28 04:57:46

标签: python datetime pandas dataframe time-series

                  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 

如何使用yearJD(朱利安日)列添加日期时间列?我试图使用:

pd.to_datetime(df, format='%Y_%d'),但这不起作用

1 个答案:

答案 0 :(得分:1)

您需要添加yearJD已转换Ansible Documentation

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=32JD=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