test_comparison.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import numpy as np
  2. import pytest
  3. import pandas as pd
  4. import pandas._testing as tm
  5. from pandas.core.arrays import FloatingArray
  6. from pandas.tests.arrays.masked_shared import (
  7. ComparisonOps,
  8. NumericOps,
  9. )
  10. class TestComparisonOps(NumericOps, ComparisonOps):
  11. @pytest.mark.parametrize("other", [True, False, pd.NA, -1.0, 0.0, 1])
  12. def test_scalar(self, other, comparison_op, dtype):
  13. ComparisonOps.test_scalar(self, other, comparison_op, dtype)
  14. def test_compare_with_integerarray(self, comparison_op):
  15. op = comparison_op
  16. a = pd.array([0, 1, None] * 3, dtype="Int64")
  17. b = pd.array([0] * 3 + [1] * 3 + [None] * 3, dtype="Float64")
  18. other = b.astype("Int64")
  19. expected = op(a, other)
  20. result = op(a, b)
  21. tm.assert_extension_array_equal(result, expected)
  22. expected = op(other, a)
  23. result = op(b, a)
  24. tm.assert_extension_array_equal(result, expected)
  25. def test_equals():
  26. # GH-30652
  27. # equals is generally tested in /tests/extension/base/methods, but this
  28. # specifically tests that two arrays of the same class but different dtype
  29. # do not evaluate equal
  30. a1 = pd.array([1, 2, None], dtype="Float64")
  31. a2 = pd.array([1, 2, None], dtype="Float32")
  32. assert a1.equals(a2) is False
  33. def test_equals_nan_vs_na():
  34. # GH#44382
  35. mask = np.zeros(3, dtype=bool)
  36. data = np.array([1.0, np.nan, 3.0], dtype=np.float64)
  37. left = FloatingArray(data, mask)
  38. assert left.equals(left)
  39. tm.assert_extension_array_equal(left, left)
  40. assert left.equals(left.copy())
  41. assert left.equals(FloatingArray(data.copy(), mask.copy()))
  42. mask2 = np.array([False, True, False], dtype=bool)
  43. data2 = np.array([1.0, 2.0, 3.0], dtype=np.float64)
  44. right = FloatingArray(data2, mask2)
  45. assert right.equals(right)
  46. tm.assert_extension_array_equal(right, right)
  47. assert not left.equals(right)
  48. # with mask[1] = True, the only difference is data[1], which should
  49. # not matter for equals
  50. mask[1] = True
  51. assert left.equals(right)