test_nat.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import numpy as np
  2. import pytest
  3. from pandas import (
  4. DatetimeIndex,
  5. NaT,
  6. PeriodIndex,
  7. TimedeltaIndex,
  8. )
  9. import pandas._testing as tm
  10. class NATests:
  11. def test_nat(self, index_without_na):
  12. empty_index = index_without_na[:0]
  13. index_with_na = index_without_na.copy(deep=True)
  14. index_with_na._data[1] = NaT
  15. assert empty_index._na_value is NaT
  16. assert index_with_na._na_value is NaT
  17. assert index_without_na._na_value is NaT
  18. idx = index_without_na
  19. assert idx._can_hold_na
  20. tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
  21. assert idx.hasnans is False
  22. idx = index_with_na
  23. assert idx._can_hold_na
  24. tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
  25. assert idx.hasnans is True
  26. class TestDatetimeIndexNA(NATests):
  27. @pytest.fixture
  28. def index_without_na(self, tz_naive_fixture):
  29. tz = tz_naive_fixture
  30. return DatetimeIndex(["2011-01-01", "2011-01-02"], tz=tz)
  31. class TestTimedeltaIndexNA(NATests):
  32. @pytest.fixture
  33. def index_without_na(self):
  34. return TimedeltaIndex(["1 days", "2 days"])
  35. class TestPeriodIndexNA(NATests):
  36. @pytest.fixture
  37. def index_without_na(self):
  38. return PeriodIndex(["2011-01-01", "2011-01-02"], freq="D")