Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

AttributeError Traceback (most recent call last)…

  1. 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'

  2. Assistant

    The error message you're encountering indicates that the ExprStringNameSpace object in your code does not have a method called json_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 inparams is a JSON string column in your DataFrame and you want to extract the value for the key risk_estimation_info.area_reliability, you may want to use the json module directly. However, as of my last update, you cannot directly extract data from JSON strings in Polars using a json_extract method. 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:

    1. Make sure that your JSON data is in a string format.
    2. 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 apply with a lambda function that parses the JSON string using json.loads and extracts the desired field.
    • This will create a new column called area_reliability containing the extracted values.

    Make sure you adjust the regex pattern or extraction logic depending on the structure of your JSON.