pandas 一个月中的一周熊猫

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25249033/
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 22:21:38  来源:igfitidea点击:

Week of a month pandas

pythonpandas

提问by user3816493

I'm trying to get week on a month, some months might have four weeks some might have five. For each date i would like to know to which week does it belongs to. I'm mostly interested in the last week of the month.

我试图在一个月内获得一周,有些月份可能有四个星期,有些可能有五个。对于每个日期,我想知道它属于哪一周。我最感兴趣的是这个月的最后一周。

data = pd.DataFrame(pd.date_range(' 1/ 1/ 2000', periods = 100, freq ='D'))

0  2000-01-01
1  2000-01-02
2  2000-01-03
3  2000-01-04
4  2000-01-05
5  2000-01-06
6  2000-01-07

回答by chrisb

See this answerand decide which week of month you want.

请参阅此答案并决定您想要的月份的哪一周。

There's nothing built-in, so you'll need to calculate it with apply. For example, for an easy 'how many 7 day periods have passed' measure.

没有内置的东西,所以你需要用apply来计算它。例如,对于一个简单的“已经过去了多少 7 天”的度量。

data['wom'] = data[0].apply(lambda d: (d.day-1) // 7 + 1)

For a more complicated (based on the calender), using the function from that answer.

对于更复杂的(基于日历),使用该答案中的函数。

import datetime
import calendar

def week_of_month(tgtdate):
    tgtdate = tgtdate.to_datetime()

    days_this_month = calendar.mdays[tgtdate.month]
    for i in range(1, days_this_month):
        d = datetime.datetime(tgtdate.year, tgtdate.month, i)
        if d.day - d.weekday() > 0:
            startdate = d
            break
    # now we canuse the modulo 7 appraoch
    return (tgtdate - startdate).days //7 + 1

data['calendar_wom'] = data[0].apply(week_of_month)

回答by vcvd

I've used the code below when dealing with dataframes that have a datetime index.

在处理具有日期时间索引的数据帧时,我使用了下面的代码。

import pandas as pd
import math

def add_week_of_month(df):
    df['week_in_month'] = pd.to_numeric(df.index.day/7)
    df['week_in_month'] = df['week_in_month'].apply(lambda x: math.ceil(x))
    return df

If you run this example:

如果你运行这个例子:

df = test = pd.DataFrame({'count':['a','b','c','d','e']},
                     index = ['2018-01-01', '2018-01-08','2018-01-31','2018-02-01','2018-02-28'])
df.index = pd.to_datetime(df.index)

you should get the following dataframe

你应该得到以下数据框

               count  week_in_month

2018-01-01     a              1
2018-01-08     b              2
2018-01-31     c              5
2018-02-01     d              1
2018-02-28     e              4

回答by citynorman

This seems to do the trick for me

这似乎对我有用

df_dates = pd.DataFrame({'date':pd.bdate_range(df['date'].min(),df['date'].max())})
df_dates_tues = df_dates[df_dates['date'].dt.weekday==2].copy()
df_dates_tues['week']=np.mod(df_dates_tues['date'].dt.strftime('%W').astype(int),4)

回答by pomber

You can get it subtracting the current week and the week of the first day of the month, but extra logic is needed to handle first and last week of the year:

你可以得到它减去当前周和当月第一天的那一周,但需要额外的逻辑来处理一年的第一周和最后一周:

def get_week(s):
    prev_week = (s - pd.to_timedelta(7, unit='d')).dt.week
    return (
        s.dt.week
        .where((s.dt.month != 1) | (s.dt.week < 50), 0)
        .where((s.dt.month != 12) | (s.dt.week > 1), prev_week + 1)
    )

def get_week_of_month(s):
    first_day_of_month = s - pd.to_timedelta(s.dt.day - 1, unit='d')
    first_week_of_month = get_week(first_day_of_month)
    current_week = get_week(s)
    return  current_week - first_week_of_month