[Numpy-discussion] more python 3.2 fun

josef.pktd at gmail.com josef.pktd at gmail.com
Thu May 19 19:30:47 EDT 2011


(just illustrating some porting fun)

I was struggling with another python 3.2 bug  in scikits.statsmodels
(with grunfeld data)

with numpy 1.5.1, I'm reading the data with

    data = recfromtxt(open(filepath + '/grunfeld.csv', 'rb'), delimiter=",",
            names=True, dtype="f8,f8,f8,a17,f8")


>>> grun_exog['firm'][:10]
array([b'General Motors', b'General Motors', b'General Motors',
       b'General Motors', b'General Motors', b'General Motors',
       b'General Motors', b'General Motors', b'General Motors',
       b'General Motors'],
      dtype='|S17')

using predefined firms defined as string

>>> firms = ['General Motors', 'Chrysler', 'General Electric', 'Westinghouse',
        'US Steel']

>>> i
'General Motors'

comparing bytes and string produces False not an element wise comparison

>>> grun_exog['firm'] == i
False

>>> grun_exog['firm'][0] == i
False
>>> grun_exog['firm'][0] , i
(b'General Motors', 'General Motors')

numpy has a convenient conversion function

>>> from numpy.compat import asbytes
>>> ( grun_exog['firm'] == asbytes(i) )[:10]
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
True], dtype=bool)


Josef



More information about the NumPy-Discussion mailing list