Python 将公历日期转换为朱利安日期

Python 将公历日期转换为朱利安日期,python,pandas,datetime,julian-date,Python,Pandas,Datetime,Julian Date,我有一个类似这样的df: ImageDate Year Month Day 20020506 2002 05 06 20030605 2003 06 05 20040201 2002 02 01 我想添加一个新列,将日期转换为朱利安日期。我很确定我需要模块datetime来完成这项工作,但我查看了文档,没有找到任何对我来说明显的关于如何完成这项工作的信息。去看看这个库: 然后做一些类似的事情:` 从jdcal导入gcal2

我有一个类似这样的df:

ImageDate   Year   Month   Day
20020506    2002   05      06
20030605    2003   06      05
20040201    2002   02      01

我想添加一个新列,将日期转换为朱利安日期。我很确定我需要模块
datetime
来完成这项工作,但我查看了文档,没有找到任何对我来说明显的关于如何完成这项工作的信息。

去看看这个库:

然后做一些类似的事情:`

从jdcal导入gcal2jd、jd2gcal

gcal2jd(2000,1,1)'


去看看这个图书馆:

然后做一些类似的事情:`

从jdcal导入gcal2jd、jd2gcal

gcal2jd(2000,1,1)'

第一种解决方案 以下是我发布的原始转换算法的python实现:

例如:

>>> gregorian_to_julian(1970, 1, 1)
2440588

第二种解决方案 或者,您可以使用内置日期对象从公历日期获取儒略日期,方法是将公历绝对日期0的儒略日期1721425添加到发布的序号日期:


第二种解决方案更简单,速度更快。

第一种解决方案
#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)
以下是我发布的原始转换算法的python实现:

例如:

>>> gregorian_to_julian(1970, 1, 1)
2440588

第二种解决方案 或者,您可以使用内置日期对象从公历日期获取儒略日期,方法是将公历绝对日期0的儒略日期1721425添加到发布的序号日期:


第二种解决方案更简单,速度更快

#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)

您可以使用%Y-%m-%d来控制格式,如果您只需要儒略日期中的年份部分,如20090年,请使用(“%Y%j”),请添加几句话来解释代码的作用,这样您就可以为您的答案获得更多的投票。虽然此代码可以解决问题,如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。好的,谢谢。。您可以使用%Y-%m-%d来控制格式,如果您只需要儒略日期中的年份部分,如20090年,请使用(“%Y%j”)添加几句话来解释代码的作用,这样您就可以为您的答案获得更多的投票。虽然此代码可以解决问题,如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。好的,谢谢。。将来会小心的。
2020090