Thanks to visit codestin.com
Credit goes to github.com

Skip to content

array(...) casts mix lists of int and string to string automatically (excepted dtype object) #6550

Open
@sdementen

Description

@sdementen

The following code explains the issue

>>> import numpy
>>> print numpy.__version__
1.10.1
>>> a = [["a", 1], [2, 3]]
>>> b = numpy.array(a)
>>> print repr(a)
[['a', 1], [2, 3]]
>>> print repr(b)
array([['a', '1'],
       ['2', '3']],
      dtype='|S1')

I would have expected that not specifying a dtype would have given the result as if I had specify a dtype(object) (to be able to hold both ints and strings)

>>> b = numpy.array(a, dtype=numpy.dtype(object))
>>> print repr(b)
array([['a', 1],
       [2, 3]], dtype=object)

as the doc http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html describes

dtype : data-type, optional
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 argument can only be used to ‘upcast’ the array. For downcasting, use the .astype(t) method.

But I may have missed some point in the docs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions