将看起来像日期的字符串转换为日期

时间:2018-05-19 20:26:30

标签: python pandas date datetime

我一直在阅读很多将字符串转换为日期时间的问题,但我没有找到类似于此数据框中显示的字符串日期。

    idAviso                     timestamp idpostulante
 1111413600  2018-02-28T20:40:28.079-0500      0z5VvGv
 1112368499  2018-02-28T20:51:02.844-0500      0z5VvGv
 1112369554  2018-02-28T20:43:50.396-0500      0z5VvGv
 1112358250  2018-02-27T16:02:19.303-0500      0zB026d
 1112358250  2018-02-27T16:02:30.036-0500      0zB026d

我的目标是将列timestamp转换为类似的内容,然后我可以将其用于某些分析

    idAviso   timestamp idpostulante
 1111413600  2018-02-28      0z5VvGv
 1112368499  2018-02-28      0z5VvGv
 1112369554  2018-02-28      0z5VvGv
 1112358250  2018-02-27      0zB026d
 1112358250  2018-02-27      0zB026d

timestamp现在应该是日期时间变量

2 个答案:

答案 0 :(得分:1)

只需使用.str[:10]删除字符串,然后像这样使用pd.to_datetime()

df['timestamp'] = pd.to_datetime(df['timestamp'].str[:10])

其他选择:

df['timestamp'] = df['timestamp'].apply(pd.Timestamp)
df['timestamp'] = pd.to_datetime(df['timestamp'])  # offset by 5 hours

完整示例:

import pandas as pd
import numpy as np

data = '''\
idAviso     timestamp                         idpostulante
1111413600  2018-02-28T20:40:28.079-0500      0z5VvGv
1112368499  2018-02-28T20:51:02.844-0500      0z5VvGv
1112369554  2018-02-28T20:43:50.396-0500      0z5VvGv
1112358250  2018-02-27T16:02:19.303-0500      0zB026d
1112358250  2018-02-27T16:02:30.036-0500      0zB026d'''

file = pd.compat.StringIO(data)
df = pd.read_csv(file, sep='\s+')

df['timestamp'] = pd.to_datetime(df['timestamp'].str[:10])

print(df)

返回:

      idAviso  timestamp idpostulante
0  1111413600 2018-02-28      0z5VvGv
1  1112368499 2018-02-28      0z5VvGv
2  1112369554 2018-02-28      0z5VvGv
3  1112358250 2018-02-27      0zB026d
4  1112358250 2018-02-27      0zB026d

另见:

https://github.com/pandas-dev/pandas/issues/16898

答案 1 :(得分:1)

这是带有偏移的ISO8061时间格式。

In [1]: x= '2018-02-28T20:40:28.079-0500'

In [2]: from dateutil.parser import parse

In [3]: parse(x)
Out[3]: datetime.datetime(2018, 2, 28, 20, 40, 28, 79000, tzinfo=tzoffset(None, -18000))

在pandas Dataframe中使用它

In [7]: df = pd.DataFrame([x])

In [8]: df
Out[8]: 
                              0
0  2018-02-28T20:40:28.079-0500

In [9]: df[0]
Out[9]: 
0    2018-02-28T20:40:28.079-0500
Name: 0, dtype: object

In [10]: df[0].apply(parse)
Out[10]: 
0   2018-02-28 20:40:28.079000-05:00
Name: 0, dtype: datetime64[ns, tzoffset(None, -18000)]