Python time formats not matching? [duplicate] - python

This question already has answers here:
How to parse string dates with 2-digit year?
(6 answers)
Closed 1 year ago.
I am getting the error:
time data '1/1/03 0:00' does not match format '%m/%d/%Y %H:%M' (match)
Does this format not match..? I am new to working with Date-Time formats so this is rather confusing to me.
This is the line of code I am using:
date_time = pd.to_datetime(df['time'], format='%m/%d/%Y %H:%M')
Note that the csv file, 'df' that is being used has 1 column named "time", hence I am getting all possible values of it with df['time'].
I should comment that this is:
12/31/16 23:00
is another entry, so I know that it goes month/day/year, is it because the year is only two digits?

The issue comes from the matching of the year. %Y matches the year with the century so in that case it should be 2003 to be matched. You should use %y instead.
date_time = pd.to_datetime(df['time'], format='%m/%d/%y %H:%M')

The problem in your case is the year:
date_time = pd.to_datetime(df['time'], format='%m/%d/%y %H:%M') # the lowercase y fixes it
Its basically the same as in the datetime module:
from datetime import datetime as dt;
dt.strptime('1/1/03 0:00', '%m/%d/%y %H:%M')
>> datetime.datetime(2003, 1, 1, 0, 0)
dt.strptime('1/1/03 0:00', '%m/%d/%Y %H:%M')
>> ## ERROR
All the codes to clarify

Related

Convert DataFrame column from string to datetime for format "January 1, 2001 Monday"

I am trying to convert a dataframe column "date" from string to datetime. I have this format: "January 1, 2001 Monday".
I tried to use the following:
from dateutil import parser
for index,v in df['date'].items():
df['date'][index] = parser.parse(df['date'][index])
But it gives me the following error:
ValueError: Cannot set non-string value '2001-01-01 00:00:00' into a StringArray.
I checked the datatype of the column "date" and it tells me string type.
This is the snippet of the dataframe:
Any help would be most appreciated!
why don't you try this instead of dateutils, pandas offer much simpler tools such as pd.to_datetime function:
df['date'] = pd.to_datetime(df['date'], format='%B %d, %Y %A')
You need to specify the format for the datetime object in order it to be parsed correctly. The documentation helps with this:
%A is for Weekday as locale’s full name, e.g., Monday
%B is for Month as locale’s full name, e.g., January
%d is for Day of the month as a zero-padded decimal number.
%Y is for Year with century as a decimal number, e.g., 2021.
Combining all of them we have the following function:
from datetime import datetime
def mdy_to_ymd(d):
return datetime.strptime(d, '%B %d, %Y %A').strftime('%Y-%m-%d')
print(mdy_to_ymd('January 1, 2021 Monday'))
> 2021-01-01
One more thing is for your case, .apply() will work faster, thus the code is:
df['date'] = df['date'].apply(lambda x: mdy_to_ymd)
Feel free to add Hour-Minute-Second if needed.

Re-formatting a date in python [duplicate]

This question already has answers here:
Convert String with month name to datetime
(2 answers)
Closed 6 months ago.
So I have a date in this format "August 10, 2022". I need to reformat the date in this way "2022-08-10". How do I do that in python ?.
For this, you can use datetime, specifically on this behaviour.
from datetime import datetime
a = "August 10, 2022"
b = datetime.strptime(a, '%B %d, %Y') # Converts to datetime format
c = b.strftime('%Y-%m-%d') # Converts from datetime to desired format
print(c)
# output
2022-08-10

How to creat one type of datetime DF format in excel [duplicate]

This question already has answers here:
Convert Pandas Column to DateTime
(8 answers)
Closed 9 months ago.
I have
5/7/2022 12:57(m/d/yyy)
5/7/2022 13:00 PM(m/d/yyy) time formats.
There are two types of time formats in a column of excel file which I have downloaded.
I want to convert it to '%Y-%m-%d %H:%M:%S'.
(The column is in string format).
I guess you have your file loaded from excel to dataframe.
df['date_col'] = pd.to_datetime(df['date_col'], format='%Y-%m-%d %H:%M:%S')
from dateutil.parser import parse
datestring = "5/7/2022 12:57"
dt = parse(datestring)
print(dt.strftime('%Y-%m-%d %H:%M:%S')) #2022-05-07 12:57:00
You can turn string input to datetime by doing this:
from datetime import datetime
example1 = "5/7/2022 12:57"
example2 = "5/7/2022 13:00 PM"
datetime_object1 = datetime.strptime(example1, "%m/%d/%Y %H:%M")
datetime_object2 = datetime.strptime(example2, "%m/%d/%Y %H:%M %p")
and then you can represent the datetime variable with a string:
formatted_datetime1 = datetime_object1.strftime("%Y-%m-%d, %H:%M:%S")
formatted_datetime2 = datetime_object1.strftime("%Y-%m-%d, %H:%M:%S")
You can try using pandas.Series.dt.strftime method, that will allow you to convert a field into the specified date_format, in this case %Y-%m-%d %H:%M:%S.
df['Column'] = df['Column'].dt.strftime('%Y-%m-%d %H:%M:%S')

