test_ops.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from datetime import datetime
  2. from dateutil.tz import tzlocal
  3. import pytest
  4. from pandas.compat import IS64
  5. from pandas import (
  6. DatetimeIndex,
  7. Index,
  8. bdate_range,
  9. date_range,
  10. )
  11. import pandas._testing as tm
  12. START, END = datetime(2009, 1, 1), datetime(2010, 1, 1)
  13. class TestDatetimeIndexOps:
  14. @pytest.mark.parametrize(
  15. "freq,expected",
  16. [
  17. ("A", "day"),
  18. ("Q", "day"),
  19. ("M", "day"),
  20. ("D", "day"),
  21. ("H", "hour"),
  22. ("T", "minute"),
  23. ("S", "second"),
  24. ("L", "millisecond"),
  25. ("U", "microsecond"),
  26. ],
  27. )
  28. def test_resolution(self, request, tz_naive_fixture, freq, expected):
  29. tz = tz_naive_fixture
  30. if freq == "A" and not IS64 and isinstance(tz, tzlocal):
  31. request.node.add_marker(
  32. pytest.mark.xfail(reason="OverflowError inside tzlocal past 2038")
  33. )
  34. idx = date_range(start="2013-04-01", periods=30, freq=freq, tz=tz)
  35. assert idx.resolution == expected
  36. def test_infer_freq(self, freq_sample):
  37. # GH 11018
  38. idx = date_range("2011-01-01 09:00:00", freq=freq_sample, periods=10)
  39. result = DatetimeIndex(idx.asi8, freq="infer")
  40. tm.assert_index_equal(idx, result)
  41. assert result.freq == freq_sample
  42. @pytest.mark.parametrize("freq", ["B", "C"])
  43. class TestBusinessDatetimeIndex:
  44. @pytest.fixture
  45. def rng(self, freq):
  46. return bdate_range(START, END, freq=freq)
  47. def test_comparison(self, rng):
  48. d = rng[10]
  49. comp = rng > d
  50. assert comp[11]
  51. assert not comp[9]
  52. def test_copy(self, rng):
  53. cp = rng.copy()
  54. repr(cp)
  55. tm.assert_index_equal(cp, rng)
  56. def test_identical(self, rng):
  57. t1 = rng.copy()
  58. t2 = rng.copy()
  59. assert t1.identical(t2)
  60. # name
  61. t1 = t1.rename("foo")
  62. assert t1.equals(t2)
  63. assert not t1.identical(t2)
  64. t2 = t2.rename("foo")
  65. assert t1.identical(t2)
  66. # freq
  67. t2v = Index(t2.values)
  68. assert t1.equals(t2v)
  69. assert not t1.identical(t2v)