ValueError: unconverted data remains: 00:00:00 - python

I have problem in convert date time.
Here is my code:
#api.multi
def action_confirm(self):
if picking.state not in ['cancel', 'draft']:
for schedule_delivery in sorted_key:
print "line 105: ", schedule_delivery
dup_picking = picking.copy()
if shift == "afternoon":
date_object = datetime.strptime(schedule_delivery, '%Y-%m-%d')
print "Line 146", date_object
# raise EnvironmentError
tanggal = datetime.strftime(date_object, "%Y-%m-%d") + ' 06:00:00'
print "Line 145", tanggal
dup_picking.min_date = tanggal
else:
dup_picking.min_date = schedule_delivery
Error:
date_object = datetime.strptime(schedule_delivery, '%Y-%m-%d')
File "/usr/lib/python2.7/_strptime.py", line 328, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: 00:00:00

You can use this line of code-
date_object = datetime.strptime(str(schedule_delivery), '%Y-%m-%d %H:%M:%S')

i know its was asked a long time ago, i hope its still relevant.
in order to avoid the remains of the time format you need to convert your datetime to date
this is the code that solved this issue for me:
self.start_date=str((datetime.strptime(str(self.start_date), '%Y-%m-%d')+timedelta(nDays)).date())
Blockquote

Change the date to a pandas date:
Time object e.g
a = pd.to_datetime(df['date']).apply(lambda x: x.date())
Then
date_object = datetime.strptime(str(schedule_delivery), '%Y-%m-%d')

Related

Error : strptime() argument 1 must be str, not int

I am trying to substract two times and getting an error. In below total error is coming up
if result[0]['outTime'] != None:
type = "bothPunchDone"
FMT = '%H:%M:%S'
total= datetime.strptime(result[0]['outTime'], FMT) - datetime.strptime(result[0]['inTime'], FMT)
I tried but not able to solve the issue.
from datetime import datetime
result = datetime.now().strftime("%H:%M:%S")
if result != None:
type = "bothPunchDone"
FMT = '%H:%M:%S'
total= datetime.strptime(result, FMT) - datetime.strptime(result, FMT)
print(total)
is working for me. Try to check the type of result[0]['outTime']

TypeError: strptime() argument 1 must be str, not datetime.date Python

Facing issues while converting date to string,
print(type(from_date))
from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d').date()
My from_date value is in an ini file,
from_date = 2018-01-01
My TraceBack log is,
Traceback (most recent call last):
File "Flexi_DailyToWeekly.py", line 87, in <module>
tuesday = get_data(session,from_keyspace, from_table, to_keyspace, to_table_tuesday, to_table_wednesday, to_table_thursday, to_table_friday, from_date, to_date)
File "Flexi_DailyToWeekly.py", line 11, in get_data
from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d')
TypeError: strptime() argument 1 must be str, not datetime.date'
I've used type(from_date) which returns from_date as a string.
Also tried,
from_date = datetime.datetime.strptime(str(from_date), '%Y-%m-%d').date()
still the same error persists.
Changed from_date as suggested in the answers below,
from_date = "2018-01-01"
The error persisits,
Traceback (most recent call last):
File "Flexi_DailyToWeekly.py", line 82, in <module>
from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d').date()
File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
(data_string, format))
ValueError: time data '"2018-01-01"' does not match format '%Y-%m-%d'
Replace
from_date = 2018-01-01
to
from_date : 2018-01-01
in ini file
Tested with code :
import configparser
import datetime
config = configparser.ConfigParser()
config.read('Test.ini')
print (config['DEFAULT']['date'])
from_date = config['DEFAULT']['date']
print(type(from_date))
from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d').date()
print (from_date)
and Test.ini is
[DEFAULT]
date : 2018-1-1
output is :
2018-1-1
<class 'str'>
2018-01-01
As per docs, The datetime.strptime() method accepts a date_string as the first argument. So, I tried this:
import datetime
test = datetime.datetime.strptime('2018-01-01', '%Y-%m-%d').date()
print(test)
#datetime.date(2018, 1, 1)
If I do from_date = 2018-01-01, I get an invalid token error for the type of from_date:
from_date = 2018-01-01
print(type(from_date))
File "<ipython-input-7-9e5449277912>", line 2
from_date = 2018-01-01
^
SyntaxError: invalid token
This means that from_date must be a string. You could use from_date = '2018-01-01' and that would work. If I check the type of from_date now, I will get a type of class string:
from_date = '2018-01-01'
print(type(from_date))
#<class 'str'>
But when I look at the first Traceback log, it raises a TypeError. This means that the value stored in from_date is of type datetime.date. Consider the following:
import datetime
from_date = datetime.date(2018, 1, 1)
test = datetime.datetime.strptime(str(from_date), '%Y-%m-%d').date()
print(test)
#2018-01-01

