Field Calculator - Dates using Python

615
2
02-21-2011 07:40 PM
AndrewWise
New Contributor
Hi All,

I am trying to parse a string field into date field but having some issues with the ArcGIS field calculator.

Strings are in the format of:
idr02_20110203_0000_256.png
being
idr02_YYYYMMDD_HHMM_256.png

I have tested my string parsing in the python console but for having some difficulty getting ArcGIS field calculator to accept it in the following format:

CodeBlock=
------
def MyFunction(nameInput):
  rasterParts = nameInput.split("_")
  newDate = datetime(int(rasterParts[1][0:4]), int(rasterParts[1][4:6]),int(rasterParts[1][6:8]),int(rasterParts[2][0:2]),int(rasterParts[2][2:4]) )
  return newDate
-----
Expression=
---
MyFunction( !Name! )
--

It keeps coming back with:

Messages
ERROR 000539: Error running expression: MyFunction("idr02_20110203_0000_256.png")  <type 'exceptions.TypeError'>: 'module' object is not callable

Can anybody spot the error or have any alternative methods for parsing out the date?

Thanks
Andrew
Tags (2)
0 Kudos
2 Replies
NiklasNorrthon
Occasional Contributor III
'module' object is not callable means that you are treating a module as a function.

datetime is a module
datetime(int(rasterParts[1][0:4]), ...) is trying to "call" the datetime module, instead of createing an object of type datetime.datetime.
0 Kudos
AndrewWise
New Contributor
Thanks Niklas,

I knew it would be something simple. Adding the extra datetime.datetime reference worked fine.

Cheers
Andrew
0 Kudos