累积重置为NaN [英] Cumsum reset at NaN

查看:48
本文介绍了累积重置为NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个名为tspandas.core.series.Series,其值为1或NaN,如下所示:

If I have a pandas.core.series.Series named ts of either 1's or NaN's like this:

3382   NaN
3381   NaN
...
3369   NaN
3368   NaN
...
15     1
10   NaN
11     1
12     1
13     1
9    NaN
8    NaN
7    NaN
6    NaN
3    NaN
4      1
5      1
2    NaN
1    NaN
0    NaN

我想计算该系列的总和,但应在NaN的位置将其重置(设置为零),如下所示:

I would like to calculate cumsum of this serie but it should be reset (set to zero) at the location of the NaNs like below:

3382   0
3381   0
...
3369   0
3368   0
...
15     1
10     0
11     1
12     2
13     3
9      0
8      0
7      0
6      0
3      0
4      1
5      2
2      0
1      0
0      0

理想情况下,我想有一个向量化的解决方案!

Ideally I would like to have a vectorized solution !

我曾经在Matlab上看到过类似的问题: 在NaN上重置Matlab cumsum?

I ever see a similar question with Matlab : Matlab cumsum reset at NaN?

但是我不知道如何翻译这行d = diff([0 c(n)]);

but I don't know how to translate this line d = diff([0 c(n)]);

推荐答案

您的Matlab代码的简单Numpy翻译是这样的:

A simple Numpy translation of your Matlab code is this:

import numpy as np

v = np.array([1., 1., 1., np.nan, 1., 1., 1., 1., np.nan, 1.])
n = np.isnan(v)
a = ~n
c = np.cumsum(a)
d = np.diff(np.concatenate(([0.], c[n])))
v[n] = -d
np.cumsum(v)

执行此代码将返回结果array([ 1., 2., 3., 0., 1., 2., 3., 4., 0., 1.]).此解决方案只能与原始解决方案一样有效,但是如果它不足以满足您的目的,也许可以帮助您提出更好的解决方案.

Executing this code returns the result array([ 1., 2., 3., 0., 1., 2., 3., 4., 0., 1.]). This solution will only be as valid as the original one, but maybe it will help you come up with something better if it isn't sufficient for your purposes.

这篇关于累积重置为NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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