简体   繁体   中英

converting a string which looks like a date into date

I been reading a lot of question's which convert string into datetime, but i didnt find a string date which looks like the shown in this dataframe.

    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

My goal is to the column timestamp convert it to something like this so then i can used it for some analysis

    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 should be now be a datetime variable

Simply strip the string with .str[:10] and then use pd.to_datetime() like this:

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

other alternatives:

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

Full example:

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)

Returns:

      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

Also look here:

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

This is ISO8061 time format with offset.

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

To work with it in 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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM