0

I have a file text with data in a following format:

rubbish & 3.97& 3.83& 3.95& 3.83& 3.82

rubbish & 4.92& 4.81& 4.88& 4.81& 4.81

rubbish & 5.90& 5.66& 5.88& 5.66& 5.66

rubbish &--- & 6.05& 6.14& 6.05& 6.05

rubbish & 6.42& 6.26& 6.46& 6.26& 6.26

rubbish &--- & 6.56& 6.63& 6.56& 6.56

And I want to read them into numpy.ndarray object so that numbers are transformed into floating point number object while the --- stay as the string objects. However, the following piece of code creates an expected numpy.array object but everything in there is a string.

import numpy as np

wejscie = open('data.dat', 'r').readlines()

def fun1(x):

  print x
  if x.strip() == '---':

    return str(x)

  else:

    return float(x)

dane = np.array([map(fun1, linijka.split('&')[1:]) for linijka in wejscie])

So is it possible to have numpy.ndarray object containing data of various types?

user2300369
  • 299
  • 1
  • 3
  • 11
  • Try `return np.nan` instead of `return str(x)`. Or some other float value that will be place holder for the string. You can't mix strings and floats in an array like this. – hpaulj Aug 04 '16 at 16:24

1 Answers1

1

The problem isn't with fun1, it's with trying to insert elements of differing types into a numpy array.

Consider the following:

>>> a = numpy.array([1])
>>> numpy.append(a,2)
array([1, 2])
>>> numpy.append(a,'b')
array(['1', 'b'],
  dtype='<U11')

You may find this helpful Store different datatypes in one NumPy array?

Community
  • 1
  • 1
dashiell
  • 812
  • 4
  • 11