test_period_asfreq.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import numpy as np
  2. import pytest
  3. from pandas._libs.tslibs import (
  4. iNaT,
  5. to_offset,
  6. )
  7. from pandas._libs.tslibs.period import (
  8. extract_ordinals,
  9. period_asfreq,
  10. period_ordinal,
  11. )
  12. import pandas._testing as tm
  13. def get_freq_code(freqstr: str) -> int:
  14. off = to_offset(freqstr)
  15. # error: "BaseOffset" has no attribute "_period_dtype_code"
  16. code = off._period_dtype_code # type: ignore[attr-defined]
  17. return code
  18. @pytest.mark.parametrize(
  19. "freq1,freq2,expected",
  20. [
  21. ("D", "H", 24),
  22. ("D", "T", 1440),
  23. ("D", "S", 86400),
  24. ("D", "L", 86400000),
  25. ("D", "U", 86400000000),
  26. ("D", "N", 86400000000000),
  27. ("H", "T", 60),
  28. ("H", "S", 3600),
  29. ("H", "L", 3600000),
  30. ("H", "U", 3600000000),
  31. ("H", "N", 3600000000000),
  32. ("T", "S", 60),
  33. ("T", "L", 60000),
  34. ("T", "U", 60000000),
  35. ("T", "N", 60000000000),
  36. ("S", "L", 1000),
  37. ("S", "U", 1000000),
  38. ("S", "N", 1000000000),
  39. ("L", "U", 1000),
  40. ("L", "N", 1000000),
  41. ("U", "N", 1000),
  42. ],
  43. )
  44. def test_intra_day_conversion_factors(freq1, freq2, expected):
  45. assert (
  46. period_asfreq(1, get_freq_code(freq1), get_freq_code(freq2), False) == expected
  47. )
  48. @pytest.mark.parametrize(
  49. "freq,expected", [("A", 0), ("M", 0), ("W", 1), ("D", 0), ("B", 0)]
  50. )
  51. def test_period_ordinal_start_values(freq, expected):
  52. # information for Jan. 1, 1970.
  53. assert period_ordinal(1970, 1, 1, 0, 0, 0, 0, 0, get_freq_code(freq)) == expected
  54. @pytest.mark.parametrize(
  55. "dt,expected",
  56. [
  57. ((1970, 1, 4, 0, 0, 0, 0, 0), 1),
  58. ((1970, 1, 5, 0, 0, 0, 0, 0), 2),
  59. ((2013, 10, 6, 0, 0, 0, 0, 0), 2284),
  60. ((2013, 10, 7, 0, 0, 0, 0, 0), 2285),
  61. ],
  62. )
  63. def test_period_ordinal_week(dt, expected):
  64. args = dt + (get_freq_code("W"),)
  65. assert period_ordinal(*args) == expected
  66. @pytest.mark.parametrize(
  67. "day,expected",
  68. [
  69. # Thursday (Oct. 3, 2013).
  70. (3, 11415),
  71. # Friday (Oct. 4, 2013).
  72. (4, 11416),
  73. # Saturday (Oct. 5, 2013).
  74. (5, 11417),
  75. # Sunday (Oct. 6, 2013).
  76. (6, 11417),
  77. # Monday (Oct. 7, 2013).
  78. (7, 11417),
  79. # Tuesday (Oct. 8, 2013).
  80. (8, 11418),
  81. ],
  82. )
  83. def test_period_ordinal_business_day(day, expected):
  84. # 5000 is PeriodDtypeCode for BusinessDay
  85. args = (2013, 10, day, 0, 0, 0, 0, 0, 5000)
  86. assert period_ordinal(*args) == expected
  87. class TestExtractOrdinals:
  88. def test_extract_ordinals_raises(self):
  89. # with non-object, make sure we raise TypeError, not segfault
  90. arr = np.arange(5)
  91. freq = to_offset("D")
  92. with pytest.raises(TypeError, match="values must be object-dtype"):
  93. extract_ordinals(arr, freq)
  94. def test_extract_ordinals_2d(self):
  95. freq = to_offset("D")
  96. arr = np.empty(10, dtype=object)
  97. arr[:] = iNaT
  98. res = extract_ordinals(arr, freq)
  99. res2 = extract_ordinals(arr.reshape(5, 2), freq)
  100. tm.assert_numpy_array_equal(res, res2.reshape(-1))