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

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')

Related

Correct format for converting french date from string to datetime?

I'm trying to convert strings to date, but it's not working for some dates in french. Like in the example below :
myDate = datetime.strptime(dateInStringFormat, "%A %d %B %Y %H:%M:%S")
Result with the error : time data '"jeudi 8 septembre 2022 13:51:13"' does not match format '%A %d %B %Y %H:%M:%S'.
I've tried a lot a variations in the format I can't make it work, anyone know how to do this please ?
try:
import locale
import datetime
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
dateInStringFormat = "jeudi 8 septembre 2022 13:51:13"
myDate = datetime.datetime.strptime(dateInStringFormat, "%A %d %B %Y %H:%M:%S")
print(myDate)
# 2022-09-08 13:51:13
myDate
# datetime.datetime(2022, 9, 8, 13, 51, 13)
I had the same problem once and found a module called dateparser to do that:
import dateparser
d = dateparser.parse("jeudi 8 septembre 2022 13:51:13")
d is now
datetime.datetime(2022, 9, 8, 13, 51, 13)

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')

String to date in pandas

I have a dataset with dates encoded as strings formatted as %B %d, %Y, eg September 10, 2021.
Using:df['sale_date'] = pd.to_datetime(df.sale_date, format = '%B %d, %Y')
produces this error ValueError: time data 'September 10, 2021' does not match format '%B %d, %Y' (match)
Manually checking with strptimedatetime.strptime('September 10, 2021', '%B %d, %Y') produces the correct datetime object.
Is there something I missed in the pd.to_datetime?
Thanks.
Upon further investigation, I found out that the error only happens on the first element of the series. It seems that the string has '\ufeff' added to it. So I just did a series.str.replace() and now it is working. Sorry for the bother. Question is how did that BOM end up there?
Very likely you have to eliminate some whitespaces first!
If I add whitespaces at the beginning, end or both..
datestring = ' September 10, 2021 '
datetime.datetime.strptime(datestring, '%B %d, %Y')
it will result in the same error message as you have..
ValueError: time data ' September 10, 2021 ' does not match format '%B %d, %Y'
As a solution for a single value use:
datestring = ' September 10, 2021 '
datestring.strip()
for a column in a dataframe use:
dummy = pd.DataFrame(columns={'Date'}, data = [' September 10, 2021 ', ' September 11, 2021 ', ' September 12, 2021 '])
dummy.Date = dummy.Date.apply(lambda x: x.strip())

convert 'July 31, 2021' to YYYY-MM-DD format caused ValueError: time data 'July 31, 2021' does not match format '%m %d, %Y'

I have a web app , using Django as backend.
I used datetime.strptime function in python3 to convert the date to the format need to input to Mysql database.
But I got the error: ValueError: time data 'July 31, 2021' does not match format '%m %d, %Y'
end_date = request.GET.getlist('end_date')[0] # end_date = 'July 31, 2021' in the test case
end_date_converted = datetime.strptime(end_date, "%m %d, %Y").strftime("%Y-%m-%d")
How could I convert 'July 31, 2021' to YYYY-MM-DD format so I could save it to MYSQL date column?
According to docs %m is "Month as a zero-padded decimal number", not the month name. You should be using
%B %d, %Y
as the format specifier. For example:
>>> datetime.strptime('July 31, 2021', '%B %d, %Y').strftime('%Y-%m-%d')
'2021-07-31'
replace %m with %B which will decode the Month full name

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

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')

Categories