Skip to content

Make the dtype object immutable and not coerce other types when compared (hashable requirements) (Trac #1127) #1725

Open

Description

@thouis

Original ticket http://projects.scipy.org/numpy/ticket/1127 on 2009-06-05 by @FrancescAlted, assigned to unknown.

The dtype object seems to be shared after a copy of a structured array is done. This is inconvenient when you change properties in one of arrays and these are propagated to the other.

The next snippet exposes the problem:

In [21]: dt = np.dtype('f4,f8')

In [22]: ra = np.empty(1, dt)

In [23]: ra.dtype
Out[23]: dtype([('f0', '<f4'), ('f1', '<f8')])

In [24]: rb = ra[:]

In [25]: ra.dtype.names = ('float', 'double')

In [26]: ra.dtype
Out[26]: dtype([('float', '<f4'), ('double', '<f8')])

In [27]: rb.dtype
Out[27]: dtype([('float', '<f4'), ('double', '<f8')])  # not the original!

Activity

  1. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    Milestone changed to 1.4.0 by @cournape on 2009-11-25

  2. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    @mwiebe wrote on 2011-03-24

    This is the PyArray_Descr object, which implements the hash method. From http://docs.python.org/glossary.html, below is the definition of hashable. To be correct, the dtype object should be made immutable and should not coerce other types to dtype during comparisons.

    ==hashable==
    An object is hashable if it has a hash value which never changes during its lifetime (it needs a hash() method), and can be compared to other objects (it needs an eq() or cmp() method). Hashable objects which compare equal must have the same hash value.

    Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally.

    All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. Objects which are instances of user-defined classes are hashable by default; they all compare unequal, and their hash value is their id().

  3. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    Milestone changed to NumPy 2.0 by @mwiebe on 2011-03-24

  4. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    Title changed from dtype is shared across structured arrays copies to Make the dtype object immutable and not coerce other types when compared (hashable requirements) by @mwiebe on 2011-03-24

  5. thouis commented on Oct 19, 2012

    @thouis
    ContributorAuthor

    @mwiebe wrote on 2011-05-23

    The dtype pickling also needs to change, because you can't have a setstate function on an immutable object.

  6. charris commented on Feb 18, 2014

    @charris
    Member

    Still in progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions