在 Python 中将年/月/日转换为年中的天数
- 2025-04-15 09:21:00
- admin 原创
- 26
问题描述:
我正在使用该datetime
模块,即:
>>> import datetime
>>> today = datetime.datetime.now()
>>> print(today)
2009-03-06 13:24:58.857946
我想计算考虑闰年的日期。例如今天(2009 年 3 月 6 日)是 2009 年的第 65 天。
我看到两个选择:
创建一个
number_of_days_in_month = [31, 28, ...]
数组,判断是否是闰年并手动汇总天数。用来
datetime.timedelta
进行猜测,然后通过二进制搜索来找到一年中的正确日期:
>>> import datetime
>>> YEAR = 2009
>>> DAY_OF_YEAR = 62
>>> d = datetime.date(YEAR, 1, 1) + datetime.timedelta(DAY_OF_YEAR - 1)
这两种方法都感觉很笨重,而且我感觉应该有一种更“Python化”的方法来计算一年中的日期。有什么想法/建议吗?
解决方案 1:
使用datetime.timetuple()
将您的datetime
对象转换为time.struct_time
对象然后获取其tm_yday
属性:
from datetime import datetime
day_of_year = datetime.now().timetuple().tm_yday # returns 1 for January 1st
解决方案 2:
您可以strftime
使用%j
格式字符串:
>>> import datetime
>>> today = datetime.datetime.now()
>>> today.strftime('%j')
'065'
但是如果你想用这个数字进行比较或计算,你必须将其转换为,int()
因为strftime()
返回的是一个字符串。如果是这样的话,最好使用DzinX 的答案。
解决方案 3:
DZinX 的答案对这个问题来说非常棒。我找到了这个问题,并在寻找反函数时使用了DZinX 的答案:将使用儒略历日期转换为日期时间。
我发现这可行:
import datetime
datetime.datetime.strptime('1936-077T13:14:15','%Y-%jT%H:%M:%S')
>>>> datetime.datetime(1936, 3, 17, 13, 14, 15)
datetime.datetime.strptime('1936-077T13:14:15','%Y-%jT%H:%M:%S').timetuple().tm_yday
>>>> 77
或者用数字表示:
import datetime
year,julian = [1936,77]
datetime.datetime(year, 1, 1)+datetime.timedelta(days=julian -1)
>>>> datetime.datetime(1936, 3, 17, 0, 0)
或者使用在某些领域流行的基于小数 1 的 jdate:
jdate_frac = (datetime.datetime(1936, 3, 17, 13, 14, 15)-datetime.datetime(1936, 1, 1)).total_seconds()/86400+1
display(jdate_frac)
>>>> 77.5515625
year,julian = [1936,jdate_frac]
display(datetime.datetime(year, 1, 1)+datetime.timedelta(days=julian -1))
>>>> datetime.datetime(1936, 3, 17, 13, 14, 15)
我不确定这里的礼仪,但我认为指向逆功能的指针可能对像我这样的人有用。
解决方案 4:
如果您有理由避免使用该datetime
模块,那么这些功能将会起作用。
def is_leap_year(year):
""" if year is a leap year return True
else return False """
if year % 100 == 0:
return year % 400 == 0
return year % 4 == 0
def doy(Y,M,D):
""" given year, month, day return day of year
Astronomical Algorithms, Jean Meeus, 2d ed, 1998, chap 7 """
if is_leap_year(Y):
K = 1
else:
K = 2
N = int((275 * M) / 9.0) - K * int((M + 9) / 12.0) + D - 30
return N
def ymd(Y,N):
""" given year = Y and day of year = N, return year, month, day
Astronomical Algorithms, Jean Meeus, 2d ed, 1998, chap 7 """
if is_leap_year(Y):
K = 1
else:
K = 2
M = int((9 * (K + N)) / 275.0 + 0.98)
if N < 32:
M = 1
D = N - int((275 * M) / 9.0) + K * int((M + 9) / 12.0) + 30
return Y, M, D
解决方案 5:
我想展示在 Python 3.4 和 Linux x64 上不同方法的性能。摘自行分析器:
Line # Hits Time Per Hit % Time Line Contents
==============================================================
(...)
823 1508 11334 7.5 41.6 yday = int(period_end.strftime('%j'))
824 1508 2492 1.7 9.1 yday = period_end.toordinal() - date(period_end.year, 1, 1).toordinal() + 1
825 1508 1852 1.2 6.8 yday = (period_end - date(period_end.year, 1, 1)).days + 1
826 1508 5078 3.4 18.6 yday = period_end.timetuple().tm_yday
(...)
所以最有效的是
yday = (period_end - date(period_end.year, 1, 1)).days + 1
解决方案 6:
只需从日期中减去 1 月 1 日:
import datetime
today = datetime.datetime.now()
day_of_year = (today - datetime.datetime(today.year, 1, 1)).days + 1
解决方案 7:
你可以简单地使用dayofyear
“pandas”提供的属性,它会返回特定年份的日期。例如
data["day_of_year"] = data.Datetime.apply(lambda x:x.dayofyear)
扫码咨询,免费领取项目管理大礼包!