0

I am attempting to pull data from an api into an empty list and convert it to a dataframe. I have used a try and except for cases where the api would return a null value as well, but still keep getting this error. This is my code: (I have written a function called get_data to pull in my required datapoints from the API:

d = []

base_url = "api url inserted here"
for i in range(len(df)):
    try: 
        address = base_url + df.loc[i,"column_for_api_call"] 
        d.append(get_data(address))
    except (IndexError, TypeError) as error: 
        print('ERROR at index {}: {!r}'.format(i, address))
        d.append(["  "])
lat = pd.DataFrame(d)

But keep getting the error TypeError: object of type 'NoneType' has no len() after the entire code runs. What am I missing?

  • The `try` block does not include the line where you call `len`... – DeepSpace Feb 10 '22 at 15:01
  • Well, if I put the ```try``` block before the for loop, then the code will break each time there's an error, rather than just returning an empty string and continuing to the next row, which is what I want. – Aamash Haroon Feb 10 '22 at 15:16
  • There is no "next row" if `df`is `None`... – DeepSpace Feb 10 '22 at 15:18
  • Sorry I need to clarify this- df is my input dataset that contains values I would like to get api calls for. It contains the column "column_for_api_call", which is a non-NULL column – Aamash Haroon Feb 10 '22 at 15:19

1 Answers1

0

df is None so your for-loop never gets entered. You should specify what should happen in that case:

if df is None:
    pass  # FIXME: what should happen here?
else:
    for i in range(len(df):
        address = base_url + df.loc[i,"column_for_api_call"] 
        d.append(get_data(address))
Jasmijn
  • 9,370
  • 2
  • 29
  • 43
  • Sorry- df is my input dataset that contains values I would like to get api calls for. It contains the column "column_for_api_call", which is a non-NULL column. – Aamash Haroon Feb 10 '22 at 15:05
  • If this is not the correct solution, please edit your question so it is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). See also [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples/20159305#20159305). – Jasmijn Feb 10 '22 at 15:09