简体   繁体   中英

Change the date format for a column in Python

I would like to change the date format of a entire column in data frame.

My code looks like this:

fwds['fwdlookupterm'] = fwds['symbol'] + datetime.datetime.strptime(fwds['expiration_date'],'%y%m%d')

When i do so, i get an error:

TypeError: strptime() argument 1 must be string, not Series

How to resolve this error?

Current code:

fwds['fwdlookupterm'] = fwds['symbol'] + pd.to_datetime(str(fwds['expiration_date']),format= '%y%m%d')

Current error:

Name: expiration_date, Length: 1266, dtype: object' does not match format '%y%m%d'

I believe you need convert column to strings, then to datetimes and last to custom format by strftime :

s = pd.to_datetime(fwds['expiration_date'].astype(str)).dt.strftime('%y%m%d')
fwds['fwdlookupterm'] = fwds['symbol'] + s

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