Arcpy ValueError: unconverted data remains: PM

12720
8
Jump to solution
11-23-2016 12:38 AM
MagalyClément-Zaber
Occasional Contributor

Hi!

This script works fine :

import time
from datetime import datetime

date_string = '2009-11-29 03:17 PM'
format = '%Y-%m-%d %I:%M %p'
format2 = '%m/%d/%Y %I:%M:%S %p'
my_date = datetime.strptime(date_string, format)
print my_date.strftime(format2)

Result :

11/29/2009 03:17:00 PM

But If I have "import arcpy", it doesn't work !!

import arcpy
import time
from datetime import datetime

date_string = '2009-11-29 03:17 PM'
format = '%Y-%m-%d %I:%M %p'
format2 = '%m/%d/%Y %I:%M:%S %p'
my_date = datetime.strptime(date_string, format)
print my_date.strftime(format2)

Result:

Traceback (most recent call last):
  File "E:\Scripts\1-Scripts\tests\ampm.py", line 40, in <module>
    my_date = datetime.strptime(date_string, format)
  File "C:\Python27\ArcGIS10.2\lib\_strptime.py", line 328, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: PM

My script needs arcpy, how to do the stuff without error ?

Thanks for help !

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
ClintonDow1
Occasional Contributor II

This is unfortunately a namespace collision in ArcPy from the early days of its design. See import arcpy tramples datetime import for more info. 

As a workaround you can use:

 

  1. import datetime  
  2. datetime.datetime.now()  
  3. import arcpy  
  4. datetime.datetime.now()  

 

Or alternatively:

 

  1. from datetime import datetime as dt  
  2. dt.now()  
  3. import arcpy  
  4. dt.now()  

View solution in original post

0 Kudos
8 Replies
NeilAyres
MVP Alum

Well, format is a keyword builtin. Its a method you can apply to strings to format them.

You are replacing this with your "format time string".

What do you want to do? Make a fixed time format string == your date_string,

or,

set a variable to the current time & date?

0 Kudos
MagalyClément-Zaber
Occasional Contributor

I have dates in string format and have to put in database in a datime format.

date_string = '11/29/2019 03:17 PM'
print date_string
format1 = '%m/%d/%Y %I:%M %p'
format2 = '%Y-%m-%d %I:%M:%S'
my_date = datetime.strptime(date_string, format1)
print my_date.strftime(format2)

Works fine without "import arcpy"

I found another way :

        ## self.lastUsageDate = '11/29/2019 03:17 PM'

        tmp = self.lastUsageDate.split(" ")

        format12 = '%I:%M:%S'
        format24 = '%H:%M:%S'
        my_hour = datetime.strptime(tmp[1], format12)
        if (tmp[2] == "PM"):
            my_hour += timedelta(hours=12)

        formatDate = '%m/%d/%Y'
        formatDateBase = '%Y-%m-%d'
        my_date = datetime.strptime(tmp[0], formatDate)

        return my_date.strftime(formatDateBase) + " " + my_hour.strftime(format24)

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Something else is going on, either with additional code you haven't provided or how this "script" is being called.   I executed the code, including import arcpy, on two separate machines with two different versions of ArcGIS for Desktop, and everything ran fine.

Does the script only include the code you have provided?  If so, how are you calling the script? 

0 Kudos
MagalyClément-Zaber
Occasional Contributor

I use PyScripter to call the script.

I put the minimun to test :

#-*- coding: utf-8 -*-
##import arcpy
from datetime import datetime

date_string = '11/29/2019 03:17 PM'
print date_string
format1 = '%m/%d/%Y %I:%M %p'
format2 = '%Y-%m-%d %I:%M:%S'
my_date = datetime.strptime(date_string, format1)
print my_date.strftime(format2)
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I don't use PyScripter nor have it installed on any machines.  When I take the code and run it as a script file directly from the ArcGIS-bundled Python interpreter, it works fine with and without the ArcPy import statement.  I am not sure what might be going on.

0 Kudos
ClintonDow1
Occasional Contributor II

This is unfortunately a namespace collision in ArcPy from the early days of its design. See import arcpy tramples datetime import for more info. 

As a workaround you can use:

 

  1. import datetime  
  2. datetime.datetime.now()  
  3. import arcpy  
  4. datetime.datetime.now()  

 

Or alternatively:

 

  1. from datetime import datetime as dt  
  2. dt.now()  
  3. import arcpy  
  4. dt.now()  
0 Kudos
BlakeTerhune
MVP Regular Contributor

I had this problem too!. Although I never knew the cause, I found that just importing datetime (your first example) worked for me. Good to know the second option is viable too. Thanks CDow-esristaff‌!

DanPatterson_Retired
MVP Emeritus

It is best to import standard modules first and in the order of their   importance.  Arcpy is not a primary module, other examples include SciPy, Pandas and NumPy which all import bits of the standards as well as bits of one another sometimes.  If you need the most current and relevant ... ordering your imports is important, since once imported, it is skipped on future attempts unless expressly done... which requires effort.

Check it out yourself, even the os modules tries to import sys.