简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM