Pandas: Nan handling with eval

Created on 23 Feb 2015  路  2Comments  路  Source: pandas-dev/pandas

For a df I'd like to handle nan's with the query functionality. df.query('~A.isnull() & ...') doesn't work because eval doesn't support function calls ("NotImplementedError: 'Call' nodes are not implemented"), and df('A != @nan') fails in the way you'd expect it to with nan comparison (that is, not filtering anything). Should we (or do we already) have a way to filter nulls with query()?

Also, I can open a separate issue if it deserves one, but this related error message seemed a bit funny

df.query('A != @np.nan')  # => NotImplementedError: N-dimensional objects, where N > 2, are not supported with eval
Error Reporting Missing-data Usage Question

Most helpful comment

The standard trick to identify NaN is to use the fact that NaN is the only number x such that x != x. So something like df.query('A != A & ...') should work.

All 2 comments

The standard trick to identify NaN is to use the fact that NaN is the only number x such that x != x. So something like df.query('A != A & ...') should work.

Ah very cool! Looks like that'll be sufficient, thanks.

Was this page helpful?
0 / 5 - 0 ratings