[英]Combine initial date with time column in a pandas dataframe as datetime
I'm reading text log files that have the year-month-day in the header, and a column with hours:minutes:seconds for each row. 我正在读取文本日志文件,该文件的标题中包含年月日,并且每行的时间为小时:分钟:秒。 Sort of like this:
有点像这样:
Yr=17 Mn= 3 Dy= 3
19:22:59.894 52
19:24:12.130 106
19:24:13.241 107
...
I have the date as a datetime.date
object: eg datetime.date(2017, 3, 3)
. 我将日期作为
datetime.date
对象:例如datetime.date(2017, 3, 3)
。 I have the times as a Series: 我有一系列的时代:
df['Time'] = pd.to_datetime(df['Time strings'], format='%H:%M:%S.%f')
How can I add together the scalar date with the array of times? 如何将标量日期与时间数组相加?
Also, some of these logs go past midnight. 此外,其中一些日志会超过午夜。 I'm thinking I need to use something like
numpy.unwrap()
to continue onto the next date, but I'm not sure how to do that with pandas.datetime
. 我在想我需要使用
numpy.unwrap()
类的东西继续到下一个日期,但是我不确定如何使用pandas.datetime
。
Instead of using pd.to_datetime
on the times , you could use pd.to_timedelta
. 而不是使用的
pd.to_datetime
的时候 ,你可以使用pd.to_timedelta
。 Then, you could simply add the datetime for that file to the entire column and it would be converted to a column of datetimes. 然后,您可以简单地将该文件的日期时间添加到整个列中,并将其转换为日期时间列。
Example 例
times = ['4:23:12.12', '11:25:43.23', '14:29:55.42']
df = pd.DataFrame(dict(times=times))
df.times = pd.to_timedelta(df.times)
df
# times
# 0 04:23:12.120000
# 1 11:25:43.230000
# 2 14:29:55.420000
file_date = datetime.date(2017, 3, 3)
df.times += file_date
df
# times
# 0 2017-03-03 04:23:12.120
# 1 2017-03-03 11:25:43.230
# 2 2017-03-03 14:29:55.420
Alternatively, you could try to read the dates/times upon input with parse_dates
. 或者,您可以尝试使用
parse_dates
输入时读取日期/时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.