Skip to content

Commit

Permalink
Merge pull request #97 from mdeegen/patch-1
Browse files Browse the repository at this point in the history
Update pandas_utils.py for column names with line breaks
  • Loading branch information
boeddeker authored Feb 28, 2024
2 parents ffb777c + a6fef74 commit 1cb98d6
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions paderbox/utils/pandas_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def py_query(
To access column names that aren't valid python identifiers (e.g. the name
contains a whitespace), you have to use the kwargs dictionary:
>>> df = pd.DataFrame([{'a b': 1, 'b': 2}, {'a b': 3, 'b': 4}])
>>> py_query(df, 'kwargs["a b"] == 1')
a b b
0 1 2
>>> df = pd.DataFrame([{'a b': 1, 'b\\nc': 2}, {'a b': 3, 'b\\nc': 4}])
>>> py_query(df, 'kwargs["a b"] == 1 and kwargs["b\\\\nc"] == 2')
a b b\\nc
0 1 2
When you need a package function, you have to specify it in the globals
dict. e.g.:
Expand Down Expand Up @@ -103,8 +103,10 @@ def py_query(

keywords = ['index'] + list(data)

def is_valid_variable_name(name):
def is_valid_variable_name(name: str):
import ast
if len(name.splitlines()) != 1:
return False
# https://stackoverflow.com/a/36331242/5766934
try:
ast.parse('{} = None'.format(name))
Expand Down

0 comments on commit 1cb98d6

Please sign in to comment.