AttributeError Traceback (most recent call last)…
- User
AttributeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_19600/3121434125.py in 1 print(datetime.now()) 2 df1.with_columns( ----> 3 pl.col("inparams").str.json_extract("risk_estimation_info.area_reliability").alias("area_reliability"), 4 ) 5 print(datetime.now())
AttributeError: 'ExprStringNameSpace' object has no attribute 'json_extract'
- Assistant
The error message you're encountering indicates that the
ExprStringNameSpaceobject in your code does not have a method calledjson_extract. This typically happens when you are trying to use a method that does not exist in the library or the specific object you're working with.Based on the context, it seems like you might be using the Polars library in Python. In Polars, the way to extract JSON data from a string column may be different than using methods available in other libraries like Pandas.
If
inparamsis a JSON string column in your DataFrame and you want to extract the value for the keyrisk_estimation_info.area_reliability, you may want to use thejsonmodule directly. However, as of my last update, you cannot directly extract data from JSON strings in Polars using ajson_extractmethod. Instead, you might look for the relevant string processing functions or transformations to achieve this.Here’s a suggestion on how you can extract JSON data using Polars:
- Make sure that your JSON data is in a string format.
- Use the
pl.col().str.extract()method coupled with a regex pattern if applicable, or convert the string to a dictionary.
Here’s an example of how you might approach it:
import polars as pl import json # Example DataFrame creation data = { "inparams": ['{"risk_estimation_info": {"area_reliability": 0.92}}', '{"risk_estimation_info": {"area_reliability": 0.85}}'] } df1 = pl.DataFrame(data) # Assuming 'inparams' contains JSON strings, we need to extract 'area_reliability' df1 = df1.with_columns( pl.col("inparams").apply(lambda x: json.loads(x)["risk_estimation_info"]["area_reliability"]).alias("area_reliability") ) print(df1)In this code:
- Replace
applywith a lambda function that parses the JSON string usingjson.loadsand extracts the desired field. - This will create a new column called
area_reliabilitycontaining the extracted values.
Make sure you adjust the regex pattern or extraction logic depending on the structure of your JSON.