熊猫用df.apply结果替换原始列空值

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

这是我的解决方案-

>>>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']

评论
  • o梦倾城
    o梦倾城 回复

    Fix your code with fillna

    df['stamp B'].fillna(result, inplace=True)
    
  • 我 也呸!
    我 也呸! 回复

    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)
    
  • 么么哒
    么么哒 回复

    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)