Solved

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!

  • 26 June 2024
  • 4 replies
  • 34 views

Userlevel 2
Badge

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=[country_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}")
 

icon

Best answer by Gustavo Alvarez 9 July 2024, 19:41

View original

4 replies

Userlevel 3
Badge +6

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, 

Userlevel 3
Badge +6

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, 

Userlevel 4
Badge +2

Hi @Gustavo Alvarez ,

Have you been able to run the code?

Userlevel 2
Badge

Yes, thanks @Dilini Fernando, @Sofie Haug.

Reply