You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
4.0 KiB
160 lines
4.0 KiB
1 year ago
|
"""
|
||
|
This file contains a minimal set of tests for compliance with the extension
|
||
|
array interface test suite, and should contain no other tests.
|
||
|
The test suite for the full functionality of the array is located in
|
||
|
`pandas/tests/arrays/`.
|
||
|
|
||
|
The tests in this file are inherited from the BaseExtensionTests, and only
|
||
|
minimal tweaks should be applied to get the tests passing (by overwriting a
|
||
|
parent method).
|
||
|
|
||
|
Additional tests should either be added to one of the BaseExtensionTests
|
||
|
classes (if they are relevant for the extension interface for all dtypes), or
|
||
|
be added to the array-specific tests in `pandas/tests/arrays/`.
|
||
|
|
||
|
"""
|
||
|
import numpy as np
|
||
|
import pytest
|
||
|
|
||
|
from pandas.core.dtypes.dtypes import DatetimeTZDtype
|
||
|
|
||
|
import pandas as pd
|
||
|
import pandas._testing as tm
|
||
|
from pandas.core.arrays import DatetimeArray
|
||
|
from pandas.tests.extension import base
|
||
|
|
||
|
|
||
|
@pytest.fixture(params=["US/Central"])
|
||
|
def dtype(request):
|
||
|
return DatetimeTZDtype(unit="ns", tz=request.param)
|
||
|
|
||
|
|
||
|
@pytest.fixture
|
||
|
def data(dtype):
|
||
|
data = DatetimeArray(pd.date_range("2000", periods=100, tz=dtype.tz), dtype=dtype)
|
||
|
return data
|
||
|
|
||
|
|
||
|
@pytest.fixture
|
||
|
def data_missing(dtype):
|
||
|
return DatetimeArray(
|
||
|
np.array(["NaT", "2000-01-01"], dtype="datetime64[ns]"), dtype=dtype
|
||
|
)
|
||
|
|
||
|
|
||
|
@pytest.fixture
|
||
|
def data_for_sorting(dtype):
|
||
|
a = pd.Timestamp("2000-01-01")
|
||
|
b = pd.Timestamp("2000-01-02")
|
||
|
c = pd.Timestamp("2000-01-03")
|
||
|
return DatetimeArray(np.array([b, c, a], dtype="datetime64[ns]"), dtype=dtype)
|
||
|
|
||
|
|
||
|
@pytest.fixture
|
||
|
def data_missing_for_sorting(dtype):
|
||
|
a = pd.Timestamp("2000-01-01")
|
||
|
b = pd.Timestamp("2000-01-02")
|
||
|
return DatetimeArray(np.array([b, "NaT", a], dtype="datetime64[ns]"), dtype=dtype)
|
||
|
|
||
|
|
||
|
@pytest.fixture
|
||
|
def data_for_grouping(dtype):
|
||
|
"""
|
||
|
Expected to be like [B, B, NA, NA, A, A, B, C]
|
||
|
|
||
|
Where A < B < C and NA is missing
|
||
|
"""
|
||
|
a = pd.Timestamp("2000-01-01")
|
||
|
b = pd.Timestamp("2000-01-02")
|
||
|
c = pd.Timestamp("2000-01-03")
|
||
|
na = "NaT"
|
||
|
return DatetimeArray(
|
||
|
np.array([b, b, na, na, a, a, b, c], dtype="datetime64[ns]"), dtype=dtype
|
||
|
)
|
||
|
|
||
|
|
||
|
@pytest.fixture
|
||
|
def na_cmp():
|
||
|
def cmp(a, b):
|
||
|
return a is pd.NaT and a is b
|
||
|
|
||
|
return cmp
|
||
|
|
||
|
|
||
|
# ----------------------------------------------------------------------------
|
||
|
class BaseDatetimeTests:
|
||
|
pass
|
||
|
|
||
|
|
||
|
# ----------------------------------------------------------------------------
|
||
|
# Tests
|
||
|
class TestDatetimeDtype(BaseDatetimeTests, base.BaseDtypeTests):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class TestConstructors(BaseDatetimeTests, base.BaseConstructorsTests):
|
||
|
def test_series_constructor(self, data):
|
||
|
# Series construction drops any .freq attr
|
||
|
data = data._with_freq(None)
|
||
|
super().test_series_constructor(data)
|
||
|
|
||
|
|
||
|
class TestGetitem(BaseDatetimeTests, base.BaseGetitemTests):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class TestIndex(base.BaseIndexTests):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class TestMethods(BaseDatetimeTests, base.BaseMethodsTests):
|
||
|
@pytest.mark.parametrize("na_action", [None, "ignore"])
|
||
|
def test_map(self, data, na_action):
|
||
|
result = data.map(lambda x: x, na_action=na_action)
|
||
|
tm.assert_extension_array_equal(result, data)
|
||
|
|
||
|
|
||
|
class TestInterface(BaseDatetimeTests, base.BaseInterfaceTests):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class TestArithmeticOps(BaseDatetimeTests, base.BaseArithmeticOpsTests):
|
||
|
implements = {"__sub__", "__rsub__"}
|
||
|
|
||
|
def _get_expected_exception(self, op_name, obj, other):
|
||
|
if op_name in self.implements:
|
||
|
return None
|
||
|
return super()._get_expected_exception(op_name, obj, other)
|
||
|
|
||
|
|
||
|
class TestCasting(BaseDatetimeTests, base.BaseCastingTests):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class TestComparisonOps(BaseDatetimeTests, base.BaseComparisonOpsTests):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class TestMissing(BaseDatetimeTests, base.BaseMissingTests):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class TestReshaping(BaseDatetimeTests, base.BaseReshapingTests):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class TestSetitem(BaseDatetimeTests, base.BaseSetitemTests):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class TestGroupby(BaseDatetimeTests, base.BaseGroupbyTests):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class TestPrinting(BaseDatetimeTests, base.BasePrintingTests):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class Test2DCompat(BaseDatetimeTests, base.NDArrayBacked2DTests):
|
||
|
pass
|