从年和儒略历日创建熊猫的日期时间


                  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'),但这不起作用

您需要添加到yearJD转换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=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

相关内容

  • 没有找到相关文章

最新更新