KeyError(f"None of [{key}] are in the [{axis_name}]") after upgrading from python 3.6 to 3.8

Viewed 8295

I just moved to a new MacBook Pro, installed python 3.8 and pip installed the packages I had in old MBP. I was running python 3.6 in old MBP. I am now getting an error in a python script and I believe the only difference is python 3.8 (instead of python 3.6)

error is ... KeyError(f"None of [{key}] are in the [{axis_name}]")

The last line in code below to define the plot is the one that is being called out for the error. Attached screenshot shows the output of the print statements and the error.

Any help is appreciated as I'm running into a wall now. TIA.

print("Index")
print(df_transpose_deaths.index)

print("Columns")
print(df_transpose_deaths.columns)

print("df transpose deaths")
print(df_transpose_deaths)

xindex = df_transpose_deaths.index
ycolumns = df_transpose_deaths.columns
deaths_world_plot = df_transpose_deaths.plot(x = xindex, y = ycolumns, kind='line', xticks=x_ticks, legend=False)

screenshot of terminal window with error message

1 Answers

I was able to implement a fix to have this script work again on my new MBP and with python 3.8 (was using python 3.6 on old MBP).

What my plot statement is doing is taking the df and using the df index as the values for 'x' and then the rest of the columns are the lines to be plotted along 'y'. I surmised that the error is saying that the index values are not found as part of the columns. So I created a new column with a new name and made the values of the index the values of the new column. I then used this new column as the 'x' in the plot statement. For the 'y' in the plot statement, I placed the column names in a list and used that for the 'y'. So I assume something changed between python 3.6 and python 3.8 in terms of being able to reference .index and .columns values within the plot statement.

New code that works.

ycolumns = df_transpose_deaths.columns
df_transpose_deaths['MYDATE'] = df_transpose_deaths.index
deaths_world_plot = df_transpose_deaths.plot(x = 'MYDATE', y = ycolumns, kind='line', xticks=x_ticks, legend=False)
Related