0

Was reviewing the accepted answer of another question.

The section in the demonstration that confuses me is

In [37]: (sign != sign.shift()).cumsum()
Out[37]: 
0    1
1    1
2    2
3    2
4    2
5    3
6    3
Name: values, dtype: int64

It appears that everytime the (sign != sign.shift()) gives a True result, cumsum() returns an incremented number, and keeps returning that number until it encounters another True.

Looking at the sparse docs for .cumsum, I have no idea how/why that behavior is occuring, however useful that it is!

My only guess is cumsum() treats True as 1 and False as 0. Doesn't seem like a pythonic thing to do -- sounds like something from C.

Could someone explain why this effect occurs?

user3556757
  • 3,469
  • 4
  • 30
  • 70

0 Answers0