【问题标题】:How to parse this type of datetime data如何解析这种类型的日期时间数据
【发布时间】:2019-07-25 11:52:03
【问题描述】:

我正在尝试将这些字符串对象转换为 python 日期时间对象,但字母 T,Z 会产生问题。

datetime.strptime(i,'%Y-%m-%d').date() for i in df['dateUpdated']

0    2014-02-01T04:41:06Z
1    2013-12-11T05:28:28Z
2    2015-11-19T22:20:29Z
3    2014-02-01T05:22:01Z
4    2016-05-19T14:31:26Z

ValueError: unconverted data remains: T04:41:06Z

【问题讨论】:

    标签: python-3.x datetime data-science


    【解决方案1】:

    假设您正在使用 Pandas,请使用 pd.to_datetime

    import pandas as pd
    
    df = pd.DataFrame({'dateUpdated': ['2014-02-01T04:41:06Z', '2013-12-11T05:28:28Z']})
    df['dateUpdated'] = pd.to_datetime(df['dateUpdated'])
    

    结果:

    0   2014-02-01 04:41:06+00:00
    1   2013-12-11 05:28:28+00:00
    Name: dateUpdated, dtype: datetime64[ns, UTC]
    

    如果您只想访问新列的日期部分,可以使用:

    df['dateUpdated'].dt.date
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 2011-04-30
      • 2018-03-07
      相关资源
      最近更新 更多