[Numpy-discussion] bug in chararray startswith function?

Michael Sorich michael.sorich at gmail.com
Mon Nov 20 01:43:49 EST 2006


python 2.4.3 with numpy 1.0

import numpy
ca = numpy.char.asarray(['hi','there'])
print ca
print ca.startswith('h')

--output
['hi' 'there']
Traceback (most recent call last):
  File "D:\eclipse\NumpyExtensions\scripts\testchararray.py", line 4, in ?
    print ca.startswith('h')
  File "C:\Python24\Lib\site-packages\numpy\core\defchararray.py",
line 271, in startswith
    return self._typedmethod('startswith', broadcast(self, prefix,
start, end), bool)
  File "C:\Python24\Lib\site-packages\numpy\core\defchararray.py",
line 154, in _typedmethod
    if chk.dtype is object_ and chk.item() is None:
AttributeError: 'NoneType' object has no attribute 'dtype'

It looks like this is because the 2 default parameters (start=None,
end=None) and when this is passed to _typedmethod, val = ('hi', 'h',
None, None). Since None does not have a dtype, the error is raised. If
one puts in the additional parameters there is not error as the
additional vars are converted in numpy int scalars
e.g.

defchararray.py
    def _typedmethod(self, name, myiter, dtype):
        result = empty(myiter.shape, dtype=dtype)
        res = result.flat
        for k, val in enumerate(myiter):
            newval = []
            for chk in val[1:]:
                if chk.dtype is object_ and chk.item() is None:
                    break
                newval.append(chk)
            this_str = val[0].rstrip('\x00')
            newitem = getattr(this_str,name)(*newval)
            res[k] = newitem
        return result



More information about the NumPy-Discussion mailing list