pandas 以特定值向前填充“时间戳"列(1秒) [英] pandas forward fill Time Stamp columns with specific value (1 second)

查看:77
本文介绍了 pandas 以特定值向前填充“时间戳"列(1秒)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pandas.Series之类的

I have a pandas.Series like:

Timestamp_data:
0   2018-09-26 04:38:32.544 
1   NaT
2   NaT
3   NaT
4   2018-09-26 04:58:32.544
5   NaT

我想从以前的可用版本中连续向每个NaT添加1秒:

And I would like to add consecutively 1 second to each NaT from the previous available:

    Timestamp_data:
0   2018-09-26 04:38:32.544 
1   2018-09-26 04:39:32.544
2   2018-09-26 04:40:32.544
3   2018-09-26 04:41:32.544
4   2018-09-26 04:58:32.544
5   2018-09-26 04:59:32.544

该帖子是这样的:

通过将x添加到上一行来填充na值熊猫

但是在当前的numpy版本中,np.diff()对于布尔型具有TypeError.

But in the present numpy version the np.diff() has a TypeError for Booleans.

推荐答案

您只能使用最后一次填充NaT并添加新的Series:

Last forward filling NaT and add new Series:

print (df)
           Timestamp_data
0 2018-09-26 04:25:32.544
1 2018-09-26 04:38:32.544
2                     NaT
3                     NaT
4                     NaT
5 2018-09-26 04:58:32.544
6                     NaT

m = df['Timestamp_data'].notna()
#old pandas alternative
#m = df['Timestamp_data'].notnull()

s = (pd.to_timedelta(df[~m].groupby(m.cumsum()).cumcount() + 1, unit='s')
       .reindex(df.index, fill_value=0))

最后一个秒是60的一秒乘以1分钟:

Last is multiple one second by 60 for add 1 minute:

df['Timestamp_data'] = df['Timestamp_data'].ffill()  + s * 60

print (df)
           Timestamp_data
0 2018-09-26 04:25:32.544
1 2018-09-26 04:38:32.544
2 2018-09-26 04:39:32.544
3 2018-09-26 04:40:32.544
4 2018-09-26 04:41:32.544
5 2018-09-26 04:58:32.544
6 2018-09-26 04:59:32.544

一秒钟只能通过60删除多个:

For one second only remove multiple by 60:

df['Timestamp_data'] = df['Timestamp_data'].ffill()  + s 
print (df)
           Timestamp_data
0 2018-09-26 04:25:32.544
1 2018-09-26 04:38:32.544
2 2018-09-26 04:38:33.544
3 2018-09-26 04:38:34.544
4 2018-09-26 04:38:35.544
5 2018-09-26 04:58:32.544
6 2018-09-26 04:58:33.544

这篇关于 pandas 以特定值向前填充“时间戳"列(1秒)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