common.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. Assertion helpers for arithmetic tests.
  3. """
  4. import numpy as np
  5. import pytest
  6. from pandas import (
  7. DataFrame,
  8. Index,
  9. Series,
  10. array,
  11. )
  12. import pandas._testing as tm
  13. from pandas.core.arrays import PandasArray
  14. def assert_invalid_addsub_type(left, right, msg=None):
  15. """
  16. Helper to assert that left and right can be neither added nor subtracted.
  17. Parameters
  18. ----------
  19. left : object
  20. right : object
  21. msg : str or None, default None
  22. """
  23. with pytest.raises(TypeError, match=msg):
  24. left + right
  25. with pytest.raises(TypeError, match=msg):
  26. right + left
  27. with pytest.raises(TypeError, match=msg):
  28. left - right
  29. with pytest.raises(TypeError, match=msg):
  30. right - left
  31. def get_upcast_box(box, vector):
  32. """
  33. Given two box-types, find the one that takes priority
  34. """
  35. if box is DataFrame or isinstance(vector, DataFrame):
  36. return DataFrame
  37. if box is Series or isinstance(vector, Series):
  38. return Series
  39. if box is Index or isinstance(vector, Index):
  40. return Index
  41. return box
  42. def assert_invalid_comparison(left, right, box):
  43. """
  44. Assert that comparison operations with mismatched types behave correctly.
  45. Parameters
  46. ----------
  47. left : np.ndarray, ExtensionArray, Index, or Series
  48. right : object
  49. box : {pd.DataFrame, pd.Series, pd.Index, pd.array, tm.to_array}
  50. """
  51. # Not for tznaive-tzaware comparison
  52. # Note: not quite the same as how we do this for tm.box_expected
  53. xbox = box if box not in [Index, array] else np.array
  54. def xbox2(x):
  55. # Eventually we'd like this to be tighter, but for now we'll
  56. # just exclude PandasArray[bool]
  57. if isinstance(x, PandasArray):
  58. return x._ndarray
  59. return x
  60. result = xbox2(left == right)
  61. expected = xbox(np.zeros(result.shape, dtype=np.bool_))
  62. tm.assert_equal(result, expected)
  63. result = xbox2(right == left)
  64. tm.assert_equal(result, expected)
  65. result = xbox2(left != right)
  66. tm.assert_equal(result, ~expected)
  67. result = xbox2(right != left)
  68. tm.assert_equal(result, ~expected)
  69. msg = "|".join(
  70. [
  71. "Invalid comparison between",
  72. "Cannot compare type",
  73. "not supported between",
  74. "invalid type promotion",
  75. (
  76. # GH#36706 npdev 1.20.0 2020-09-28
  77. r"The DTypes <class 'numpy.dtype\[datetime64\]'> and "
  78. r"<class 'numpy.dtype\[int64\]'> do not have a common DType. "
  79. "For example they cannot be stored in a single array unless the "
  80. "dtype is `object`."
  81. ),
  82. ]
  83. )
  84. with pytest.raises(TypeError, match=msg):
  85. left < right
  86. with pytest.raises(TypeError, match=msg):
  87. left <= right
  88. with pytest.raises(TypeError, match=msg):
  89. left > right
  90. with pytest.raises(TypeError, match=msg):
  91. left >= right
  92. with pytest.raises(TypeError, match=msg):
  93. right < left
  94. with pytest.raises(TypeError, match=msg):
  95. right <= left
  96. with pytest.raises(TypeError, match=msg):
  97. right > left
  98. with pytest.raises(TypeError, match=msg):
  99. right >= left