Export ArcGIS spatially enabled dataframe to feature class

04-04-2022 01:16 PM
New Contributor

I'm working to develop a tool that converts JSON data pulled from an API into a feature class to be stored in an ESRI file geodatabase. I'm currently testing the functionality in a Jupyter notebook within an ArcGIS Pro project but my goal is to eventually convert my script into a geoprocessing tool that can be used by non-developer users. However, I've gotten stuck in the prototype phase and cannot figure out if I am running into a bug or if there is an issue with my code.

Here is my code:

import requests
import arcpy
from arcgis.features import GeoAccessor, GeoSeriesAccessor
import pandas as pd

# pull public geojson data from socrata

r = requests.get('https://data.bayareametro.gov/resource/ufzj-38uk.json')
j = r.json()
df = pd.DataFrame.from_dict(j)

# Convert to Spatially Enabled Pandas Dataframe

sedf = pd.DataFrame.spatial.from_df(df, geometry_column='shape')

# Export Spatially Enabled Pandas Dataframe to FGDB Feature Class



This is the error message I received:

ValueError Traceback (most recent call last)
In [22]:
Line 5: sanitize_columns=True)

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py, in to_featureclass:
Line 2388: has_m=has_m,

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_io\fileops.py, in to_featureclass:
Line 853: df.columns = original_columns

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\generic.py, in __setattr__:
Line 5478: return object.__setattr__(self, name, value)

File pandas\_libs\properties.pyx, in pandas._libs.properties.AxisProperty.__set__:
Line 66:

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\generic.py, in _set_axis:
Line 670: self._mgr.set_axis(axis, labels)

File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\pandas\core\internals\managers.py, in set_axis:
Line 221: f"Length mismatch: Expected axis has {old_len} elements, new "

ValueError: Length mismatch: Expected axis has 42 elements, new values have 41 elements

Here is what I tried to resolve the issue:

I reviewed the GeoAccessor class method [to_featureclass](https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#arcgis.features.GeoAcces...) API reference and tried to set the path to the File Geodatabase explicitly as well as provide explicit values for the `has_z`, `haz_m`, and `sanitize_columns` parameters as shown below and got the same error message.


I seem to always run into issues when trying to perform seemingly basic things with these spatially enabled dataframes.

0 Kudos
4 Replies
MVP Esteemed Contributor

Someone else recently posted about this same (or at least a very similar) issue: https://community.esri.com/t5/arcgis-api-for-python-questions/my-agol-notebooks-are-not-working/m-p/...

Do you have any other Python envs where you could try the same code? If it doesn't actually need arcpy, there's no reason you can't run this sort of thing elsewhere.

- Josh Carlson
Kendall County GIS
New Contributor

Yeah I just took a look at that post. I do have other environments but ultimately, I'm going to turn this script into a geoprocessing tool for non-programmer ArcGIS Pro users to use to pull in layers from our Socrata data portal. Also, I'm not sure that you can write to file geodatabases without arcpy, correct? 

0 Kudos
MVP Esteemed Contributor

By default, no. If you want to write to a file geodatabase from outside of arcpy, you've got to download the File Geodatabase API and get that set up on your system.


There's a separate space for the FGDB API here on the Community, too! Not incredibly active, but you can see how other folks are using this.



- Josh Carlson
Kendall County GIS
New Contributor

Hi all, Can someone please clear me the below post mentioned:


I am little bit confused about the API.

0 Kudos