Esri / arcgis-python-api

Documentation and samples for ArcGIS API for Python
https://developers.arcgis.com/python/
Apache License 2.0
1.89k stars 1.1k forks source link

df.spatial.to_table error with update to ArcGIS Pro 3.2 #1727

Closed knewellcob closed 11 months ago

knewellcob commented 11 months ago

Describe the bug I have a python script that worked perfectly last week...before I upgraded from ArcGIS Pro 2.9.5 to 3.2 (see below script). The script is run within the python window in Pro. When I re-ran this script this week, after updating to Pro 3.2, the script no longer works. I was able to test it on a workstation that was still on 2.9.5, and it works! So that is pretty clear to me that something has changed from the python/panda libraries between 2.9.5 and 3.2. The error I am getting happens on the last line of the script when I try to write the dataframe to a table. Yes, I know the table does not have any geometry, but this worked in the past and created a fgdb table perfectly. To Reproduce Steps to reproduce the behavior:

import arcpy
arcpy.env.workspace = "N:\ArcShared\Planning\CompPlanUpdate_2025\Data\BellinghamProfile\BhamDataBook_2022.gdb" 
arcpy.env.overwriteOutput = True

from arcgis import GIS
import pandas as pd
import requests
import json

response = requests.get('https://api.census.gov/data/2022/acs/acs5/profile?get=NAME,DP05_0001E,DP05_0002E,DP05_0003E,DP05_0005E,DP05_0006E,DP05_0007E,DP05_0008E,DP05_0009E,DP05_0010E,DP05_0011E,DP05_0012E,DP05_0013E,DP05_0014E,DP05_0015E,DP05_0016E,DP05_0017E,DP05_0018E,DP05_0073E,DP05_0079E,DP05_0080E,DP05_0081E,DP05_0082E,DP05_0083E,DP05_0084E,DP05_0085E,DP05_0019E,DP05_0024E&for=place:05280&in=state:53')

df = pd.DataFrame(response.json()[1:])

cols = response.json()[0]
column_dict = {}
for c in cols:
    column_dict.update({cols.index(c):c})

df.rename(columns=column_dict, inplace=True)
df = df.astype({"NAME": str, "DP05_0001E": int, "DP05_0002E": int, "DP05_0003E": int, "DP05_0005E": int, "DP05_0006E": int, "DP05_0007E": int, "DP05_0008E": int, "DP05_0009E": int, "DP05_0010E": int, "DP05_0011E": int, "DP05_0012E": int, "DP05_0013E": int, "DP05_0014E": int, "DP05_0015E": int, "DP05_0016E": int, "DP05_0017E": int, "DP05_0018E": float,"DP05_0073E": int,"DP05_0079E": int,"DP05_0080E": int,"DP05_0081E": int,"DP05_0082E": int,"DP05_0083E": int,"DP05_0084E": int,"DP05_0085E": int,"DP05_0019E":int,"DP05_0024E":int})

processed_table = df.spatial.to_table('DP05_Pop_Age_Sex_Race')

error:

Traceback (most recent call last):
  File "<string>", line 22, in <module>
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_accessor.py", line 2718, in to_table
    table = run_and_hide(
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_tools\_utils.py", line 30, in run_and_hide
    raise err
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_tools\_utils.py", line 23, in run_and_hide
    res = fn(**kwargs)
  File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\geo\_io\fileops.py", line 630, in to_table
    with arcpy.da.InsertCursor(fc, icols) as irows:
TypeError: 'field_names' must be string or non empty list or tuple of strings

Expected behavior This script worked with no errors before updating to ArcGIS Pro 3.2. The script creates a table with the file geodatabase.

Platform (please complete the following information):

achapkowski commented 11 months ago

@knewellcob what version of the arcgis library are you using?

import arcgis
arcgis.__version__

Next,

try changing processed_table = df.spatial.to_table('DP05_Pop_Age_Sex_Race') to processed_table = df.spatial.to_table(os.path.join(arcpy.env.workspace, 'DP05_Pop_Age_Sex_Race'))

Let me know what happens.

knewellcob commented 11 months ago

version 2.2.0

That change did seem to do the trick! However, with that change my field names all got changed from the original and so subsequent calls and calculations are failing because of field name changes (I did not post the full script which performs field calculations after creating the fgdb table). For example "DP05_0001E" changed to "dp05_0001_e" and so on...Is there and obvious reason why the field name would be changed? It consistently changed all to lower case and changed the "E" at the end of each field name to "_e".


From: Andrew @.> Sent: Wednesday, December 13, 2023 3:00 AM To: Esri/arcgis-python-api @.> Cc: Newell, Kate @.>; Mention @.> Subject: Re: [Esri/arcgis-python-api] df.spatial.to_table error with update to ArcGIS Pro 3.2 (Issue #1727)

You don't often get email from @.*** Learn why this is importanthttps://aka.ms/LearnAboutSenderIdentification

@knewellcobhttps://github.com/knewellcob what version of the arcgis library are you using?

import arcgis arcgis.version

Next,

try changing processed_table = df.spatial.to_table('DP05_Pop_Age_Sex_Race') to processed_table = df.spatial.to_table(os.path.join(arcpy.env.workspace, 'DP05_Pop_Age_Sex_Race'))

Let me know what happens.

— Reply to this email directly, view it on GitHubhttps://github.com/Esri/arcgis-python-api/issues/1727#issuecomment-1853699917, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ANJ2HJUGV4M3FXVSRBDNTNDYJGDDFAVCNFSM6AAAAABASD37BWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNJTGY4TSOJRG4. You are receiving this because you were mentioned.Message ID: @.***>

achapkowski commented 11 months ago

@knewellcob the fields get sanitized . You can set that property to False to not do it.