Numpy: adding a field to a structured array fails in Python Toolbox, but not in IDE, or Python window

11545
10
05-29-2015 12:13 AM
NathanielRoth
New Contributor III

I've got a script that I'm trying to get into a python toolbox for use and distribution. It adds a new column to a dataset that containing the quantile that a data field falls into (number of quantiles is an input as is the field, and an option to invert the quantile numbers).

Basically:

It extracts the needed fields (OID, and the selected field) from a feature class using arcpy.da.FeatureClassToNumPyArray.

When I try to add a new field to the the resulting structured array using the code in a Python Toolbox, I get the following error:

Traceback (most recent call last):

  File "<string>", line 133, in execute

TypeError: data type not understood

What's odd about this, is the same code differing (as far as I can see) only in externally providing the parameter values works fine in an IDE (Eclipse/PyDev using the same Python interpreter). So does cutting and pasting the lines of code into the Python window in ArcCatalog.

If I run the same section of code for creating the copy of the structured array using only the existing fields, it also works (at least that far) in the Python Toolbox.

I have tried multiple formulations of the dtype values with the same result from: <i4, int, integer

ArcGIS 10.2.2, with the default installation of python(2.7.5) and numpy (v1.7.1).

If anyone wants to take a look, my toolbox is available at: CenterForRegionalChange/QuantileCalc · GitHub

Thanks,

Nate

0 Kudos
10 Replies
isburns
New Contributor III

This may be old, but nothing has changed in ArcMap and it's still a problem there. This does not appear to be an issue in ArcGIS Pro however. I was having an identical problem doing a similar task using ExtendTable and a numpy array to add fields to an existing feature class and thank goodness for this thread because it solved my issue. Simply casting the field names in the array to string solved it.

The simplest demonstration of this is putting the code from Esri's ExtendTable documentation in the execute block of a Python Toolbox like below.

# In ArcMap, this results in "data type not understood"
# In ArcGIS Pro, this succeeds
array = numpy.array([(1, 'a', 1111.0), (2, 'b', 2222.22)],
                numpy.dtype([('idfield',numpy.int32),
                            ('textfield', '|S256'),
                            ('doublefield','<f8')]))

# In ArcMap, this succeeds
# In ArcGIS Pro, this succeeds
array = numpy.array([(1, 'a', 1111.0), (2, 'b', 2222.22)],
            numpy.dtype([(str('idfield'),numpy.int32),
                        (str('textfield'), '|S256'),
                        (str('doublefield'),'<f8')]))

 

I've also attached a Python Toolbox that uses the same code to demonstrate it.

0 Kudos