-1

I am using datetime in Arcpy to convert a "Sample Date" field in a feature class to the day of the year. The date in the field is in a month/day/year format and it doesn't include any leading zeros (ex. 7/8/2018 instead of 07/08/2018).

Whenever I try running the following line of code,

dtm = datetime.datetime.strptime(row, '%Y/%m/%d %H:%M')

I get the following error:

ValueError: time data '[datetime.datetime(2003, 6, 3, 0, 0)]' does not match format '%Y/%m/%d %H:%M'

The sample date field that I am using does not include any time data which I believe is where the two zeros at the end of the error message are coming from. I just assumed that they were hours and minutes but I am not sure.

Does anyone know what the zeros are so that I can change my code to fit the correct format or how I can avoid the zeros altogether? I tried datetime.date instead of datetime.datetime as well and still had complications. I can post the long version of the code if needed but the code that I provided is the cause of the error message. I have included the link of the datetime reference I used. The website is for strftime not strptime but I believe that it works the same. Trying strftime throws an error as well. Link: http://strftime.org/

Sheldore
  • 37,862
  • 7
  • 57
  • 71
PythonPerson
  • 11
  • 2
  • 6
  • 1
    Can you please break this down into paragraphs or more code-based? I've just opened this on my phone and it's literally a wall of text – roganjosh Aug 29 '18 at 20:56
  • 1
    You should apply `strptime` to a string, not a `datetime` object. Your `datetime` objects do not have any format. – Stop harming Monica Aug 29 '18 at 21:03

1 Answers1

1

You are using the wrong datetime method. The error that you got shows that value in row is already a datetime object.

strptime takes a string and a datetime string format and returns a datetime object.

For example this code: datetime.datetime.strptime('2018/08/30 10:30', '%Y/%m/%d %H:%M') returns the object datetime.datetime(2018, 8, 30, 10, 30)

strftime returns a string representing the date in the format specified.

You already have a datetime in row so you need to use strftime to convert that to a string.

The proper code to convert row into a date string would be:

dtm = row.strftime('%Y/%m/%d %H:%M')

This will return the string representing the datetime. For example for the datetime object of datetime.datetime(2003, 6, 3, 0, 0) the returned string would be '2003/06/03 00:00'

BigGerman
  • 525
  • 3
  • 11
  • Whenever I try that I get an error message that reads "AttributeError: 'list' object has no attribute 'strftime' ". – PythonPerson Sep 04 '18 at 22:22
  • If row is a list then you need to pass in the index of the list that contains the datetime object. So if the list only contains one thing, i.e. `[datetime.datetime(2003, 6, 3, 0, 0)]` then you would use `row[0]`. So then your code would be `dtm = row[0].strftime('%Y/%m/%d %H:%M')` – BigGerman Sep 05 '18 at 16:18
  • Thank you that worked. The last line of the code is: arcpy.CalculateField_management(fc, "DOY", dtm.timetuple().tm_yday) which is supposed to take the dtm and calculate the day of the year from it and add it to a field called "DOY". However, when I try this I get an error message that reads "AttributeError: 'str' object has no attribute 'timetuple' " so does that mean I need to do a strptime function on a new line after the strftime one to get the object back into a datetime format? And if so what would that look like? – PythonPerson Sep 05 '18 at 20:33
  • Just do the `.timetuple().tm_yday` function on the original datetime object: `row[0].timetuple().tm_yday` – BigGerman Sep 06 '18 at 13:53
  • I don't think you are using CalculateField correctly though. CalculateField can calculate a value for all records in the table at once so there is no reason to use it while iterating through the table row-by-row – BigGerman Sep 06 '18 at 14:01