'How to convert a Pandas Timestamp to UTC seconds as an int?
I tried to convert it over like this but it still doesn't work as intended.
ts = pd.Timestamp('2022-01-02T12')
ts_utc = ts.replace(tzinfo=timezone.utc)
x = pd.Timestamp.utcnow()
ts_delta = x - ts_utc
ts_new = ts_delta.total_seconds()
time_yesterday = ts - pd.Timedelta(days=1)
ts_y_utc = time_yesterday.replace(tzinfo=timezone.utc)
ts_y_delta = x - ts_y_utc
ts_y_new = ts_y_delta.total_seconds()
Solution 1:[1]
This code works and returns UTC seconds from the Pandas Timestamp.
ts = pd.Timestamp('2022-01-02T12')
timestamp_og = time.mktime(ts.timetuple())
dt = datetime.fromtimestamp(timestamp_og)
timestamp = dt.replace(tzinfo=timezone.utc).timestamp()
time_yesterday = ts - pd.Timedelta(hours=1)
timestamp2_og = time.mktime(time_yesterday.timetuple())
dt_2 = datetime.fromtimestamp(timestamp2_og)
timestamp2 = dt_2.replace(tzinfo=timezone.utc).timestamp()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Noah Pravecek |
