0

Assume that I have the following list:

import numpy as np

a = [[1,    1.1, 'a'],
     ['ab', '1', '1.1']]
a_np = np.array(a)
print(a_np)

>>> [['1' '1.1' 'a']
    ['ab' '1' '1.1']]

I would like to turn a_np into an array of np.float64 values. However...

print(np.float64(a_np))
>>> ValueError: could not convert string to float: 'a'

...some of these values are not able to be turned into floats.

Is there a pythonic way to turn the array into an array of floats while turning the rest of the values into np.nan objects?

Desired:

print(pythonic_float_function(a_np))
>>> [[ 1.   1.1  nan]
    [ nan  1.   1.1]]
Paul Terwilliger
  • 1,596
  • 1
  • 20
  • 45

0 Answers0