-1
$\begingroup$

I have gone through a lot of forums and discussions. However, can't get my problem solved.

Note: 'Released' column has same format for all entries and there are no null values or unwanted spaces in it.

I am working on data-set Google Play Store Apps (csv). It contains the 'Released' column having the date of release, in string type. I want to convert to date format, so that I can calculate how many years that app has been on the store. Data-set has the date stored in string type and has the format as follows.

Mar 7, 2016
sep 24, 2016
Jan 10, 2018
Jan 13, 2020
Dec 6, 2012

When I pick a specific date string, my code converts it to the respective date format. But, when I apply the same code to full column, I get the error

# This code executes fine and converts the string to date.
s = 'Mar 7, 2016'
d = dt.datetime.strptime(s, '%b %d, %Y')
print(d.strftime('%d-%m-%y'))

# When same code applies to column, it gives the error "time data '0' does not match format %b  %d, %Y"
d = df['Released'].apply(lambda x: dt.datetime.strptime(x, '%b %d, %Y'))

# This code gives the error, "day is out of range for month: 0"
d = pd.to_datetime(df['Released']).strptime('%b %d, %Y')

# This code does not give any error, however, converts all the dates to NAT.
d = pd.to_datetime(df['Released'], format='%d-%m-%y', errors='coerce')

I have also tried arrow and dateutil libraries, they also work fine on single date. However, whenever I give them the series/column of data-frame they both give the same error as I have mentioned above.

$\endgroup$

1 Answer 1

1
$\begingroup$

This is caused by the fact that there is at least one value in the column that is equal to '0', which is not a date in the format you specified and which can therefore not be converted. Coercing the errors works, but you should specify the correct format:

d = pd.to_datetime(df['Released'], format='%b %d, %Y', errors='coerce')
$\endgroup$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.