将DataFrame中的时间转换为时间戳
import pandas as pd
import numpy as np
import datetime,time
data = pd.read_csv('C:/Users/1701/Desktop/WT02122.csv',index_col=False)
data['temp'] = pd.to_datetime(data.RecTm)
data['temp1'] = data.temp.astype('str')
#################################################
def convert(x):
d = datetime.datetime.strptime(x,"%Y-%m-%d %H:%M:%S")
t = d.timetuple()
timeStamp = int(time.mktime(t))
return timeStamp
###################################################
data['timestamp'] = data.temp1.apply(lambda x: convert(x))
data
将时间戳还原为时间格式
#data['temp'] = data.timestamp.astype('str')
data.timestamp.astype('str')
# # 输入毫秒级的时间,转出正常格式的时间
def timeStamp(x):
timeStamp = float(x)
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
return otherStyleTime
# 2019-08-14 10:40:06
data['t'] = data.timestamp.apply(lambda x :timeStamp(x))
data