Skip to content

concatenating with empty list changes dtype to float64 #10135

Closed

Description

@nschloe

When concatenating anything with an empty list, the dtype of the result is augmented to float64.

MWE:

import numpy

print(numpy.concatenate([[0]]).dtype)  # int64
print(numpy.concatenate([[0], [1]]).dtype)  # int64
print(numpy.concatenate([[0], []]).dtype)  # float64!
print(numpy.concatenate([[], [0]]).dtype)  # float64!

Activity

  1. pv commented on Nov 30, 2017

    @pv
    Member
  2. nschloe commented on Jan 12, 2018

    @nschloe
    ContributorAuthor

    Not sure why I closed this one here. The explanation makes sense, indeed

    print(numpy.array([]).dtype)  # float64

    The numpy.array documentation says about the optional dtype argument to numpy.array:

    The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence.

    This doesn't seem to be the case here: A clearly smaller dtype than float64 is, e.g., int8. Taking int8 as default input type for empty arrays would also prevent the needless upcasting as described in the original post in most cases. The only time where this would fail is

    e = numpy.array([], dtype=numpy.int8)
    x = numpy.array([], dtype=numpy.uint8)
    
    (e*x).dtype == numpy.int16
  3. rkern commented on Jan 12, 2018

    @rkern
    Member

    Empty arrays are a special case that doesn't really fall under that rule (and is rare enough that that part of the docstring is probably just distracting rather than enlightening). The general default for an array where there are no values to inspect is float64. Compare to ones(), zeros(), etc.

  4. eric-wieser commented on Jan 12, 2018

    @eric-wieser
    Member

    I think @nschloe is questioning whether that's the right default for empty arrays, which IMO is:

    • worthy of its own issue
    • almost certainly something we can't do to preserve compatibility
  5. nschloe commented on Jan 14, 2018

    @nschloe
    ContributorAuthor

    I think @nschloe is questioning whether that's the right default for empty arrays, which IMO is: worthy of its own issue

    Indeed. I'll open one.

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