该月的第几周?
python 是否提供了一种轻松获取当月当前周 (1:4) 的方法?
Does python offer a way to easily get the current week of the month (1:4) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
python 是否提供了一种轻松获取当月当前周 (1:4) 的方法?
Does python offer a way to easily get the current week of the month (1:4) ?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(23)
为了使用直除法,您要查看的日期所在的月份需要根据该月第一天的位置(在一周内)进行调整。因此,如果您的月份恰好从星期一(一周的第一天)开始,您可以按照上面的建议进行除法。但是,如果该月从星期三开始,您需要加 2,然后进行除法。这一切都封装在下面的函数中。
In order to use straight division, the day of month for the date you're looking at needs to be adjusted according to the position (within the week) of the first day of the month. So, if your month happens to start on a Monday (the first day of the week), you can just do division as suggested above. However, if the month starts on a Wednesday, you'll want to add 2 and then do the division. This is all encapsulated in the function below.
我知道这已经有很多年了,但我花了很多时间试图找到这个答案。我制定了自己的方法并认为我应该分享。
日历模块有一个monthcalendar方法,它返回一个二维数组,其中每行代表一周。例如:
结果:
那么 numpy 的结果是你的朋友在哪里。我在美国,所以我希望这一周从周日开始,并将第一周标记为 1:
返回
I know this is years old, but I spent a lot of time trying to find this answer. I made my own method and thought I should share.
The calendar module has a monthcalendar method that returns a 2D array where each row represents a week. For example:
result:
So numpy's where is your friend here. And I'm in USA so I want the week to start on Sunday and the first week to be labelled 1:
returns
如果您的第一周从该月的第一天开始,您可以使用整数除法:
If your first week starts on the first day of the month you can use integer division:
查看包 Pendulum
Check out the package Pendulum
这个版本可以改进,但作为 python 模块(日期时间和日历)的第一眼,我提出了这个解决方案,我希望有用:
This version could be improved but as a first look in python modules (datetime and calendar), I make this solution, I hope could be useful:
date_value 应采用时间戳格式
这将在所有情况下给出完美的答案。它纯粹基于 ISO 日历
date_value should in timestamp format
This will give the perfect answer in all the cases. It is purely based on ISO calendar
乔希的答案必须稍作调整,以适应第一天是周日。
Josh's answer has to be tweaked slightly to accomodate the first day falling on a Sunday.
数据['wk_of_mon'] = (数据['dataset_date'].dt.day - 1) // 7 + 1
data['wk_of_mon'] = (data['dataset_date'].dt.day - 1) // 7 + 1
查看 python 日历 模块
Check out the python calendar module
我找到了一个非常简单的方法:
如果是第一周则返回 0
I found a quite simple way:
returns 0 if first week
乔什的答案似乎是最好的,但我认为我们应该考虑这样一个事实:只有当周四属于该月时,一周才属于该月。至少iso 是这么说的。
根据该标准,一个月最多可以有 5 周。一天可以属于一个月,但它所属的一周可能不属于。
我已经考虑到,只需添加一个简单的
其中 ret_val 正是 Josh 的计算值。于 2017 年 6 月(有 5 周)和 2017 年 9 月进行了测试。通过“2017-09-01”将返回 0,因为该天属于不属于 9 月的一周。
最正确的方法是让该方法返回输入日期所属的周数和月份名称。
Josh' answer seems the best but I think that we should take into account the fact that a week belongs to a month only if its Thursday falls into that month. At least that's what the iso says.
According to that standard, a month can have up to 5 weeks. A day could belong to a month, but the week it belongs to may not.
I have taken into account that just by adding a simple
where ret_val is exactly Josh's calculated value. Tested on June 2017 (has 5 weeks) and on September 2017. Passing '2017-09-01' returns 0 because that day belongs to a week that does not belong to September.
The most correct way would be to have the method return both the week number and the month name the input day belongs to.
@Manuel Solorzano 的答案的变体:
例如:
A variation on @Manuel Solorzano's answer:
E.g.:
假设我们有某个月的日历,如下所示:
我们说第 1 ~ 3 天属于第 1 周,第 4 ~ 10 天属于第 2 周等等。
在这种情况下,我相信特定日期的 week_of_month 应该计算如下:
但是,如果我们想要获取该日期所在工作日的第 n 个,例如第 1 天是第 1 个星期五,第 8 天是第 2 个星期五星期五,第 6 天是第一个星期三,那么我们可以简单地返回 (day - 1)//7 + 1
Say we have some month's calender as follows:
We say day 1 ~ 3 belongs to week 1 and day 4 ~ 10 belongs to week 2 etc.
In this case, I believe the week_of_month for a specific day should be calculated as follows:
However, if instead we want to get the nth of the weekday that date is, such as day 1 is the 1st Friday, day 8 is the 2nd Friday, and day 6 is the 1st Wednesday, then we can simply return (day - 1)//7 + 1
您正在寻找的答案是
(dm-dw+(dw-dm)%7)/7+1
,其中dm
是该月的日期,dw
是星期几,%
是正余数。这来自于月份偏移量 (
mo
) 和月份中的第几周 (wm
) 的关联,其中月份偏移量是第一天开始的时间。如果我们考虑所有这些变量都从 0 开始,您可以求解
mo
的模 7,因为这会导致wm
变量丢失,因为它只显示为倍数7然后您只需将月份偏移量代入原始方程并求解
wm
由于
dm
和dw
始终成对出现,因此可以将它们偏移任何数量,因此,将所有内容切换为从 1 开始只会将等式更改为(dm-dw + (dw-dm)%7)/7 + 1
。当然,python
datetime
库从 1 开始dm
,从 0 开始dw
。因此,假设date
是一个 < code>datatime.date 对象,您可以使用由于内部位始终是 7 的倍数(字面上是
dw*7
),您可以看到第一个-date.dayofweek()
只是将值向后调整为最接近的 7 倍数。整数除法也可以执行此操作,因此可以进一步简化为注意
dayofweek()
将星期日置于周末。The answer you are looking for is
(dm-dw+(dw-dm)%7)/7+1
wheredm
is the day of the month,dw
is the day of the week, and%
is the positive remainder.This comes from relating the month offset (
mo
) and the week of the month (wm
), where the month offset is how far into the week the first day starts. If we consider all of these variables to start at 0 we haveYou can solve this modulo 7 for
mo
as that causes thewm
variable drops out as it only appears as a multiple of 7Then you just substitute the month offset into the original equation and solve for
wm
As
dm
anddw
are always paired, these can be offset by any amount, so, switching everything to start a 1 only changes the the equation to(dm-dw + (dw-dm)%7)/7 + 1
.Of course the python
datetime
library startsdm
at 1 anddw
at 0. So, assumingdate
is adatatime.date
object, you can go withAs the inner bit is always a multiple of 7 (it is literally
dw*7
), you can see that the first-date.dayofweek()
simply adjusts the value backwards to closest multiple of 7. Integer division does this too, so it can be further simplified toBe aware that
dayofweek()
puts Sunday at the end of the week.这应该可以做到。
This should do it.
移至该月一周的最后一天并除以 7
Move to last day of week in month and divide to 7
您可以简单地执行以下操作:
You can simply do as follow:
还有一个解决方案,其中星期日是一周的第一天,仅基于 Python。
One more solution, where Sunday is first day of week, base Python only.
获取月份周数的简单方法;
如果数据类型是 datetime64 那么
An Easy way to get a week number of month;
if the datatype is datetime64 then
我发现的最好的解决方案是这个函数
the best solution that I found is this function
此解决方案旨在复制 Python 中 WeekOfMonth 的 Java 实现,并遵循类似于 ISO 8601 约定。
一周的第一天是星期一
第一周的最少天数是 4。这意味着从星期一到星期四(含)之间开始的周是整周
This solution was intended to replicate the Java implementation of WeekOfMonth in Python and follow a pattern similar to the ISO 8601 convention.
First day of the week is Monday
Minimal number of days in the first week is 4. This implies weeks starting on and between Monday and Thursday inclusive are full weeks