Python dataframe converts multiple datetime formats

I have a pandas.dataframe like this (the "col" column has two formats):

    col                            val
'12/1/2013'                       value1
'1/22/2014 12:00:01 AM'           value2
'12/10/2013'                      value3
'12/31/2013'                      value4 

      

I want to convert them to datetime and I am considering using:

test_df['col']= test_df['col'].map(lambda x: datetime.strptime(x, '%m/%d/%Y'))    
test_df['col']= test_df['col'].map(lambda x: datetime.strptime(x, '%m/%d/%Y %H:%M %p'))

      

Obviously any of them works for all df. I am thinking about using try and except but got no luck, no suggestions?

+3


source to share


2 answers


Just use to_datetime

, this is male / female enough to handle both of these formats:

In [4]:
df['col'] = pd.to_datetime(df['col'])
df.info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 4 entries, 0 to 3
Data columns (total 2 columns):
col    4 non-null datetime64[ns]
val    4 non-null object
dtypes: datetime64[ns](1), object(1)
memory usage: 96.0+ bytes

      



The df now looks like this:

In [5]:
df

Out[5]:
                  col     val
0 2013-12-01 00:00:00  value1
1 2014-01-22 00:00:01  value2
2 2013-12-10 00:00:00  value3
3 2013-12-31 00:00:00  value4

      

+5


source


You can create a new column:

test_df['col1'] = pd.Timestamp(test_df['col']).to_datetime()

      



and then drop col and rename col1.

+1


source







All Articles