user8270077
user8270077

Reputation: 5071

How to insert an isna() clause in a Pandas query

I would like to create an isna() clause using the .query() method in Pandas

I am getting an error though.

For a reproducible example:

import pandas as pd
import seaborn as sns

mpg = sns.load_dataset('mpg')

mpg[mpg['cylinders'].isna()] # This works

mpg.query('cylinders.isna()') # This raises an exception
TypeError: 'Series' objects are mutable, thus they cannot be hashed

Upvotes: 3

Views: 1995

Answers (1)

jezrael
jezrael

Reputation: 862831

Use parameter engine='python' for change default engine='numexpr':

print(mpg.query('cylinders.isna()', engine='python'))

Sample:

mpg = pd.DataFrame({'cylinders':['a', np.nan]})
print(mpg)
  cylinders
0         a
1       NaN

print(mpg.query('cylinders.isna()', engine='python'))
  cylinders
1       NaN

More information about query is in Dynamic Expression Evaluation in pandas using pd.eval().

Upvotes: 7

Related Questions