splinter splinter - 2 months ago 14
Python Question

Using `apply` on datetime64 series in pandas

I have a Series of

datetime64[ns]
objects. I would like to apply
strftime('%d-%m-%Y')
to all of them, for reversing the order of year,day,month. I tried using:

series.apply(time.strftime('%d-%m-%Y'))


But I get the error:


TypeError: 'str' object is not callable


Any idea on the right way to do this elegantly?

Answer

I think you can use Series.dt.strftime:

series.dt.strftime('%d-%m-%Y')

Or if is necessary convert Series to_datetime first:

pd.to_datetime(series).dt.strftime('%d-%m-%Y')