简体   繁体   中英

Converting a Datetime column back to an Object

I have a column of dates that I converted to datetime64[ns] using

 dftest['date'] = pd.to_datetime(dftest['date'])

Now I want to convert the datetime back to an object, so I can concatenate it as a string. What is the best way to do this?

You could call apply and use datetime.strptime :

In [37]:

df = pd.DataFrame(['05/06/2015 00:00', '22/06/2015 00:00'], columns=['date'])
df['date']= pd.to_datetime(df['date'])
df
Out[37]:
        date
0 2015-05-06
1 2015-06-22

In [38]:

df['date'] = df['date'].apply(lambda x: dt.datetime.strftime(x,'%Y-%m-%d'))
df
Out[38]:
         date
0  2015-05-06
1  2015-06-22
In [39]:

df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 2 entries, 0 to 1
Data columns (total 1 columns):
date    2 non-null object
dtypes: object(1)
memory usage: 32.0+ bytes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM