Skip to content

Instantly share code, notes, and snippets.

@unutbu unutbu/gist:7070565
Created Oct 20, 2013

Embed
What would you like to do?
import numpy as np
import pandas as pd
nan = np.nan
def array_equivalent(a1, a2):
try:
a1, a2 = np.asarray(a1), np.asarray(a2)
except (TypeError, ValueError):
return False
a1_mask = pd.isnull(a1)
a2_mask = pd.isnull(a2)
if np.isscalar(a1_mask):
return (np.isscalar(a2_mask)
and ((a1_mask and a2_mask) # both are nans
or a1 == a2)) # they compare equal
else:
result = (a1.shape == a2.shape
and (a1_mask == a2_mask).all()
and np.array_equal(a1[~a1_mask], a2[~a2_mask]))
return result
def array_equivalent2(a1, a2):
try:
a1, a2 = np.asarray(a1), np.asarray(a2)
except (TypeError, ValueError):
return False
result = (a1.shape == a2.shape) and ((a1 == a2) | ((a1 != a1) & (a2 != a2))).all()
return result
left = pd.Float64Index([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, nan], dtype='object')
right = pd.Float64Index([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, nan], dtype='object')
print(array_equivalent(left, right))
# True
print(array_equivalent2(left, right))
# False
print(left != left)
# [False False False False False False False]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.