test_interval.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import operator
  2. import numpy as np
  3. import pytest
  4. from pandas.core.dtypes.common import is_list_like
  5. import pandas as pd
  6. from pandas import (
  7. Categorical,
  8. Index,
  9. Interval,
  10. IntervalIndex,
  11. Period,
  12. Series,
  13. Timedelta,
  14. Timestamp,
  15. date_range,
  16. period_range,
  17. timedelta_range,
  18. )
  19. import pandas._testing as tm
  20. from pandas.core.arrays import (
  21. BooleanArray,
  22. IntervalArray,
  23. )
  24. from pandas.tests.arithmetic.common import get_upcast_box
  25. @pytest.fixture(
  26. params=[
  27. (Index([0, 2, 4, 4]), Index([1, 3, 5, 8])),
  28. (Index([0.0, 1.0, 2.0, np.nan]), Index([1.0, 2.0, 3.0, np.nan])),
  29. (
  30. timedelta_range("0 days", periods=3).insert(3, pd.NaT),
  31. timedelta_range("1 day", periods=3).insert(3, pd.NaT),
  32. ),
  33. (
  34. date_range("20170101", periods=3).insert(3, pd.NaT),
  35. date_range("20170102", periods=3).insert(3, pd.NaT),
  36. ),
  37. (
  38. date_range("20170101", periods=3, tz="US/Eastern").insert(3, pd.NaT),
  39. date_range("20170102", periods=3, tz="US/Eastern").insert(3, pd.NaT),
  40. ),
  41. ],
  42. ids=lambda x: str(x[0].dtype),
  43. )
  44. def left_right_dtypes(request):
  45. """
  46. Fixture for building an IntervalArray from various dtypes
  47. """
  48. return request.param
  49. @pytest.fixture
  50. def interval_array(left_right_dtypes):
  51. """
  52. Fixture to generate an IntervalArray of various dtypes containing NA if possible
  53. """
  54. left, right = left_right_dtypes
  55. return IntervalArray.from_arrays(left, right)
  56. def create_categorical_intervals(left, right, closed="right"):
  57. return Categorical(IntervalIndex.from_arrays(left, right, closed))
  58. def create_series_intervals(left, right, closed="right"):
  59. return Series(IntervalArray.from_arrays(left, right, closed))
  60. def create_series_categorical_intervals(left, right, closed="right"):
  61. return Series(Categorical(IntervalIndex.from_arrays(left, right, closed)))
  62. class TestComparison:
  63. @pytest.fixture(params=[operator.eq, operator.ne])
  64. def op(self, request):
  65. return request.param
  66. @pytest.fixture(
  67. params=[
  68. IntervalArray.from_arrays,
  69. IntervalIndex.from_arrays,
  70. create_categorical_intervals,
  71. create_series_intervals,
  72. create_series_categorical_intervals,
  73. ],
  74. ids=[
  75. "IntervalArray",
  76. "IntervalIndex",
  77. "Categorical[Interval]",
  78. "Series[Interval]",
  79. "Series[Categorical[Interval]]",
  80. ],
  81. )
  82. def interval_constructor(self, request):
  83. """
  84. Fixture for all pandas native interval constructors.
  85. To be used as the LHS of IntervalArray comparisons.
  86. """
  87. return request.param
  88. def elementwise_comparison(self, op, interval_array, other):
  89. """
  90. Helper that performs elementwise comparisons between `array` and `other`
  91. """
  92. other = other if is_list_like(other) else [other] * len(interval_array)
  93. expected = np.array([op(x, y) for x, y in zip(interval_array, other)])
  94. if isinstance(other, Series):
  95. return Series(expected, index=other.index)
  96. return expected
  97. def test_compare_scalar_interval(self, op, interval_array):
  98. # matches first interval
  99. other = interval_array[0]
  100. result = op(interval_array, other)
  101. expected = self.elementwise_comparison(op, interval_array, other)
  102. tm.assert_numpy_array_equal(result, expected)
  103. # matches on a single endpoint but not both
  104. other = Interval(interval_array.left[0], interval_array.right[1])
  105. result = op(interval_array, other)
  106. expected = self.elementwise_comparison(op, interval_array, other)
  107. tm.assert_numpy_array_equal(result, expected)
  108. def test_compare_scalar_interval_mixed_closed(self, op, closed, other_closed):
  109. interval_array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed)
  110. other = Interval(0, 1, closed=other_closed)
  111. result = op(interval_array, other)
  112. expected = self.elementwise_comparison(op, interval_array, other)
  113. tm.assert_numpy_array_equal(result, expected)
  114. def test_compare_scalar_na(self, op, interval_array, nulls_fixture, box_with_array):
  115. box = box_with_array
  116. obj = tm.box_expected(interval_array, box)
  117. result = op(obj, nulls_fixture)
  118. if nulls_fixture is pd.NA:
  119. # GH#31882
  120. exp = np.ones(interval_array.shape, dtype=bool)
  121. expected = BooleanArray(exp, exp)
  122. else:
  123. expected = self.elementwise_comparison(op, interval_array, nulls_fixture)
  124. if not (box is Index and nulls_fixture is pd.NA):
  125. # don't cast expected from BooleanArray to ndarray[object]
  126. xbox = get_upcast_box(obj, nulls_fixture, True)
  127. expected = tm.box_expected(expected, xbox)
  128. tm.assert_equal(result, expected)
  129. rev = op(nulls_fixture, obj)
  130. tm.assert_equal(rev, expected)
  131. @pytest.mark.parametrize(
  132. "other",
  133. [
  134. 0,
  135. 1.0,
  136. True,
  137. "foo",
  138. Timestamp("2017-01-01"),
  139. Timestamp("2017-01-01", tz="US/Eastern"),
  140. Timedelta("0 days"),
  141. Period("2017-01-01", "D"),
  142. ],
  143. )
  144. def test_compare_scalar_other(self, op, interval_array, other):
  145. result = op(interval_array, other)
  146. expected = self.elementwise_comparison(op, interval_array, other)
  147. tm.assert_numpy_array_equal(result, expected)
  148. def test_compare_list_like_interval(self, op, interval_array, interval_constructor):
  149. # same endpoints
  150. other = interval_constructor(interval_array.left, interval_array.right)
  151. result = op(interval_array, other)
  152. expected = self.elementwise_comparison(op, interval_array, other)
  153. tm.assert_equal(result, expected)
  154. # different endpoints
  155. other = interval_constructor(
  156. interval_array.left[::-1], interval_array.right[::-1]
  157. )
  158. result = op(interval_array, other)
  159. expected = self.elementwise_comparison(op, interval_array, other)
  160. tm.assert_equal(result, expected)
  161. # all nan endpoints
  162. other = interval_constructor([np.nan] * 4, [np.nan] * 4)
  163. result = op(interval_array, other)
  164. expected = self.elementwise_comparison(op, interval_array, other)
  165. tm.assert_equal(result, expected)
  166. def test_compare_list_like_interval_mixed_closed(
  167. self, op, interval_constructor, closed, other_closed
  168. ):
  169. interval_array = IntervalArray.from_arrays(range(2), range(1, 3), closed=closed)
  170. other = interval_constructor(range(2), range(1, 3), closed=other_closed)
  171. result = op(interval_array, other)
  172. expected = self.elementwise_comparison(op, interval_array, other)
  173. tm.assert_equal(result, expected)
  174. @pytest.mark.parametrize(
  175. "other",
  176. [
  177. (
  178. Interval(0, 1),
  179. Interval(Timedelta("1 day"), Timedelta("2 days")),
  180. Interval(4, 5, "both"),
  181. Interval(10, 20, "neither"),
  182. ),
  183. (0, 1.5, Timestamp("20170103"), np.nan),
  184. (
  185. Timestamp("20170102", tz="US/Eastern"),
  186. Timedelta("2 days"),
  187. "baz",
  188. pd.NaT,
  189. ),
  190. ],
  191. )
  192. def test_compare_list_like_object(self, op, interval_array, other):
  193. result = op(interval_array, other)
  194. expected = self.elementwise_comparison(op, interval_array, other)
  195. tm.assert_numpy_array_equal(result, expected)
  196. def test_compare_list_like_nan(self, op, interval_array, nulls_fixture):
  197. other = [nulls_fixture] * 4
  198. result = op(interval_array, other)
  199. expected = self.elementwise_comparison(op, interval_array, other)
  200. tm.assert_equal(result, expected)
  201. @pytest.mark.parametrize(
  202. "other",
  203. [
  204. np.arange(4, dtype="int64"),
  205. np.arange(4, dtype="float64"),
  206. date_range("2017-01-01", periods=4),
  207. date_range("2017-01-01", periods=4, tz="US/Eastern"),
  208. timedelta_range("0 days", periods=4),
  209. period_range("2017-01-01", periods=4, freq="D"),
  210. Categorical(list("abab")),
  211. Categorical(date_range("2017-01-01", periods=4)),
  212. pd.array(list("abcd")),
  213. pd.array(["foo", 3.14, None, object()], dtype=object),
  214. ],
  215. ids=lambda x: str(x.dtype),
  216. )
  217. def test_compare_list_like_other(self, op, interval_array, other):
  218. result = op(interval_array, other)
  219. expected = self.elementwise_comparison(op, interval_array, other)
  220. tm.assert_numpy_array_equal(result, expected)
  221. @pytest.mark.parametrize("length", [1, 3, 5])
  222. @pytest.mark.parametrize("other_constructor", [IntervalArray, list])
  223. def test_compare_length_mismatch_errors(self, op, other_constructor, length):
  224. interval_array = IntervalArray.from_arrays(range(4), range(1, 5))
  225. other = other_constructor([Interval(0, 1)] * length)
  226. with pytest.raises(ValueError, match="Lengths must match to compare"):
  227. op(interval_array, other)
  228. @pytest.mark.parametrize(
  229. "constructor, expected_type, assert_func",
  230. [
  231. (IntervalIndex, np.array, tm.assert_numpy_array_equal),
  232. (Series, Series, tm.assert_series_equal),
  233. ],
  234. )
  235. def test_index_series_compat(self, op, constructor, expected_type, assert_func):
  236. # IntervalIndex/Series that rely on IntervalArray for comparisons
  237. breaks = range(4)
  238. index = constructor(IntervalIndex.from_breaks(breaks))
  239. # scalar comparisons
  240. other = index[0]
  241. result = op(index, other)
  242. expected = expected_type(self.elementwise_comparison(op, index, other))
  243. assert_func(result, expected)
  244. other = breaks[0]
  245. result = op(index, other)
  246. expected = expected_type(self.elementwise_comparison(op, index, other))
  247. assert_func(result, expected)
  248. # list-like comparisons
  249. other = IntervalArray.from_breaks(breaks)
  250. result = op(index, other)
  251. expected = expected_type(self.elementwise_comparison(op, index, other))
  252. assert_func(result, expected)
  253. other = [index[0], breaks[0], "foo"]
  254. result = op(index, other)
  255. expected = expected_type(self.elementwise_comparison(op, index, other))
  256. assert_func(result, expected)
  257. @pytest.mark.parametrize("scalars", ["a", False, 1, 1.0, None])
  258. def test_comparison_operations(self, scalars):
  259. # GH #28981
  260. expected = Series([False, False])
  261. s = Series([Interval(0, 1), Interval(1, 2)], dtype="interval")
  262. result = s == scalars
  263. tm.assert_series_equal(result, expected)