pandas.tseries.offsets.WeekOfMonth Not getting desired output

32 views Asked by At

I used the pd.offsets.LastWeekOfMonth method to get the 2nd last Thursday from the input date. And also I am trying to get the Wednesday, Tuesday, Monday and Friday that are before the 2nd lst Thursday. For this I used the pd.offsets.WeekOfMonth method.

Give my input code, I am getting inconsistent output, or rather I am unable to understand it.

My code

x = Df["Date"][2512]
print(x)
print("------")
print(x + pd.offsets.LastWeekOfMonth(n=2, weekday=3))
print(x + pd.offsets.WeekOfMonth(n=2, week=3, weekday=2))
print(x + pd.offsets.WeekOfMonth(n=2, week=3, weekday=1))
print(x + pd.offsets.WeekOfMonth(n=2, week=3, weekday=0))
print(x + pd.offsets.WeekOfMonth(n=2, week=2, weekday=4))`

Output

2023-02-22 15:30:00
------
2023-03-30 15:30:00
2023-04-26 15:30:00
2023-03-28 15:30:00
2023-03-27 15:30:00
2023-04-21 15:30:00

The Date above the dotted line is the input date.

The pf.offsets.LastWeekOfMonth method correctly returns the 2nd last Thursday as expected.

pd.offsets.WeekOfMonth method is what is giving me trouble. The Wednesday (2nd line below dotted line) and the Friday (the last line) is what is not giving me the expected output. I would like to understand why. Could Python experts shed some light please.

0

There are 0 answers