最新消息:

pandas中的to_datetime函数

IT技术 ipcpu 1200浏览 0评论

一、概述

在日常数据处理中,最为恼火的就是时间的处理。时间可以有很多种表达形式,比如下面这张图,我们看到同样是 2017 年 1 月 5 日,可以有很多种时间格式,我们需要先将格式统一才能进行后面的工作,Pandas 提供了这个函数 to_datetime ().

二、简单用法

dates1 = ['2017-01-05', 'Jan 5, 2017', '01/05/2017', '2017.01.05', '2017/01/05', '20170105']
print(pd.to_datetime(dates1))

dates2 = ['2017-01-05 2:30:00 PM', 'Jan 5, 2017 14:30:00', '01/05/2016', '2017.01.05', '2017/01/05', '20170105']
print(pd.to_datetime(dates2))

运行结果如下,

DatetimeIndex(['2017-01-05', '2017-01-05', '2017-01-05', '2017-01-05',
               '2017-01-05', '2017-01-05'],
              dtype='datetime64[ns]', freq=None)
DatetimeIndex(['2017-01-05 14:30:00', '2017-01-05 14:30:00',
               '2016-01-05 00:00:00', '2017-01-05 00:00:00',
               '2017-01-05 00:00:00', '2017-01-05 00:00:00'],
              dtype='datetime64[ns]', freq=None)

三、错误处理

如果我们处理的数据中包含了一些无法识别的时间格式或者是字符串,就会报错,

#ignore 忽略,原样输出; raise 直接报错,默认行为; coerce如果出错,设置成NaT
errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’

我们来看个例子

#代码
dates3 = ['2017-01-05 2:30:00 PM', 'Jan 5, 2017 14:30:00', 'ABCDEFG', '2017.01.05', '2017/01/05', '20170105']
print(pd.to_datetime(dates3, errors='coerce'))
#输出
DatetimeIndex(['2017-01-05 14:30:00', '2017-01-05 14:30:00',
                               'NaT', '2017-01-05 00:00:00',
               '2017-01-05 00:00:00', '2017-01-05 00:00:00'],
              dtype='datetime64[ns]', freq=None)

四、时间戳转换为时间

时间戳也是很常用的时间表现形式,他需要指定专门的参数unit来处理

timestamp1 = [1551966534, 1551968279, 1551969279]
print(pd.to_datetime(timestamp1, unit='s' ))
timestamp2 = [1564733056000, 1564819456000, 1564905856000]
print(pd.to_datetime(timestamp2, unit='ms'))

运行结果如下

DatetimeIndex(['2019-03-07 13:48:54', '2019-03-07 14:17:59',
               '2019-03-07 14:34:39'],
              dtype='datetime64[ns]', freq=None)
DatetimeIndex(['2019-08-02 08:04:16', '2019-08-03 08:04:16',
               '2019-08-04 08:04:16'],
              dtype='datetime64[ns]', freq=None)

五、特殊格式的处理

在与各种云计算厂商打交道的时候,会发现他们的时间格式也不一样。
比如阿里云使用2019-08-02T21:14:00Z,而金山云使用2019-08-01T21:14+0800 ,我们来看下能不能转换

dates = ['2019-08-01T21:14+0800', '2019-07-01 00:00', '2019-08-02T21:14:00Z']
print(pd.to_datetime(dates, errors='coerce'))

运行结果

Index([2019-08-01 21:14:00+08:00, 2019-07-01 00:00:00,
       2019-08-02 21:14:00+00:00],
      dtype='object')

很遗憾,得到了一个Index结果,而不是DatetimeIndex。
这种情况我们可以指定format参数来实现转换,如下,

ds1 = ['2019-08-01T21:14+0800', '2019-08-02T21:14+0800', '2019-08-03T21:14+0800']
print(pd.to_datetime(ds1, errors='coerce'))

ds1 = ['2019-08-01T21:14+0800', '2019-08-02T21:14+0800', '2019-08-03T21:14+0800']
print(pd.to_datetime(ds1, errors='coerce', format='%Y-%m-%dT%H:%M+0800'))

ds2 = ['2019-08-01T21:14:00Z', '2019-08-02T21:14:00Z', '2019-08-03T21:14:00Z']
print(pd.to_datetime(ds2, errors='coerce', format='%Y-%m-%dT%H:%M:%SZ'))

运行结果

DatetimeIndex(['2019-08-01 21:14:00+08:00', '2019-08-02 21:14:00+08:00',
               '2019-08-03 21:14:00+08:00'],
              dtype='datetime64[ns, pytz.FixedOffset(480)]', freq=None)
DatetimeIndex(['2019-08-01 21:14:00', '2019-08-02 21:14:00',
               '2019-08-03 21:14:00'],
              dtype='datetime64[ns]', freq=None)
DatetimeIndex(['2019-08-01 21:14:00', '2019-08-02 21:14:00',
               '2019-08-03 21:14:00'],
              dtype='datetime64[ns]', freq=None)

六、官方文档链接

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

转载请注明:IPCPU-网络之路 » pandas中的to_datetime函数

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 常见用法是,df[0] = pd.to_datetime(df[0], unit='s')
    wss88482年前 (2021-08-12)Reply