Pandas str split. Can I skip line which gives troubles?

Viewed 24

I have a dataframe (all5) including one column with dates('CREATIE_DATUM'). Sometimes the notation is 01/JAN/2015 sometimes it's written as 01-JAN-15. I only need the year, so I wrote the following code line:

all5[['Day','Month','Year']]=all5['CREATIE_DATUM'].str.split('-/',expand=True)

but I get the following error:

columns must be same length as key

so I assume somewhere in my dataframe (>100.000 lines) a value has more than two '/' signs. How can I make my code skip this line?

1 Answers

You can try to use pd.to_datetime and then use .dt property to access day, month and year:

x = pd.to_datetime(all5["CREATIE_DATUM"])

all5["Day"] = x.dt.day
all5["Month"] = x.dt.month
all5["Year"] = x.dt.year
Related