我有包含以下格式的df列:2020-04-20T03:18:07.000+0000,我想将它转换成如下格式:2020-04-20 03:18:07.000000
我就是这样做的:
def _to_timestamp(cls, col):
"""
Convert a column of a dataframe from String to Timestamp if applicable
:param col: A Series object representing a column of a dataframe.
"""
try:
col = pd.to_datetime(col)
except ValueError:
print(
"Could not convert field to timestamps: {0}".format(col.name)
)
return col
converted = []
for i in col:
try:
converted.append(i.fromisoformat()) <-- PROBLEM IS HERE
except ValueError:
converted.append(pd.np.NaN)
except AttributeError:
converted.append(pd.np.NaN)
cols =['originDate', 'destenationDate','caseTimestamp']
df[cols] = df[cols].apply(
lambda x: self._to_timestamp(x)
)如果我将i.fromisoformat()改为i.timestamp(),这个函数给了我Nan,但是它返回UNIX,这不是我所需要的,我在这里做错了什么?
错误是:
AttributeError:(“‘时间戳’对象没有属性'fromisoformat'",‘发生在索引originDate')
发布于 2020-04-26 15:21:23
据我所知,您希望将特定的字符串对象转换为datetime对象。您可以以这种方式转换它:
from datetime import datetime
df[cols] = df[cols].apply(lambda x: datetime.strptime(x, '%Y-%m-%dT%H:%M:%S.%f%z'))https://stackoverflow.com/questions/61442283
复制相似问题