【发布时间】:2013-02-19 02:18:34
【问题描述】:
我正在尝试使用 pandas read_csv 方法读取一个简单的空格分隔文件。但是,熊猫似乎没有遵守我的dtype 论点。也许我指定的不正确?
我已经将我对read_csv 的有点复杂的调用提炼为这个简单的测试用例。我实际上在我的“真实”场景中使用了converters 参数,但为了简单起见,我删除了它。
下面是我的 ipython 会话:
>>> cat test.out
a b
0.76398 0.81394
0.32136 0.91063
>>> import pandas
>>> import numpy
>>> x = pandas.read_csv('test.out', dtype={'a': numpy.float32}, delim_whitespace=True)
>>> x
a b
0 0.76398 0.81394
1 0.32136 0.91063
>>> x.a.dtype
dtype('float64')
我也尝试过将其与dtype 或numpy.int32 或numpy.int64 一起使用。这些选择会导致异常:
AttributeError: 'NoneType' object has no attribute 'dtype'
我假设 AttributeError 是因为 pandas 不会自动尝试将浮点值转换/截断为整数?
我在一个 32 位机器上运行 32 位版本的 Python。
>>> !uname -a
Linux ubuntu 3.0.0-13-generic #22-Ubuntu SMP Wed Nov 2 13:25:36 UTC 2011 i686 i686 i386 GNU/Linux
>>> import platform
>>> platform.architecture()
('32bit', 'ELF')
>>> pandas.__version__
'0.10.1'
【问题讨论】:
-
我认为这类似于this issue on github...
-
@AndyHayden 我认为你是对的。
AttributeError问题正是 github 问题所提到的。但是,在我的其他场景中,值是浮点数,但当我尝试使用 float32 而不是 float64 等时,pandas 不服从dtype参数。