简体   繁体   English

将Dataframe对象转换为Date Python

[英]Converting Dataframe Object into Date Python

A python dataframe 'dff' containing data imported from SAS is having a date field whose type is showing as below by python: 包含从SAS导入的数据的python数据帧'dff'具有一个date字段,其类型由python显示如下:

dff.effvdate.head()
Out[47]: 
0    b'09JUL2013'
1    b'17OCT2013'
2    b'03MAR2014'
3    b'08MAY2014'
4    b'10MAR2015'
Name: effvdate, dtype: object

I am trying to convert this date to datetype as below: 我正在尝试将此日期转换为datetype,如下所示:

dff['REPORT_MONTH'] =[dt.datetime.strptime(d,"%d%b%Y").date() for d in dff['effvdate']]

showing this error 显示此错误

TypeError: strptime() argument 1 must be str, not bytes

As i am new to python, need help on this. 由于我是python的新手,因此需要帮助。

The error TypeError: strptime() argument 1 must be str, not bytes gives you the information you need to know! 错误TypeError: strptime() argument 1 must be str, not bytes它为您提供了您需要了解的信息! d is in bytes, not a string. d以字节为单位,而不是字符串。 you should cast d to string. 您应该将d转换为字符串。

dff['REPORT_MONTH'] =[dt.datetime.strptime(d.decode('UTF-8'),"%d%b%Y").date() for d in dff['effvdate']]

should work. 应该管用。

see What does the 'b' character do in front of a string literal? 请参见字符串文字前的'b'字符做什么?

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

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