Python Pandas datetime set_index gives unexpected results

Viewed 108

I've got a dataframe that looks like this:

and I want to make 'TIME_STAMP_NEW' column as index. Current code:

twoweektable['TIME_STAMP_NEW'] = pd.to_datetime(twoweektable['TIME_STAMP_NEW'])
twoweektable.set_index('TIME_STAMP_NEW',inplace=True)

However, the result index looks like this

Any ideas on why there is unexpected 'T' and decimal in second?

2 Answers

The 'T' is the delimiter telling pandas where to separate the date and time. It is part of the ISO-8601 standard and shouldn't be a problem for pandas to interpret regardless of how you intend to use the timestamps.

To get rid of the trailing decimal, try formatting to resolution of seconds, then rounding to the nearest second:

pd.to_datetime("twoweektable['TIME_STAMP_NEW']",format="%Y-%m-%d %H:%M:%S").round('s')

I'm assuming that the original column is a string. Generally pandas convert string to a full date with nanoseconds (default argument for unit parameter).

You can try adding

pd.to_datetime(twoweektable['TIME_STAMP_NEW'], unit='s')

That'll remove the nanoseconds stamp.

Related