test_period.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. """
  2. This file contains a minimal set of tests for compliance with the extension
  3. array interface test suite, and should contain no other tests.
  4. The test suite for the full functionality of the array is located in
  5. `pandas/tests/arrays/`.
  6. The tests in this file are inherited from the BaseExtensionTests, and only
  7. minimal tweaks should be applied to get the tests passing (by overwriting a
  8. parent method).
  9. Additional tests should either be added to one of the BaseExtensionTests
  10. classes (if they are relevant for the extension interface for all dtypes), or
  11. be added to the array-specific tests in `pandas/tests/arrays/`.
  12. """
  13. import numpy as np
  14. import pytest
  15. from pandas._libs import iNaT
  16. from pandas.compat import is_platform_windows
  17. from pandas.compat.numpy import np_version_gte1p24
  18. from pandas.core.dtypes.dtypes import PeriodDtype
  19. import pandas as pd
  20. import pandas._testing as tm
  21. from pandas.core.arrays import PeriodArray
  22. from pandas.tests.extension import base
  23. @pytest.fixture(params=["D", "2D"])
  24. def dtype(request):
  25. return PeriodDtype(freq=request.param)
  26. @pytest.fixture
  27. def data(dtype):
  28. return PeriodArray(np.arange(1970, 2070), freq=dtype.freq)
  29. @pytest.fixture
  30. def data_for_twos(dtype):
  31. return PeriodArray(np.ones(100) * 2, freq=dtype.freq)
  32. @pytest.fixture
  33. def data_for_sorting(dtype):
  34. return PeriodArray([2018, 2019, 2017], freq=dtype.freq)
  35. @pytest.fixture
  36. def data_missing(dtype):
  37. return PeriodArray([iNaT, 2017], freq=dtype.freq)
  38. @pytest.fixture
  39. def data_missing_for_sorting(dtype):
  40. return PeriodArray([2018, iNaT, 2017], freq=dtype.freq)
  41. @pytest.fixture
  42. def data_for_grouping(dtype):
  43. B = 2018
  44. NA = iNaT
  45. A = 2017
  46. C = 2019
  47. return PeriodArray([B, B, NA, NA, A, A, B, C], freq=dtype.freq)
  48. @pytest.fixture
  49. def na_value():
  50. return pd.NaT
  51. class BasePeriodTests:
  52. pass
  53. class TestPeriodDtype(BasePeriodTests, base.BaseDtypeTests):
  54. pass
  55. class TestConstructors(BasePeriodTests, base.BaseConstructorsTests):
  56. pass
  57. class TestGetitem(BasePeriodTests, base.BaseGetitemTests):
  58. pass
  59. class TestIndex(base.BaseIndexTests):
  60. pass
  61. class TestMethods(BasePeriodTests, base.BaseMethodsTests):
  62. def test_combine_add(self, data_repeated):
  63. # Period + Period is not defined.
  64. pass
  65. @pytest.mark.parametrize("periods", [1, -2])
  66. def test_diff(self, data, periods):
  67. if is_platform_windows() and np_version_gte1p24:
  68. with tm.assert_produces_warning(RuntimeWarning, check_stacklevel=False):
  69. super().test_diff(data, periods)
  70. else:
  71. super().test_diff(data, periods)
  72. class TestInterface(BasePeriodTests, base.BaseInterfaceTests):
  73. pass
  74. class TestArithmeticOps(BasePeriodTests, base.BaseArithmeticOpsTests):
  75. implements = {"__sub__", "__rsub__"}
  76. def test_arith_frame_with_scalar(self, data, all_arithmetic_operators):
  77. # frame & scalar
  78. if all_arithmetic_operators in self.implements:
  79. df = pd.DataFrame({"A": data})
  80. self.check_opname(df, all_arithmetic_operators, data[0], exc=None)
  81. else:
  82. # ... but not the rest.
  83. super().test_arith_frame_with_scalar(data, all_arithmetic_operators)
  84. def test_arith_series_with_scalar(self, data, all_arithmetic_operators):
  85. # we implement substitution...
  86. if all_arithmetic_operators in self.implements:
  87. s = pd.Series(data)
  88. self.check_opname(s, all_arithmetic_operators, s.iloc[0], exc=None)
  89. else:
  90. # ... but not the rest.
  91. super().test_arith_series_with_scalar(data, all_arithmetic_operators)
  92. def test_arith_series_with_array(self, data, all_arithmetic_operators):
  93. if all_arithmetic_operators in self.implements:
  94. s = pd.Series(data)
  95. self.check_opname(s, all_arithmetic_operators, s.iloc[0], exc=None)
  96. else:
  97. # ... but not the rest.
  98. super().test_arith_series_with_scalar(data, all_arithmetic_operators)
  99. def _check_divmod_op(self, s, op, other, exc=NotImplementedError):
  100. super()._check_divmod_op(s, op, other, exc=TypeError)
  101. def test_add_series_with_extension_array(self, data):
  102. # we don't implement + for Period
  103. s = pd.Series(data)
  104. msg = (
  105. r"unsupported operand type\(s\) for \+: "
  106. r"\'PeriodArray\' and \'PeriodArray\'"
  107. )
  108. with pytest.raises(TypeError, match=msg):
  109. s + data
  110. def test_direct_arith_with_ndframe_returns_not_implemented(
  111. self, data, frame_or_series
  112. ):
  113. # Override to use __sub__ instead of __add__
  114. other = pd.Series(data)
  115. if frame_or_series is pd.DataFrame:
  116. other = other.to_frame()
  117. result = data.__sub__(other)
  118. assert result is NotImplemented
  119. class TestCasting(BasePeriodTests, base.BaseCastingTests):
  120. pass
  121. class TestComparisonOps(BasePeriodTests, base.BaseComparisonOpsTests):
  122. pass
  123. class TestMissing(BasePeriodTests, base.BaseMissingTests):
  124. pass
  125. class TestReshaping(BasePeriodTests, base.BaseReshapingTests):
  126. pass
  127. class TestSetitem(BasePeriodTests, base.BaseSetitemTests):
  128. pass
  129. class TestGroupby(BasePeriodTests, base.BaseGroupbyTests):
  130. pass
  131. class TestPrinting(BasePeriodTests, base.BasePrintingTests):
  132. pass
  133. class TestParsing(BasePeriodTests, base.BaseParsingTests):
  134. @pytest.mark.parametrize("engine", ["c", "python"])
  135. def test_EA_types(self, engine, data):
  136. super().test_EA_types(engine, data)
  137. class Test2DCompat(BasePeriodTests, base.NDArrayBacked2DTests):
  138. pass