I have a below sample Data Frame and would like to subset the dataframe which has the last business day of particular week to separate data frame. I have tried many ways but not able to do for weekday.
df =
Date Open High Low Close Adj Close Volume
0 2007-06-01 0.33979 0.33979 0.33979 0.33979 0.33979 1591888
1 2007-06-02 0.33074 0.33074 0.33074 0.33074 0.33074 88440
2 2007-06-04 0.33526 0.33526 0.33526 0.33526 0.33526 3538
3 2007-06-05 0.32113 0.32113 0.32113 0.32113 0.32113 3550
4 2007-06-06 0.34713 0.34713 0.34713 0.34713 0.34713 670
5 2007-06-07 0.34713 0.34713 0.34713 0.34713 0.34713 670
OUTPUT
Date Open High Low Close Adj Close Volume
1 2007-06-02 0.33074 0.33074 0.33074 0.33074 0.33074 88440
2 2007-06-07 0.34713 0.34713 0.34713 0.34713 0.34713 670
Code I have tried:
df_output = df.loc[df.Date.isin(df.Date + pd.offsets.BWeekEnd(1))]
Could this help you?
import pandas as pd
import numpy as np
df = pd.DataFrame({"Date": ["2007-06-01",
"2007-06-02",
"2007-06-04",
"2007-06-05",
"2007-06-06",
"2007-06-07"],
"High": [0.33979,
0.33074,
0.33526,
0.32113,
0.34713,
0.34713]})
df["week"] = df["Date"].astype("M8[us]").dt.week
last_day = df.groupby("week")["Date"].max().values
del df["week"]
df[df["Date"].isin(last_day)]