Python Pandas - 從 Timestamp 物件獲取當前日期和時間


使用 timestamp.today() 方法從 Timestamp 物件獲取當前日期和時間。

首先,匯入需要的庫 −

import pandas as pd
import datetime

在 Pandas 中建立時間戳

timestamp = pd.Timestamp(datetime.datetime(2021, 10, 10))

顯示時間戳

print("Timestamp: ", timestamp)

獲取當前日期和時間

res = timestamp.today()

示例

以下是程式碼

import pandas as pd
import datetime

# set the timestamp in Pandas
timestamp = pd.Timestamp(datetime.datetime(2021, 10, 10))

# display the Timestamp
print("Timestamp: ", timestamp)

# display the day from given timestamp
print("Day Name:", timestamp.day_name())

# getting the current date and time
res = timestamp.today()

# display the current date and time
print("\nToday's Date and time...\n", res)

# display the current day
print("Today's Day:", res.day_name())

輸出

這將生成以下程式碼

Timestamp: 2021-10-10 00:00:00
Day Name: Sunday

Today's Date and time...
2021-10-03 13:22:28.891506
Today's Day: Sunday

更新時間: 2021-10-13

8K+ 瀏覽

開啟您的職業生涯

完成課程獲取認證

開始
廣告
© . All rights reserved.