[英]How to add the date in a datetime object when only time is present in timestamp
I am working on some time series data and the timestamp only includes the time( HH:MM:SS
) , but I need to add the YY/MM/DD
to the timestamp. 我正在处理一些时间序列数据,并且时间戳仅包含time( HH:MM:SS
),但是我需要将YY/MM/DD
到时间戳中。 I working with pandas dataframe. 我使用熊猫数据框。
I tried using pd.to_datetime(), but it enters the current date that I call it. 我尝试使用pd.to_datetime(),但是它输入了我称之为的当前日期。
df_17c = pd.read_csv(file_17c,sep ='\t', header = None,names=['TimeStamp','x','y','z'], usecols =[0,3,4,5])
df_17s = pd.read_csv(file_17s,sep ='\t', header = None,names = ['TimeStamp','x','y','z'],usecols =[0,1,2,3])
TimeStamp x y z
0 23:59:58 26799 -218 0
1 23:59:58 26797 -218 0
2 23:59:58 26795 -218 0
3 23:59:58 26793 -218 0
4 23:59:58 26792 -217 0
The "TimeStamp" column is a object type ( string). “ TimeStamp”列是对象类型(字符串)。 When I convert using .to_datetime()
it yields datetime object with the current date. 当我使用.to_datetime()
转换时,它将产生带有当前日期的datetime对象。
df_17c["Date"]= pd.to_datetime(df_17c['TimeStamp'])
TimeStamp x y z
0 2019-06-26 23:59:58 26799 -218 0
1 2019-06-26 23:59:58 26797 -218 0
2 2019-06-26 23:59:58 26795 -218 0
3 2019-06-26 23:59:58 26793 -218 0
4 2019-06-26 23:59:58 26792 -217 0
This isn't probably the most efficient way, but it's simple (basically add the date to the front of the string) 这可能不是最有效的方法,但是很简单(基本上将日期添加到字符串的开头)
date = '2017-01-09T' # or whatever (note the T)
pd.to_datetime(df['TimeStamp'].apply(lambda s: date+s))
example 例
df = pd.DataFrame({'time': ['08:11:09', '17:09:34']})
# time
# 0 08:11:09
# 1 17:09:34
date_func = '2017-01-09T{}'.format # avoid the use of lambda + more efficient
df['datetime'] = pd.to_datetime(df['time'].apply(date_func))
output 输出
time datetime
0 08:11:09 2017-01-09 08:11:09
1 17:09:34 2017-01-09 17:09:34
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.