0

I am trying to convert an array of np.string_s that are all representing numbers into floats for mathematical use. I am reading data from a fits file using pyfits.

data = read_fits(datafile)
glon = np.asarray(data['GLON'], dtype = float); glat = data['GLAT']
# heliocentric distance in kpc
D = data['D'] 
for i in range(len(glon)):
    #glon[i] = (glon[i]).astype(float)

I have left a couple of my attempts to change the type, but float() and all similar operations I have heard of are failing. Yet I do not think I have a bogus value as I have looked through a print of the data after reading and it is all numerical.

4
  • 1
    What is the value inside data['GLON']? What errors are you getting when you say "failing"? Commented Jun 24, 2015 at 18:20
  • The value of data['GLON'] is <type 'numpy.ndarray'>. Commented Jun 24, 2015 at 18:50
  • The error is ValueError: could not convert string to float if I attempt to convert in any way. It is unsupported operand type(s) for /: 'numpy.string_' and 'int' for my other purposes. Commented Jun 24, 2015 at 18:52
  • What's the dtype of data['GLON']. I'm guessing np.string_. Also you could experiment with [float(i) for i in data['GLON]]` (assuming it is 1d). Commented Jun 24, 2015 at 19:21

1 Answer 1

1

Row [0] had blank characters...

Sign up to request clarification or add additional context in comments.

1 Comment

I thought it was ok to answer your own question. In fact as a reader I prefer it that way. I tend to focus on questions without an answer, and often skip ones with an accepted one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.