Issue
df.head(3)
Out[20]:
First_date Second_date
0 2021-09-18 2021-09-17
1 2021-09-22 2021-09-17
2 2021-09-27 2021-09-17
I have calculated the difference between the two date_columns but I want to exclude only Sundays from the dates, right now what I have calculated his for a whole week.
df["First_date"] = df['First_date'].astype('datetime64[ns]')
df["Second_date"] = df['Second_date'].astype('datetime64[ns]')
df["days"] = df["First_date"].sub(df ["Second_date"],axis=0)
df.head(3)
Out[20]:
First_date Second_date days
0 2021-09-18 2021-09-17 1
1 2021-09-22 2021-09-17 5
2 2021-09-27 2021-09-17 10
df.head(3)
Out[20]:
First_date Second_date days expected_outcome
0 2021-09-18 2021-09-17 1 1
1 2021-09-22 2021-09-17 5 4
2 2021-09-27 2021-09-17 10 8
this is how I used to calculate the number of days till now. Thanks for your help :)
Solution
Try:
df["First_date"] = pd.to_datetime(df["First_date"])
df["Second_date"] = pd.to_datetime(df["Second_date"])
sundays = df.apply(lambda x: len(pd.date_range(x["Second_date"], x["First_date"], freq="W-SUN")), axis=1)
df["days"] = (df["First_date"]-df["Second_date"]).dt.days-sundays
>>> df
First_date Second_date days
0 2021-09-18 2021-09-17 1
1 2021-09-22 2021-09-17 4
2 2021-09-27 2021-09-17 8
Answered By - not_speshal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.