Reputation: 2216
I have below dataframe df
, where stamp B
are null sometimes. Have to fill such null values with date of Stamp A
and respective time from the Time
column
stamp A stamp B Time
0 2012-10-08 18:15:05 2012-10-08 18:15:05 19:00:01
1 2012-10-09 12:15:05 NaT 18:45:09
2 2012-10-11 18:13:00 NaT 12:20:20
3 2012-10-11 08:15:15 2012-10-11 18:15:05 22:10:05
4 2012-10-12 18:15:20 2012-10-12 17:10:20 19:34:12
Here is my solution -
>>>from datetime import dateime as dtm
>>>result = df[df['stamp B'].isnull()].apply(lambda x: dtm.combine(x['stamp A'].date(), dtm.strptime(x["Time"], "%H:%M:%S").time()), axis=1)
It returns result
as below:
1 2012-10-09 18:45:09
2 2012-10-11 12:20:20
dtype: datetime64[ns]
But not sure, how to replace this result
withNaT
values in the original dataframe df['stamp B']
Upvotes: 2
Views: 60
Reputation: 150745
I would extract the date from stamp A
, add the Time
, then do a fillna
on stamp B
:
s = df['stamp A'].dt.normalized() + pd.to_timedelta(df['Time'])
df['stamp B'] = df['stamp B'].fillna(s)
Upvotes: 3
Reputation: 862671
Use Series.dt.floor
for remove times and add timedeltas by to_timedelta
and then replace missing values by Series.combine_first
:
dates = df['stamp A'].dt.floor('d').add(pd.to_timedelta(df['Time']))
df['stamp B'] = df['stamp B'].combine_first(dates)
print (df)
stamp A stamp B Time
0 2012-10-08 18:15:05 2012-10-08 18:15:05 19:00:01
1 2012-10-09 12:15:05 2012-10-09 18:45:09 18:45:09
2 2012-10-11 18:13:00 2012-10-11 12:20:20 12:20:20
3 2012-10-11 08:15:15 2012-10-11 18:15:05 22:10:05
4 2012-10-12 18:15:20 2012-10-12 17:10:20 19:34:12
Upvotes: 3