pythondataframeindex-error

recursive DataFrame query


I'm writing small program to utilize data from API endpoint. As I need to solve typical problem of parent-child recursive relation I wrote function as below:

def get_client_path(client_id, _df: pd.DataFrame):
client_full_path = str(client_id)
current_id = client_id
while True:
    parent_record = _df.loc[_df['id'] == current_id]
    if math.isnan(parent_record['data.parentClient'].iloc[0]):
        break
    parent_id = int(parent_record['data.parentClient'].iloc[0])
    current_id = parent_id
    ic(current_id)
    client_full_path = f"{parent_id} - " + client_full_path

What drives me crazy is that the code throws an error: line 1686, in _validate_integer raise IndexError("single positional indexer is out-of-bounds") in line math.isnan(parent_record['data.parentClient'].iloc[0]):

Strange thing is that the value of parent_record['data.parentClient'].iloc[0] can be printed out without any errors and shows proper value. It's 64bit float.

Why I need that line? Because API endpoint returns "null" in case no parent is available.

What am I missing here?


Solution

  • The error message is typical of .iloc when you are trying to slice a dataframe at a point it doesn't exist.

    It's not 100% clear from your post what the cause of the error might be, but as you're slicing at the 0th index, it's likely that parent_record['data.parentClient'] returns an empty dataframe under certain conditions.