I cannot understand how to use the values in the arrays created with arcpy.FeaturesToNumPyArray.

I want to create an array and then convert them to a pandas DataFrame. So I created an array from FeaturesToNumPyArray and tried to extract "SHAPE@XY" values. But the problem is the values are stored in some kind of 2 dimensional arrays. I thought it would be easy to split them vertically with numpy.vsplit but Python tells that the array is not a 2 dimensional.

If possible I would like to covert all table values into a DataFrame, and then use for some calculations because I think that would be easy to calculate and easy to see the codes afterwards.

The codes are like this. The FeatureClass I used here are just points with X and Y coordinates plus few additional information. Points will be extracted via Python Script tool (ArcToolBox).

import arcpy
import numpy as np
import pandas as pd
shp = inPointShape
arr = arcpy.da.FeatureClassToNumPyArray(shp, ["SHAPE@XY"])
df = pd.DataFrame(arr, columns=["X","Y"])

This doesn't cause an error but no values are passed.

However, when I print the array (the SHAPE@XY) it shows as below.

[([141.20304792, 39.71085087],) ([141.03026594, 39.69363465],) ([141.7986943 , 39.29263103],) ([140.95115842, 39.23922211],)]

By avoiding "SHAPE@XY" and converting to DataFrame works fine as shown below. But I want to do this by including "SHAPE@XY" as well.

fieldNames = [f.name for f in arcpy.ListFields(shp)]
df = pd.DataFrame()
for i, item in enumerate(fieldNames):
    if item.upper() == "SHAPE":
    df[str(item)] = arr[str(item)]

Is there anyway to extract "SHAPE@XY"?

  • 1
    Additional note for study. I just realized that I should have used arr = arcpy.da.FeatureClassToNumPyArray(shp, ["SHAPE@X", "SHAPE@Y"]) in the first place. Then I just convert it to df=pd.DataFrame(arr). In the beginning I wrote as (shp,["SHAPE@X"],["SHAPE@Y"]) for function argument, and this made me confuse. It was a simple mistake. However, extracting "SHAPE@XY" would be done by BERA's answer. Thank you very much for the help. – Tim Feb 6 at 8:37

You dont need the numpy Array, use pd.DataFrame.from_records and the da.SearchCursor:

import arcpy
import pandas as pd

fc = r'C:\Default.gdb\adress1'
fieldlist = ['SHAPE@XY']

df = pd.DataFrame.from_records(data=arcpy.da.SearchCursor(fc,fieldlist), columns=fieldlist)
df[['X','Y']] = df['SHAPE@XY'].apply(pd.Series) #Split XY tuple into separate columns

Or use SHAPE@X, SHAPE@Y and you dont need to split the SHAPE@XY tuple

  • 1
    Thank you very much for the reply and quick response. After writing this question I saw an article about SearchCursor and it seems it works better than the FeatureClassToNumPyArray. Changing the fieldlist to SHAPE@X and SHAPE@Y seem to separate the columns, and works fine as I wanted. However, this helped me a lot. Thanks again. – Tim Feb 6 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.