When checking the data type of string with one character, i am getting dtype as <U1 as expected.
print(numpy.array(["a"]).dtype)
Output : <U1
But after adding an integer to the array, why does it consume 21 characters ?
print(numpy.array([1,"a"]).dtype)
Output : <U21
Why does it consume 21 characters?
Because the elements are being promoted, this means numpy transforms the elements to
For example if we use promote_types:
Output
Regarding the U21, it consists of two parts, as you already know, the U which denotes Unicode and the 21 denotes the number of elements it can hold, see more on this answer.
So as 8 can be cast to int64, and it can hold at least 20 characters (platform dependent though), it's being transformed to U21. The know the number of characters a number can have you can do:
Output
In particular:
Output
You can keep U1, by doing:
Output
See more on this GitHub issue.