3

I'm quite new to Python and ArcGIS API for Python. I'm trying to create a report of all the items owned by one of my ArcGIS online organization user. For each item owned by the user I want to record some item info in a Pandas dataframe, then export a CSV as a report.

# Import the modules
import os
import pandas as pd
from arcgis.gis import GIS
# Login to arcgis online 
gis = GIS(url='https://myorganization.com',
 username= 'myUsername')
# Define the user 
user = 'UserToCheck'
# Search for contents owned by a specific user
all_contents = gis.content.search(query="owner:" + user,
 max_items=3000
 )
# Check the number of contents
len(all_contents)
# Define the columns title to insert in the dataframe we want to export 
title = 'TestTitle',
description = 'testDescription'
item_id = 'TestID'
item_type = 'TestItemType'
date_creation_unix = int("1415400560000")
date_modified_unix = int("1415969577000")
url = 'TestUrl'
usage = int("142348")
# Define the columns
columns = {'content title': title,
 'description': description,
 'id' : item_id,
 'item_type' : item_type,
 'date_creation_UNIX' : date_creation_unix, 
 'date_modified_UNIX' : date_modified_unix,
 'url' : url,
 'usage_last_year': usage
 }
columns
# Create the dataframe using the columns defined in the previous step
df = pd.DataFrame(data=columns)
# print the dataframe 
print(df)
x = 0
for content in all_contents: 
 df_usage = content.usage('1Y') # create a report dataframe about last year (it include 2 columns, "Day" and "Usage". I'm only interested about the "Usage" and i need to sum this value along all the year)
 sum_usage = sum(df_usage["Usage"]) # store only the sum of "Usage" column 
 to_append = (content.title, 
 content.description, 
 content.id, 
 content.type, 
 content.created, 
 content.modified, 
 content.url,
 sum_usage
 )
 
 # append "to_append" as a row to the dataframe 
 df_lenght = len(df)
 df.loc[df_lenght] = to_append
 x = x + 1
 print (x, " raw added to the dataframe") 

it seems working well, but sometimes I have the following error:

IndexError Traceback (most recent call last)
In [24]:
Line 5: df_usage = content.usage('1Y') # create a report dataframe about last year (it include a row per day)
File C:\Users\Me\AppData\Local\Programs\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\arcgis\gis\__init__.py, in usage:
Line 9060: if not self.layers[0].container:
IndexError: list index out of range
---------------------------------------------------------------------------

I noticed that the first time the error refers to a certain row (120), and the first 119 rows were well recorded in the dataframe. The second time, using the same script, happened with another row (somewhere around 200)

Why do I have this error and how could I fix it?

Hornbydd
44.9k5 gold badges43 silver badges84 bronze badges
asked Jan 29, 2021 at 16:28
1
  • Did you find a resolution to this? Having a very similar error on using item.usage('7D') Commented May 10, 2021 at 11:13

1 Answer 1

0

I would suggest adding try/except statements to highlight which items the usage method is failing on. Maybe certain items do not contain this information and hence the index error.

try:
 df_usage = content.usage('1Y')
 sum_usage = sum(df_usage["Usage"])
except IndexError:
 sum_usage = -9999
 print("Error with {0}".format(content.id))

This will allow the code to continue through each item (unless a different error is present other than the index error) and add a -9999 flag in the usage_last_year column.

answered Jul 4, 2022 at 8:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.