I am using dictionary comprehension to compare each dataframe from within a dictionary to its corresponding dataframe in locals(). For some reason, I am getting a key error when accessing the locals() dataframe from within the dictionary comprehension expression.
Here is the setup:
imort numpy as np
imort pandas as pd
df=pd.DataFrame(np.random.randint(3,size=[2,2]),columns=['A','B'])
# A B
# 0 1 2
# 1 2 0
# Create dictionary of datafame(s) (only 1 here)
OldDFs={'df':df}
Here is the failure when my dictionary comprehension expression refers to a locals()
dataframe:
#####################################################
# This is the "key test expression"
#####################################################
{ ky:locals()[ky] for (ky,OldDF1) in OldDFs.items() }
# Console message: KeyError 'df'
The key ky
is of the correct type:
{ ky:type(ky) for (ky,OldDF1) in OldDFs.items() } # {'df': str}
It works outside of dictionary comprehension:
locals()['df'] # Prints "df"
The dictionary comprehension expression works if I refer to a dictionary other than locals()
:
{ ky:OldDFs[ky] for (ky,OldDF1) in OldDFs.items() } # Prints "df"
What is wrong with the “key test expression” above?
Background: In the actual situation, OldDFs consists of many dataframes read from a pickle file. The actual test I want to perform is
{ ky:OldDF1.equals(locals()[ky])
for (ky,OldDF1) in OldDFs.items() }
0
This Q&A strongly suggests that locals()
within a comprehension expression contains only the variables within the scope of the comprehension expression. The following confirms this by explicitly showing that locals()
contains only the variables used in the comprehension expression:
>>> { ky:locals() for (ky,OldDF1) in OldDFs.items() }
{'df': {'.0': <dict_itemiterator at 0x1ce48fbf040>,
'ky': 'df',
'OldDF1': A B
0 2 0
1 1 2}}
The numbers within df
differ from those in the orignally
posted question because of the random generation.
The Q&A also provides work-arounds.