Boolean series key will be reindexed to match dataframe index

905    Asked by AnneBell in Devops , Asked on Jul 1, 2021

Here is how I encountered the error:

df.loc[a_list][df.a_col.isnull()]

The type of a_list is Int64Index, it contains a list of row indexes. All of these row indexes belong to df.

The df.a_col.isnull() part is a condition I need for filtering.

If I execute the following commands individually, I do not get any warnings:

df.loc[a_list]
df[df.a_col.isnull()]

But if I put them together df.loc[a_list][df.a_col.isnull()], I get the warning message (but I can see the result):

Boolean Series key will be reindexed to match DataFrame index

What is the meaning of this error message “userwarning: boolean series key will be reindexed to match dataframe index”? Does it affect the result that is returned?

Your approach will work but notwithstanding the warning, but it's best not to rely on implicit, unclear action.

Solution for this is to make the selection of indices in a_list a boolean mask:

    df[df.index.isin(a_list) & df.a_col.isnull()]


Your Answer

Interviews

Parent Categories