Search code examples
pythonpandasdataframedatetimeunix-timestamp

Convert GPS time to datetime in Python


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?


Solution

  • 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 )