Compare Series Containing None

I use the Python shift function to compare if the value in the series is equal to the previous value. Primarily

import pandas as pd

a = pd.Series([2, 2, 4, 5])

a == a.shift()
Out[1]: 
0    False
1     True
2    False
3    False
dtype: bool

This is as expected. (The first comparison is False because we are comparing from an NAoffset row). Now I have a series where I have no value, i.e. None, like this

b = pd.Series([None, None, 4, 5])

Here comparing the two NonegivesFalse

b == b.shift()
Out[3]: 
0    False
1    False
2    False
3    False
dtype: bool

I would agree to accept some kind of philosophical justification, arguing that comparison is Nonepointless, etc., however

c = None
d = None
c == d
Out[4]: True

What's going on here?!

And what I really want to know is; how can I do a comparison of my b-Series, given that I want it to be Nonetreated as equal? That is, I want to b == b.shift()give the same result as a == a.shift().

+6
3

None NaN NaN , :

[54]:
b = pd.Series([None, None, 4, 5])
b

Out[54]: 
0    NaN
1    NaN
2    4.0
3    5.0
dtype: float64

:

In[55]:
b==b

Out[55]: 
0    False
1    False
2     True
3     True
dtype: bool

, , :

In[68]:
( (b == b.shift())  | ( (b != b.shift()) &  (b != b) ) )

Out[68]: 
0     True
1     True
2    False
3    False
dtype: bool

, , shift , :

In[69]:
b.shift()

Out[69]: 
0    NaN
1    NaN
2    NaN
3    4.0
dtype: float64

, NaN True , NaN, .

False-positive, , :

In[70]:
( (b == b.shift())  | ( (b != b.shift()) &  (b != b) ) )[1:]

Out[70]: 
1     True
2    False
3    False
dtype: bool

, , Pandas numpy, float - int None, None NaN int s

, a , False, :

In[78]:
result = pd.Series( ( (b == b.shift())  | ( (b != b.shift()) &  (b != b) ) ) )
result.iloc[0] = False
result

Out[78]: 
0    False
1     True
2    False
3    False
dtype: bool
+4

(.. ), , rolling numpy:

import numpy as np

b = [None, None, 4, 5] 
# or list(b) if b is a pandas Series

np.roll(b,1) == b

:

> array([False,  True, False, False])
+1

, , /Numpy.

None, workaroud, apply:

In[1]:
foo = pd.Series([None, 'a'])
foo==None

Out[1]:
0    False
1    False
dtype: bool 


In[2]:
foo.apply(lambda a:a==None)
Out[2]: 
0     True
1    False
dtype: bool
0

Source: https://habr.com/ru/post/1683904/


All Articles