I have a date string that I am trying to format using python datetime.
print(datestr, type(datestr)
2006-06-09T00:00:00 <class 'str'>
from datetime import datetime
import numpy as np
lastDate = datetime.strptime(datestr, '%Y-%m-%d')
# Calculate Months since
mosLast = (pd.to_datetime('today') - lastDate)/np.timedelta64(1, 'M')
Traceback:
lastDate = datetime.strptime(datestr, '%Y-%m-%d')
File "/Applications/Anaconda/anaconda3/lib/python3.9/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/Applications/Anaconda/anaconda3/lib/python3.9/_strptime.py", line 352, in _strptime
raise ValueError("unconverted data remains: %s" %
ValueError: unconverted data remains: T00:00:00
I resolved this with:
# Format date
lastDate = datetime.strptime(datestr, '%Y-%m-%dT%H:%M:%S').strftime('%Y-%m-%d')
lastDate = datetime.strptime(datestr, '%Y-%m-%d')
# Calculate Months since last sale date
mosLastSale = (pd.to_datetime('today') - lastDate)/np.timedelta64(1, 'M')
Related
The format of my csv file is this
Zeitstempel;Iteration;lag
"2022-01-26T22:28:11.347Z","1","2"
"2022-01-26T22:28:11.348Z","2","1"
and my python code is this
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
csv_data = 'lag.log'
df = pd.read_csv(csv_data, encoding='latin1', sep=',', header=1,
names=['Zeitstempel', 'Iteration', 'Lag'])
df_cleaned = df.dropna()
x = df_cleaned['Zeitstempel'].values
y = df_cleaned['Lag'].values
#2022-01-26T21:59:30.810Z
dates = [dt.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.{0}Z").date() for date in x]
plt.plot_date(dates, y)
plt.show()
and the console output is this
Traceback (most recent call last):
File "chart.py", line 19, in <module>
dates = [dt.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.{0}Z").date() for date in x]
File "chart.py", line 19, in <listcomp>
dates = [dt.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.{0}Z").date() for date in x]
File "/usr/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/usr/lib/python3.8/_strptime.py", line 349, in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data '2022-01-26T22:28:11.348Z' does not match format '%Y-%m-%dT%H:%M:%S.{0}Z'
Is there a way to convert the iso string to a datetime
Python's datetime.fromisoformat does not accept the 'Z' suffix, so you'll need to remove it.
from datetime import datetime
def datetime_from_js_isoformat(string: str) -> datetime:
"""Creates a datetime object from a JavaScript ISO format string."""
if string.endswith('Z'):
return datetime.fromisoformat(string[:-1])
return datetime.fromisoformat(string)
My issue is ValueError which is rising, because of bad formatting of my datetime string.
# month is given. Sometimes with seconds as '.Z' (int) and sometimes as '.0000Z' (float)
try:
next_month = datetime.datetime.strptime(month, '%Y-%m-%dT%H:%M:%S.%fZ')
except ValueError:
print('asd')
next_month = datetime.datetime.strptime(month, '%Y-%m-%dT%H:%M:%SZ')
next_month += relativedelta.relativedelta(months=1)
Django error trace stack is hard to copy, but here is the raise:
raise ValueError("time data %r does not match format %r" %(data_string, format))
With this, I'm pretty sure, it's about datetime. (about datetime, hehe)
Found some questions on this topic, but none of proposed solutions worked. Most of them where about not placing the exact piece of code that generates this error in try-except.
The question is: why does ValueError is not being handled ?
EDIT: reporoduction snippet:
Below code run in PyCharm (python3.7, same as in above use), does not reproduce this error.
import datetime
if __name__ == "__main__":
d1 = '2021-03-24T11:20:33Z'
d2 = "2021-03-10T20:45:01.036000Z"
d = d2 # for quick change
try:
next_month = datetime.datetime.strptime(
d, '%Y-%m-%dT%H:%M:%S.%fZ')
print('try: ', next_month)
except ValueError:
next_month = datetime.datetime.strptime(
d, '%Y-%m-%dT%H:%M:%SZ')
print('except: ', next_month)
EDIT2:
In django error page: ValueError at /api/sessions_stats_csv/
Traceback (most recent call last):
django_server_dev | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
django_server_dev | response = get_response(request)
django_server_dev | File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 179, in _get_response
django_server_dev | response = wrapped_callback(request, *callback_args, **callback_kwargs)
django_server_dev | File "/app/label_it/rest_api/statistics_view.py", line 334, in AllSessionsDetailsCSV
django_server_dev | rows = ExtractSessionsForUser(item, params['for_month'])
django_server_dev | File "/app/label_it/rest_api/statistics_view.py", line 485, in ExtractSessionsForUser
django_server_dev | format_date = datetime.datetime.strptime(
django_server_dev | File "/usr/local/lib/python3.9/_strptime.py", line 568, in _strptime_datetime
django_server_dev | tt, fraction, gmtoff_fraction = _strptime(data_string, format)
django_server_dev | File "/usr/local/lib/python3.9/_strptime.py", line 349, in _strptime
django_server_dev | raise ValueError("time data %r does not match format %r" %
django_server_dev | ValueError: time data '2021-03-24T11:20:33Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'
how must I change an element from a list that is in this form: 05/06/2020, to get a date object like:
date_object = datetime.strptime(listelement, '%m/%d/%y') ?
Here is my Code:
daten = {}
with open("Observed_Monthly_Rain_Gauge_Accumulations_-_Oct_2002_to_May_2017.csv", 'r') as csvfile:
regen_csv = csv.reader(csvfile)
next(regen_csv, None)
for rows in regen_csv:
keys = rows[:1]
strt= str(keys[0]) in
date_object = datetime.strptime(strt, '%m/%d/%y')
values = rows[1:]
daten[strt] = values
print(daten)
Traceback (most recent call last):
File *directory*, line 16, in <module>
date_object = datetime.strptime(strt, '%m/%d/%y')
File "C:\Program Files\Python37\lib\_strptime.py", line 577, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "C:\Program Files\Python37\lib\_strptime.py", line 362, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: 02
Heres a link to the list with the data:
https://data.seattle.gov/api/views/rdtp-hzy3/rows.csv?accessType=DOWNLOAD
You need to use %Y for years in the form 2020. A full list of format codes can be found here.
date_object = datetime.strptime(strt, '%m/%d/%Y')
I'm trying to dynamically convert a string seconds like below to a string date.
'1545239561 +0100'
The problem is the timezone inserted at the end, and I can't find any python time object method using the good format to retrieve the date from this string.
My tries :
>>>seconds = '1545239561 +0100'
>>>time.strftime('%y%m%d-%H%M%S-%f', datetime.datetime.fromtimestamp(seconds)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)
>>>time.strptime(seconds)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 559, in _strptime_time
tt = _strptime(data_string, format)[0]
File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '1545239561 +0100' does not match format '%a %b %d %H:%M:%S %Y'
>>>time.strptime(seconds, "%S +%Z")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 559, in _strptime_time
tt = _strptime(data_string, format)[0]
File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '1545239561 +0100' does not match format '%S +%Z'
I would try to process both those value separately and merge them into single datetime:
>>>from datetime import datetime
>>>s = '1545239561 +0100'
>>>seconds, offset = s.split()
>>>datetime.fromtimestamp(int(seconds)).replace(tzinfo=datetime.strptime(offset, "%z").tzinfo)
datetime.datetime(2018, 12, 19, 17, 12, 41, tzinfo=datetime.timezone(datetime.timedelta(0, 3600)))
Yes #mfrackwiak...
I did this
>>> epoch = "1545239561 +0100"
>>> seconds, offset = epoch.split()
>>> datetime.fromtimestamp(int(seconds)).replace(tzinfo=datetime.strptime(offset, "%z").tzinfo).strftime('%Y-%m-%d %H:%M:%S-%Z')
'2018-12-19 18:12:41-UTC+01:00'
>>>
You can try the below example :
from datetime import datetime,timedelta
# split the timevalue and offset value
ss = '1545239561 +0100'.split()
format = "%A, %B %d, %Y %I:%M:%S"
# calculate the hour and min in the offset
hour = int(ss[1][0:2])
min = int(ss[1][2:])
# calculate the time from the sec and convert it to datetime object
time_from_sec = datetime.strptime(datetime.fromtimestamp(int(ss[0])).strftime(
format), format)
# add the offset delta value to the time calculated
time_with_delta_added = time_from_sec + timedelta(hours=hour,minutes=min)
print(time_with_delta_added)
Output :
2018-12-19 12:22:41
I'm trying to learn the functions of Python but there is one thing I came across which I cannot figure out.
calculated_time = '2014.03.08 11:43:12'
>>> calculated_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
>>> print calculated_time
2017-09-22 15:59:34
Now when I run:
cmpDate = datetime.strptime(calculated_time, '%Y.%m.%d %H:%M:%S')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/_strptime.py", line 332, in _strptime
(data_string, format))
ValueError: time data '2017-09-22 16:35:12' does not match format'%Y.%m.%d %H:%M:%S'
I can't understand why, if I directly pass this date then it is running but when I pass it after storing in a variable then I've got an error.
They are not the same formats :
'%Y.%m.%d %H:%M:%S'
'%Y-%m-%d %H:%M:%S'
Notice the different separators between year, month and day?
You need to use a consistent datetime format between strftime and strptime.
As an example :
>>> from datetime import datetime
>>> datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2017-09-22 15:06:51'
>>> datetime.strptime(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "%Y-%m-%d %H:%M:%S")
datetime.datetime(2017, 9, 22, 15, 3, 52)
>>> datetime.strptime(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "%Y.%m.%d %H:%M:%S")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/media/steam/anaconda3/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/media/steam/anaconda3/lib/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data '2017-09-22 15:03:57' does not match format '%Y.%m.%d %H:%M:%S'
>>> datetime.now().strftime("%Y.%m.%d %H:%M:%S")
'2017.09.22 15:06:55'
>>> datetime.strptime(datetime.now().strftime("%Y.%m.%d %H:%M:%S"), "%Y.%m.%d %H:%M:%S")
datetime.datetime(2017, 9, 22, 15, 4, 3)
because you specified a format of '%Y.%m.%d %H:%M:%S' but you put '-' between the date elements instead of "."