I am using a script that someone else created (and another person has been using successfully for months), and I can't figure out this error:
Exception has occurred: ValueError
time data '2022-12-28 14:43:08.908271' does not match format '%m/%d/%Y %I:%M:%S %p' (match)
File ". . .\res-script.py", line 19, in <module>
end = pd.to_datetime(yesterday, format="%m/%d/%Y %I:%M:%S %p").normalize()
I'm trying this and still getting the same error:
today_date = datetime.today().strftime('%Y-%m-%d')
yesterday = datetime.today()
end = pd.to_datetime(yesterday, format='%Y-%m-$d %H:%M:%S').normalize()
After a few people pointing out the obvious, we have it fixed! Either of these will work:
end = pd.to_datetime(yesterday, format='%Y-%m-%d %H:%M:%S.%f').normalize()
or
end = pd.to_datetime(yesterday).normalize()
Related
For the node 'TransactionDate' i have a logic before updating it for policy"POL000002NGJ".
The logic i am trying to implement is If existing 'TransactionDate' < than today, then add 5 days with current value and parse it to xml.
Transaction Date Format in XML : 2020-03-23T10:56:15.00
Please Note that, If i parsing the DateTime value like below, It works good But i dont want to hardcode the value... I want to Parse it as a string object to handle for any datetime in format ""%Y-%m-%dT%H:%M:%S.%f""...
# <TransactionDate>
today = datetime.now()
TransactionDate = doc.find('TransactionDate')
Date = '2020-03-24T10:56:15.00'
previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f")
if previous_update < today:
today = previous_update - timedelta(days=-5)
TransactionDate = today.strftime("%Y-%m-%dT%H:%M:%S.%f")
Below code while parsing it as a DateTime Object, I have an issue.. I got struck here and referenced other answers in stackoverflow and python forums, But still i got struct up here and unable to resolve the issue...
if any help to fix will be a great helpful. Thanks. Below code using lxml and getting help to support below code will helpful. Because i already completed for other nodes. My understanding is Date variable is calling as None.. But struck here to fix.. Please help..
# <TransactionDate>
today = datetime.now()
TransactionDate = doc.find('TransactionDate')
Date = str(TransactionDate)
previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f")
if previous_update < today:
today = previous_update - timedelta(days=-5)
TransactionDate = today.strftime("%Y-%m-%dT%H:%M:%S.%f")
Full Code is Below
from lxml import etree
from datetime import datetime, timedelta
import random, string
doc = etree.parse(r'C:\Users\python.xml')
# <PolicyId> - Random generated policy number
Policy_Random_Choice = 'POL' + ''.join(random.choices(string.digits, k=6)) + 'NGJ'
# <TransactionDate>
today = datetime.now()
TransactionDate = doc.find('TransactionDate')
Date = str(TransactionDate)
previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f")
if previous_update < today:
today = previous_update - timedelta(days=-5)
TransactionDate = today.strftime("%Y-%m-%dT%H:%M:%S.%f")
#Parsing the Variables
replacements = [Policy_Random_Choice , TransactionDate ]
targets = doc.xpath('//ROW[PolicyId="POL000002NGJ"]')
for target in targets:
target.xpath('./PolicyId')[0].text = replacements[0]
target.xpath('.//TransactionDate')[0].text = replacements[1]
print(etree.tostring(doc).decode())
Sample XML
<TABLE>
<ROW>
<PolicyId>POL000002NGJ</PolicyId>
<BusinessCoverageCode>COV00002D3X1</BusinessCoverageCode>
<TransactionDate>2020-03-23T10:56:15.00</TransactionDate>
</ROW>
<ROW>
<PolicyId>POL111111NGJ</PolicyId>
<BusinessCoverageCode>COV00002D3X4</BusinessCoverageCode>
<TransactionDate>2020-03-23T10:56:15.00</TransactionDate>
</ROW>
</TABLE>
Maybe the find method is wrong. Try this one
# <TransactionDate>
today = datetime.now()
TransactionDate = doc.xpath('//ROW/TransactionDate') # Change find to xpath
Date = str(TransactionDate[0].text) # Use the first one
previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f")
For a uni project we have to create a program conforming to some tests that we have been given. The tests use magic numbers as inputs for the functions. I know how to get it to return a datetime object. Just dont know how to raise the error!
my code:
import datetime
def time_to_datetime(datetimeSTR):
datetime_obj = datetime.datetime.strptime(datetimeSTR, '%Y/%m/%d %H:%M')
if datetimeSTR != datetime.datetime.strptime(datetimeSTR, '%Y/%m/%d %H:%M'):
raise ValueError('The time must have the format YYYY/MM/DD HH:MM')
else:
return datetime_obj
Test Code:
import datetime
import os
import unittest # Standard unittest framework.
import utils # The module implementing JourneyOptions class.
class TestTimeToDatetime(unittest.TestCase):
"""Tests for the time_to_datetime function."""
def test_invalid_time_is_rejected(self):
with self.assertRaises(ValueError) as cm:
utils.time_to_datetime('2019/06/09 12:60')
self.assertEqual(
'The time must have the format YYYY/MM/DD HH:MM',
str(cm.exception))
def test_valid_time_yields_a_dattime_object(self):
d = utils.time_to_datetime('2019/06/09 12:59')
self.assertTrue(isinstance(d, datetime.datetime))
these are the results i get:
======================================================================
ERROR: test_valid_time_yields_a_dattime_object (__main__.TestTimeToDatetime)
Traceback (most recent call last):
File "C:/Users/s5115426/Desktop/tests/test_utils.py", line 21, in test_valid_time_yields_a_dattime_object
d = utils.time_to_datetime('2019/06/09 12:59')
File "C:\Users\s5115426\Desktop\tests\utils.py", line 25, in time_to_datetime
raise ValueError('The time must have the format YYYY/MM/DD HH:MM')
ValueError: The time must have the format YYYY/MM/DD HH:MM
======================================================================
FAIL: test_invalid_time_is_rejected (__main__.TestTimeToDatetime)
Traceback (most recent call last):
File "C:/Users/s5115426/Desktop/tests/test_utils.py", line 18, in test_invalid_time_is_rejected
str(cm.exception))
AssertionError: 'The time must have the format YYYY/MM/DD HH:MM' != 'unconverted data remains: 0'
- The time must have the format YYYY/MM/DD HH:MM
+ unconverted data remains: 0
Any help would be much appreciated!
The error that you are making is that strptime raises a value error if the date string does not match the given format. for example:
date_str = '2019/06/09 12:60'
datetime_obj = datetime.datetime.strptime(date_str, '%Y/%m/%d %H:%M')
.
.
ValueError: unconverted data remains: 0
datetime_obj never gets set because strptime never returns a value.
Also, as noted, you are comparing the response from strptime to the inout string -- that will never work.
Try a simpler approach:
import datetime
def time_to_datetime(datetimeSTR):
datetime_obj = datetime.datetime.strptime(datetimeSTR, '%Y/%m/%d %H:%M')
return datetime_obj
Or you can catch any exception and reraise (imo, bad practice, but might be worth doing to illustrate.
import datetime
def time_to_datetime(datetimeSTR):
try:
datetime_obj = datetime.datetime.strptime(datetimeSTR, '%Y/%m/%d %H:%M')
except ValueErr as err:
print(err)
raise ValueError('The time must have the format YYYY/MM/DD HH:MM')
else:
return datetime_obj
This way, you see how strptime behaves for various inputs in your test and you have error handling in your function .... though, as I indicated, reraising an error is (imo) bad practice.
I am trying to create a number of constraints for some other code based on twitter handle sets.
I am having issues with the following code because:
TypeError: can't compare datetime.datetime to str
It seems that even though I have changed Last_Post to a datetime object initially, when i compare it to datetime.datetime.today() it is converting to string. Yes, I have checked the to ensure that Last_post is converting properly. Im not really sure what is going on. Help?
for handle in handles:
try:
user = api.get_user(handle)
#print json.dumps(user, indent = 4)
verified = user["verified"]
name = user['name']
language = user['lang']
follower_count = user['followers_count']
try:
last_post = user['status']['created_at']
last_post = datetime.strptime(last_post, '%a %b %d %H:%M:%S +0000 %Y')
except:
last_post = "User has not posted ever"
location = user['location']
location_ch = location_check(location)
if location_ch is not "United States":
location_output.append(False)
else:
location_output.append(True)
new_sum.append(follower_count)
if language is not "en":
lang_output.append(False)
else:
lang_output.append(True)
if datetime.datetime.today() - datetime.timedelta(days=30) > last_post:
recency.append(False)
else:
recency.append(True)
I think you need to convert the twitter date to a timestamp:
import time
ts = time.strftime('%Y-%m-%d %H:%M:%S', time.strptime(tweet['created_at'],'%a %b %d %H:%M:%S +0000 %Y'))
This is my code:
shift_from = datetime.strptime(shift_change_ob.shift_date, '%Y-%m-%d %H:%M:%S')
shift_to = datetime.strptime(shift_change_ob.shift_date_to, '%Y-%m-%d %H:%M:%S')
shift_time_from = self.get_zone_time(shift_from)
shift_time_to= self.get_zone_time(shift_to)
time_from = datetime.strptime(str(shift_time_from),"%Y-%m-%d %H:%M:%S").strftime("%H.%M")
time_to = datetime.strptime(str(shift_time_to),"%Y-%m-%d %H:%M:%S").strftime("%H.%M")
I want to get only time from shift_time_from and shift_time_to. When I do this I get:
ValueError: unconverted data remains: +00:00
How can I solve this?
If shift_from and shift_to are correct, then use the method time for getting only time part:
shift_time_from = shift_from.time()
shift_time_to = shift_to.time()
I would like to parse a date that can come in several formats, that I know beforehand. If I could not parse, I return nil. In ruby, I do like this:
DATE_FORMATS = ['%m/%d/%Y %I:%M:%S %p', '%Y/%m/%d %H:%M:%S', '%d/%m/%Y %H:%M', '%m/%d/%Y', '%Y/%m/%d']
def parse_or_nil(date_str)
parsed_date = nil
DATE_FORMATS.each do |f|
parsed_date ||= DateTime.strptime(date_str, f) rescue nil
end
parsed_date
end
This is concise and works. How can I do the same thing in Python?
I would just try dateutil. It can recognize most of the formats:
from dateutil import parser
parser.parse(string)
if you end up using datetime.strptime as suggested #RocketDonkey:
from datetime import datetime
def func(s,flist):
for f in flist:
try:
return datetime.strptime(s,f)
except ValueError:
pass
You can use try/except to catch the ValueError that would occur when trying to use a non-matching format. As #Bakuriu mentions, you can stop the iteration when you find a match to avoid the unnecessary parsing, and then define your behavior when my_date doesn't get defined because not matching formats are found:
You can use try/except to catch the ValueError that would occur when trying to use a non-matching format:
from datetime import datetime
DATE_FORMATS = ['%m/%d/%Y %I:%M:%S %p', '%Y/%m/%d %H:%M:%S', '%d/%m/%Y %H:%M', '%m/%d/%Y', '%Y/%m/%d']
test_date = '2012/1/1 12:32:11'
for date_format in DATE_FORMATS:
try:
my_date = datetime.strptime(test_date, date_format)
except ValueError:
pass
else:
break
else:
my_date = None
print my_date # 2012-01-01 12:32:11
print type(my_date) # <type 'datetime.datetime'>
After your tip, RocketDonkey, and the one from Bakuriu, I could write a shorter version. Any problem with it?
def parse_or_none(date):
for date_format in DATE_FORMATS:
try:
return datetime.strptime(date, date_format)
except ValueError:
pass
return None
Modifying root's answer to handle m/d/y:
from dateutil import parser
parser.parse(string, dayfirst=False)
There's also a yearfirst option, to prefer y/m/d.