2013-03-04 17 views
36

Pandalar read_csv yöntemi ile basit bir boşluk ayrılmış dosyayı okumaya çalışıyorum. Ancak, pandalar benim dtype argümanına uymuyor gibi görünüyor. Belki yanlış bir şekilde belirliyorum?pandas pandas.read_csv ile dtype float32 belirtme 0.10.1

Bu basit test vakasına, read_csv numaralı telefon hattımı biraz karmaşık bir şekilde ayırdım. Aslında 'gerçek' senaryomda converters argümanını kullanıyorum ama bunu basitlik için kaldırdım. Ben de numpy.int32 veya numpy.int64 bir dtype ile bu kullanarak bu denedim

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

:

Aşağıda benim ipython oturumdur. Bu seçimler bir istisna neden: pandalar otomatik bir tamsayı içine/dönüştürmek float değerleri kesecek denemez çünkü

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

Ben AttributeError varsayıyorum nedir?

32 bit bir Python sürümü olan 32 bit makinede çalışıyorum.

# 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 

Birlikte doğruladı: gerçekten float32 desteklemediği

>>> !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.10.1 Bu [github üzerinde bu konuda] (https://github.com/pydata/pandas/issues/2570) benzer düşünüyorum altında benim için iyi Yukarıdaki eserler ... –

+0

@AndyHayden Haklı olduğunu düşünüyorum. 'AttributeError' sorunu, github sorununun sözünü ettiği şeydir. Ancak, diğer senaryonda değerler yüzer fakat pandalar float64 yerine float32 kullanmayı denediğimde "dtype" argümanına uymazlar. –

cevap

22

0.10.1 çok

bu http://pandas.pydata.org/pandas-docs/dev/whatsnew.html#dtype-specification

böyle 0.11 yapabilirsiniz

bkz 0.11-dev, bu çalışır (32-bit ve 64-bit, sonuçları aynıdır)

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' veya 'convert_objects' tercih edilen yol olabilir mi bu? –

+0

Eğer bir tür dtype gerekiyorsa, sonra astype kullanın, convert_objects daha fazla nesne dtypes dönüştürmek için (ve önceki sürümlerinde olduğu gibi gerekli değildir) – Jeff

+1

Yani bu pandalar bir hata olarak kabul edilir? Ben bir 'dtype' geçebilir ve ben istedim ya da bir hata, vb almak alamadım biraz aldatıcı gibi görünüyor. –

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

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

pandalar

+1

fyi, bu inplace (hangisi gizlidir) ve float olmayan veriler için güvenli değildir – Jeff

+0

@ jeff Evet, bu inplace dökümdür ve float değerleri için güvenli değildir – pravin

+0

'df = pd.read_csv ('sample. dışarı ', dönüştürücüler = {' bir ': lambda x: pd.np.float32 (x)}, delim_whitespace = Doğru) 'da çalışmıyor gibi görünüyor. – pravin