Esri / arcgis-python-api

Documentation and samples for ArcGIS API for Python
https://developers.arcgis.com/python/
Apache License 2.0
1.89k stars 1.1k forks source link

to_featureset() - UTC date/time fields not working #1588

Closed GeeFernando closed 1 year ago

GeeFernando commented 1 year ago

to_featureset() - UTC date/time fields not working

Describe the bug I noticed after the latest ArcGIS Python API update 2.1.0.2 in AGOL Notebooks - UTC enabled dateTime fields cannot be converted to featuresets. It was working fine with ArcGIS Python API 2.0.1.

Most of our dateTime data is in local timezones, so we first need to convert it to UTC before uploading it onto ArcGIS Online. I know that I can subtract pd.Timedelta(hours=10) to convert Canberra/Australia times to UTC, but this gets quite complex with daylight savings.

To Reproduce Steps to reproduce the behaviour:

# Relevant libraies to use 'Spatially Enabled Dataframes'
from arcgis.features import GeoAccessor, GeoSeriesAccessor

import pandas as pd

utcDF = pd.DataFrame({
    'DateTimeField': ['2023-06-13 10:30:00'],
    'stringField': ['hello world'],
    'lat': [-35.308097],
    'lon': [149.124386]
})

# Convert the 'DateTimeField' column to a pandas datetime object
utcDF['DateTimeField'] = pd.to_datetime(utcDF['DateTimeField'])

# make 'DateTimeField' timezone aware
utcDF['DateTimeField'] = utcDF['DateTimeField'].dt.tz_localize(tz='Australia/Canberra')

# convert 'DateTimeField' to UTC timezone
utcDF['DateTimeField'] = utcDF['DateTimeField'].dt.tz_convert(tz='UTC')

utcDF = pd.DataFrame.spatial.from_xy(df = utcDF,
                                     x_column = 'lon',
                                     y_column = 'lat',
                                     sr = 4326)

utcFeatureSet = utcDF.spatial.to_featureset()

error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/tmp/ipykernel_38/3507368548.py in <cell line: 30>()
     28                                      sr = 4326)
     29 
---> 30 utcFeatureSet = utcDF.spatial.to_featureset()

/opt/conda/lib/python3.9/site-packages/arcgis/features/geo/_accessor.py in to_featureset(self)
   3571         from arcgis.features import FeatureSet
   3572 
-> 3573         return FeatureSet.from_dict(self.__feature_set__)
   3574 
   3575     # ----------------------------------------------------------------------

/opt/conda/lib/python3.9/site-packages/arcgis/features/geo/_accessor.py in __feature_set__(self)
   3424                 column = {
   3425                     "name": col,
-> 3426                     "type": _look_up[dtype],
   3427                     "alias": col,
   3428                 }

KeyError: datetime64[ns, UTC]

Expected behaviour This works fine in previous versions.

Platform (please complete the following information):

nanaeaubry commented 1 year ago

Thank you for bringing this up, we were able to fix the issue for the next release! There will be a beta release in July. :)

GeeFernando commented 1 year ago

Thank you for bringing this up, we were able to fix the issue for the next release! There will be a beta release in July. :)

Whoa - that was a quick response/fix. Thanks so much. :)