pandas python,如何知道一年中的周数,星期六作为一周的第一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31984119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:46:10  来源:igfitidea点击:

python, how to know the week number in the year of the day, Saturday as the first day of the week

pythonpandas

提问by fuji4900

I am using python/pandas, and want to know how to get the week number in the year of one day while Saturday as the first day of the week. i did search a lot, but all the way takes either Monday or Sunday as the first day of week... Please help...thanks

我正在使用 python/pandas,并想知道如何在星期六作为一周的第一天时获取某一年的周数。我确实搜索了很多,但一直将周一或周日作为一周的第一天...请帮忙...谢谢

Thanks all! really appreciated all your quick answers..but i have to apology that i am not making my question clearly.

谢谢大家!真的很感谢你所有的快速回答......但我必须道歉,我没有清楚地提出我的问题。

I want to know the week number in the year. for example, 2015-08-09 is week 32 while Monday as first day of week, but week 33 while Saturday as first day of week.

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

Thanks @Cyphase and everyone, I changed a bit the code of Cyphase and it works.

感谢@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

回答by EdChum

If I understand correctly the following does what you want:

如果我理解正确,以下内容可以满足您的需求:

In [101]:
import datetime as dt
import pandas as pd
?
df = pd.DataFrame({'date':pd.date_range(start=dt.datetime(2015,8,9), end=dt.datetime(2015,9,1))})
df['week'] = df['date'].dt.week.shift(-2).ffill()
df['orig week'] = df['date'].dt.week
df['day of week'] = df['date'].dt.dayofweek
df

Out[101]:
         date  week  orig week  day of week
0  2015-08-09    33         32            6
1  2015-08-10    33         33            0
2  2015-08-11    33         33            1
3  2015-08-12    33         33            2
4  2015-08-13    33         33            3
5  2015-08-14    33         33            4
6  2015-08-15    34         33            5
7  2015-08-16    34         33            6
8  2015-08-17    34         34            0
9  2015-08-18    34         34            1
10 2015-08-19    34         34            2
11 2015-08-20    34         34            3
12 2015-08-21    34         34            4
13 2015-08-22    35         34            5
14 2015-08-23    35         34            6
15 2015-08-24    35         35            0
16 2015-08-25    35         35            1
17 2015-08-26    35         35            2
18 2015-08-27    35         35            3
19 2015-08-28    35         35            4
20 2015-08-29    36         35            5
21 2015-08-30    36         35            6
22 2015-08-31    36         36            0
23 2015-09-01    36         36            1

The above uses dt.weekand shifts by 2 rows and then forward fills the NaNvalues.

上面使用dt.weekand shifts by 2 行,然后向前填充NaN值。

回答by KiDo

import datetime
datetime.date(2015, 8, 9).isocalendar()[1]

回答by Cyphase

You could just do this:

你可以这样做:

from datetime import date


def week_number(start_week_on, date_=None):
    assert 0 <= start_week_on <= 6

    if not date_:
        date_ = date.today()

    __, normal_current_week, normal_current_day = date_.isocalendar()

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

    return week


print("Week starts     We're in")
for start_week_on in range(7):
    this_week = week_number(start_week_on)
    print("   day {0}         week {1}".format(start_week_on, this_week))

Output on day 4 (Thursday):

第 4 天(星期四)的输出:

Week starts     We're in
   day 0         week 33
   day 1         week 33
   day 2         week 33
   day 3         week 33
   day 4         week 33
   day 5         week 32
   day 6         week 32