question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dtype of mixed ints and strings depends on the element order

See original GitHub issue

The 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:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sebergcommented, Jan 29, 2021

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.

1reaction
charriscommented, Aug 25, 2016

Looks like it is saving space for the maximum size int with sign in the U21 case. Could just be a logic error somewhere…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found