Python: How to find the nth weekday of the year?

情到浓时终转凉″ 提交于 2021-02-08 19:36:35

问题


I have seen a lot of similar posts on "nth weekday of the month", but my question pertains to "nth weekday of the year".

Background: I have a table that has daily sales data. There are 3 columns: date, day of week (Mon, Tue, Wed etc.) and sales. I would like to match nth weekday of Year 1 with Year 2 and compare sales that way.

  • Example1: 01/06/2020 matches with 01/04/2021, both are the 1st Monday of that year.
  • Example2: 11/02/2019 matches with 10/31/2020, both are the 44th Saturday of that year.

As you can see, I can't simply do a "nth weekday of the MONTH" because sometimes the matched nth weekday would fall in different months (as seen in 11/02/2019 & 10/31/2020).

I am manipulating the table in pandas. I am wondering if there's a quick way for me to create a column that helps me to calculate the "nth weekday of the year" for me, so that I could later match based on that value?

Thanks for your help.


回答1:


The pandas package has some good time/date functions.

For example

import pandas as pd
s = pd.date_range('2020-01-01', '2020-12-31', freq='D').to_series()
print(s.dt.dayofweek)

gives you the weekdays as integers.

2020-01-01    2
2020-01-02    3
2020-01-03    4
2020-01-04    5
2020-01-05    6
2020-01-06    0
2020-01-07    1
2020-01-08    2
2020-01-09    3
2020-01-10    4

(Monday=0)

Then you can do

mondays = s.dt.dayofweek.eq(0) 

If you want to find the first Monday of the year use.

print(mondays.idxmax())                                                    
Timestamp('2020-01-06 00:00:00', freq='D')

Or the 5th Monday:

n = 4                                                                           
print(s[mondays].iloc[n])                                                                
Timestamp('2020-02-03 00:00:00')

If your sales dataframe is df then to compare sales on the first 5 Mondays of two different years you could do something like this:

mondays = df['Date'].dt.dayofweek.eq(0)
mondays_in_y1 = (df['Year'] == 2019) & mondays
mondays_in_y2 = (df['Year'] == 2020) & mondays 

pd.DataFrame({
    2019: df.loc[mondays_in_y1, 'Sales'].values[:5],
    2020: df.loc[mondays_in_y2, 'Sales'].values[:5]
})



回答2:


IIUC you can play from

import pandas as pd
import numpy as np

df = pd.DataFrame({"date":pd.date_range(start="2020-01-01",
                                        end="2020-12-31")})
# weekday number Monday is 0
df["dow"] = df["date"].dt.weekday

# is weekday as int
df["is_weekday"] =  (df["dow"]<5).astype(int)

df["n"] = df["is_weekday"].cumsum()

# remove weekends
df["n"] = np.where(df["n"]==df["n"].shift(), np.nan, df["n"])

df[df["n"]==100]["date"]

Edit In two lines only

df["n"] = (df["date"].dt.weekday<5).astype(int).cumsum()
df["n"] = np.where(df["n"]==df["n"].shift(), np.nan, df["n"])



回答3:


You can try using dt.week. It returns a series, but you can simply define a new column with these values.

For example:

import pandas as pd
rng = pd.date_range('2015-02-24', periods=5, freq='D')
df = pd.DataFrame({ 'Date': rng, 'Val' : np.random.randn(len(rng))})

Output:

       Date        Val

0   2015-02-24  -0.977278
1   2015-02-25  0.950088
2   2015-02-26  -0.151357
3   2015-02-27  -0.103219
4   2015-02-28  0.410599

The you should input df['Week_Number'] = df['Date'].dt.week, so you will make a new column with the week number:

       Date        Val     Week_Number

0   2015-02-24  -0.977278   9
1   2015-02-25   0.950088   9
2   2015-02-26  -0.151357   9
3   2015-02-27  -0.103219   9
4   2015-02-28   0.410599   9

Hope it helps. It's my first contribution.



来源:https://stackoverflow.com/questions/61106105/python-how-to-find-the-nth-weekday-of-the-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!