test_datetime.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.core.dtypes.dtypes import DatetimeTZDtype
  16. import pandas as pd
  17. from pandas.core.arrays import DatetimeArray
  18. from pandas.tests.extension import base
  19. @pytest.fixture(params=["US/Central"])
  20. def dtype(request):
  21. return DatetimeTZDtype(unit="ns", tz=request.param)
  22. @pytest.fixture
  23. def data(dtype):
  24. data = DatetimeArray(pd.date_range("2000", periods=100, tz=dtype.tz), dtype=dtype)
  25. return data
  26. @pytest.fixture
  27. def data_missing(dtype):
  28. return DatetimeArray(
  29. np.array(["NaT", "2000-01-01"], dtype="datetime64[ns]"), dtype=dtype
  30. )
  31. @pytest.fixture
  32. def data_for_sorting(dtype):
  33. a = pd.Timestamp("2000-01-01")
  34. b = pd.Timestamp("2000-01-02")
  35. c = pd.Timestamp("2000-01-03")
  36. return DatetimeArray(np.array([b, c, a], dtype="datetime64[ns]"), dtype=dtype)
  37. @pytest.fixture
  38. def data_missing_for_sorting(dtype):
  39. a = pd.Timestamp("2000-01-01")
  40. b = pd.Timestamp("2000-01-02")
  41. return DatetimeArray(np.array([b, "NaT", a], dtype="datetime64[ns]"), dtype=dtype)
  42. @pytest.fixture
  43. def data_for_grouping(dtype):
  44. """
  45. Expected to be like [B, B, NA, NA, A, A, B, C]
  46. Where A < B < C and NA is missing
  47. """
  48. a = pd.Timestamp("2000-01-01")
  49. b = pd.Timestamp("2000-01-02")
  50. c = pd.Timestamp("2000-01-03")
  51. na = "NaT"
  52. return DatetimeArray(
  53. np.array([b, b, na, na, a, a, b, c], dtype="datetime64[ns]"), dtype=dtype
  54. )
  55. @pytest.fixture
  56. def na_cmp():
  57. def cmp(a, b):
  58. return a is pd.NaT and a is b
  59. return cmp
  60. @pytest.fixture
  61. def na_value():
  62. return pd.NaT
  63. # ----------------------------------------------------------------------------
  64. class BaseDatetimeTests:
  65. pass
  66. # ----------------------------------------------------------------------------
  67. # Tests
  68. class TestDatetimeDtype(BaseDatetimeTests, base.BaseDtypeTests):
  69. pass
  70. class TestConstructors(BaseDatetimeTests, base.BaseConstructorsTests):
  71. def test_series_constructor(self, data):
  72. # Series construction drops any .freq attr
  73. data = data._with_freq(None)
  74. super().test_series_constructor(data)
  75. class TestGetitem(BaseDatetimeTests, base.BaseGetitemTests):
  76. pass
  77. class TestIndex(base.BaseIndexTests):
  78. pass
  79. class TestMethods(BaseDatetimeTests, base.BaseMethodsTests):
  80. def test_combine_add(self, data_repeated):
  81. # Timestamp.__add__(Timestamp) not defined
  82. pass
  83. class TestInterface(BaseDatetimeTests, base.BaseInterfaceTests):
  84. pass
  85. class TestArithmeticOps(BaseDatetimeTests, base.BaseArithmeticOpsTests):
  86. implements = {"__sub__", "__rsub__"}
  87. def test_arith_frame_with_scalar(self, data, all_arithmetic_operators):
  88. # frame & scalar
  89. if all_arithmetic_operators in self.implements:
  90. df = pd.DataFrame({"A": data})
  91. self.check_opname(df, all_arithmetic_operators, data[0], exc=None)
  92. else:
  93. # ... but not the rest.
  94. super().test_arith_frame_with_scalar(data, all_arithmetic_operators)
  95. def test_arith_series_with_scalar(self, data, all_arithmetic_operators):
  96. if all_arithmetic_operators in self.implements:
  97. ser = pd.Series(data)
  98. self.check_opname(ser, all_arithmetic_operators, ser.iloc[0], exc=None)
  99. else:
  100. # ... but not the rest.
  101. super().test_arith_series_with_scalar(data, all_arithmetic_operators)
  102. def test_add_series_with_extension_array(self, data):
  103. # Datetime + Datetime not implemented
  104. ser = pd.Series(data)
  105. msg = "cannot add DatetimeArray and DatetimeArray"
  106. with pytest.raises(TypeError, match=msg):
  107. ser + data
  108. def test_arith_series_with_array(self, data, all_arithmetic_operators):
  109. if all_arithmetic_operators in self.implements:
  110. ser = pd.Series(data)
  111. self.check_opname(ser, all_arithmetic_operators, ser.iloc[0], exc=None)
  112. else:
  113. # ... but not the rest.
  114. super().test_arith_series_with_scalar(data, all_arithmetic_operators)
  115. def test_divmod_series_array(self):
  116. # GH 23287
  117. # skipping because it is not implemented
  118. pass
  119. class TestCasting(BaseDatetimeTests, base.BaseCastingTests):
  120. pass
  121. class TestComparisonOps(BaseDatetimeTests, base.BaseComparisonOpsTests):
  122. pass
  123. class TestMissing(BaseDatetimeTests, base.BaseMissingTests):
  124. pass
  125. class TestReshaping(BaseDatetimeTests, base.BaseReshapingTests):
  126. pass
  127. class TestSetitem(BaseDatetimeTests, base.BaseSetitemTests):
  128. pass
  129. class TestGroupby(BaseDatetimeTests, base.BaseGroupbyTests):
  130. pass
  131. class TestPrinting(BaseDatetimeTests, base.BasePrintingTests):
  132. pass
  133. class Test2DCompat(BaseDatetimeTests, base.NDArrayBacked2DTests):
  134. pass