<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Feature Layer to Geodataframe: DateTime Field Issues in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/feature-layer-to-geodataframe-datetime-field/m-p/1240997#M8162</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am currently working with the API to pull data from a feature layer, transform it to a geodataframe with geopandas, and push it to a PostgreSQL database with PostGIS. (&amp;lt;-- just explaining the need&amp;nbsp; to turn the layer into a gdf, since geopandas has a nifty .to_postgis method)&lt;/P&gt;&lt;P&gt;Unfortunately, this is changing the DateTime field into a format I can't understand or parse. Below is my code, and the resulting DT field. The original field is in the routine Esri DateTime format.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;gis = GIS()
url_2000 = 'https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/Historic_Geomac_Perimeters_All_Years_2000_2018/FeatureServer/0'
layer_2000 = FeatureLayer(url_2000)
fset_2000 = layer_2000.query()
gjson_string_2000 = fset_2000.to_geojson
gjson_dict_2000 = json.loads(gjson_string_2000)
gdf_2000 = gpd.GeoDataFrame.from_features(gjson_dict_2000['features'])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RainaMonaghan2_0-1671012847063.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/58484iC11D560CADCA897D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="RainaMonaghan2_0-1671012847063.png" alt="RainaMonaghan2_0-1671012847063.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Any help figuring this out would be much appreciated, thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 14 Dec 2022 10:16:47 GMT</pubDate>
    <dc:creator>RainaMonaghan2</dc:creator>
    <dc:date>2022-12-14T10:16:47Z</dc:date>
    <item>
      <title>Feature Layer to Geodataframe: DateTime Field Issues</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/feature-layer-to-geodataframe-datetime-field/m-p/1240997#M8162</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am currently working with the API to pull data from a feature layer, transform it to a geodataframe with geopandas, and push it to a PostgreSQL database with PostGIS. (&amp;lt;-- just explaining the need&amp;nbsp; to turn the layer into a gdf, since geopandas has a nifty .to_postgis method)&lt;/P&gt;&lt;P&gt;Unfortunately, this is changing the DateTime field into a format I can't understand or parse. Below is my code, and the resulting DT field. The original field is in the routine Esri DateTime format.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;gis = GIS()
url_2000 = 'https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/Historic_Geomac_Perimeters_All_Years_2000_2018/FeatureServer/0'
layer_2000 = FeatureLayer(url_2000)
fset_2000 = layer_2000.query()
gjson_string_2000 = fset_2000.to_geojson
gjson_dict_2000 = json.loads(gjson_string_2000)
gdf_2000 = gpd.GeoDataFrame.from_features(gjson_dict_2000['features'])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RainaMonaghan2_0-1671012847063.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/58484iC11D560CADCA897D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="RainaMonaghan2_0-1671012847063.png" alt="RainaMonaghan2_0-1671012847063.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Any help figuring this out would be much appreciated, thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2022 10:16:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/feature-layer-to-geodataframe-datetime-field/m-p/1240997#M8162</guid>
      <dc:creator>RainaMonaghan2</dc:creator>
      <dc:date>2022-12-14T10:16:47Z</dc:date>
    </item>
    <item>
      <title>Re: Feature Layer to Geodataframe: DateTime Field Issues</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/feature-layer-to-geodataframe-datetime-field/m-p/1241043#M8163</link>
      <description>&lt;P&gt;This is the UNIX timestamp/epoch in milliseconds. If you want to use the FeatureSet as a dataframe the easiest way is the spatially enabled dataframe.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis import GIS
from arcgis.features import FeatureLayer, FeatureSet
from datetime import datetime
import json
import geopandas as gpd

gis = GIS()
url_2000 = 'https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/Historic_Geomac_Perimeters_All_Years_2000_2018/FeatureServer/0'
layer_2000 = FeatureLayer(url_2000)
fset_2000 = layer_2000.query()
fset_2000.sdf&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The conversion from UTC timestamp to the datetime object is done under the hood.&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you still want to use the GeoDataFrame from the geopandas module, you have to write your own conversion implementation. The to_geojson property of the FeatureSet is going to reconvert the datetime object back into an UTC timestamp. This seems a little bit awkward. The following function is converting the UTC timestamp into the ISO format string, because your code reloads the GeoJSON into a python object again. Furthermore, the to_geojson also encodes your strings into unicode escaped characters ('\uxxx') - I love our German Umlauts. I would suggest to stay away from the to_geojson approach.&lt;/P&gt;&lt;P&gt;The following function directly converts the UTC timestamps into the ISO string format.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def convert_timestamps(feature_set: FeatureSet, field_names, use_iso=True):
    """Converts timestamps (UNIX epoch in [ms]) into datetime objects by modifying the feature set directly."""
    for feature in feature_set.features:
        for attribute_name in feature.attributes:
            if attribute_name in field_names:
                if use_iso:
                    feature.attributes[attribute_name] = datetime.utcfromtimestamp(feature.attributes[attribute_name] * 1e-3).isoformat()
                else:
                    feature.attributes[attribute_name] = datetime.utcfromtimestamp(feature.attributes[attribute_name] * 1e-3)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So that, you just need to call it by using:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;convert_timestamps(fset_2000, ['perimeterdatetime'])
gjson_string_2000 = fset_2000.to_geojson
gjson_dict_2000 = json.loads(gjson_string_2000)
gdf_2000 = gpd.GeoDataFrame.from_features(gjson_dict_2000['features'])&lt;/LI-CODE&gt;&lt;P&gt;The geopandas dataframe contains the correct ISO string. You have to reconvert it into a datetime object, again. This is only needed because of the to_geojson property behavior.&lt;/P&gt;&lt;P&gt;Hope it helps.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Dec 2022 14:21:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/feature-layer-to-geodataframe-datetime-field/m-p/1241043#M8163</guid>
      <dc:creator>Jan-Tschada</dc:creator>
      <dc:date>2022-12-14T14:21:59Z</dc:date>
    </item>
  </channel>
</rss>

