lakshmen
lakshmen

Reputation: 29064

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'

Upvotes: 0

Views: 924

Answers (1)

jezrael
jezrael

Reputation: 862621

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

Upvotes: 1

Related Questions