changing all dates to standard date time in dataframe

Referencing How to convert a given ordinal number (from Excel) to a date, convert the ordinal values to datetime using from_excel_ordinal

m = df['Plan Start Date'].str.isdigit()

Or, if you have a column of objects –

df['Plan Start Date'].astype(str).str.isdigit()

Next, apply the function on a subset of the rows using apply

df.loc[m, 'Plan Start Date'] = \
df.loc[m, 'Plan Start Date']\
  .astype(int)\
  .apply(from_excel_ordinal)

Finally, convert the entire column to datetime using pd.to_datetime, giving a uniform result –

df['Plan Start Date'] = pd.to_datetime(df['Plan Start Date'], errors="coerce")

df

   Plan Start Date
0       2017-08-16
1       2017-05-31
2       2017-05-31
3       2017-05-31
4       2017-05-31
5       2016-04-21
6       2016-02-25
7       2016-12-15
8       2016-12-15
9       2016-12-15
10      2016-01-04
11      2016-01-04
12      2015-12-29
13      2015-12-29
14      2015-12-29
15      2015-12-29
16      2016-03-31
17      2016-03-31
18      2016-03-31
19      2016-03-31
20      2016-03-31
21      2017-01-24
22      2015-11-25

Leave a Comment