Skip to content

Pandas Series.ne operator returning unexpected result against two slices of same Series #19855

Closed
@hunterjackson

Description

@hunterjackson

So I have this series of integers shown below

    from pandas import Series
    s = Series([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

And I want to see how many times the numbers changes over the series, so I compare two slices of the same string to one another.

    s[1:].ne(s[:-1])
    Out[4]: 
    0      True
    1     False
    2     False
    3     False
    4     False
    5     False
    6     False
    7     False
    8     False
    9     False
    10    False
    11    False
    12    False
    13    False
    14    False
    15    False
    16    False
    17    False
    18    False
    19    False
    20    False
    21    False
    22    False
    23    False
    24    False
    25    False
    26    False
    27    False
    28    False
    29    False
    30    False
    31    False
    32    False
    33    False
    34    False
    35    False
    36    False
    37    False
    38    False
    39     True
    dtype: bool

Not only does the output using the Series.ne method not make any logical sense to me but the output is also longer than either of the inputs which is especially confusing.

I think this might be related to this #1134

Apologies if this isn't an issue but I haven't been able to find any satisfactory explanation for this behavior

tl;dr:

Where s is a pandas.Series of int's

[i != s[:-1][idx] for idx, i in enumerate(s[1:])] != s[:-1].ne(s[1:]).tolist()

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselvesNumeric OperationsArithmetic, Comparison, and Logical operationsUsage Question

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions