【问题标题】:Python: How to parse dates in mixed format?Python:如何以混合格式解析日期?
【发布时间】:2020-07-22 11:40:19
【问题描述】:

我必须解析一个混合格式的日期列:

0         1972-12-31
1         1980-03-31
2         1980-03-31
3         1973-08-31
4         1985-06-28
            ...     
44215    2017 Nov 17
44216     2009-02-13
44217     2018 Jul 3
44218     2011-03-15
44219     2017 Nov 8
Name: publish_time, Length: 44220, dtype: object

我尝试用 pandas 解析它:

pd.datetime.strptime(metadata['publish_time'], '%Y-%m-%d')

但它给了我这个错误:

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: FutureWarning: The pandas.datetime class is deprecated and will be removed from pandas in a future version. Import from datetime instead.
  """Entry point for launching an IPython kernel.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-83-fa9f0e16e2d9> in <module>()
----> 1 pd.datetime.strptime(metadata['publish_time'], '%Y-%m-%d')

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

知道如何解决这个问题吗?

【问题讨论】:

    标签: python pandas dataframe date parsing


    【解决方案1】:

    pd.to_datetime 在识别不同的日期格式方面非常聪明。

    这样的事情会起作用:

    In [153]: df = pd.DataFrame({'date': ['1973-08-31','2017 Nov 17', '2009-02-13','2018 Jul 3']})                                                                                                              
    
    In [154]: df                                                                                                                                                                                                
    Out[154]: 
              date
    0   1973-08-31
    1  2017 Nov 17
    2   2009-02-13
    3   2018 Jul 3
    
    In [155]:  df['date'] = pd.to_datetime(df['date'])                                                                                                                                                          
    
    In [156]: df                                                                                                                                                                                                
    Out[156]: 
            date
    0 1973-08-31
    1 2017-11-17
    2 2009-02-13
    3 2018-07-03
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-19
      • 2017-04-11
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多