My time format is screwy, but it seemed workable, as a string with the following format:
'47:37:00'
I tried to set a variable where:
DT = '%H:%M:%S'
So I could find the difference between two times, but it's given me the following error:
ValueError: time data '47:37:00' does not match format '%H:%M:%S'
Is it possible there are more elements to my time stamps than I thought? Or that it's formatted in minutes/seconds/milliseconds? I can't seem to find documentation that would help me determine my time format so I could set DT and do arithmetic on it.
It's because you set 47 to %H, that is not a proper value.
Here is an example:
import datetime
dt = datetime.datetime.strptime('2016/07/28 12:37:00','%Y/%m/%d %H:%M:%S')
print dt
Output: 2016-07-28 12:37:00
You wrote "I can't seem to find documentation that would help me determine my time format so I could set DT and do arithmetic on it"
Try this: https://docs.python.org/3/library/datetime.html
Way down to the bottom.
And yes, when the %H is matched with 47, you get boom the error.
Related
I am trying to convert some data from a .txt file to a dataframe to use it for some analysis
the form of the data in the .txt is a follows
DATE_TIME VELOC MEASURE
[m/s] [l/h]
A 09.01.2023 12:45:20 ??? ???
A 09.01.2023 12:46:20 0,048 52,67
A 09.01.2023 12:47:20 0,049 53,77
A 09.01.2023 12:48:20 0,050 54,86
I load the data to a dataframe no problem i covnert the str values of the measurement to float etc everything is good as shows in the
image
the problem I get is when trying to convert the column of the date time that is string to datetime pandas format using this line of code:
volume_flow['DATE_TIME'] = pd.to_datetime(volume_flow['DATE_TIME'], format = '%d.%m.%Y %H:%M:S')
and i get the following error
ValueError: time data '09.01.2023 12:46:20' does not match format '%d.%m.%Y %H:%M:S' (match)
but i don't see how the format is off
I am really lost as to why this is caused as i used the same code with different formats of datetime before with no problem
further more i tried using format = '%dd.%mm.%yyyy %H:%M:S' as well with the same results and when i let the pandas.to_datetime convert it automatically it confuses the day and the month of the data. the data is between 09.01-12.01 so you can't really tell if one is the month or day just by the values.
I think you should go from this
(..., format='%d.%m.%Y %H:%M:S')
to this
(..., format='%d.%m.%Y %H:%M:%S')
You forgot the percentage character!
check the documentations for correct time format. You will note that the directive %S represents the seconds.
Second as a decimal number [00,61].
so i am taking the difference between two times (2022-07-20 23:10:00.990000) and 2022-07-20 23:10:02.100000. that gives me back 0:00:01.110000. i want that to transform to HH:MM:SS without the microseconds. the easiest way to take off microseconds is to do "avg_inqueue_time = str(avg_inqueue_time).split(".")[0]" that will give me 0:00:01. then i try to do avg_inqueue_time_transformed = datetime.strptime('%H:%M:%S', avg_inqueue_time) but gives an error ValueError: time data '%H:%M:%S' does not match format '0:02:07'.
any ideas how to transform that?
I believe a fast approach is to take a different time delta object where you only pick the seconds.
import datetime
time_1 = datetime.datetime(2022,7,20,23,10,00,990000)
time_2 = datetime.datetime(2022,7,20,23,10,2,100000)
timediff=(time_2-time_1)
timediff_wo_microseconds = datetime.timedelta(days=timediff.days, seconds=timediff.seconds)
print(timediff_wo_microseconds)
I am using datetime.strptime() to convert a string containing time and date from a sensor into a datetime object.
The code sometimes fails. Minimal example:
datetime.strptime('1/9/2021 24:01:53', '%d/%m/%Y %H:%M:%S')
Output error:
ValueError: time data '1/9/2021 24:01:53' does not match format '%d/%m/%Y %H:%M:%S'
I am guessing this has to do with the fact that the time is more than 23:59:59 - which seems to me a non-realistic time (I would think that 1/9/2021 24:01:53 could potentially be 2/9/2021 00:01:53 - a time format which I have never seen).
Is this a non-standard way of representing time or possibly a hardware/software issue with the sensor acquisition system? If it is a different way of representing time, how can I convert it to a standard datetime object?
Kind regards,
D.F.
If the hour exceeds 23 in a variable representing time, a good option is to create a timedelta from it, which you can then add to a datetime object. For given example that might look like
from datetime import datetime, timedelta
def custom_todatetime(s):
"""
split date/time string formatted as 'DD/MM/YYYY hh:mm:ss' into date and time parts.
parse date part to datetime and add time part as timedelta.
"""
parts = s.split(' ')
seconds = sum(int(x) * 60 ** i for i, x in enumerate(reversed(parts[1].split(':'))))
return datetime.strptime(parts[0], "%d/%m/%Y") + timedelta(seconds=seconds)
s = '1/9/2021 24:01:53'
print(custom_todatetime(s))
# 2021-09-02 00:01:53
Note: conversion of hh:mm:ss to seconds taken from here - give a +1 there if helpful.
I have a time string obtained from API, it's UTC+0. I would like to change to other time zone.
I have tried below but it doesn't work. Could you please give me some idea ? many thanks.
utc0time='2021-04-17T15:50:14.614646+00:00'
dt = datetime.strptime('utc0time', '%Y-%m-%dT%H:%M:%S%z'). #it results as an error, not match the format
time.mktime(dt.timetuple())
calendar.timegm(dt.timetuple())
You could actually use timedelta in datetime module to +/- number of hours to achieve the time in other timezone you wish.
Here is an example where you can use timedelta:
https://www.geeksforgeeks.org/python-datetime-timedelta-function/
thanks for the comments and it gave me the idea. Because i only need to convert from one time zone to another one, i don't need to convert to multi-timezone. I don't use pytz this time. I used a silly method, changed the str to timestamp first, then used timedelta to adjust the hours. Below is my final code.
utc0time='2021-04-17T15:50:14.614646+00:00'
utc0time = utc0time[:-13]
timestamp = time.mktime(time.strptime(utc0time, '%Y-%m-%dT%H:%M:%S'))
datatimeformat = datetime.fromtimestamp(timestamp)
utc8time = datatimeformat + timedelta(hours = 8)
I am attempting to create a time series index using pandas. Currently this is the code I am running:
date_string = df3["Date"]
date_times = pd.to_datetime(date_string, yearfirst=True, format='%Y%m%d%H%M')
df3_i = df3.set_index(date_times)
Yet I am getting constant errors. Can anyone explain?
Error:
ValueError: time data '2017-03-08 13:53' does not match format '%Y%m%d%H:%M' (match)
That's because the format is '%Y-%m-%d %H:%M'
There are special character combinations that are meant to represent the numeric components of the date and time. A great reference can be found here
You have a time string of '2017-03-08 13:53' as evidenced by you error message. From the link you'll find that:
4 digit year is '%Y'
2 digit month is '%m'
2 digit day is '%d'
2 digit hour is '%H'
2 digit minute is '%M'
So you still need to represent the other string bits like the dashes, space, and the colon
Thus '%Y-%m-%d %H:%M'
Use this instead
date_string = df3["Date"]
date_times = pd.to_datetime(date_string, yearfirst=True, format='%Y-%m-%d %H:%M')
df3_i = df3.set_index(date_times)
If that doesn't work, then you have inconsistent date formats and my first course of action would be to yell at whoever created the thing I'm trying to parse.
If that happens to be your scenario, ask another question... Or I might.