How to Handle Exceptions With ast.literal_eval in a Pandas
To handle exceptions and use ast.literal_eval
in Pandas we can define new function:
import ast
import pandas as pd
def parse_eval(value):
try:
return ast.literal_eval(value)
except (ValueError, SyntaxError):
return value
df = pd.DataFrame({'col': ['1', '2', '{"a": 3}', '[[']})
df['col'].apply(parse_eval)
result:
0 1
1 2
2 {'a': 3}
3 [[
Name: col, dtype: object
Otherwise error will be raised:
df['col'].apply(ast.literal_eval)
The error is:
SyntaxError: unexpected EOF while parsing
ast.literal_eval + exception
To use ast.literal_eval
in Pandas we need the .apply()
method. This will apply the ast.literal_eval
function to each value of a column in a Pandas DataFrame.
To handle exceptions in Python we use the try-except
block - to surround the ast.literal_eval
call. Finally we return a default value in the except block in case of error.
In the example above, the parse_eval function takes a string value and:
- returns parsed value from using
ast.literal_eval
- when possible - returns the original string - if an exception occurs during the call to
ast.literal_eval
The apply
method applies the parse_eval
function to each element of the 'col' column in the DataFrame df.