How to fix error: 'AnnAssign' nodes are not implemented in Python

Question:

I try to do following:

import pandas as pd
d = {'col1': [1, 7, 3, 6], 'col2': [3, 4, 9, 1]}
df = pd.DataFrame(data=d)

out = df.query('col1 > col2')

out=    col1  col2
1     7     4
3     6     1

This works OK. But when I modify column name col1 –> col1:suf

d = {'col1:suf': [1, 7, 3, 6], 'col2': [3, 4, 9, 1]}
    df = pd.DataFrame(data=d)
    
    out = df.query('col1:suf > col2')

I get an error:

‘AnnAssign’ nodes are not implemented

Is there easy way to avoid this behavior? Or course renaming headers etc. is a workaround

Asked By: Bugger_man

||

Answers:

The colon : is a special character in SQL queries. You need to enclose it in backticks.

Try this :

out = df.query('`col1:suf` > col2')

Output :

print(out)

   col1:suf  col2
1         7     4
3         6     1
Answered By: abokey

According to ValentinFFM’s comment on this issue, you need to put a backtick quote around your column name like

df.query('`Column: Name`==value')
Answered By: Jason Lee
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.