1

When I append elements to a list that have the following format and type:

data.append([float, float, string])

Then stack the list using:

data = np.hstack(data)

And then finally reshape the array and transpose using:

data = np.reshape(data,(-1,3)).T

All the array elements in data are changed to strings. I want (and expected) the first and second columns in data to be of type float and the third of type string, but instead they are all of type string. [Interestingly, if I do not append the string elements to data and adjust the newshape to (-1,2), both columns are floats.] I cannot figure this one out. Any help would be appreciated.

Sebekemsaf
  • 23
  • 4
  • I think as soon as numpy touches it it converts them to a common format unless told otherwise. I don't know if you can tell it dtypes with hstack, but here is an alternative: https://stackoverflow.com/questions/26018781/numpy-is-it-possible-to-preserve-the-dtype-of-columns-when-using-column-stack – Markus Oct 18 '18 at 14:56
  • Markus, thanks for the help. Your comments guided me to this: https://stackoverflow.com/questions/42813011/replace-an-element-in-a-numpy-array-at-specific-index I was able to find a solution here using dtype=object. – Sebekemsaf Oct 18 '18 at 18:24

1 Answers1

1

Because of the mix of numbers and strings, np.array will use the common format: string. The solution here is to convert data to type object which supports mixed element types. This is performed by using:

data = np.array(data, dtype=object)

prior to hstack.

Sebekemsaf
  • 23
  • 4