我有这样的df:
ImageDate Year Month Day
20020506 2002 05 06
20030605 2003 06 05
20040201 2002 02 01
我想添加一个新列,将日期转换为julian日期。我很确定我需要模块datetime
才能执行此操作,但我查看了文档并且没有找到任何关于如何执行此操作的内容。
答案 0 :(得分:1)
去看看这个图书馆:https://pypi.python.org/pypi/jdcal
做一些事情:`
来自jdcal import gcal2jd,jd2gcal
gcal2jd(2000,1,1)'
答案 1 :(得分:1)
以下是我对here发布的原始转换算法的python实现:
def gregorian_to_julian(year, month, day):
i = int((month - 14) / 12)
jd = day - 32075
jd += int((1461 * (year + 4800 + i)) / 4)
jd += int((367 * (month - 2 - (12 * i))) / 12)
jd -= int((3 * int((year + 4900 + i) / 100)) / 4)
return jd
例如:
>>> gregorian_to_julian(1970, 1, 1)
2440588
或者,您可以使用内置日期对象从公历中获取儒略日,方法是将公历绝对日期0的儒略日添加到已发布的here的序数日期中:1,721,425:
>>> from datetime import date
>>> date(1970, 1, 1).toordinal() + 1721425
2440588
第二种解决方案更简单,更快。
答案 2 :(得分:0)
#Import python datetime library.
import datetime as dt
#Lets consider you want to convert the below date. You can have the date in any order like CCYY-MM-YY or MM-DD-CCYY or with different separator like CCYY/MM/YY.
GregDate = '2020-03-30'
# Specify the date and format within strptime in this case format is %Y-%m-%d (if your data was 2020/03/30' then use %Y/%m/%d) and specify the format you want to convert into in strftime, in this case it is %Y%j. This gives out Julian date in the form CCYYDDD, if you need only in YYDDD give the format as %y%j
Juldate = dt.datetime.strptime(GregDate, "%Y-%m-%d").strftime('%Y%j')
# Print your variable to display the result
print(Juldate)
2020090