I have a database application that I connect to using SQLAlchemy. In the database, I store coordinates in the fields x_coord and y_coord, with a third field spatial_reference_code that stores the EPSG code of the coordinate system that x_coord and y_coord are in for the record.
I've written a generic function that takes a SQLAlchemy query object with any preexisting filters on the data table and exports it to a feature class. The function converts the SQLAlchemy object to a pandas data frame, then uses the pandas data frame (after using data_frame.as_matrix()) as the data parameter to arcpy.da.NumPyArrayToFeatureClass. When running this code, it fails in NumPyArrayToFeatureClass with the error "TypeError: narray.fields require"
Below is a minimum reproducible example, adapted from Esri's sample code, that generates the problem:
import os import arcpy import numpy import pandas out_fc = os.path.join(os.getcwd(), "test.shp") # Create a numpy array with an id field, and coordinate fields array = numpy.array([(1, -116.809, 34.271), (2, -114.241, 33.684)], numpy.dtype([('idfield',numpy.int32),('x_coord', '<f8'), ('y_coord', '<f8')])) # converting to pandas data frame to simulate conditions in my actual code - this causes the failure df = pandas.DataFrame(array) # Define a spatial reference for the output feature class sr = arcpy.SpatialReference(4269) # Export the numpy array to a feature class using the X and Y fields to # represent the output point feature arcpy.da.NumPyArrayToFeatureClass(df.as_matrix(), out_fc, ['x_coord', 'y_coord'], sr)
My code does not start with a numpy array and convert it to a data frame before converting it back, but the example does that to simulate the conditions of my code (where the data is retrieved from SQLAlchemy into a pandas data frame). If I run that sample code and pass "array" directly to NumPyArrayToFeatureClass, it works - but when I convert it to a pandas data frame and then back using df.as_matrix() - it fails.
For machine specs, this code is running in the Python interpreter provided by ArcGIS Pro 1.3 (Python 3.4.4 - in a conda environment I created from the main ArcGIS Pro environment). Pandas is version 0.17.1, numpy is version 1.9.3, and SQLAlchemy is version 1.1.2. Running on Windows 10 (10.0.14393).
I see one other reference to this issue over on StackOverflow, but that post does not resolve my problem.
Has anyone worked with this arcpy.NumPyArrayToFeatureClass and can help me debug this issue?