1

So I have a feature class in a GDB that contains time data that I wish to carry out calculations through arcpy. This data was entered from a string to a datetime format into the featureclass through a script that handles a special type of ASCII file.

The output format of this tuple, when accessed using arcpy.da.SearchCursor() takes the form of the following:

(datetime.datetime(1899, 12, 30, 13, 59, 18),)

Where the type of value I need to carry out further calculation needs to take the following form:

datetime.datetime(1899, 12, 30, 0, 0)

I get to this problem when I try the following code:

fc = "C:\\scripting\\timetest.gdb\\test1\\"
rows = arcpy.da.SearchCursor(fc,["time"])
a = []
for row in rows:
    a.append(row)

And when I look at the type of the output, I receive a 'tuple', which is the normal data output of the arcpy.da.SearchCursor() class.

type(a[0])
<type 'tuple'>

But what I really want to do is break the information out of the tuple so that I can carry out normal python operations with the datetime.datetime objects.

The help pages haven't been terribly useful in finding a way to break the data out of the tuple format. I can't print the information inside the tuple, nor can I access like an array. What I seek to do is get at the datetime.datetime object inside the tuple so that I may carry out my analysis. Has anyone run into this problem before?

I've uploaded a shapefile with the problem that I'm running at http://brianbancroft.ca/docs/timetest.zip in case someone wants to try running it.

  • It's not a tuple any more, it's a datetime object and that depends on the database. What data source are you using? A python DateTime object should be able to work with the data from the search cursor and do simple operations like add/difference/break up into hours etc.. You might need to import it from one of several long formats though. – Michael Stimson Dec 15 '15 at 23:27
  • 1
    This blog helps: pymotw.com/2/datetime – Emil Brundage Dec 16 '15 at 0:08
  • I like that tutorial @EmilBrundage, I think that's one of the ones I read through when I was starting with python. Why is it Brian that the 2nd element in the tuple is empty? datetime.datetime(1899, 12, 30, 13, 59, 18) is a constructor which returns a datetime object so saying something like thisDT = datetime.datetime(1899, 12, 30, 13, 59, 18) will allow you to access the object but it may not necessarily be compatible with the feature class date/datetime object, have a read of webhelp.esri.com/arcgisdesktop/9.2/… to expand on my previous. – Michael Stimson Dec 16 '15 at 1:46
  • @MichaelMiles-Stimson, I wish it was a datetime.datetime object. Then I would be able to manipulate the data and it wouldn't be a problem for me at all. Sadly this isn't the case. It's a tuple without a second element, and I'm not sure how to get the datetime.datetime object out of it. I've modified the question to show the full method and sample data. Thanks for your thoughts so far! – brianbancroft Dec 18 '15 at 23:05
  • I also like that tutorial, @EmilBrundage. I hope to get to the point where I could use it! Thanks! – brianbancroft Dec 18 '15 at 23:07
1

While I don't have an answer to the problem of extracting data from a tuple with a single element, I have found a workaround.

Instead of using arcpy.da.SearchCursor(), I found that arcpy.SearchCursor() did the job. Using the same sample data, I found that you could extract datetime.datetime objects by carrying out the following:

a = []
rows = arcpy.SearchCursor(fc)
for row in rows:
    a.append(row.getValue("time"))

If you're attempting to extract value from multiple fields, this will result in a few extra lines of code in the for/while loop, but it will get the job done all the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.