python pandas timestamp

# 1. 构造Timestamp
# ts_input参数支持4种格式,datetime-like, str, int, float
# 1.1 datetime-like
import datetime
import pandas as pd
time_str = "2020-08-01 10:20:00"
time_dt = datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
ts1 = pd.Timestamp(time_dt)
print(1,ts1)

# 1.2 str
ts2 = pd.Timestamp(time_str)
print(2,ts2)

# 1.3 int, 指的是时间戳
timestamp = time_dt.timestamp()
print(3,timestamp)

print(4,datetime.datetime.fromtimestamp(timestamp))

ts3 = pd.Timestamp(timestamp)
print(5,ts3)

# 1.4 float, float格式的时间戳
timestamp_f = time_dt.timestamp()
ts4 = pd.Timestamp(timestamp_f)
print(6,ts4)
av=ts1.date()
avv=ts1.time()

 

import datetime
import time
 
current_time = time.time()  
now = datetime.datetime.utcfromtimestamp(current_time ).strftime("%Y-%m-%d %H:%M:%S")
print(now)
 

 

posted @ 2023-03-24 19:19  myrj  阅读(59)  评论(0编辑  收藏  举报