Python datetime.strptime - ValueError: time data does not match format

I am new in Python. I try write program who will read from file and write it to database. There is part of my code :
import datetime
format = '[%d/%b/%Y:%I:%M:%S '
mytime = '[29/Oct/2017:13:11:38 '
now = datetime.datetime.strptime(mytime, format)
When I run script I have this error :
now = datetime.datetime.strptime(mytime, format)
File "/usr/lib/python2.7/_strptime.py", line 332, in _strptime
(data_string, format))
ValueError: time data '[29/Oct/2017:13:11:38 ' does not match format '[%d/%b/%Y:%I:%M:%S '
I dont understood this mistake. It match format. There is no extra space or anything like that. And I use this 2 lines of code
format = '[%d/%b/%Y:%I:%M:%S '
now = datetime.datetime.strptime(mytime, format)
in another program and it works fine. Where is problem ?
Looking at the docs, %I matches hours for times in AM/PM format, meaning valid values range from 01 to 12. You have 13 (1 PM)
I guess you need to use %H
import datetime
mytime = '[29/Oct/2017:13:11:38 '
fmt = '[%d/%b/%Y:%H:%M:%S '
now = datetime.datetime.strptime(mytime, fmt)
print("now: %s" % now.strftime('%Y-%m-%d %H:%M:%S'))
prints:
now: 2017-10-29 13:11:38
Check this:
fmt = '[%d/%b/%Y:%I:%M:%S '
for test in range(1, 24):
mytime = '[29/Oct/2017:%02d:11:38 ' % test
now = datetime.datetime.strptime(mytime, fmt)
print("now: %s" % now.strftime('%Y-%m-%d %H:%M:%S'))
Will work "fine" (I quote because 12 is assumed to be midnight) until test hits 13:
now: 2017-10-29 01:11:38
now: 2017-10-29 02:11:38
now: 2017-10-29 03:11:38
now: 2017-10-29 04:11:38
now: 2017-10-29 05:11:38
now: 2017-10-29 06:11:38
now: 2017-10-29 07:11:38
now: 2017-10-29 08:11:38
now: 2017-10-29 09:11:38
now: 2017-10-29 10:11:38
now: 2017-10-29 11:11:38
now: 2017-10-29 00:11:38
Traceback (most recent call last):
File "./stack_096.py", line 12, in <module>
now = datetime.datetime.strptime(mytime, fmt)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/_strptime.py", line 500, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/_strptime.py", line 337, in _strptime
(data_string, format))
ValueError: time data '[29/Oct/2017:13:11:38 ' does not match format '[%d/%b/%Y:%I:%M:%S '

Date input format? [duplicate]

This question already has answers here:
AttributeError: 'datetime' module has no attribute 'strptime'
(5 answers)
Closed 5 years ago.
I have code like this:
import time
import datetime
def dates():
date1 = str(input('Date start: '))
try:
dt_start = datetime.strptime(date1, '%d, %m, %Y')
except ValueError:
print ("Incorrect format")
date2 = str(input('Date end: '))
try:
dt_end = datetime.strptime(date2, '%d, %m, %Y')
except ValueError:
print ("Incorrect format")
if date1 > date2:
print("Error!")
dates()
And I want to define date input format like d.m.Y.
For example, when I input "17.12.1995". I got error:
'module' object has no attribute 'strptime'.
How to define user input format?
datetime is a module, there is a class with the same name (datetime) within it and that class has a class method strptime(). You need to either call it like:
dt_start = datetime.datetime.strptime(date1, '%d, %m, %Y')
Or to change your import statement:
from datetime import datetime
so that it imports only the datetime class to your current namespace.

I'm receiving an error when formatting a date in Python, why?

This is my code for formatting a date:
def updateUserDBDates():
global userDB, currentDate, previousDate, changeInDate
index = 0
index2 = 0
userDB[1] = datetime.strptime("%d-%m-%Y", userDB[0])
userDB[0] = datetime.today().strftime("%d-%m-%Y")
saveData()
currentDate = userDB[0]
previousDate = userDB[1]
changeInDate = currentDate - previousDate
and I get this error:
File "/home/nathan/Documents/project001/programFiles/Project 001.py", line 170, in updateUserDBDates
userDB[1] = datetime.strptime("%d-%m-%Y", userDB[0])
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '%d-%m-%Y' does not match format '28-09-2013'
From what I can see everything should work fine, what is causing this error and how can I fix it easily?
datetime.datetime.strptime receive date_str, format as arguments (not format, date_str):
>>> import datetime
>>> datetime.datetime.strptime('28-09-2013', '%d-%m-%Y')
datetime.datetime(2013, 9, 28, 0, 0)
The argument ordering for strptime() is wrong.
http://docs.python.org/2/library/datetime.html#datetime.datetime.strptime

Categories

Resources