Calculating today's date in API for Python Field Calc

267
3
03-03-2021 01:41 PM
AFackler_NAPSG
New Contributor III

I am trying to calculate today's date using a calculate field function in Notebooks. In regular python code, I can set the expression type as Arcade and use "Now()" to calculate the date. However, that doesn't seem to work in Notebooks. Below is my calculation and error, any suggestions? I only really care about getting the date, the time isn't as important.

dateCalc = calculate_fields(input_layer=joinedData,
field_name="data_date",
data_type="Date",
expression="Now()")

 

Output:

gaierror                                  Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/urllib3/connection.py in _new_conn(self)
    156             conn = connection.create_connection(
--> 157                 (self._dns_host, self.port), self.timeout, **extra_kw
    158             )

/opt/conda/lib/python3.6/site-packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
     60 
---> 61     for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
     62         af, socktype, proto, canonname, sa = res

/opt/conda/lib/python3.6/socket.py in getaddrinfo(host, port, family, type, proto, flags)
    744     addrlist = []
--> 745     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    746         af, socktype, proto, canonname, sa = res

gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

NewConnectionError                        Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    671                 headers=headers,
--> 672                 chunked=chunked,
    673             )

/opt/conda/lib/python3.6/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    375         try:
--> 376             self._validate_conn(conn)
    377         except (SocketTimeout, BaseSSLError) as e:

/opt/conda/lib/python3.6/site-packages/urllib3/connectionpool.py in _validate_conn(self, conn)
    993         if not getattr(conn, "sock", None):  # AppEngine might not have  `.sock`
--> 994             conn.connect()
    995 

/opt/conda/lib/python3.6/site-packages/urllib3/connection.py in connect(self)
    299         # Add certificate verification
--> 300         conn = self._new_conn()
    301         hostname = self.host

/opt/conda/lib/python3.6/site-packages/urllib3/connection.py in _new_conn(self)
    168             raise NewConnectionError(
--> 169                 self, "Failed to establish a new connection: %s" % e
    170             )

NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7fa24c82f7f0>: Failed to establish a new connection: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

MaxRetryError                             Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    448                     retries=self.max_retries,
--> 449                     timeout=timeout
    450                 )

/opt/conda/lib/python3.6/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    719             retries = retries.increment(
--> 720                 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
    721             )

/opt/conda/lib/python3.6/site-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
    435         if new_retry.is_exhausted():
--> 436             raise MaxRetryError(_pool, url, error or ResponseError(cause))
    437 

MaxRetryError: HTTPSConnectionPool(host='geoanalytics.arcgis.com', port=443): Max retries exceeded with url: /arcgis/rest/services/GeoAnalyticsTools/GPServer/CalculateField/submitJob (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fa24c82f7f0>: Failed to establish a new connection: [Errno -2] Name or service not known',))

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/arcgis/gis/_impl/_con/_connection.py in post(self, path, params, files, **kwargs)
    676                                           cert=cert,
--> 677                                           files=files)    678         except requests.exceptions.SSLError as err:

/opt/conda/lib/python3.6/site-packages/requests/sessions.py in post(self, url, data, json, **kwargs)
    577 
--> 578         return self.request('POST', url, data=data, json=json, **kwargs)
    579 

/opt/conda/lib/python3.6/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    529         send_kwargs.update(settings)
--> 530         resp = self.send(prep, **send_kwargs)
    531 

/opt/conda/lib/python3.6/site-packages/requests/sessions.py in send(self, request, **kwargs)
    642         # Send the request
--> 643         r = adapter.send(request, **kwargs)
    644 

/opt/conda/lib/python3.6/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    515 
--> 516             raise ConnectionError(e, request=request)
    517 

ConnectionError: HTTPSConnectionPool(host='geoanalytics.arcgis.com', port=443): Max retries exceeded with url: /arcgis/rest/services/GeoAnalyticsTools/GPServer/CalculateField/submitJob (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fa24c82f7f0>: Failed to establish a new connection: [Errno -2] Name or service not known',))

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
<ipython-input-11-a98d23214be8> in <module>
      2                             field_name="data_date",
      3                             data_type="Date",
----> 4                             expression="Now()")
/opt/conda/lib/python3.6/site-packages/arcgis/geoanalytics/manage_data.py in calculate_fields(input_layer, field_name, data_type, expression, track_aware, track_fields, time_boundary_split, time_split_unit, time_reference, output_name, gis, context, future)
    974             gpjob = _execute_gp_tool(gis, tool_name, params, param_db, return_values, _use_async, url, True, future=future)
    975             return GAJob(gpjob=gpjob, return_service=output_service)
--> 976         _execute_gp_tool(gis, tool_name, params, param_db, return_values, _use_async, url, True, future=future)
    977         return output_service
    978     except:

/opt/conda/lib/python3.6/site-packages/arcgis/geoprocessing/_support.py in _execute_gp_tool(gis, task_name, params, param_db, return_values, use_async, url, webtool, add_token, return_messages, future)
    388                 job_info = gptool._con.post(submit_url, gp_params)
    389         else:
--> 390             job_info = gptool._con.post(submit_url, gp_params)
    391         job_id = job_info['jobId']
    392         if future:

/opt/conda/lib/python3.6/site-packages/arcgis/gis/_impl/_con/_connection.py in post(self, path, params, files, **kwargs)
    684         except requests.exceptions.ConnectionError as errCE:
    685             raise requests.exceptions.ConnectionError(
--> 686                 "A connection error has occurred: %s" % errCE)    687         except requests.exceptions.InvalidHeader as errIH:
    688             raise requests.exceptions.InvalidHeader(

ConnectionError: A connection error has occurred: HTTPSConnectionPool(host='geoanalytics.arcgis.com', port=443): Max retries exceeded with url: /arcgis/rest/services/GeoAnalyticsTools/GPServer/CalculateField/submitJob (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fa24c82f7f0>: Failed to establish a new connection: [Errno -2] Name or service not known',))   

 

0 Kudos
3 Replies
jcarlson
Regular Contributor II

Your error seems to suggest that it isn't connecting. Can you do other things with the layer, like query it or visualize it?

Also, consider just using python's own datetime.datetime.now(), if the using of Arcade really ends up being the issue.

- Josh Carlson
Kendall County GIS
0 Kudos
AFackler_NAPSG
New Contributor III

Do I have to import a datetime tool to use that? I can do anything I want with the layer and the connection is coming from the expression (I think. I'm new to this stuff) 

AdamFackler1_0-1614810216866.png

 

0 Kudos
MinbinJiang
Esri Contributor

The error message indicates the connection failure to your server.. I would suggest looking from here before going down to the date

0 Kudos