2

I have to compare two date columns in a dataframe, I have tried np.where method

end_date = datetime.strptime(df3['end_date'], '%Y-%m-%d') start_date = datetime.strptime(df3['start_date'], '%Y-%m-%d') diff = end_date - start_date if diff > 0: raise Exception("employee's leave 'start_date' cannot be greater than leave 'end_date'") error: strptime() argument 1 must be str, not Series 
1
  • You are doing a few things wrong here. First off, you are working with a pandas DataFrame, you must use pd.to_datetime. and secondly, I'd normalize then subtract and lastly compare to pd.Timedelta. If you added sample dataframe to this question, with expected output. Someone will show you complete code. Commented Feb 17, 2022 at 16:30

2 Answers 2

2

There's a easiest way.

You don't have to convert them with datetime. You can do it directly with pandas.

There's a api available with pandas as to_datetime

To substract between two days, first convert the string to datetime.

df3['dt_end_date'] = pd.to_datetime(df3['end_date']) df3['dt_start_date'] = pd.to_datetime(df3['start_date']) df3['difference'] = df3['dt_end_date'] - df3['dt_start_date'] 

That's it you will have the answer !!!

Sign up to request clarification or add additional context in comments.

Comments

0

I think you're confusing how to use it dataframe here.

If you consider a normal value, you can use datetime module but you should use to_datetime() to convert a type of columne and dt attribute to count days.

For your goal, you can make another columne for diff to save timedelta, then you can check if there is negative value in the diff columns, as follows:

import pandas as pd df3 = pd.DataFrame({ 'start_date': ['2020-01-09', '2020-02-02'], 'end_date': ['2020-01-11', '2020-02-01'] }) df3['end_date'] = pd.to_datetime(df3['end_date'], format='%Y-%m-%d') df3['start_date'] = pd.to_datetime(df3['start_date'], format='%Y-%m-%d') df3['diff'] = df3['end_date'].dt.day - df3['start_date'].dt.day print(df3) # start_date end_date diff #0 2020-01-09 2020-01-11 2 #1 2020-02-02 2020-02-01 -1 if any(val < 0 for val in df3['diff'].values): # to check if negative value exists raise Exception("employee's leave 'start_date' cannot be greater than leave 'end_date'") 

You can raise Exception if there is any negative days, as follows:

Traceback (most recent call last): File "C:/Users/ABC/stackoverflow/main.py", line 16, in <module> raise Exception("employee's leave 'start_date' cannot be greater than leave 'end_date'") Exception: employee's leave 'start_date' cannot be greater than leave 'end_date' 

1 Comment

@Shovarnu Dutta hope it help u.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.