How to I extract a date from the following Python string '2021-05-16T13:24:31+0000'? [duplicate]

This question already has answers here:
How do I parse an ISO 8601-formatted date?
(29 answers)
Closed 1 year ago.
I have the following date string which I tried to extract the date from. I am getting an error as I am obviously not parsing it correctly.
from datetime import datetime
date_string = '2021-05-16T13:24:31+0000'
date_format = "%Y-%m-%d'T'%H:%M:%S'+0000'"
date_object = datetime.strptime(date_string, date_format)
print(date_object.date)
What would be the correct parsing format for this date string '2021-05-16T13:24:31+0000'?
Thank you.
Those single quotes are messing with the date format, without them the code works fine:
from datetime import datetime
date_string = '2021-05-16T13:24:31+0000'
date_format = '%Y-%m-%dT%H:%M:%S+0000'
date_object = datetime.strptime(date_string, date_format)
print(date_object.strftime('%Y-%m-%d'))

Date time format conversion in python [duplicate]

This question already has answers here:
Convert 12-hour date/time to 24-hour date/time
(8 answers)
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 7 years ago.
I have a string containing time stamp in format
(DD/MM/YYYY HH:MM:SS AM/PM), e.g."12/20/2014 15:25:05 pm"
.
The time here is in 24 Hrs format.
I need to convert into same format but with time in 12-Hrs format.
I am using python version 2.6.
I have gone through time library of python but couldn't come up with any solution.
View Live ideOne use Python datetime,
>>> from datetime import datetime as dt
>>> date_str='12/20/2014 15:25:05 pm'
>>> date_obj = dt.strptime(date_str, '%m/%d/%Y %H:%M:%S %p')
>>> dt.strftime(date_obj, '%m/%d/%Y %I:%M:%S %p')
'12/20/2014 03:25:05 PM'
The trick is to convert your Input date string to Python datetime object and then convert it back to date string
import datetime
#Input Date String
t = "12/20/2014 15:25:05 pm"
#Return a datetime corresponding to date string
dateTimeObject = datetime.datetime.strptime(t, '%m/%d/%Y %H:%M:%S %p')
print dateTimeObject
Output: 2014-12-20 15:25:05
#Return a string representing the date
dateTimeString = datetime.datetime.strftime(dateTimeObject, '%m/%d/%Y %I:%M:%S %p')
print dateTimeString
Output: 12/20/2014 03:25:05 PM
After creating a datetime object using strptime you then call strftime and pass the desired format as a string see the docs:
In [162]:
t = "12/20/2014 15:25:05 pm"
dt.datetime.strftime(dt.datetime.strptime(t, '%m/%d/%Y %H:%M:%S %p'), '%m/%d/%Y %I:%M:%S %p')
Out[162]:
'12/20/2014 03:25:05 PM'
Shortest & simplest solution --
I really appreciate & admire (coz I barely manage to read man pages :P) you going through time documentation, but why use "astonishing" & "cryptic" code when simple code could get the job done
Just extract the hour part as int & replace it by hrs-12 if it is greater than 12
t = "12/20/2014 15:25:05 pm"
hrs = int( t.split()[1][:2] )
if hrs > 12:
t = t.replace( str(hrs), str(hrs-12) )
Output
See explaination & live output here
Using Lambda
If you like one liners, checkout f() below
t = "12/20/2014 15:25:05 pm"
f = lambda tym: tym.replace(str(int(tym.split()[1][:2])), str(int(tym.split()[1][:2])-12)) if int(tym.split()[1][:2]) > 12 else tym
print(f(t))

Categories

Resources