繁体   English   中英

更改Python中列的日期格式

[英]Change the date format for a column in Python

我想更改数据框中整列的日期格式。

我的代码如下所示:

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

当我这样做时,我得到一个错误:

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

如何解决这个错误?

当前代码:

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

当前错误:

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

我相信您需要将列转换为字符串,然后转换为日期时间,最后通过strftime转换为自定义格式:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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