2013-03-04 4 views
36

私はpandas read_csvメソッドで単純なスペース区切りファイルを読み込もうとしています。しかし、パンダは私のdtype議論に従っていないようです。たぶん私はそれを間違って指定していますか?pandas 0.10.1でdtype float32を指定する

私はこの簡単なテストケースに対して、read_csvへのやや複雑な呼び出しを書き留めました。私は実際には私の '実際の'シナリオでconverters引数を使用していますが、これを簡単にするために削除しました。私もnumpy.int32またはnumpy.int64dtypeでこれを使用して、これを試してみた

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

以下は私のipythonセッションです。これらの選択肢は、例外が発生:

AttributeError: 'NoneType' object has no attribute 'dtype' 

私はパンダが自動的に整数に浮動小数点値を切り捨てる/変換しようとしないのでAttributeErrorがあると仮定していますか?

私は、32ビット版のPythonを搭載した32ビットマシンで動作しています。

>>> !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' 
+0

の下で私のために正常に動作します... –

+0

@AndyHayden私はあなたが正しいと思います。 'AttributeError'の問題はgithubの問題に言及しています。しかし、私の他のシナリオでは値は浮動小数点型ですが、float64などの代わりにfloat32を使用しようとすると、pandasは 'dtype'引数に従いません。 –

答えて

22

0.10.1は本当にのfloat32非常

をサポートしていません。このhttp://pandas.pydata.org/pandas-docs/dev/whatsnew.html#dtype-specification

は、あなたがこのような0.11でこれを行うことができます参照してください。

# dont' use dtype converters explicity for the columns you care about 
# they will be converted to float64 if possible, or object if they cannot 
df = pd.read_csv('test.csv'.....) 

#### this is optional and related to the issue you posted #### 
# force anything that is not a numeric to nan 
# columns are the list of columns that you are interesetd in 
df[columns] = df[columns].convert_objects(convert_numeric=True) 


    # astype 
    df[columns] = df[columns].astype('float32') 

see http://pandas.pydata.org/pandas-docs/dev/basics.html#object-conversion 

Its not as efficient as doing it directly in read_csv (but that requires 

私が持つことが確認されています0.11-dev、これは動作します(32ビットと64ビットの結果は同じです)

In [5]: x = pd.read_csv(StringIO.StringIO(data), dtype={'a': np.float32}, delim_whitespace=True) 

In [6]: x 
Out[6]: 
     a  b 
0 0.76398 0.81394 
1 0.32136 0.91063 

In [7]: x.dtypes 
Out[7]: 
a float32 
b float64 
dtype: object 

In [8]: pd.__version__ 
Out[8]: '0.11.0.dev-385ff82' 

In [9]: quit() 
[email protected]:~/pandas$ uname -a 
Linux precise32 3.2.0-23-generic-pae #36-Ubuntu SMP Tue Apr 10 22:19:09 UTC 2012 i686 i686 i386 GNU/Linux 

some low-level changes) 
+0

' astype'または 'convert_objects'が望ましい方法でしょうかこの? –

+0

特定のdtypeが必要な場合にastypeを使用すると、convert_objectsはオブジェクトdtypesから変換するためのものです(以前のバージョンのように必要ではありません) – Jeff

+1

これはpandasのバグと考えられますか?私は 'dtype'を渡すことができ、私が尋ねたものやエラーなどを得ることができないと少し欺まれているようです。 –

7
In [22]: df.a.dtype = pd.np.float32 

In [23]: df.a.dtype 
Out[23]: dtype('float32') 

上記の私は、これは[githubの上でこの問題](https://github.com/pydata/pandas/issues/2570)に似て考えるパンダ0.10.1

+1

fyiこれはインプレース(暗黙的)であり、float以外のデータに対しては安全ではありません – Jeff

+0

@Jeffはい、これはインプレースキャストで、float以外の値に対しては安全ではありません – pravin

+0

'df = pd.read_csv( 'sample。コンバータ '=' '':ラムダx:pd.np.float32(x)}、delim_whitespace = True) 'も動作していないようです。 – pravin

関連する問題