test_tools.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import numpy as np
  2. import pytest
  3. from pandas import (
  4. Period,
  5. PeriodIndex,
  6. period_range,
  7. )
  8. import pandas._testing as tm
  9. class TestPeriodRepresentation:
  10. """
  11. Wish to match NumPy units
  12. """
  13. @pytest.mark.parametrize(
  14. "freq, base_date",
  15. [
  16. ("W-THU", "1970-01-01"),
  17. ("D", "1970-01-01"),
  18. ("B", "1970-01-01"),
  19. ("H", "1970-01-01"),
  20. ("T", "1970-01-01"),
  21. ("S", "1970-01-01"),
  22. ("L", "1970-01-01"),
  23. ("U", "1970-01-01"),
  24. ("N", "1970-01-01"),
  25. ("M", "1970-01"),
  26. ("A", 1970),
  27. ],
  28. )
  29. def test_freq(self, freq, base_date):
  30. rng = period_range(start=base_date, periods=10, freq=freq)
  31. exp = np.arange(10, dtype=np.int64)
  32. tm.assert_numpy_array_equal(rng.asi8, exp)
  33. class TestPeriodIndexConversion:
  34. def test_tolist(self):
  35. index = period_range(freq="A", start="1/1/2001", end="12/1/2009")
  36. rs = index.tolist()
  37. for x in rs:
  38. assert isinstance(x, Period)
  39. recon = PeriodIndex(rs)
  40. tm.assert_index_equal(index, recon)