[Numpy-discussion] constructing char arrays with "where"

Darren Dale dd55 at cornell.edu
Tue Mar 21 08:27:34 CST 2006


There is some disparity between the numpy, Numeric and numarray flavors of the 
"where" function, as concerns creation of char arrays. I got bit by the 
limitation in numarray. I found a workaround for my matplotlib code that 
didnt use "where", but perhaps it is worth mentioning.

Darren

#################################################
import Numeric, numpy, numarray

In [1]: numpy.where([True, False], 'T', 'F')
Out[1]:
array([T, F],
      dtype='|S1')
dtype='|S1')

In [2]: numpy.where([True, False], 'True', 'False')
Out[2]:
array([True, False],
      dtype='|S5')

#################################################
In [3]: Numeric.where([True, False], 'T', 'F')
Out[3]: array([T, F],'c')

In [4]: Numeric.where([True, False], 'True', 'False')
---------------------------------------------------------------------------
exceptions.ValueError                                Traceback (most recent 
call last)

/home/darren/src/matplotlib/<ipython console>

/usr/lib64/python2.4/site-packages/Numeric/Numeric.py in where(condition, x, 
y)
    603     y where condition is respectively true or false.
    604     """
--> 605     return choose(not_equal(condition, 0), (y, x))
    606
    607 def compress(condition, m, axis=-1):

ValueError: array dimensions must agree
################################################
In [23]: numarray.where([True, False], 'T', 'F')
---------------------------------------------------------------------------
exceptions.TypeError                                 Traceback (most recent 
call last)

/home/darren/src/matplotlib/<ipython console>

/usr/lib64/python2.4/site-packages/numarray/generic.py in where(condition, x, 
y, out)
   1066         if x is None or y is None:
   1067             raise ValueError("Invalid parameter list")
-> 1068         return choose(ufunc.not_equal(condition, 0), (y,x), out)
   1069
   1070 def clip(m, m_min, m_max):

/usr/lib64/python2.4/site-packages/numarray/ufunc.py in choose(selector, 
population, outarr, clipmode)
   1988                        [0, shape[i]) result in an exception.
   1989     """
-> 1990     return _choose(selector, population, outarr, clipmode)
   1991
   1992 def _scatteredPseudos( scattered ):

/usr/lib64/python2.4/site-packages/numarray/ufunc.py in __call__(self, inarr1, 
inarr2, outarr, clipmode)
   1894     def __call__(self, inarr1, inarr2, outarr=None, clipmode=RAISE):
   1895         """The standard calling interface for UFuncs"""
-> 1896         computation_mode, woutarr, cfunc, ufargs = \
   1897                           self._setup(inarr1, inarr2, outarr, 
clipmode)
   1898

/usr/lib64/python2.4/site-packages/numarray/ufunc.py in _setup(self, in1, in2, 
outarr, clipmode)
   1910
   1911         if outarr is None:
-> 1912             convType = _maxPopType(in2)
   1913         else:
   1914             convType = outarr._type

/usr/lib64/python2.4/site-packages/numarray/ufunc.py in _maxPopType(xs)
   1852             t = type(x)
   1853         else:
-> 1854             t = _nc._maxtype(x)
   1855
   1856         if maxT is None:

/usr/lib64/python2.4/site-packages/numarray/numarraycore.py in _maxtype(args)
    129             return complex
    130     else:
--> 131         return PyLevel2Type[_numarray._maxtype(args)]
    132
    133 def _storePyValueInBuffer(buffer, Ctype, index, value):

TypeError: Expecting a python numeric type, got something else.




More information about the Numpy-discussion mailing list