2

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?

1
  • Did you find a resolution to this? Having a very similar error on using item.usage('7D')
    – jakc
    May 10 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.