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+ 浏览

开启您的职业生涯

完成课程获取认证

开始
广告