出现AttributeError:“ str”对象没有属性“ strftime”,可能是什么问题?

时间:2020-06-28 18:51:23

标签: python pandas

我正在尝试将日期转换为字符串并得到以下错误。

    Traceback (most recent call last):
  File "C:/BAB/POC/Slam.py", line 113, in <module>
    sheet0[col] = format_datecol(sheet0, col)
  File "C:/BAB/POC/Slam.py", line 85, in format_datecol
    newSheet[datecolumnname] = newSheet[datecolumnname].apply(lambda x: x.strftime('%d%b%Y'))
  File "C:\2020\python\lib\site-packages\pandas\core\series.py", line 3848, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas\_libs\lib.pyx", line 2327, in pandas._libs.lib.map_infer
  File "C:/BAB/POC/Slam.py", line 85, in <lambda>
    newSheet[datecolumnname] = newSheet[datecolumnname].apply(lambda x: x.strftime('%d%b%Y'))
AttributeError: 'str' object has no attribute 'strftime

'

代码在这里...

for col in keycols0:
    if '_DT'.casefold() or 'DATE'.casefold() in col:
        sheet0[col] = format_datecol(sheet0, col)



def format_datecol(newSheet,datecolumnname):
    newSheet[datecolumnname] = newSheet[datecolumnname].apply(lambda x: x.strftime('%d%b%Y'))
    # newSheet[datecolumnname] = newSheet[datecolumnname].apply(lambda x: datetime.strptime(x,'%d%b%Y'))
    return newSheet[datecolumnname]

当条件为或运算符高于错误..对于以下代码,我没有得到错误,当我执行多个值检查时出现了问题。

for col in keyColumnNames:
        if '_DT'.casefold() in col:
            tempSheet[col] = format_datecol(tempSheet, col)

def format_datecol(newSheet,datecolumnname):
    newSheet[datecolumnname] = newSheet[datecolumnname].apply(lambda x: x.strftime('%d%m%Y'))
    return newSheet[datecolumnname]

会有什么问题?

0 个答案:

没有答案