[英]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_TIME
的hh.mm.ss
是hh.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.