Python Numpy: np.array converts list values with string lengths longer than 30 to '<str_, len() = ##>'

Viewed 55

I have a pythonList of strings:

pythonList= ['Pricing1', 'Expense_Inflation_Rate_Annual.fac', 'Vertical', 'MNT_INFL_PC']

When I try to access all the strings by index like pythonList[1] they work just fine.

I converted the array into a numpyArray so I could use the np.where function, but now when I try to access all the strings like numpyArray[1] the 'Expense_Inflation_Rate_Annual.fac' value now holds '<str_, len() = 33>' instead while the other 3 work as intended.

My code for the conversion is simply:

numpyArray = np.array(pythonList)

Edit:

For more context I am using Python 3.9.2 and have discovered with testing that numpy does not like strings longer than 30 characters and anything more than 30 gets the <str_, len() = ##> input.

Here is the code I used to read in pythonList:

    with open (r'c:\\temp\{}'.format(resultFileName)) as lp:
        fileData = lp.readlines()
    pythonList= []
    for row in fileData:
        pythonList.append(row.split(','))

    numpyArray= np.array(pythonList)

If anyone could provide more detail on this, it would be greatly appreciated.

Thanks.

1 Answers
In [114]: pythonList = [
     ...:     "Pricing1",
     ...:     "Expense_Inflation_Rate_Annual.fac",
     ...:     "Vertical",
     ...:     "MNT_INFL_PC",
     ...: ]
In [115]: np.array(pythonList)
Out[115]: 
array(['Pricing1', 'Expense_Inflation_Rate_Annual.fac', 'Vertical',
       'MNT_INFL_PC'], dtype='<U33')

It's not clear where you get the '<str_, len() = 33>'. If I index that string, the result is an object of type str_` and length 33.

In [116]: np.array(pythonList)[1]
Out[116]: 'Expense_Inflation_Rate_Annual.fac'
In [117]: type(np.array(pythonList)[1])
Out[117]: numpy.str_
In [118]: len(np.array(pythonList)[1])
Out[118]: 33

From pythonList we get something similar, except the type is slightly different:

In [119]: type(pythonList[1])
Out[119]: str
In [120]: len(pythonList[1])
Out[120]: 33

A Python list contains str objects that vary in length. When converted to a numpy array, they are stored as bytes with a Un dtype. Here U33 is the length of the longest string in the input.

You mention using where, but don't elaborate.

numpy arrays of strings don't have a lot of advantages relative to lists of strings.

Related