Noobie Noobie - 5 months ago 8
Python Question

how to extract up to the minute component of a unix timestamp?

I have a list of POSIX timestamps such as

1331567982
that I process using
datetime.utcfromtimestamp
which gives me a date format like:
year - month - day - hour - minute - seconds
.

However, for resampling purposes I just need to extract the timestamp up to the minute precision, that is
year - month - day - hour - minute
, but no seconds.

How can I do that using functions from the
datetime
module only (no Pandas)?

Answer

You can use the datetime.replace function:

import datetime
d = datetime.datetime.utcfromtimestamp(1331567982)
d.replace(second=0, microsecond=0)