简体   繁体   English

当时间戳中仅存在时间时,如何在日期时间对象中添加日期

[英]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.

相关问题 Pandas dataframe - 如何为现有日期列综合添加唯一时间戳,其中仅包含日期但不包含时间? - Pandas dataframe - How to synthetically add a unique timestamp for existing date column, which contains only date but no time? 如何将Python日期时间对象转换为时间戳而没有时间和时间倒退? - How to convert a Python datetime object into timestamp without time and backwards? 如何在 python 中将时间(对象)转换为日期时间或时间戳数据类型? - How to convert Time (object) to datetime or timestamp data type in python? 如何仅打印日期而不包括datetime.datetime.strptime()中的时间 - how to print only date and not include the time in datetime.datetime.strptime() 如何将系列值添加到日期/日期时间对象? - How to add series values to date/datetime object? 从数据框列中获取正确的日期时间对象,其中包含带有日期和时间的随机字符串 - Get correct datetime object from dataframe column with random string present with date and time 如何设置只有日期(没有时间)的默认时间戳? - How to set the default timestamp with the date only (without the time)? 将datetime.date对象与datetime.time对象组合时出错 - Error when combining datetime.date object with datetime.time object Pandas:如何按日期时间列分组,仅使用时间并丢弃日期 - Pandas: How to group by a datetime column, using only the time and discarding the date 在 datetime 对象中添加或更改时间 - Add or change time in datetime object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM