我正在从磁盘将csv文件读取到pandas DataFrame中,并希望根据索引时间戳对DataFrame进行切片/筛选。
这就是我目前所掌握的:
INDEX_COL_NAME = 'Zeit'
DELIM_SIGN = ';'
DECIMAL_SIGN = ','
KEEP_COLUMNS = [-2]
ENCODING = 'ISO-8859-1'
DATE = (2016, 8, 11)
START = (10, 52, 0)
END = (10, 53, 0)
df = pd.read_csv('data.csv',
delimiter=DELIM_SIGN,
decimal=DECIMAL_SIGN,
index_col=False,
parse_dates=[INDEX_COL_NAME],
infer_datetime_format=True,
encoding=ENCODING)
df.set_index(INDEX_COL_NAME, inplace=True)
df = df[KEEP_COLUMNS]
date = pd.datetime(*DATE)
start = date.replace(hour=START[0], minute=START[1], second=START[2])
end = date.replace(hour=END[0], minute=END[1], second=END[2])
数据如下(简称片段(:
Zeit;FU_P1;FU_P2;DIR_01;FIR_01;WAAGE_B1.I;WAAGE_B1.T;WAAGE_B1.X;WAAGE_B2.I;WAAGE_B2.T;WAAGE_B2.X;WAAGE_B3.I;WAAGE_B3.T;WAAGE_B3.X;WAAGE_B4.I;WAAGE_B4.T;WAAGE_B4.X;LEITFÄHIGKEIT_1.COND;LEITFÄHIGKEIT_2.COND
11.08.2016 10:51:59; 20,0; 0,00; 991,19;29,21; 0,0;Empty; 239; 1,0;Empty;-11,600; 0,0;Empty;-0,023; 0,0;Empty;-1,776; 0,3;Empty;
11.08.2016 10:52:00; 20,0; 0,00; 991,22;29,11; 0,0;Empty; 239; 1,0;Empty;-11,600; 0,0;Empty;-0,023; 0,0;Empty;-1,787; 0,3;Empty;
11.08.2016 10:52:10; 20,0; 0,00; 991,08;29,24; 0,0;Empty; 239; 1,0;Empty;-11,600; 0,0;Empty;-0,023; 1,0;Empty;-1,840; 0,3;Empty;
11.08.2016 10:52:20; 20,0; 0,00; 990,95;28,95; 0,0;Empty; 239; 1,0;Empty;-11,600; 0,0;Empty;-0,023; 0,0;Empty;-1,947; 0,3;Empty;
11.08.2016 10:52:30; 20,0; 0,00; 990,94;28,96; 0,0;Empty; 238; 1,0;Empty;-11,600; 0,0;Empty;-0,022; 0,0;Empty;-2,059; 0,3;Empty;
11.08.2016 10:52:40; 20,0; 0,00; 990,82;28,91; 0,0;Empty; 238; 1,0;Empty;-11,600; 0,0;Empty;-0,021; 0,0;Empty;-2,155; 0,3;Empty;
11.08.2016 10:52:50; 20,0; 0,00; 990,80;29,37; 0,0;Empty; 238; 1,0;Empty;-11,600; 0,0;Empty;-0,020; 0,0;Empty;-2,249; 0,0;Empty;
11.08.2016 10:53:00; 20,0; 0,00; 990,71;29,15; 0,0;Empty; 239; 1,0;Empty;-11,600; 0,0;Empty;-0,021; 1,0;Empty;-2,309; 0,5;Empty;
11.08.2016 10:53:01; 20,0; 0,00; 990,78;29,04; 0,0;Empty; 239; 1,0;Empty;-11,600; 0,0;Empty;-0,021; 0,2;Empty;-2,350; 0,5;Empty;
然而,由于,我无法获得所需的切片
print(df.ix[start:end]
打印一个空的DataFrame。
这些元素是索引的一部分
print(df.index)
显示
DatetimeIndex(['2016-11-08 10:45:27', '2016-11-08 10:45:28',
'2016-11-08 10:45:29', '2016-11-08 10:45:30',
'2016-11-08 10:45:31', '2016-11-08 10:45:32',
'2016-11-08 10:45:33', '2016-11-08 10:45:34',
'2016-11-08 10:45:35', '2016-11-08 10:45:36',
...
'2016-11-08 15:59:51', '2016-11-08 15:59:52',
'2016-11-08 15:59:53', '2016-11-08 15:59:54',
'2016-11-08 15:59:55', '2016-11-08 15:59:56',
'2016-11-08 15:59:57', '2016-11-08 15:59:58',
'2016-11-08 15:59:59', '2016-11-08 16:00:00'],
dtype='datetime64[ns]', name='Zeit', length=10408, freq=None)
并且从数据记录的总开始到其结束,每秒钟都有行。
此外
print(start in df.index)
给出
False
我也不太明白。
如何执行不期望的切片/过滤?我错过了什么?
索引似乎很好(您也可以使用pd.Timestamp
或仅使用字符串进行切片,而不是使用日期时间对象(。
问题是日-月订单。IIUC,字符串11.08.2016
被转换为11月8日而不是8月11日。将参数dayfirst=True
添加到pd.read_csv
应该可以对其进行排序。