Hor*_*ann 374
datetime.date有一个isocalendar()方法,它返回一个包含日历周的元组:
>>> import datetime
>>> datetime.date(2010, 6, 16).isocalendar()[1]
24
Run Code Online (Sandbox Code Playgroud)
datetime.date.isocalendar()是一个实例方法,以给定日期实例的相应顺序返回包含年,周数和工作日的元组.
jot*_*cor 105
您可以直接从datetime获取周数作为字符串.
>>> import datetime
>>> datetime.date(2010, 6, 16).strftime("%V")
'24'
Run Code Online (Sandbox Code Playgroud)
您还可以获得更改strftime参数的年份周数的不同"类型":
%U - 当前年份的周数,从第一个星期日开始,作为第一周的第一天.
%V - 当前年份的ISO 8601周数(01至53),其中第1周是当前年度至少4天的第一周,周一是第一周的第一周.
%W - 当前年份的周数,从第一个星期一开始,作为第一周的第一天.
我从这里得到它.它在Python 2.7.6中适用于我
Byr*_*ahl 74
我相信date.isocalendar()这将是答案.本文解释了ISO 8601日历背后的数学.查看Python文档的datetime页面的date.isocalendar()部分.
>>> dt = datetime.date(2010, 6, 16)
>>> wk = dt.isocalendar()[1]
24
Run Code Online (Sandbox Code Playgroud)
.isocalendar()返回一个3元组(年,周数,周日).dt.isocalendar()[0]返回年份,dt.isocalendar()[1]返回周数,dt.isocalendar()[2]返回工作日.很简单就可以了.
Bar*_*ers 27
这是另一种选择:
import time
from time import gmtime, strftime
d = time.strptime("16 Jun 2010", "%d %b %Y")
print(strftime("%U", d))
Run Code Online (Sandbox Code Playgroud)
打印24.
请参阅:http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior
dra*_*hri 24
有许多系统用于周编号。以下是最常见的系统,简单地附上代码示例:
ISO:第一周从星期一开始,必须包含 1 月 4 日。ISO 日历已经在 Python 中实现:
>>> from datetime import date
>>> date(2014, 12, 29).isocalendar()[:2]
(2015, 1)
Run Code Online (Sandbox Code Playgroud)北美:第一周从星期日开始,必须包含 1 月 1 日。以下代码是我针对北美系统的 Python 的 ISO 日历实现的修改版本:
from datetime import date
def week_from_date(date_object):
date_ordinal = date_object.toordinal()
year = date_object.year
week = ((date_ordinal - _week1_start_ordinal(year)) // 7) + 1
if week >= 52:
if date_ordinal >= _week1_start_ordinal(year + 1):
year += 1
week = 1
return year, week
def _week1_start_ordinal(year):
jan1 = date(year, 1, 1)
jan1_ordinal = jan1.toordinal()
jan1_weekday = jan1.weekday()
week1_start_ordinal = jan1_ordinal - ((jan1_weekday + 1) % 7)
return week1_start_ordinal
Run Code Online (Sandbox Code Playgroud)
>>> from datetime import date
>>> week_from_date(date(2014, 12, 29))
(2015, 1)
Run Code Online (Sandbox Code Playgroud)>>> from datetime import date
>>> from epiweeks import Week
>>> Week.fromdate(date(2014, 12, 29))
(2014, 53)
Run Code Online (Sandbox Code Playgroud)Mar*_*som 21
其他人建议的ISO周是好的,但它可能不符合您的需求.它假定每周从星期一开始,这导致在年初和年末出现一些有趣的异常.
如果你宁愿使用一个定义,即第1周始终是1月1日到1月7日,不管星期几,都使用这样的推导:
>>> testdate=datetime.datetime(2010,6,16)
>>> print(((testdate - datetime.datetime(testdate.year,1,1)).days // 7) + 1)
24
Run Code Online (Sandbox Code Playgroud)
Cha*_*ter 17
通常获取当前周数(从星期日开始):
from datetime import *
today = datetime.today()
print today.strftime("%U")
Run Code Online (Sandbox Code Playgroud)
小智 13
对于一年中瞬时周的整数值,请尝试:
import datetime
datetime.datetime.utcnow().isocalendar()[1]
Run Code Online (Sandbox Code Playgroud)
Jak*_*kov 12
我发现这是获取周数的最快方法;所有变体。
from datetime import datetime
dt = datetime(2021, 1, 3) # Date is January 3rd 2021 (Sunday), year starts with Friday
dt.strftime("%W") # '00'; Monday is considered first day of week, Sunday is the last day of the week which started in the previous year
dt.strftime("%U") # '01'; Sunday is considered first day of week
dt.strftime("%V") # '53'; ISO week number; result is '53' since there is no Thursday in this year's part of the week
Run Code Online (Sandbox Code Playgroud)
进一步的说明%V可以在 Python 文档中找到:
ISO 年由 52 或 53 个完整周组成,一周从星期一开始,到星期日结束。ISO 年份的第一周是包含星期四的一年中的第一个(公历)日历周。这称为第 1 周,该星期四的 ISO 年与其公历年相同。
https://docs.python.org/3/library/datetime.html#datetime.date.isocalendar
注意:请记住返回值是一个字符串,因此int如果您需要数字,请将结果传递给构造函数。
如果您只是全面使用isocalendar周数,则以下内容应足够:
import datetime
week = date(year=2014, month=1, day=1).isocalendar()[1]
Run Code Online (Sandbox Code Playgroud)
这将检索isocalendar为我们的周数返回的元组的第二个成员.
但是,如果您要使用处理公历的日期函数,则仅使用isocalendar不起作用!请看以下示例:
import datetime
date = datetime.datetime.strptime("2014-1-1", "%Y-%W-%w")
week = date.isocalendar()[1]
Run Code Online (Sandbox Code Playgroud)
这里的字符串表示将2014年第一周的星期一作为我们的日期返回.当我们使用isocalendar在这里检索周数时,我们希望得到相同的周数,但我们不会.相反,我们得到一周的数字2.为什么?
格里高利历中的第1周是包含星期一的第一周.isocalendar中的第1周是包含星期四的第一周.2014年初的部分周包含星期四,所以这是isocalendar的第1周,并且是date第2周.
如果我们想要获得格里高利周,我们将需要从isocalendar转换为Gregorian.这是一个简单的功能,可以解决这个问题.
import datetime
def gregorian_week(date):
# The isocalendar week for this date
iso_week = date.isocalendar()[1]
# The baseline Gregorian date for the beginning of our date's year
base_greg = datetime.datetime.strptime('%d-1-1' % date.year, "%Y-%W-%w")
# If the isocalendar week for this date is not 1, we need to
# decrement the iso_week by 1 to get the Gregorian week number
return iso_week if base_greg.isocalendar()[1] == 1 else iso_week - 1
Run Code Online (Sandbox Code Playgroud)
我将讨论总结为两个步骤:
datetime对象。datetime对象或date对象的函数来计算周数。暖身
from datetime import datetime, date, time
d = date(2005, 7, 14)
t = time(12, 30)
dt = datetime.combine(d, t)
print(dt)
Run Code Online (Sandbox Code Playgroud)
第一步
要手动生成datetime对象,我们可以使用datetime.datetime(2017,5,3)或datetime.datetime.now()。
但实际上,我们通常需要解析一个现有的字符串。我们可以使用strptime函数,比如datetime.strptime('2017-5-3','%Y-%m-%d')你必须在其中指定格式。不同格式代码的详细信息可以在官方文档中找到。
或者,更方便的方法是使用dateparse模块。例如dateparser.parse('16 Jun 2010'),dateparser.parse('12/2/12')或dateparser.parse('2017-5-3')
以上两种方法都会返回一个datetime对象。
第二步
使用获取的 datetime对象调用strptime(format). 例如,
Python
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object. This day is Sunday
print(dt.strftime("%W")) # '00' Monday as the 1st day of the week. All days in a new year preceding the 1st Monday are considered to be in week 0.
print(dt.strftime("%U")) # '01' Sunday as the 1st day of the week. All days in a new year preceding the 1st Sunday are considered to be in week 0.
print(dt.strftime("%V")) # '52' Monday as the 1st day of the week. Week 01 is the week containing Jan 4.
Run Code Online (Sandbox Code Playgroud)
决定使用哪种格式非常棘手。更好的方法是获取date要调用的对象isocalendar()。例如,
Python
dt = datetime.strptime('2017-01-1','%Y-%m-%d') # return a datetime object
d = dt.date() # convert to a date object. equivalent to d = date(2017,1,1), but date.strptime() don't have the parse function
year, week, weekday = d.isocalendar()
print(year, week, weekday) # (2016,52,7) in the ISO standard
Run Code Online (Sandbox Code Playgroud)
实际上,您将更有可能用来date.isocalendar() 准备每周报告,尤其是在Christmas-New Year购物旺季。
小智 6
您可以尝试%W指令,如下所示:
d = datetime.datetime.strptime('2016-06-16','%Y-%m-%d')
print(datetime.datetime.strftime(d,'%W'))
Run Code Online (Sandbox Code Playgroud)
'%W':一年中的周数(星期一作为一周的第一天)作为十进制数.在第一个星期一之前的新年中的所有日子被认为是在第0周.(00,01,...,53)
小智 5
对于 pandas 用户,如果你想获取一列周数:
df['weekofyear'] = df['Date'].dt.week
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
223703 次 |
| 最近记录: |