Reading a csv file with a timestamp column, with pandas

advertisements

When doing:

import pandas
x = pandas.read_csv('data.csv', parse_dates=True, index_col='DateTime',
                                names=['DateTime', 'X'], header=None, sep=';')

with this data.csv file:

1449054136.83;15.31
1449054137.43;16.19
1449054138.04;19.22
1449054138.65;15.12
1449054139.25;13.12

(the 1st colum is a UNIX timestamp, i.e. seconds elapsed since 1/1/1970), I get this error when resampling the data every 15 second with x.resample('15S'):

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex

It's like the "datetime" information has not been parsed:

                 X
DateTime
1.449054e+09  15.31
1.449054e+09  16.19
...

How to import a .CSV with date stored as timestamp with pandas module?

Then once I will be able to import the CSV, how to access to the lines for which date > 2015-12-02 12:02:18 ?


My solution was similar to Mike's:

import pandas
import datetime
def dateparse (time_in_secs):
    return datetime.datetime.fromtimestamp(float(time_in_secs))

x = pandas.read_csv('data.csv',delimiter=';', parse_dates=True,date_parser=dateparse, index_col='DateTime', names=['DateTime', 'X'], header=None)

out = x.truncate(before=datetime.datetime(2015,12,2,12,2,18))