将毫秒数据读取到pandas中
我有一个文件,里面的数据是这样的,我想把它加载进来,并把时间戳这一列(表示毫秒)作为日期时间索引。
x y
timestamp
0 50 90
125 37 87
234 37 87
344 37 87
453 37 87
562 26 78
656 26 78
766 26 78
875 26 78
984 30 77
当我把时间戳指定为索引时,它变成了浮点数索引。
cur_df = pd.read_csv(cur_file, sep=',', comment='#', index_col = 'timestamp', parse_dates=True)
编辑:我添加了一个函数来解析日期,并加了一个虚拟日期:
def convert_time(a):
sec = int(math.floor(a/1000))
millisec = int(((a/1000.0)-int(math.floor(a/1000.0)))*1000)
time = '2012-01-01 00:00:%d.%d' % (sec, millisec)
return parser.parse(time)
cur_df = pd.read_csv(cur_file, sep=',', comment='#', index_col = 'timestamp', parse_dates=True, date_parser=convert_time)
现在它工作得不错!
如果有更好的方法,我会很感激任何建议;)
1 个回答
4
我觉得有点类似,但更简单一点(Python 的 datetime.datetime
使用的是微秒,所以要乘以 1000):
In [12]: import datetime
In [13]: def convert_time(a):
...: ms = int(a)
...: return datetime.datetime(2012, 1, 1, 0, 0, 0, ms*1000)
In [14]: pd.read_csv(cur_file, sep=',', index_col = 'timestamp', parse_dates=True, date_parser=convert_time)
Out[14]:
x y
timestamp
2012-01-01 00:00:00 50 90
2012-01-01 00:00:00.125000 37 87
2012-01-01 00:00:00.234000 37 87
2012-01-01 00:00:00.344000 37 87
2012-01-01 00:00:00.453000 37 87
2012-01-01 00:00:00.562000 26 78
2012-01-01 00:00:00.656000 26 78
2012-01-01 00:00:00.766000 26 78
2012-01-01 00:00:00.875000 26 78
2012-01-01 00:00:00.984000 30 77