python,如何知道一年中的周数,星期六是

2024-06-02 18:15:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python/pandas,想知道如何获得一年中一天的周数,而星期六是一周的第一天。 我确实找了很多,但一路上都把周一或周日作为一周的第一天。。。 请帮忙…谢谢

谢谢大家!非常感谢你的快速回答……但我不得不道歉,我没有把我的问题说清楚。

我想知道一年中的周数。例如,2015-08-09是第32周,周一是一周的第一天,而第33周是一周的第一天,周六是一周的第一天。

谢谢@Cyphase和所有人,我稍微修改了Cyphase的代码,它就工作了。

def week_number(start_week_on, date_=None):
    assert 1 <= start_week_on <= 7  #Monday=1, Sunday=7

    if not date_:
        date_ = date.today()

    __, normal_current_week, normal_current_day = date_.isocalendar()
    print date_, normal_current_week, normal_current_day

    if normal_current_day >= start_week_on:
        week = normal_current_week + 1
    else:
        week = normal_current_week

    return week

Tags: 代码numberpandasdateifondefcurrent