kjmerf
kjmerf

Reputation: 4345

merge strips timestamp

I am merging two dataframes:

df = pd.merge(source, extracted, left_on = 'SESSION_SCHED', right_on = 'COMPL_DATE')

Note that SESSION_SCHED is a date but does not have a timestamp. COMPL_DATE has a timestamp before the merge but not after.

Example:

>>> print(extracted['COMPL_DATE'])
0      2015-11-25 10:25:00
5      2016-09-30 13:31:00
10     2013-09-13 00:00:00
15     2014-12-18 17:00:00

>>> print(df['COMPL_DATE'])
0   2015-13-22
1   2013-01-30
2   2013-09-13
3   2011-10-18
4   2013-09-13

How can I merge the dataframes while keeping the timestamp in the COMPL_DATE column?

Upvotes: 2

Views: 66

Answers (2)

kjmerf
kjmerf

Reputation: 4345

Upon review, the real reason the timestamp did not display after the merge is that the only rows that were selected in the merge had 00:00:00 for a timestamp. Hence, this time is assumed and not explicitly displayed.

Upvotes: 0

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210852

extracted.assign(x=extracted['COMPL_DATE'].dt.normalize() - will add a new column x with truncated time (i.e. 00:00:00) - we can use this column for joining:

df = pd.merge(source, extracted.assign(x=extracted['COMPL_DATE'].dt.normalize()), 
              left_on = 'SESSION_SCHED', right_on = 'x')

Upvotes: 1

Related Questions