Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Python Pandas - Xây dựng datetime UTC naive từ timestamp POSIX


Để xây dựng một datetime UTC naive từ timestamp POSIX, hãy sử dụng phương thức timestamp.utcfromtimestamp(). Truyền POSIX làm đối số.

Trước tiên, nhập các thư viện cần thiết:

import pandas as pd

Tạo một timestamp:

timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624')

Xây dựng datetime UTC naive từ timestamp POSIX. POSIX được truyền làm đối số:

timestamp.utcfromtimestamp(1631717502)

Ví dụ

Dưới đây là mã:

import pandas as pd
# tạo một timestamp
timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624')
# hiển thị Timestamp
print("Timestamp...\n", timestamp)
# xây dựng datetime UTC naive từ timestamp POSIX
# POSIX được truyền làm đối số
print("\nXây dựng Datetime UTC...\n", timestamp.utcfromtimestamp(1631717502))

Kết quả

Đoạn mã trên sẽ cho ra kết quả sau:

Timestamp... 2021-09-14 15:12:34.261811624
Construct UTC Datetime... 2021-09-15 14:51:42