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')
Related
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')
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)
There is a source column of values from Excel that I would like to convert to dates (python).
df['DATE_'] = pd.to_datetime(df['DATE_'], format='%Y%m%d.0')
Source column from Excel:
'2010-06-16 00:00:00'
Unfortunately, system generates the following error.
Traceback (most recent call last):
File "C:/Users/103925alf1/PycharmProjects/p03/venv/Include/p03.py", line 5, in <module>
df['DATE_'] = pd.to_datetime(df['DATE_'], format='%Y%m%d.0')
File "C:\Users\103925alf1\PycharmProjects\p03\venv\lib\site-packages\pandas\core\tools\datetimes.py", line 728, in to_datetime
values = convert_listlike(arg._values, format)
File "C:\Users\103925alf1\PycharmProjects\p03\venv\lib\site-packages\pandas\core\tools\datetimes.py", line 435, in _convert_listlike_datetimes
raise e
File "C:\Users\103925alf1\PycharmProjects\p03\venv\lib\site-packages\pandas\core\tools\datetimes.py", line 400, in _convert_listlike_datetimes
arg, format, exact=exact, errors=errors
File "pandas\_libs\tslibs\strptime.pyx", line 142, in pandas._libs.tslibs.strptime.array_strptime
ValueError: time data '2010-06-16 00:00:00' does not match format '%Y%m%d.0' (match)
Process finished with exit code 1
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 "."