Skip to main content
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!


Gustavo Alvarez
Seasoned
Forum|alt.badge.img+2

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}")
 

Best answer by Gustavo Alvarez

Yes, thanks @Dilini Fernando, @Sofie Haug.

View original
Did this topic help you find an answer to your question?

4 replies

Sofie Haug
Seasoned Practitioner
Forum|alt.badge.img+7
  • Cognite Academy Instructor
  • 157 replies
  • June 28, 2024

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, 


Sofie Haug
Seasoned Practitioner
Forum|alt.badge.img+7
  • Cognite Academy Instructor
  • 157 replies
  • June 28, 2024

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, 


Dilini Fernando
Seasoned Practitioner
Forum|alt.badge.img+2

Hi @Gustavo Alvarez ,

Have you been able to run the code?


Gustavo Alvarez
Seasoned
Forum|alt.badge.img+2

Reply


Cookie Policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie Settings