test_is_monotonic.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from pandas import (
  2. Index,
  3. NaT,
  4. date_range,
  5. )
  6. def test_is_monotonic_with_nat():
  7. # GH#31437
  8. # PeriodIndex.is_monotonic_increasing should behave analogously to DatetimeIndex,
  9. # in particular never be monotonic when we have NaT
  10. dti = date_range("2016-01-01", periods=3)
  11. pi = dti.to_period("D")
  12. tdi = Index(dti.view("timedelta64[ns]"))
  13. for obj in [pi, pi._engine, dti, dti._engine, tdi, tdi._engine]:
  14. if isinstance(obj, Index):
  15. # i.e. not Engines
  16. assert obj.is_monotonic_increasing
  17. assert obj.is_monotonic_increasing
  18. assert not obj.is_monotonic_decreasing
  19. assert obj.is_unique
  20. dti1 = dti.insert(0, NaT)
  21. pi1 = dti1.to_period("D")
  22. tdi1 = Index(dti1.view("timedelta64[ns]"))
  23. for obj in [pi1, pi1._engine, dti1, dti1._engine, tdi1, tdi1._engine]:
  24. if isinstance(obj, Index):
  25. # i.e. not Engines
  26. assert not obj.is_monotonic_increasing
  27. assert not obj.is_monotonic_increasing
  28. assert not obj.is_monotonic_decreasing
  29. assert obj.is_unique
  30. dti2 = dti.insert(3, NaT)
  31. pi2 = dti2.to_period("H")
  32. tdi2 = Index(dti2.view("timedelta64[ns]"))
  33. for obj in [pi2, pi2._engine, dti2, dti2._engine, tdi2, tdi2._engine]:
  34. if isinstance(obj, Index):
  35. # i.e. not Engines
  36. assert not obj.is_monotonic_increasing
  37. assert not obj.is_monotonic_increasing
  38. assert not obj.is_monotonic_decreasing
  39. assert obj.is_unique