|
1 | 1 | from contextlib import nullcontext
|
| 2 | +import copy |
2 | 3 |
|
3 | 4 | import numpy as np
|
4 | 5 | import pytest
|
5 | 6 |
|
6 |
| -from pandas import MultiIndex, Series |
| 7 | +from pandas._libs.missing import is_matching_na |
| 8 | + |
| 9 | +from pandas.core.dtypes.common import is_float |
| 10 | + |
| 11 | +from pandas import Index, MultiIndex, Series |
7 | 12 | import pandas._testing as tm
|
8 | 13 |
|
9 | 14 |
|
@@ -65,3 +70,54 @@ def test_equals_false_negative():
|
65 | 70 | assert s1.equals(s4)
|
66 | 71 | assert s1.equals(s5)
|
67 | 72 | assert s5.equals(s6)
|
| 73 | + |
| 74 | + |
| 75 | +def test_equals_matching_nas(): |
| 76 | + # matching but not identical NAs |
| 77 | + left = Series([np.datetime64("NaT")], dtype=object) |
| 78 | + right = Series([np.datetime64("NaT")], dtype=object) |
| 79 | + assert left.equals(right) |
| 80 | + assert Index(left).equals(Index(right)) |
| 81 | + assert left.array.equals(right.array) |
| 82 | + |
| 83 | + left = Series([np.timedelta64("NaT")], dtype=object) |
| 84 | + right = Series([np.timedelta64("NaT")], dtype=object) |
| 85 | + assert left.equals(right) |
| 86 | + assert Index(left).equals(Index(right)) |
| 87 | + assert left.array.equals(right.array) |
| 88 | + |
| 89 | + left = Series([np.float64("NaN")], dtype=object) |
| 90 | + right = Series([np.float64("NaN")], dtype=object) |
| 91 | + assert left.equals(right) |
| 92 | + assert Index(left).equals(Index(right)) |
| 93 | + assert left.array.equals(right.array) |
| 94 | + |
| 95 | + |
| 96 | +def test_equals_mismatched_nas(nulls_fixture, nulls_fixture2): |
| 97 | + # GH#39650 |
| 98 | + left = nulls_fixture |
| 99 | + right = nulls_fixture2 |
| 100 | + if hasattr(right, "copy"): |
| 101 | + right = right.copy() |
| 102 | + else: |
| 103 | + right = copy.copy(right) |
| 104 | + |
| 105 | + ser = Series([left], dtype=object) |
| 106 | + ser2 = Series([right], dtype=object) |
| 107 | + |
| 108 | + if is_matching_na(left, right): |
| 109 | + assert ser.equals(ser2) |
| 110 | + elif (left is None and is_float(right)) or (right is None and is_float(left)): |
| 111 | + assert ser.equals(ser2) |
| 112 | + else: |
| 113 | + assert not ser.equals(ser2) |
| 114 | + |
| 115 | + |
| 116 | +def test_equals_none_vs_nan(): |
| 117 | + # GH#39650 |
| 118 | + ser = Series([1, None], dtype=object) |
| 119 | + ser2 = Series([1, np.nan], dtype=object) |
| 120 | + |
| 121 | + assert ser.equals(ser2) |
| 122 | + assert Index(ser).equals(Index(ser2)) |
| 123 | + assert ser.array.equals(ser2.array) |
0 commit comments