如何使用熊猫将字符串转换为时间戳

时间:2020-04-26 14:17:26

标签: python pandas

我的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)
            )

该函数为列提供Nan 如果我将i.fromisoformat()更改为i.timestamp(),它可以工作,但会返回UNIX Posix,这不是我所需要的 我在这里做什么错了?

错误是:

AttributeError :(“ Timestamp”对象没有属性 'fromisoformat'“,'发生在索引originDate')

1 个答案:

答案 0 :(得分:0)

据我了解,您希望将特定的字符串对象转换为datetime对象。您可以通过以下方式进行转换:

from datetime import datetime
df[cols] = df[cols].apply(lambda x: datetime.strptime(x, '%Y-%m-%dT%H:%M:%S.%f%z'))