31

I'm trying to iterate an array of values generated with numpy.linspace:

slX = numpy.linspace(obsvX, flightX, numSPts)
slY = np.linspace(obsvY, flightY, numSPts)

for index,point in slX:
    yPoint = slY[index]
    arcpy.AddMessage(yPoint)

This code worked fine on my office computer, but I sat down this morning to work from home on a different machine and this error came up:

File "C:\temp\gssm_arcpy.1.0.3.py", line 147, in AnalyzeSightLine
  for index,point in slX:
TypeError: 'numpy.float64' object is not iterable

slX is just an array of floats, and the script has no problem printing the contents -- just, apparently iterating through them. Any suggestions for what is causing it to break, and possible fixes?

5
  • Same versions of numpy? Same versions of Python? Same versions of Arc? Finally, do you need to work in float64? Commented May 31, 2013 at 14:06
  • 4
    One does not iterate over an array of floats with for index, point in slX:. This will not work in any version of Python with any version of numpy. Instead, did you actually run for index, point in enumerate(slX): on your office computer? That would seem to be what you are intending to accomplish. Commented May 31, 2013 at 14:22
  • Is slX supposed to be an iterable? Looks like it's returned as a 64 bit float, which is why you cannot iterate through it. Is it possible that slX is getting returned as a iterable with a length of one and numpy interprets that as a float? Commented May 31, 2013 at 14:22
  • 2
    Same versions of numpy, python, and Arc. I don't need to work in float64 and frankly didn't know I was until the error was thrown. Robert, as far as I know the code is exactly the same (I saved it in my Dropbox folder, however, so it's possible there was a sync error somewhere between there and here which means I'm working with an older version; the enumerate(slX) makes a lot more sense) Commented May 31, 2013 at 14:30
  • A quick test confirms this is not an ArcPy problem; it is a pure Python question. E.g., for i,j in numpy.linspace(0,1): ... print j creates the same error. The variant for j in numpy.linspace(0,1): ... print j works fine, indicating what the problem is. Commented May 31, 2013 at 17:10

1 Answer 1

11

numpy.linspace() gives you a one-dimensional NumPy array. For example:

>>> my_array = numpy.linspace(1, 10, 10)
>>> my_array
array([  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])

Therefore:

for index,point in my_array

cannot work. You would need some kind of two-dimensional array with two elements in the second dimension:

>>> two_d = numpy.array([[1, 2], [4, 5]])
>>> two_d
array([[1, 2], [4, 5]])

Now you can do this:

>>> for x, y in two_d:
    print(x, y)

1 2
4 5
Sign up to request clarification or add additional context in comments.

1 Comment

Muller: Sorry for necropost. I am having a similar error message when defining variance: I already defined the mean, mean(Y). Now I set Def :Var(Y): y_bar=mean(Y)

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.