Skip to main content

At the "Solution - Hands On Exercise" lab, in the point 5 "Adding events", I have an issue running the code.
I have modified the code in order to see what error there in the script and the message say: "An error occurred while creating events: Out of range float values are not JSON compliant. Make sure your data does not contain NaN(s) or +/- Inf!"
Please your guidance for this.

 

from cognite.client.data_classes import Event

store = ]
# Cycle through each row
for _,row in df.iterrows():

    # Unpack the row
    _,_,_,Disaster_Type,Disaster_Subtype,_,Country,_,_,_,Location,_,Start_Year,Start_Month,Start_Day,End_Year,End_Month,End_Day = row

    # Create start and end datetimes
    start_date = pd.to_datetime(f'{Start_Year}-{Start_Month}-{int(Start_Day)}')
    end_date = pd.to_datetime(f'{End_Year}-{End_Month}-{int(End_Day)}')

    # Create metadata
    metadata = {'Location':Location}

    # Check for NaN or infinite values in start and end times and replace with None if found
    start_time = int(start_date.timestamp()*1000) if pd.notna(start_date) else None
    end_time = int(end_date.timestamp()*1000) if pd.notna(end_date) else None

    # Create the event and store
    event = Event(start_time=int(start_date.timestamp()*1000), end_time=int(end_date.timestamp()*1000),metadata=metadata,data_set_id=data_set_id, type=Disaster_Type, subtype=Disaster_Subtype, asset_ids=acountry_to_id Country]])
    store.append(event)

# Attempt to create events, handling potential API errors
try:
    client.events.create(store)
    print("Events created successfully!")
except Exception as e:
    print(f"An error occurred while creating events: {e}")
 

Hi @Gustavo Alvarez 

I would need a bit more info to be able to help you! Which course are you currently working on? (you can send us the url for example)
And which CDF project are you working in? 

Best Regards, 


Can you try to clear the memory and try again as well, I just ran the code and I it ran with no issues. 

 

Best Regards, 


Hi @Gustavo Alvarez ,

Have you been able to run the code?


Yes, thanks @Dilini Fernando, @Sofie Haug.


Reply