Skip to content

fill_value attribute gives TypeError for structured masked array fields with character dtype #7259

Closed

Description

@gerritholl

For a structured masked array, when accessing a field with a character dtype, the fill_value is evidently not correctly set. When X is a structured masked array with a structured dtype such that field A is a character array, accessing X['A'].fill_value gives TypeError: byte indices must be integers or slices, not tuple. The offending line of code is:

return self._fill_value[()]

with a full example shown below.

$ ipython3 --pylab
Python 3.5.0 (default, Nov 20 2015, 16:20:41) 
Type "copyright", "credits" or "license" for more information.

IPython 4.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
Using matplotlib backend: TkAgg
Python are go!

In [1]: print(numpy.version.version)
1.12.0.dev0+0de1234

In [2]: X = numpy.ma.empty(shape=1, dtype=[("A", "S1", 2)])

In [3]: Xa = X["A"]

In [5]: Xa.fill_value
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-d077d77d307b> in <module>()
----> 1 Xa.fill_value

/home/users/gholl/venv/stable-3.5/lib/python3.5/site-packages/numpy/ma/core.py in get_fill_value(self)
   3528         if self._fill_value is None:
   3529             self._fill_value = _check_fill_value(None, self.dtype)
-> 3530         return self._fill_value[()]
   3531 
   3532     def set_fill_value(self, value=None):

TypeError: byte indices must be integers or slices, not tuple

This problem is triggered, among other locations, upon calling repr(Xa), so it has a rather high impact in practice.

Activity

  1. gerritholl commented on Feb 16, 2016

    @gerritholl
    ContributorAuthor

    It appears to be ultimately due to the fact that most numpy scalars are valid to be indexed with (), but numpy.bytes_ is not:

    In [36]: numpy.int64(10)[()]
    Out[36]: 10
    
    In [37]: numpy.bytes_("ABC")
    Out[37]: b'ABC'
    
    In [38]: numpy.bytes_("ABC")[()]
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-38-4912a5675720> in <module>()
    ----> 1 numpy.bytes_("ABC")[()]
    
    TypeError: byte indices must be integers or slices, not tuple
    
  2. gerritholl commented on Feb 16, 2016

    @gerritholl
    ContributorAuthor

    See also my comment on the commit triggering this problem: f8f753b#commitcomment-16120218

  3. added a commit that references this issue on Feb 22, 2016
    bec40c2
  4. added a commit that references this issue on Mar 22, 2016
    5accbf6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions