读取带有时间戳列的csv和pandas [英] Reading a csv with a timestamp column, with pandas

查看:789
本文介绍了读取带有时间戳列的csv和pandas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行时:

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

与此 data.csv 档案:

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

(第一列是UNIX时间戳,即自1970年1月1日以来经过的秒数),当每15秒重新采样数据时, c $ c> x.resample('15S'):

  TypeError:仅在DatetimeIndex ,TimedeltaIndex或PeriodIndex 

这是因为datetime信息尚未解析:

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

如何导入.CSV,日期存储为pandas模块的时间戳?



然后,一旦我能够导入CSV,如何访问行的日期> 2015-12-02 12:02:18

解决方案

我的解决方案与Mike的类似:

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


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

这篇关于读取带有时间戳列的csv和pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