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.
Related
I'm trying to parse dates in the form of "Sept 9, 2021" using datetime
I was trying the following format:
"%m %d, %y" but it raises a ValueError while I'm trying to convert it
(i.e using int(datetime.strptime("Sept 9, 2021","%m %d, %y").timestamp()))
How can I tell which format am I supposed to use?
Thanks
For strptime there are different format code than usual.
You use %b to refer to a Three-digit string month, which means that you must use Sep instead of Sept, and you use %Y to convert a 4 digit year.
So the code would be like the following
int(datetime.datetime.strptime("Sep 9, 2021", "%b %d, %Y").timestamp())
# 1631138400
For more information see Format Codes.
To my knowledge the key work for September is Sep (not Sept).
You can check this link for more details on that: Python Date abbreviation
I advice you to use something similar to this snippet of code:
from datetime import datetime
date_s = "Sep 9, 2021"
datetime_object = datetime.strptime(date_s.upper().replace("SEPT", "SEP"), '%b %d, %Y')
print(datetime_object)
You can also check this Stack thread that seem similar to your question:
Date Handling in Python for Sept
There are a couple of ways how to get the timestamp from your string:
First solution is to use dateparser module from PyPi (pip install dateparser). This module can help you to get datetime object from any string which is similar to datetime string.
Solution:
import dateparser
date_time_object = dateparser.parse("Sept 9, 2021").timestamp()
print(date_time_object)
Result: 1631127600.0
Another issue is that your string contains one unnecessary string in name of month so this solution is a bit wrong and won't help you but only for understanding, I will offer this:
Solution:
from datetime import datetime
time_stamp = int(datetime.strptime("Sep 9, 2021","%b %d, %Y").timestamp())
print(time_stamp)
Result: 1631127600
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
I have string date data with the following format:
4/16/15 23:50
When I try to convert into a datetime object:
print datetime.datetime.strptime(fecha2, '%d/%m/%y %H:%M')
I get this error:
ValueError: time data '4/16/15 23:50' does not match format '%d/%m/%y %H:%M'
According to this list:
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior, I am using the right format. Where am I wrong?
you have month and day reversed:
print datetime.datetime.strptime(fecha2, '%m/%d/%y %H:%M')
Also very handy:
from dateutil.parser import parse
parse("4/16/15 23:50")
I know this topic is old but maybe it will help someone. I have faced recently similar issue where the date was actually correctly typed and it throwed the same error:
datetime.datetime.strptime('2018-05-02T14:27:56+00:00', "%Y-%m-%dT%H:%M:%S%z")
error: time data '2018-05-02T14:27:56+00:00' does not match format '%Y-%m-%dT%H:%M:%S%z'
The issue was that i was trying to run the python 3.7 code with python 3.6
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