dtype of mixed ints and strings depends on the element order
See original GitHub issueThe dtype of NumPy array created from a Python list containing both ints and strings depends on the order of the elements:
In [3]: np.array(['a', 1]).dtype
Out[3]: dtype('<U1')
In [4]: np.array([1, 'a']).dtype
Out[4]: dtype('<U21')
tested on Python 3.5.2 and NumPy 1.11.1
Note that it’s related (but not the same) to issue #6550
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Pandas convert mixed types to string - python - Stack Overflow
This calls the lambda elt: str(int(elt)) if isinstance(elt, float) else str(elt) function over each element of the 'mixed' column.
Read more >3. Strings, lists, and tuples — Beginning Python Programming ...
Strings, lists, and tuples are all sequence types, so called because they behave like a sequence - an ordered collection of objects. Squence...
Read more >Overview of Pandas Data Types - Practical Business Python
Pandas Data Types ; object, str or mixed, string_, unicode_, mixed types, Text or mixed numeric and non-numeric values ; int64, int, int_,...
Read more >NumPy Array Data Type - Data, Science, Energy
Having a data type (dtype) is one of the key features that distinguishes NumPy arrays from lists. In lists, the types of elements...
Read more >Strings and numbers in the same matrix - MATLAB Answers
The data is made up of both strings and regular numbers, mixed. Storing the data as a matrix is fine (I've used a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Its fixed and deprecated in 1.20, you have to use
np.array([1, "a"], dtype="U")
, which will give you the identical result in both cases to begin with. The result is than always length 1, just because that is the more common variant currently.Looks like it is saving space for the maximum size int with sign in the
U21
case. Could just be a logic error somewhere…