Setting DateOnly Field back to NULL

305
5
03-18-2024 03:48 PM
Labels (1)
ccowin_odfw
Occasional Contributor

Hello,

I'm a little stuck, I'm trying to create an internal interface for a web map so that non-gis users can set alerts for a specific area, mostly for wildfires. The main data is a hosted feature so have some code that does a query based on the objectid of the row. There are two date only fields that I can't get to reset to null, I've tried setting it to None and 'NULL' which are both converted to '' in the JSON. I can't find anything in the documentation about it either.

Saw this post: https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/unable-to-set-date-field-to-nu..., however they went from a python dictionary and converted the whole thing to JSON. I'm just copying the existing JSON and then inserting the value from a dataframe, how can I insert a JSON NULL as NULL instead of 'NULL'?


My update code:

def post_to_agol(rows_to_update, object_to_update, out_fields, query_field):
    # Rows_to_update: dataframe with one row
    # object_to_update: name of the object in the layer, have a couple other 
    # tables that I update too and use this function for that too
    # out_fields: fields to get in the query, doesnt like fields with raw 
    # HTML in it so gotta kick those out
    # query_field: ObjectID for one row and a group for another that will all 
    # then be given the same data.

    object_id = rows_to_update['subdivision'][0]
    where = "{} = '{}'".format(query_field, object_id)

    agol_fields = list(out_fields.keys())
    area = object_to_update.query(where=where, out_fields=agol_fields)
    features_for_update = []

    for subdivision in area:
        feature_to_be_updated = deepcopy(subdivision)

        for agol_field, sql_field in out_fields.items():
            feature_to_be_updated.attributes[agol_field] = rows_to_update[sql_field].iloc[0]

        features_for_update.append(feature_to_be_updated)

    object_to_update.edit_features(updates=features_for_update)

 Error:

Exception at /alert/1
Cannot perform operation. Invalid operation parameters.
'updates' parameter is invalid
Invalid esriFieldTypeDateOnly value .
(Error Code: 400)
0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

A quick field calculator demo using numpy's NaT (not a time), pandas has one as well

datetime.png

 


... sort of retired...
0 Kudos
ccowin_odfw
Occasional Contributor

Well I guess you answered my question, that does get NaT in the proper place:

{'attributes': {'OBJECTID': '1',
                'alert_enabled': '0',
                'alert_end': NaT,
                'alert_start': NaT,
                'alert_text': '',
                'alert_title': 'None'},
 'geometry': {'rings': [long list of points here that is not relevant],
              'spatialReference': {'latestWkid': 2992, 'wkid': 2992}}}

 

However now I'm getting a new error:

ValueError at /alert/1
cannot convert float NaN to integer

 

So it seems like the arcgis api for python is attempting to convert it to an epoch time and that obviously can't be done from a NaN or NaT... 

0 Kudos
DanPatterson
MVP Esteemed Contributor

There is no integer none-type like floats or time.  you have to use an old-school integer sentinel like -999


... sort of retired...
0 Kudos
ccowin_odfw
Occasional Contributor

Maybe I'm misunderstanding you, but when using -999 it doesn't give an error but it also doesn't overwrite the data.

0 Kudos
DanPatterson
MVP Esteemed Contributor

The point was... no integer none-type ... in order to flag that there is nodata/null/nan/nat in a cell for integers in code, you would have to use the "old" method of using a number that couldn't possibly exist in it. -999 was just an example.  For 64 bit integer fields I would use  2^63-1(9,223,372,036,854,775,807).  Personally, I don't permit nulls in my data fields, using appropriate "sentinels" that will quickly denote something is amiss should I try to perform calculations using the data in the field.

But, I digress,  your 'alert ' requirements need something, and if -999 won't overwrite it, then you have to find something permissable that does


... sort of retired...
0 Kudos