gpt4 book ai didi

python - 根据年份和儒略日在 pandas 中创建日期时间

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:00 26 4
gpt4 key购买 nike

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

最佳答案

您需要添加到转换后的yearJDto_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

关于python - 根据年份和儒略日在 pandas 中创建日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38067348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com