0

I have a field in my dataframe df, labeled date, but after testing it a lot, I figured out that it is a string and not a date format.

Transaction_id | Date
ABC              4/1/2016 9:13:58 PM
CDE              10/3/2015 10:12:25 AM
EFG              12/12/2017 3:02:45 PM

I need to split the Date column up into Date & time and I want them to be in datetime format. I don't know how to do this with regex, since the lengths are different.

Output:

Transaction_id | Date                    | Date2       | Time            
ABC              4/1/2016 9:13:58 PM       04/01/2016    21:13:58
CDE              10/3/2015 10:12:25 AM     10/03/2016    10:12:25
EFG              12/12/2017 3:02:45 PM     12/12/2017    15:02:45
2
  • 1
    FYI, your dates are of not consistent format. First row is 12-hr format, others are 24-hr. Commented Sep 19, 2017 at 20:00
  • 1
    sorry - typo; should have been 3 instead of 15; fixed, thank you Commented Sep 19, 2017 at 20:03

1 Answer 1

4

Note that I placed a errors='coerce' to handle non-sensical date data.

date2 = pd.to_datetime(df.Date, errors='coerce')

df.assign(Date2=date2.dt.date, Time=date2.dt.time)

  Transaction_id                   Date       Date2      Time
0            ABC    4/1/2016 9:13:58 PM  2016-04-01  21:13:58
1            CDE  10/3/2015 10:12:25 AM  2015-10-03  10:12:25
2            EFG  12/12/2017 3:02:45 PM  2017-12-12  15:02:45
Sign up to request clarification or add additional context in comments.

6 Comments

Date parsing issue, with 9:13:58 PM should be 17:13:58?
Thanks... will fix
Data is inconsistent with 12hr and 24hr timestamps.
Great! Thank you so much!
@jeangelj not yet. This is broken. the %p directive isn't being respected. I'm looking for a fix.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.