Created
October 20, 2013 14:46
-
-
Save unutbu/7070565 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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