1

I have a Pandas df with Latitude and Longitude columns I am trying to export to a featureclass using ArcPy. I cannot resolve the fiona and GeoPandas issue so I tried to use MakeXYEventLayer_management and FeatureClassToFeatureclass_conversion

import pandas as pd, numpy as np, calendar
from os.path import splitext
from arcpy import MakeXYEventLayer_management, FeatureClassToFeatureClass_conversion
data = 'Sample_Data_2021.xlsx'
df = pd.read_excel(data)
MakeXYEventLayer_management(df, 'LONGITUDE', 'LATITUDE', 'data_xy', spRef)
FeatureClassToFeatureClass_conversion('data_xy', outDir, outFeatureClass)

Traceback (most recent call last):
  File ".../main.py", line 105, in <module>
MakeXYEventLayer_management(df, 'LONGITUDE', 'LATITUDE', 'data_xy', spRef)
  File "...\management.py", line 9601, in MakeXYEventLayer
raise e
  File "...\management.py", line 9598, in MakeXYEventLayer
retval = convertArcObjectToPythonObject(gp.MakeXYEventLayer_management(*gp_fixargs((table, in_x_field, in_y_field, out_layer, spatial_reference, in_z_field), True)))
  File "...\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: Error in executing tool

I tried using df.LATITUDE\df['LATITUDE'] in the XY inputs but got the same error. I could convert it to CSV but then I would loose all the dtypes I assigned. Is there a way to directly convert a Pandas df to a featureclass?

4
  • This may help you - community.esri.com/t5/arcgis-api-for-python-questions/…
    – enolan
    Dec 9, 2022 at 20:16
  • 2
    Why use a pandas df when you can go straight from Excel to feature class? Note a feature class is not a feature dataset. Feature datasets are collections of feature classes
    – user2856
    Dec 9, 2022 at 21:46
  • I expect that the reason they're avoiding using ExcelToTable_conversion() is due to the dtypes (as mentioned in the OP). ArcGIS quite often gets these wrong on importing Excel. Does the schema.ini work-around to specify data types work for Excel, or is it just CSV? I can't remember. Dec 12, 2022 at 3:50
  • The 2 reasons for going to Pandas is to force the dtypes because esri doesn't get them right and I was having difficulties doing the table edits and setting the dtypes outside of Pandas. Since Pandas comes with the ArcPy env I figured I could do my work there. Yes, I am aware of the difference between an esri featureclass and esri featuredataset; I just typed the wrong thing.
    – MrKingsley
    Dec 12, 2022 at 13:24

1 Answer 1

1

I do this using NumPyArrayToFeatureClass. You'll need to convert your df to a np array. I use this to get the dtypes right.

import arcpy
import numpy as np
import pandas as pd
import os

# list data types
list_dtypes = [i.str for i in df.dtypes]

# turn object type into 255 character string type
# turn datetimes type into M8[us] type
list_dtypes = ['<U255'  if d == '|O'      else d for d in list_dtypes]
list_dtypes = ['M8[us]' if d == '<M8[ns]' else d for d in list_dtypes]

# format as list of tuples (field name and adjusted dtype)
tup_dtypes =  [(f,t) for f,t in zip(list(df.columns.values),list_dtypes)]

# write to array, recasting the to_records array with .astype()
array = df.to_records(index=False).astype(tup_dtypes)

# save to feature class
outPath = os.path.join(outDir, outFeatureClass)
arcpy.da.NumPyArrayToFeatureClass(array, outPath, ("LONGITUDE", "LATITUDE"))

3
  • Is it still arcpy.da.NumPyArrayToFeatureClass? I treied arcpy.da, arcpy.managment, arcpy.NumPyArrayToFeatureClass_management and I keep getting the error Cannot find reference.
    – MrKingsley
    Dec 12, 2022 at 19:49
  • Did you update the output parameter? I tweaked the example a bit for you. Here's the link. pro.arcgis.com/en/pro-app/latest/arcpy/data-access/…
    – Brennan
    Dec 12, 2022 at 20:48
  • I got an error but also a shapefile that looks to be complete. Traceback (most recent call last): File ".../main.py", line 124, in <module> da.NumPyArrayToFeatureClass(array, outPath, ("LONGITUDE", "LATITUDE")) SystemError: <built-in function NumPyArrayToFeatureClass> returned NULL without setting an error
    – MrKingsley
    Dec 13, 2022 at 12:27

Your Answer

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

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