Python Pandas Date in format 'Thursday, March 03, 2019' want to convert to %m/%d/%y - python

I am using the python pandas and datetime libraries to convert dates in a date column from the following format: 'Thursday, March 03, 2019' to: '3/3/2019'.
Below is the code I am using to get me the result, but I continue to get a ValueError. 'Unconverted Data Remains'.
Does anyone know a way around this issue?
df_['Date'] = df_['Date'].apply(lambda x: dt.datetime.strptime(x, '%A, %B %d, %Y').strftime('%d/%m/%Y'))

I think you can use exact=False in pandas.to_datetime if your date string is part of some other string.
exact behaviour : If True, require an exact format match. - If False, allow the format to match anywhere in the target string.
ex :
In [6]: pd.to_datetime("Send this to me on Thursday, March 31, 2015", format='%A, %B %d, %Y', exact=False)
Out[6]: Timestamp('2015-03-31 00:00:00')

Related

time data 'June 13, 1980 (United States)' does not match format '%m/%d/%Y' (match)

How can i passs a datetime format on a column with str such as June 13, 1980 (United States)
i tried df['format_released'] = pd.to_datetime(df['released'], format='%m/%d/%Y')
got this error
time data 'June 13, 1980 (United States)' does not match format '%m/%d/%Y' (match)
The correct format is: pd.to_datetime(pd.to_datetime(df['released'], format='%B %d, %Y')
For the full name, you need to specify %B for the format.
You don't need the value "(United States)" in the string.
You need to preprocess the column to discard the non relevant data.
Using str.replace:
df['format_released'] = pd.to_datetime(df['released'].str.replace(r'\s*(.*$', '', regex=True), format='%B %d, %Y')
Or using str.extract:
df['format_released'] = pd.to_datetime(df['released'].str.extract(r'(\w+ \d+, \d+)', expand=False), format='%B %d, %Y')

How to convert string date (Nov 13, 2020) to datetime in pandas?

I have a dataset with abbreviated month names, and I have tried to follow some other solutions posted here, such as :
r.Date = pd.to_datetime(r.Date, format='%MMM %d, %Y')
but unfortunately it is giving me a ValueError: time data 'Nov 13, 2020' does not match format '%d %B, %Y' (match). The months dates are all abbreviated.
Change to
pd.to_datetime('Nov 13, 2020',format='%b %d, %Y')
Out[23]: Timestamp('2020-11-13 00:00:00')

How to custom parse dates in pandas

I know this seems like a duplicated question (and it kinda is), but previous answers didn't let me achieve what I'm looking for. I have a date Series with the following format:
date
Jun 13 14:46
Jun 13 17:11
And so, I wanted to turn it into a datetime object. I did the following:
pd.to_datetime(df.date, format='%b %d %I:%M')
Which based on this question should be enough: Convert custom date formats in pandas
But, I'm still getting ValueError: time data 'Jun 13 14:46' does not match format '%b %d %I:%M' (match)
What am I missing?
Thanks
try pd.to_datetime(df.date, format='%b %d %H:%M')
%I is for a 12-hour clock.
%H is for a 24-hour clock.

Converting string in python to date format

I'm having trouble converting a string to data format. I'm using the time module to convert a string to the YYYY-MM-DD format. The code below is what I've tried but I get the following error.
sre_constants.error: redefinition of group name 'Y' as group 5; was group 3
Here is the code
import time
review_date = "April 18, 2018"
review_date = time.strptime(review_date, '%m %d %Y %I:%Y%m%d')
Firstly, the error is because you're using %Y, %m, and %d twice in your time.strptime() call.
Secondly, you're using the wrong format. The format you pass to strptime() has to match the format of the date / time string you pass, which in this case is: %B %d, %Y.
This is a good reference on the different format types.
I normally use datetime for this:
from datetime import datetime
review_date = "April 18, 2018"
review_date = datetime.strptime(review_date, '%B %d, %Y').strftime('%Y-%m-%d')
This code returns review_date = '2018-04-18'. See https://docs.python.org/3/library/datetime.html
The date format for April is %B. strptime() converts to a datetime object, .strftime() converts the datetime object to a string.
time.strptime() is for parsing strings into date/time structures. It takes two arguments, the string to be parsed and another string describing the format of the string to be parsed.
Try this:
time.strptime("April 18, 2018", "%B %d, %Y")
... and notice that "%B %d, %Y" is:
Full locale name of the month ("April")
[Space]
Date of the month (18)
[Comma]
[Space]
Four digit year (2018)
The format string specification that you provided bears no resemblance to the formatting of your date string.
These "magic" formatting codes are enumerated in the documentation for time.strftime()
review_date = time.strptime(review_date, '%B %d, %Y')
import time
review_date = "April 18, 2018"
review_date = time.strptime(review_date, '%B %d, %Y')
That's what you should have

ValueError: time data '27th August 2017 23:32:58' does not match format '%d-%m-%Y %H:%M:%S' (match)

i am using pandas and odo to import csv files into a database, there is a date field in file with this format 27th August 2017 23:06:25 i would like to convert is to this format %d-%m-%Y %H:%M:%S.
Here is my the piece of code i am using:
df['Date'] = pd.to_datetime(df['Date'], format='%d-%m-%Y %H:%M:%S')
I end up with the error
ValueError: time data '27th August 2017 23:32:58' does not match format '%d-%m-%Y %H:%M:%S' (match)
Anyone having an idea solving this? please
pandas can parse this fine without a format specifier:
In[25]:
pd.to_datetime('27th August 2017 23:32:58')
Out[25]: Timestamp('2017-08-27 23:32:58')
So you don't need to state the format for this example
The other point here is that even if you tried something like:
In[28]:
pd.to_datetime('27th August 2017 23:32:58', format='%dth %B %Y %H:%M:%S')
Out[28]: Timestamp('2017-08-27 23:32:58')
Which does work it will fail for date strings like:
'3rd June 2011 12:11:23'
because of the 'rd', you can't pass a format to handle the day format using to_datetime, see the python strptime reference. You would need to strip those out in order for it to work but pandas is man/woman enough to sniff the format so there is no need

Categories