Esri / arcgis-python-api

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

arcgis.features module for tables and edit_features issues #1577

Closed MSLTrebesch closed 1 year ago

MSLTrebesch commented 1 year ago

Describe the bug I'm using edit_features to add records from a Pandas dataframe (not spatially enabled) to add to a table in a hosted feature service.

I have typically been able to follow this workflow, but am now getting errors when this code runs.

To Reproduce Steps to reproduce the behavior:

result_deleted is a Pandas dataframe (non-spatial)

##append deleted users to hosted table
AGOL_item_deleted_users = gis.content.get(AGOL_feature_service_itemID_Deleted_Users)
tableAGOL_deleted_users = AGOL_item_deleted_users.tables[0]

try:
    print("updating deleted users table")
    AGOLTableDeletedUsers = tableAGOL_deleted_users.edit_features(adds= result_deleted)
    print("Success")
except Exception as e:
    print("Unable to add deleted user to table")
    print(e)

error:

pdating deleted users table
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\features\layer.py:3273: FutureWarning: Using short name for 'orient' is deprecated. Only the options: ('dict', list, 'series', 'split', 'records', 'index') will be used in a future version. Use one of the above to silence this warning.
  [{"attributes": row} for row in adds[cols].to_dict(orient="record")],
Unable to add deleted user to table
Circular reference detected

Expected behavior using edit_features on a table when appending records from a Pandas dataframe had worked in the past, with previous API versions

The "future warning" error was given, but the data updated.
The Circular Reference Detected error is new.

Platform (please complete the following information):

nanaeaubry commented 1 year ago

Hello thanks for reporting this, there seems to be a typo in the code causing this issue. It has been fixed for the next release

MSLTrebesch commented 1 year ago

@nanaeaubry Thanks. Will this be for the next major release or the next patch release? I ask because I need to know how long to wait until I develop a work around. Any recommendations for a work around? I suppose use append? Or is there a better way to export my Pandas dataframe out to another format to input it into edit_features?

Also, the persisting "future warning error" that has always been present even before this bug was filed - is there an issue with my code that causes that, or is that just part of how the API is behaving?

Thanks!

nanaeaubry commented 1 year ago

@MSLTrebesch This will go into a beta 2.2.0 release that comes out in beginning of July.

For a workaround you could transform your dataframe into a list of features to pass in instead of the dataframe so it skips that loop. For the future warning I am unsure... that is a weird one. I will look into it and post here again.

nanaeaubry commented 1 year ago

@MSLTrebesch So the future warning is because the orient parameter is being set in the code and we had to update that. If you continue to get it after upgrading to the next version when it is released, let us know so we can see why. I am not getting it on my end but I did get a full on error that the orient was wrong. This might be do to dependency upgrades that make it that production is already at the next version where it wasn't a warning anymore but an error now. Thank you for bringing it up to up so we could fix it!

MSLTrebesch commented 1 year ago

@MSLTrebesch This will go into a beta 2.2.0 release that comes out in beginning of July.

For a workaround you could transform your dataframe into a list of features to pass in instead of the dataframe so it skips that loop. For the future warning I am unsure... that is a weird one. I will look into it and post here again.

@nanaeaubry Thanks for the tip. So, for the dataframe to list (no features, just rows from a dataframe) What does the structure of the list have to look like? Do I need to include a list of column names?
(I'm supposing so, otherwise, how would edit_features know what to do with the items in the list.)

Like this?
dataframe df:

 Name  Age

0 Tony 35 1 Steve 70 2 Bruce 45 3 Peter 20

import pandas as pd

# Converting dataframe to list
li = [df.columns.values.tolist()] + df.values.tolist()

# Printing list
print(li)

Output:

[[‘Name’, ‘Age’], [‘Tony’, 35], [‘Steve’, 70], [‘Bruce’, 45], [‘Peter’, 20]]

nanaeaubry commented 1 year ago

Sorry should have provided some documentation and examples.

Here we have a small guide showing how to use the edit_features method: https://developers.arcgis.com/python/guide/editing-features/#adding-features

You can see an example of what a feature that will have to go into the list of features will look like. We also have methods such as to_feature_collection in our GeoAccessor class. Since this is a table you can still use the .spatial namespace but you might be limited in the usability of some methods. It's worth trying though. Here is some doc for that: https://developers.arcgis.com/python/guide/introduction-to-the-spatially-enabled-dataframe/#export-options

You might have to do some data conversion to use some methods. Here is a small guide that does that: https://developers.arcgis.com/python/samples/html-table-to-pandas-data-frame-to-portal-item/