0

I have a pandas dataframe with numerous columns, one of which is the time in GPS time, like such:

GPS_time
1635751985
1635751985
1635751986
1635751987
1635751987
..........

How would I go about converting this to datetime or UTC within Python?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • what sort of dataframe do you have (such as a Pandas DataFrame)? it will likely provide methods to convert your timestamps to another format – ti7 Oct 10 '22 at 17:22
  • Sorry, yes it's a Pandas DataFrame. I've been looking for that, but can't seem to find anyting. – matrix_season Oct 10 '22 at 17:23

2 Answers2

2

You can use the Pandas .to_datetime() method to do this conversion!

>>> df = pd.DataFrame({"dates": [1635751985, 1635751985, 1635751986]})
>>> df
        dates
0  1635751985
1  1635751985
2  1635751986
>>> pd.to_datetime(df["dates"], unit="s")
0   2021-11-01 07:33:05
1   2021-11-01 07:33:05
2   2021-11-01 07:33:06
Name: dates, dtype: datetime64[ns]

Note that this conversion is from your integer values to storing the values as datetime64[ns]
Once converted, you can control how they're displayed with .dt.strftime() (see How to change the datetime format in Pandas )

ti7
  • 16,375
  • 6
  • 40
  • 68
  • Good answer. I did some performance tests (with 100k rows) and this is around 3x faster than manual for-loop. – xyres Oct 10 '22 at 17:49
  • Ah, did not realize that would actually work! Goes to show how much there is to learn still with any python package, haha. Thanks!! – matrix_season Oct 10 '22 at 18:00
0

This format is called Unix timestamp.

You can convert it to a datetime object like this:

from datetime import datetime

dt = datetime.fromtimestamp(1635751985)

# use a for-loop to convert multiple items

By default, the object will be in your local timezone.

If you want UTC, you can use the pytz library:

from pytz import timezone

dt = datetime.fromtimestamp(1635751985, timezone('UTC'))
xyres
  • 20,487
  • 3
  • 56
  • 85
  • 1
    this will only convert one value at a time, which is very inefficient if they have a large dataframe! – ti7 Oct 10 '22 at 17:20
  • @ti7 Ah, didn't consider that! Hopefully someone will provide a better answer in that regard. Or I'll update if I should come up with something. – xyres Oct 10 '22 at 17:23