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?
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?
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 )
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'))