0

I have a column of timestamps that I would like to convert to datetime in my pandas dataframe. The format of the dates is %Y-%m-%d-%H-%M-%S which pd.to_datetime does not recognize. I have manually entered the format as below:

df['TIME'] = pd.to_datetime(df['TIME'], format = '%Y-%m-%d-%H-%M-%S')

My problem is some of the times do not have seconds so they are shorter
(format = %Y-%m-%d-%H-%M).

How can I get all of these strings to datetimes?

I was thinking I could add zero seconds (-0) to the end of my shorter dates but I don't know how to do that.

Blaztix
  • 1,223
  • 1
  • 19
  • 28
acrow
  • 21
  • 2

1 Answers1

0

try strftime and if you want the right format and if Pandas can't recognize your custom datetime format, you should provide it explicetly

from functools import partial

df1 = pd.DataFrame({'Date': ['2018-07-02-06-05-23','2018-07-02-06-05']})
newdatetime_fmt = partial(pd.to_datetime, format='%Y-%m-%d-%H-%M-%S')
df1['Clean_Date'] = (df1.Date.str.replace('-','').apply(lambda x: pd.to_datetime(x).strftime('%Y-%m-%d-%H-%M-%S'))
                                                 .apply(newdatetime_fmt))

print(df1,df1.dtypes)

output:

              Date          Clean_Date
0  2018-07-02-06-05-23 2018-07-02 06:05:23
1     2018-07-02-06-05 2018-07-02 06:05:00 
Date                  object
Clean_Date    datetime64[ns]
Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • The datetime is in one column with the hyphenated format (i.e. 2018-1-1-0-49-32) some don't have seconds (i.e 2018-1-1-0-49). I want them to be recognized as datetime objects so I can sort by day and time. – acrow Feb 27 '19 at 20:22