Code:
pd.to_datetime(dataset['startdate'] ,format="%Y-%m-%d %H:%M:%S%Z")
I got following error
ValueError: time data '2020-02-25 14:56:05+01' does not match format '%Y-%m-%d %H:%M:%S%Z' (match)
help much appreciate.
Your format has a %Z (uppercase) at the end, which according to docs timezone name (like GMT, PSD …). You probably want to use %z (lowercase) which is UTC offset (like ±HHMM[SS[.ffffff]]). However not sure if that would work +01, you might need +0100.
Times series data contains +01 and +02 as well due to daylight saving. That was causing an error. Should be using %z as well
Related
I have a date as "1-Jun". How can I convert this to 01/06? I am using Strptime. I thought this is going to be easy but it is not.
Error that I am getting: time data '1-Jun' does not match format '%d-%Mmm'.
This is the command I am using. Can anyone help me with this?
datetime.datetime.strptime(date, '%d-%Mmm').strftime('%m/%d')
There's no such format as %Mmm, what you need to match Jun is %b ("Locale's abbreviated month name"). Also, if you want 01/06 rather than 06/01 it is going to be '%d/%m' in strftime:
print(datetime.datetime.strptime('1-Jun', '%d-%b').strftime('%d/%m'))
I tried to submit a time var with value of 2016-03-12T01:47:57+00:00 in a timestamp field, it gives me error saying to check the syntax for errors, however when I use a function to normalize the date
t = datetime.datetime.strptime(data['time'], '%Y-%m-%dT%H:%M:%SZ').strftime('%Y-%m-%d:%H:%M:%S')
I get an error like this.
time data '2016-03-12T01:47:57+00:00' does not match format '%Y-%m-%dT%H:%M:%SZ'
What's causing your problem has already been clarified by others, but please allow me to suggest my favorite solution for cases such as yours:
from dateutil import parser
parser.parse(data['time'])
More about the dateutil module here.
There are a few problems here:
The %Z (note the captial Z!) is for time zone, for example GMT. I
think you want the lower case option: %z, which is for UTC offset.
You can read here in the docs what all the options do :)
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
You need the % symbol before each option. You cannot write %Sz, you must write %S%z. Otherwise Python is trying to match something like 2016-03-12T01:47:57z, rather than 2016-03-12T01:47:57+00:00
Unfortunately, you can't use the %z option with strptime, see this answer:
ISO to datetime object: 'z' is a bad directive
My solution:
It sounds like you don't even want to use the UTC offset. That's fine! If you can't change the way your date string is generated, perhaps this is the best option (though it's maybe a little dirty):
t = datetime.datetime.strptime(data['time'][:-6], '%Y-%m-%dT%H:%M:%S')
This will remove the UTC offset from the string.
If you can change the way your datetime string is being generated, that would be a better solution, but I realise you might not be able to do so.
I hope this helps!
I'm using the following code:
data['Input_volTargetStart'][1]>time.strptime(data['Dates'][1], "%d %b $y")
When I try to run it, I get this error:
ValueError: time data '04-Jun-99' does not match format '%d %b $y'
I have tried possibly all combinations but am unable to get the result for this conversion.
You can actually put the dashes ('-') as part of the format, i.e "%d-%b-%y".
This is what you'll need.
from datetime import datetime
a = "04-Jun-99"
frmt = datetime.strptime(a, "%d-%b-%y")
print(frmt)
>>1999-06-04 00:00:00
Implement that example into your code to get proper output.
I am somewhat new to Python and have a seemingly simple question.
I have a python script that interacts with an API (RHN Satellite if you're curious). This API returns a date in the form of a string and it always trims leading 0's. For example, 6/1/13 or 10/9/12. I need to convert this string to a date and determine the day of the year it is.
Here is what I know:
today = datetime.datetime.now()
print today.strftime('%j')
...will return today's day of year (175). This works fine for a datetime object but I am having trouble converting the string given by the API to an actual date. If I use:
date = datetime.datetime.strptime(var, '%m/%d/$y')
I get error:
ValueError: time data '5/2/13' does not match format '%m/%d/$y'
I'm guessing because it's expecting leading 0's ? How do I get around this?
In the end, I am trying to subtract the variable date given from the current date but I can't do that until I convert the string.
Thanks for the help!
I think you just have a typo, use %y instead of $y:
date = datetime.datetime.strptime(var, '%m/%d/%y')
This code works for me, provided you change $y to %y in the format code.
Correct the $y to %y and I'd use format instead of strftime:
from datetime import datetime
print format(datetime.strptime('5/2/13', '%m/%d/%y'), '%j')
a have a litle problem to parse a string into a datetime object in python.
The following code working for some values but not always and a dont know whats wrong with this litel peace of code.
datetime.datetime.strptime("22.12.2012 17:00", '%d.%m.%Y %I:%M')
ValueError: time data '22.12.2012 17:00' does not match format '%d.%m.%Y %I:%M'
I think the problem clould somthing with the time and am/pm?. Because the error pops only by time > 12:00 and the string "22.12.2012 17:00" works fine.
Thanks for help
Try the %H token instead of %I:
import datetime
datetime.datetime.strptime("22.12.2012 17:00", '%d.%m.%Y %H:%M')
%I is for 12-hour date format. %H is for 24-hour date format, as explained in the docs: http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior