python3 cant convert string to datetime object - python-3.x

I have the following code:
from datetime import datetime
date_time_str = '2020-07-17 21:59:49.55'
date_time_obj = datetime.strptime(date_time_str, '%y-%m-%d %H:%M:%S.%f')
print "The type of the date is now", type(date_time_obj)
print "The date is", date_time_obj
Which results in the err:
Traceback (most recent call last):
File "main.py", line 5, in <module>
date_time_obj = datetime.strptime(date_time_str, '%y-%m-%d %H:%M:%S.%f')
File "/usr/lib/python2.7/_strptime.py", line 332, in _strptime
(data_string, format))
ValueError: time data '2020-07-17 21:59:49.553' does not match format '%y-%m-%d %H:%M:%S.%f'
Why cant I convert this date? The following example works:
date_time_str = '18/09/19 01:55:19'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')

First of all, this is not valid Python 3 code. You've used the Python 2 print statement in your code, and trying to run this on Python 3 causes a SyntaxError.
As the error indicates, your date string does not match the format you specified. Take a look at the format codes; the first issue I notice is that you give a 4-digit year (2020) but try to line it up with %y, which is for two-digit year. There may be other issues as well, which should be easy to find looking through that table.

Related

How to convert string timeseries to datetime object in a dataframe while preserving the format of the original datetime?

I have a pandas dataframe whose first column is string representation of timeseries. I would like to convert this column into datetime object using the following line of code:
df['Timestamp'] = pd.to_datetime(
df['Timestamp'], format='%m-%d-%Y %H:%M:%S').dt.strftime('%Y-%m-%d %H:%M:%S')
I want to preserve the format in the datetime object to be month first and year last but I am receiving an error message:
df['Timestamp'] = pd.to_datetime(
File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\pandas\core\tools\datetimes.py", line 1051, in to_datetime
values = convert_listlike(arg._values, format)
File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\pandas\core\tools\datetimes.py", line 394, in _convert_listlike_datetimes
res = _to_datetime_with_format(
File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\pandas\core\tools\datetimes.py", line 514, in _to_datetime_with_format
raise err
File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\pandas\core\tools\datetimes.py", line 501, in _to_datetime_with_format
res = _array_strptime_with_fallback(
File "C:\ProgramData\Anaconda3\envs\tf\lib\site-packages\pandas\core\tools\datetimes.py", line 437, in _array_strptime_with_fallback
result, timezones = array_strptime(arg, fmt, exact=exact, errors=errors)
File "pandas\_libs\tslibs\strptime.pyx", line 150, in pandas._libs.tslibs.strptime.array_strptime
ValueError: time data ' 06-30-2022 19:41:15' does not match format '%m-%d-%Y %H:%M:%S' (match)
The format inside pd.to_datetime is forcing pd.to_datetime to seek only datetimes with this format. Remove it and just apply dt.strftime:
df['Timestamp'] = pd.to_datetime(df['Timestamp']).dt.strftime('%Y-%m-%d %H:%M:%S')
There is a space before your month value: ' 06-30-2022 19:41:15'. You need to strip the string before applying the to_datetime. Something like this:
df['Timestamp'] = pd.to_datetime(
df['Timestamp'].str.strip(), format='%m-%d-%Y %H:%M:%S'
).dt.strftime('%Y-%m-%d %H:%M:%S')

Python strptime cannot understand timezone offset

I have a very simple timestamp I need to parse:
10/2/2020 3:19:42 PM (UTC-7)
But using python 3.6, when I try to parse this, I get the following:
>>> datetime.strptime('10/2/2020 3:19:42 PM (UTC-7)', '%m/%d/%Y %I:%M:%S %p (%Z%z)')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '10/2/2020 3:19:42 PM (UTC-7)' does not match format '%m/%d/%Y %I:%M:%S %p (%Z%z)'
I have tried dateutil.parser, as well as several variations of the format string. The piece that's tripping up strptime is the (UTC-7) portion.
Is the string format wrong? How can I parse this string and receive the timezone information as well? Any help is appreciated.
Edit: If the string is (UTC-0700) then the parsing works. But I cannot control how the timestamps are being formatted, is there a way to parse them in their current format (UTC-7)?
Ah, it turned out to be quite silly:
>>> import dateutil
>>> dateutil.parser.parse(dt, fuzzy=True)
datetime.datetime(2020, 10, 2, 15, 19, 42, tzinfo=tzoffset(None, 25200))
Should have used fuzzy logic before. :-)
EDIT: The above does NOT work (thanks to #wim for pointing it out) - Fuzzy flag is ignoring the sign of the offset string.
Here is code that works:
>>> from datetime import datetime
>>> import re
>>> dt = '10/2/2020 3:19:42 PM (UTC-7)'
>>> sign, offset = re.search('\(UTC([+-])(\d+)\)', dt).groups()
>>> offset = f"0{offset}00" if len(offset) == 1 else f"{offset}00"
>>> dt = re.sub(r'\(UTC.\d+\)', f'(UTC{sign}{offset})', dt)
>>> datetime.strptime(dt, '%m/%d/%Y %I:%M:%S %p (%Z%z)')
datetime.datetime(2020, 10, 2, 15, 19, 42, tzinfo=datetime.timezone(datetime.timedelta(-1, 61200), 'UTC'))

change String to datetime from an input file

I am getting an input file which contains datetime. My purpose is to read the file and filter the db.
Input file sample
2019-05-31 15:21:53
Below code work fine in my PC (python 3.6.4)
f=open("lastRun.txt", "r+")
lastHour=(f.read())
f.close()
lHourDate = datetime.datetime.strptime(lastHour, '%Y-%m-%d %H:%M:%S')
print(lHourDate)
however in production we have (Pyhton 3.6.7)
where the same code is throwing below error.
Traceback (most recent call last):
File "hourlyLoadEscenic.py", line 37, in <module>
lHourDate = datetime.strptime(lastHour, "%Y-%m-%d %H:%M:%S")
File "/usr/local/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/local/lib/python3.6/_strptime.py", line 365, in _strptime
data_string[found.end():])
ValueError: unconverted data remains:
I can do 'print' and see the lastHour has the input field. but in date conversion it is throwing error.
Please suggest

ValueError: unconverted data remains: 10

I have dates in string format delimited with space, and I am trying to use datatime to convert them to dates so later on I can sort my data based on the name's column and date's column.Here is my code:
from datetime import datetime
DATE = '1 nov 2010'
current_date = datetime.strptime(DATE, '%d %b %y')
print(current_date)
and here is the error I am getting:
Traceback (most recent call last):
File "DEL.py", line 4, in <module>: current_date = datetime.strptime(DATE, '%d %b %y')
File "lib\_strptime.py", line 565, in _strptime_datetime: tt, fraction = _strptime(data_string, format)
File "lib\_strptime.py", line 365, in _strptime:
data_string[found.end():])
ValueError: unconverted data remains: 10
I have checked all the previous questions on this topic and it seems none of them apply in this scenario as I do not have any hidden character or anything and it is as simple as typing the dates. Appreciate if someone can help me out with this.
The %y parameter of strptime is "Year without century". An uppercase %Y is what you need.

How to get time in MM:SS format? (python3)

I'm trying to write a program that takes a certain time in MM:SS format, but whenever I try this:
a = input("Enter time here: ")
I get:
Traceback (most recent call last):
File "X", line 26, in <module>
if switch == False and (int(inpt[i])>=0 or int(inpt[i]) <= 9):
ValueError: invalid literal for int() with base 10: ':'
Am I supposed to use the datetime module to make this work? If so, how?
Thanks in advance :)

Resources