Home > Enterprise >  Calculate difference between two dates stored in columns excluding only Sunday in python?
Calculate difference between two dates stored in columns excluding only Sunday in python?

Time:09-22

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 :)

CodePudding user response:

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

CodePudding user response:

Try:

df["First_date"] = pd.to_datetime(df["First_date"])
df["Second_date"] = pd.to_datetime(df["Second_date"])
df['diff'] = df.apply(lambda x: len(pd.bdate_range(x['Second_date'], x['First_date'], freq="C", weekmask="Mon Tue Wed Thu Fri Sat", closed='right')), axis=1)
print(df)

Output:

  First_date Second_date  diff
0 2021-09-18  2021-09-17     1
1 2021-09-22  2021-09-17     4
2 2021-09-27  2021-09-17     8
  • Related