它看起来像参数index_col=0
优先于dtype
参数,如果你删除index_col
param 然后你可以调用set_index
after:
In [235]:
fra = pd.read_csv(io.StringIO('date,close\n20140101,10.2\n20140102,10.5'), dtype={'date': np.str_, 'close': np.float})
fra
Out[235]:
date close
0 20140101 10.2
1 20140102 10.5
In [236]:
fra = fra.set_index('date')
fra.index
Out[236]:
Index(['20140101', '20140102'], dtype='object')
另一种方法是删除index_col
参数并调用set_index
在返回的 df 上read_csv
所以它变成了一行:
In [237]:
fra = pd.read_csv(io.StringIO('date,close\n20140101,10.2\n20140102,10.5'), dtype={'date': np.str_, 'close': np.float}).set_index('date')
fra.index
Out[237]:
Index(['20140101', '20140102'], dtype='object')
Update
这是一个bug https://github.com/pydata/pandas/issues/9435这是针对版本0.17.0