I'm converting the date of this string in this way, but I get the error "time data 'Aug 6, 2022, 10:44 AM' does not match format '%m %d, %Y, %I:%Mp'"
fechaDAT = 'Aug 6, 2022, 10:44 AM'
dateC = datetime.strptime(fechaDAT, "%m %d, %Y, %I:%Mp")
here is the right format :
dateC = datetime.strptime(fechaDAT, "%b %d, %Y, %I:%M %p")
%b for month abbrevation
%p for locale AM/PM
Related
today = datetime.strptime('Sep 3 2021 2:58 pm', '%b %d %Y %I:%M %p')
Returns the following error: time data Sep 03 2021 2:58 pm does not match format %b %d %Y %I:%M %p
When I run it, it works fine. Perhaps you can try the following:
today = datetime.strptime('Sep 3 2021 2:58 pm', '%b %-d %Y %I:%M %p')
%-d should be used if the day number is not zero-padded (i.e. Sep 3 instead of Sep 03). According to this website this is platform-specific, so perhaps you are using a platform that requires you to specify it is not zero-padded.
I am getting the following error:
ValueError: time data 'Feb 1, 2017 0:03 pm' does not match format '%b %d, %Y %I:%M %p'
Here is the code :
from datetime import datetime
latest_datetime = 'Feb 1, 2017 0:03 pm'
datetime_obj = datetime.strptime(latest_datetime, "%b %d, %Y %I:%M %p")
I'm unable to figure out why I get the error.
A 12-hour clock has no 0 hour; %I will only match 1 through to 12. Your timestamp has an impossible time in it:
0:03 pm
From the strftime() and strptime() Behavior documentation:
%I
Hour (12-hour clock) as a zero-padded decimal number.
01, 02, ..., 12
Assuming 0 is really 12, you could repair this by replacing the ' 0:' with '12:' (note the leading space for the zero!):
>>> from datetime import datetime
>>> latest_datetime = 'Feb 1, 2017 0:03 pm'
>>> datetime.strptime(latest_datetime.replace(' 0:', '12:'), "%b %d, %Y %I:%M %p")
datetime.datetime(2017, 2, 1, 12, 3)
It doesn't really matter if you have one or two spaces between the year and the hour, the string will be parsed either way.
I have date in string:
Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)
and I use (according to https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior):
datetime.strptime(datetime_string, '%a %b %m %Y %H:%M:%S %z %Z')
but I get error:
ValueError: 'z' is a bad directive in format '%a %b %m %Y %H:%M:%S %z %Z'
How to do it correctly?
%z is the +0200, %Z is CEST. Therefore:
>>> s = "Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)"
>>> datetime.strptime(s, '%a %b %d %Y %H:%M:%S GMT%z (%Z)')
datetime.datetime(2016, 10, 4, 12, 13, tzinfo=datetime.timezone(datetime.timedelta(0, 7200), 'CEST'))
I also replaced your %m with %d; %m is the month, numerically, so in your case 04 would be parsed as April.
python datetime can't parse the GMT part (You might want to specify it manually in your format). You can use dateutil instead:
In [16]: s = 'Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)'
In [17]: from dateutil import parser
In [18]: parser.parse(s)
Out[18]: d = datetime.datetime(2016, 10, 4, 12, 13, tzinfo=tzoffset(u'CEST', -7200))
In [30]: d.utcoffset()
Out[30]: datetime.timedelta(-1, 79200)
In [31]: d.tzname()
Out[31]: 'CEST'
Simpler way to achieve this without taking care of datetime formatting identifiers will be the usage of dateutil.parser(). For example:
>>> import dateutil.parser
>>> date_string = 'Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)'
>>> dateutil.parser.parse(date_string)
datetime.datetime(2016, 10, 4, 12, 13, tzinfo=tzoffset(u'CEST', -7200))
If you want to parse all you datetime data in a column in pandas DataFrame, you can use apply method to apply together with dateutil.parser.parse to parse whole column:
from dateutil.parser import parse
df['col_name'] = df['col_name'].apply(parse)
I have a DataFrame column where value is of string type 'June 6, 2016, 6' and I want to convert it into DataTime as 'YYYY-MM-DD HH:MM' format.
When tried convert by just taking value , I could able to convert it into right format.
import datetime
stringDate = "June 6, 2016, 11"
dateObject = datetime.datetime.strptime(stringDate, "%B %d, %Y, %H")
print dateObject
**Output : 2016-06-06 11:00:00**
But when I tried different options to apply the same conversion on python dataframe columns I'm not getting time part in the conversion.
**Option1**
df['Date'] = df.Date.apply(lambda x: dt.datetime.strptime(x, "%B %d, %Y, %H").date())
**Option2**
df['Date'] = pd.to_datetime(df['Date'] = df.Date.apply(lambda x: dt.datetime.strptime(x, "%B %d, %Y, %H"))
Output: both cases got 2016-06-06
Any suggestions will be appreciated.
I think you need add parameter format to to_datetime:
print (pd.to_datetime('June 6, 2016, 11', format='%B %d, %Y, %H'))
2016-06-06 11:00:00
It works with DataFrame too:
df = pd.DataFrame({'Date':['June 6, 2016, 11', 'May 6, 2016, 11']})
print (df)
Date
0 June 6, 2016, 11
1 May 6, 2016, 11
print (pd.to_datetime(df['Date'], format='%B %d, %Y, %H'))
0 2016-06-06 11:00:00
1 2016-05-06 11:00:00
Name: Date, dtype: datetime64[ns]
def deadlines(t):
'''shows pretty time to deadlines'''
fmt = '%a %d %m %Y %I:%M %p %Z'
dt = datetime.strptime( t , fmt )
print 'dt ', repr(dt)
first = 'Sun 11 May 2014 05:00 PM PDT'
deadlines(first)
ValueError: time data 'Sun 11 May 2014 02:00 PM PDT' does not match format ' %a %d %m %Y %I:%M %p %Z '
Whats wrong with this?
%m matches months represent as a two-digit decimal (in [01, 12]). Use %b for abbreviated month names, or %B for full month names instead:
fmt = '%a %d %b %Y %I:%M %p %Z'
A table showing the date format directives and their meanings can be found here.
If you're having trouble parsing PDT using %Z:
Per the time.strptime docs:
Support for the %Z directive is based on the values contained in
tzname and whether daylight is true. Because of this, it is
platform-specific except for recognizing UTC and GMT which are always
known (and are considered to be non-daylight savings timezones).
So, if parsing the date string without PDT works:
In [73]: datetime.strptime('Sun 11 May 2014 05:00 PM', '%a %d %b %Y %I:%M %p')
Out[73]: datetime.datetime(2014, 5, 11, 17, 0)
but
datetime.strptime('Sun 11 May 2014 05:00 PM PDT', '%a %d %b %Y %I:%M %p %Z')
raises a ValueError, then you may need strip off the timezone name (they are, in general, ambiguous anyway):
In [10]: datestring = 'Sun 11 May 2014 05:00 PM PDT'
In [11]: datestring, _ = datestring.rsplit(' ', 1)
In [12]: datestring
Out[12]: 'Sun 11 May 2014 05:00 PM'
In [13]: datetime.strptime(datestring, '%a %d %b %Y %I:%M %p')
Out[13]: datetime.datetime(2014, 5, 11, 17, 0)
or use dateutil:
In [1]: import dateutil.parser as parser
In [2]: parser.parse('Sun 11 May 2014 05:00 PM PDT')
Out[2]: datetime.datetime(2014, 5, 11, 17, 0)