简体   繁体   English

如何将熊猫数据框日期和不同的时间格式连接到单个时间戳?

[英]How to concatenate pandas dataframe date and different time formats to single timestamp?

I have two columns in a pandas data frame as outlined below. 我在pandas数据框中有两列,如下所示。 Notice how some of the EVENT_TIME is in hh.mm.ss , some is in hh:mm:ss AM/PM format. 请注意,某些EVENT_TIMEhh.mm.sshh.mm.ss ,有些是hh:mm:ss AM/PM格式。

在此处输入图片说明

When running... 跑步时...

import pandas

df['EVENT_DATE'] = pd.to_datetime(df['EVENT_DATE'], format='%Y%m%d')

print(df['EVENT_DATE'])

...I can get EVENT_DATE in a consumable (for my purposes) format (eg 1999-07-28 ). ...我可以使用(出于我的目的)可消耗格式(例如1999-07-28 )获得EVENT_DATE

But when running... 但是跑步的时候

df['EVENT_TIME'] = pd.to_datetime(df['EVENT_TIME'], format='%H.%M.%S', errors='coerce')
df['EVENT_TIME'] = pd.to_datetime(df['EVENT_TIME'], format='%I:%M:%S %p', errors='coerce')

print(df['EVENT_TIME'])

... 1900-01-01 is added to the times and is not being applied to all rows. ... 1900-01-01已添加到时间中,并且未应用于所有行。

1900-01-01 16:40:00
1900-01-01 15:55:00
1900-01-01 14:30:00
1900-01-01 13:26:00
NaT
NaT
NaT
NaT

How do I concatenate the date and times (which include multiple time formats) in a single timestamp? 如何在单个时间戳中连接日期和时间(包括多种时间格式)?

Edit1: EDIT1:

@Wen-Ben 's solution got me here: @ Wen-Ben的解决方案使我在这里:

1      19:53:00
11     14:30:00
15     16:30:00

Then to concatenate EVENT_DATE and EVENT_TIME, I found this (which works): 然后将EVENT_DATE和EVENT_TIME串联起来,我发现了这一点(有效):

df['TIMESTAMP'] = df.apply(lambda r : pd.datetime.combine(r['EVENT_DATE'], r['EVENT_TIME']),1)

...results in: ...结果是:

1     1999-07-28 19:53:00
11    2001-07-28 14:30:00
15    2002-06-07 16:30:00

Next I want to get this into ISO8601 format. 接下来,我想将其转换为ISO8601格式。 So I found this (which works): 所以我发现了这个(有效):

pd.to_datetime(df['TIMESTAMP']).apply(lambda x: x.strftime('%Y%m%dT%H:%M%SZ'))

...results in: ...结果是:

1      19990728T19:5300Z
11     20010728T14:3000Z
15     20020607T16:3000Z

HERES MY NEW PROBLEM: 这里是我的新问题:

Running print(TIMESTAMP) still shows the concatenated versions (eg 1999-07-28 19:53:00 ) instead of the ISO version (eg 19990728T19:5300Z ) 运行print(TIMESTAMP)仍显示连接版本(例如1999-07-28 19:53:00 )而不是ISO版本(例如19990728T19:5300Z

How do I get the ISO8601 column "added" to the dataframe? 如何将ISO8601列“添加”到数据框?

Ideally, I want it to take the place of TIMESTAMP . 理想情况下,我希望它代替TIMESTAMP I want it as a transformation of the data, not tacked on as a new column. 我希望将其作为数据的转换,而不是作为新列添加。

Using fillna 使用fillna

s1=pd.to_datetime(df['EVENT_TIME'], format='%H.%M.%S', errors='coerce')
s2=pd.to_datetime(df['EVENT_TIME'], format='%I:%M:%S %p', errors='coerce')
df['EVENT_TIME']=s1.fillna(s2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在pandas数据帧中将不同的日期时间格式转换为MM / DD / YYYY格式 - Converting different date time formats to MM/DD/YYYY format in pandas dataframe 如何将不同格式的日期时间戳转换成hive表中的时间戳格式 - How to convert different formats of date timestamp to the format of timestamp in hive table 数据帧中的不同时间格式 - Different time formats in dataframe 如何标准化熊猫中的不同日期格式? - How to standardise different date formats in pandas? 从 pandas dataframe 中的字符串中获取不同格式的日期 - Fetching date in different formats from string in pandas dataframe 将不同的日期格式转换为数据框列中的单一格式 - Converting different date formats to a single format in a dataframe column 如何将 pandas dataframe 列中的两种不同日期格式转换为相同格式? - How to convert two different date formats from a pandas dataframe column into same format? 在熊猫数据框中转换日期格式 - Converting date formats in pandas dataframe 熊猫优雅地处理具有不同日期时间格式的列 - pandas handle column with different date time formats gracefully Pandas dataframe - 如何为现有日期列综合添加唯一时间戳,其中仅包含日期但不包含时间? - Pandas dataframe - How to synthetically add a unique timestamp for existing date column, which contains only date but no time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM