user7153 user7153 - 4 months ago 18
Python Question

drop hour,min,sec from timestemp?

I want to convert a date from sas, 14487, to 1999-08-31.

How can I convert the current results '1999-08-31 00:00:00' to '1999-08-31'?
I've tried ser.normalize(). But it doesn't help.

ser = pd.to_timedelta(14487, unit='D') + pd.Timestamp('1960-1-1')
ser.normalize()


yield

Timestamp('1999-08-31 00:00:00')

Answer

normalize just resets the time to midnight (according to the docs).

You can use strftime (see docs here). Also, here is a list of the format specifiers you can use.

ser = pd.to_timedelta(14487, unit='D') + pd.Timestamp('1960-1-1')
ser.strftime('%Y-%m-%d')

outputs

'1999-08-31'
Comments