Python 类型错误:strptime() 参数 1 必须是字符串,而不是系列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46737330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:48:08  来源:igfitidea点击:

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

pythondatetime

提问by Ashish Choudhary

I am writing a single entry from a column of a data frame

我正在从数据框的列中写入单个条目

2011100101 is interpreted as 1 AM of 1st October 2011.

2011100101 被解释为 2011 年 10 月 1 日凌晨 1 点。

I want it to change in the format as YYYY-Mmm-dd HH

我希望它的格式更改为 YYYY-Mmm-dd HH

train['date1']=datetime.strptime(train['ID'], '%Y%m%d%H')

but getting an error TypeError: strptime() argument 1 must be string, not Series

但得到一个错误 TypeError: strptime() 参数 1 必须是字符串,而不是系列

How to change in the required format for the entire entries in a single column?

如何更改单个列中整个条目所需的格式?

回答by bouletta

you can use the apply()method

你可以使用apply()方法

train['date1'] = train['ID'].apply(lambda x: datetime.strptime(x, '%Y%m%d%H'))