How to convert Python Unix timestamp to datetime

Charlie Craine
2 min readNov 18, 2022

--

In this tutorial, learn how to convert Unix timestamps to datetime for a year, month, day, and hour!

I spend a lot of time on Kaggle and often find that adding little scripts to my toolbelt has helped. In this quick post, I will show you how to convert a Unix timestamp from the OTTO Kaggle competition.

First, here is a link to the full notebook. If you are a member of Kaggle you can clone using my notebook, or just grab the code snippets.

After a bit of cleanup with the JSON from the competition, I was able to get the data into a much easier-to-manage dataframe. The timestamp is in milliseconds, not seconds. That tripped me up for a bit when trying to break out the code.

As you can see, the timestamp is useless to explore as a human. We can do better. The first step is to break out the dates:

Next job to be done is to break out the year:

I have broken out the month, day, and hour in the full notebook here. Enjoy the notebook and please upvote if it was helpful!

--

--

Charlie Craine

Kaggle Grandmaster. Startups @ Amazon Web Services, Hiker.