user11509999
user11509999

Reputation:

DataFrame : Compare dates from two different columns

Compare dates from different columns in a same day.

df

            a                           b      
    0   2020-07-17 00:00:01.999    2020-07-17 12:00:01.999
    1   2020-06-15 13:14:01.999    2020-02-14 12:00:01.999
    2   2020-09-05 16:14:01.999    2020-09-05 11:59:01.999
    3   2020-11-17 23:14:01.999    2020-11-17 05:30:01.999

Expected Output

            a                           b                       output
    0   2020-07-17 00:00:01.999    2020-07-17 12:00:01.999       True
    1   2020-06-15 13:14:01.999    2020-02-14 12:00:01.999       False
    2   2020-09-05 16:14:01.999    2020-09-05 11:59:01.999       True
    3   2020-11-17 23:14:01.999    2020-11-17 05:30:01.999       True

Should i convert dates to string(strf date) and compare them or any other way?

Upvotes: 1

Views: 74

Answers (2)

Animesh Mukherkjee
Animesh Mukherkjee

Reputation: 430

What you have is timestamp, and to get date out of it you should use .date() method, assuming the dataframe is df.

df['output'] = df.apply(lambda row: row['a'].date() == row['b'].date(), axis=1)

If columns 'a' and 'b' are strings use

df['output'] = df.apply(lambda row: pd.Timestamp(row['a']).date() == pd.Timestamp(row['b']).date(), axis=1)

Upvotes: 0

bigbounty
bigbounty

Reputation: 17358

Convert the datetime to datetime objects either by using pd.to_datetime or while reading from csv. Then use dt.date function to compare the dates

In [22]: df = pd.read_csv("a.csv", parse_dates=["a","b"])

In [23]: df
Out[23]:
                        a                       b
0 2020-07-17 00:00:01.999 2020-07-17 12:00:01.999
1 2020-06-15 13:14:01.999 2020-02-14 12:00:01.999
2 2020-09-05 16:14:01.999 2020-09-05 11:59:01.999
3 2020-11-17 23:14:01.999 2020-11-17 05:30:01.999

In [24]: df["c"] = df["a"].dt.date == df["b"].dt.date

In [25]: df
Out[25]:
                        a                       b      c
0 2020-07-17 00:00:01.999 2020-07-17 12:00:01.999   True
1 2020-06-15 13:14:01.999 2020-02-14 12:00:01.999  False
2 2020-09-05 16:14:01.999 2020-09-05 11:59:01.999   True
3 2020-11-17 23:14:01.999 2020-11-17 05:30:01.999   True

Upvotes: 2

Related Questions