test_to_offset.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import re
  2. import pytest
  3. from pandas._libs.tslibs import (
  4. Timedelta,
  5. offsets,
  6. to_offset,
  7. )
  8. @pytest.mark.parametrize(
  9. "freq_input,expected",
  10. [
  11. (to_offset("10us"), offsets.Micro(10)),
  12. (offsets.Hour(), offsets.Hour()),
  13. ("2h30min", offsets.Minute(150)),
  14. ("2h 30min", offsets.Minute(150)),
  15. ("2h30min15s", offsets.Second(150 * 60 + 15)),
  16. ("2h 60min", offsets.Hour(3)),
  17. ("2h 20.5min", offsets.Second(8430)),
  18. ("1.5min", offsets.Second(90)),
  19. ("0.5S", offsets.Milli(500)),
  20. ("15l500u", offsets.Micro(15500)),
  21. ("10s75L", offsets.Milli(10075)),
  22. ("1s0.25ms", offsets.Micro(1000250)),
  23. ("1s0.25L", offsets.Micro(1000250)),
  24. ("2800N", offsets.Nano(2800)),
  25. ("2SM", offsets.SemiMonthEnd(2)),
  26. ("2SM-16", offsets.SemiMonthEnd(2, day_of_month=16)),
  27. ("2SMS-14", offsets.SemiMonthBegin(2, day_of_month=14)),
  28. ("2SMS-15", offsets.SemiMonthBegin(2)),
  29. ],
  30. )
  31. def test_to_offset(freq_input, expected):
  32. result = to_offset(freq_input)
  33. assert result == expected
  34. @pytest.mark.parametrize(
  35. "freqstr,expected", [("-1S", -1), ("-2SM", -2), ("-1SMS", -1), ("-5min10s", -310)]
  36. )
  37. def test_to_offset_negative(freqstr, expected):
  38. result = to_offset(freqstr)
  39. assert result.n == expected
  40. @pytest.mark.parametrize(
  41. "freqstr",
  42. [
  43. "2h20m",
  44. "U1",
  45. "-U",
  46. "3U1",
  47. "-2-3U",
  48. "-2D:3H",
  49. "1.5.0S",
  50. "2SMS-15-15",
  51. "2SMS-15D",
  52. "100foo",
  53. # Invalid leading +/- signs.
  54. "+-1d",
  55. "-+1h",
  56. "+1",
  57. "-7",
  58. "+d",
  59. "-m",
  60. # Invalid shortcut anchors.
  61. "SM-0",
  62. "SM-28",
  63. "SM-29",
  64. "SM-FOO",
  65. "BSM",
  66. "SM--1",
  67. "SMS-1",
  68. "SMS-28",
  69. "SMS-30",
  70. "SMS-BAR",
  71. "SMS-BYR",
  72. "BSMS",
  73. "SMS--2",
  74. ],
  75. )
  76. def test_to_offset_invalid(freqstr):
  77. # see gh-13930
  78. # We escape string because some of our
  79. # inputs contain regex special characters.
  80. msg = re.escape(f"Invalid frequency: {freqstr}")
  81. with pytest.raises(ValueError, match=msg):
  82. to_offset(freqstr)
  83. def test_to_offset_no_evaluate():
  84. msg = str(("", ""))
  85. with pytest.raises(TypeError, match=msg):
  86. to_offset(("", ""))
  87. def test_to_offset_tuple_unsupported():
  88. with pytest.raises(TypeError, match="pass as a string instead"):
  89. to_offset((5, "T"))
  90. @pytest.mark.parametrize(
  91. "freqstr,expected",
  92. [
  93. ("2D 3H", offsets.Hour(51)),
  94. ("2 D3 H", offsets.Hour(51)),
  95. ("2 D 3 H", offsets.Hour(51)),
  96. (" 2 D 3 H ", offsets.Hour(51)),
  97. (" H ", offsets.Hour()),
  98. (" 3 H ", offsets.Hour(3)),
  99. ],
  100. )
  101. def test_to_offset_whitespace(freqstr, expected):
  102. result = to_offset(freqstr)
  103. assert result == expected
  104. @pytest.mark.parametrize(
  105. "freqstr,expected", [("00H 00T 01S", 1), ("-00H 03T 14S", -194)]
  106. )
  107. def test_to_offset_leading_zero(freqstr, expected):
  108. result = to_offset(freqstr)
  109. assert result.n == expected
  110. @pytest.mark.parametrize("freqstr,expected", [("+1d", 1), ("+2h30min", 150)])
  111. def test_to_offset_leading_plus(freqstr, expected):
  112. result = to_offset(freqstr)
  113. assert result.n == expected
  114. @pytest.mark.parametrize(
  115. "kwargs,expected",
  116. [
  117. ({"days": 1, "seconds": 1}, offsets.Second(86401)),
  118. ({"days": -1, "seconds": 1}, offsets.Second(-86399)),
  119. ({"hours": 1, "minutes": 10}, offsets.Minute(70)),
  120. ({"hours": 1, "minutes": -10}, offsets.Minute(50)),
  121. ({"weeks": 1}, offsets.Day(7)),
  122. ({"hours": 1}, offsets.Hour(1)),
  123. ({"hours": 1}, to_offset("60min")),
  124. ({"microseconds": 1}, offsets.Micro(1)),
  125. ({"microseconds": 0}, offsets.Nano(0)),
  126. ],
  127. )
  128. def test_to_offset_pd_timedelta(kwargs, expected):
  129. # see gh-9064
  130. td = Timedelta(**kwargs)
  131. result = to_offset(td)
  132. assert result == expected
  133. @pytest.mark.parametrize(
  134. "shortcut,expected",
  135. [
  136. ("W", offsets.Week(weekday=6)),
  137. ("W-SUN", offsets.Week(weekday=6)),
  138. ("Q", offsets.QuarterEnd(startingMonth=12)),
  139. ("Q-DEC", offsets.QuarterEnd(startingMonth=12)),
  140. ("Q-MAY", offsets.QuarterEnd(startingMonth=5)),
  141. ("SM", offsets.SemiMonthEnd(day_of_month=15)),
  142. ("SM-15", offsets.SemiMonthEnd(day_of_month=15)),
  143. ("SM-1", offsets.SemiMonthEnd(day_of_month=1)),
  144. ("SM-27", offsets.SemiMonthEnd(day_of_month=27)),
  145. ("SMS-2", offsets.SemiMonthBegin(day_of_month=2)),
  146. ("SMS-27", offsets.SemiMonthBegin(day_of_month=27)),
  147. ],
  148. )
  149. def test_anchored_shortcuts(shortcut, expected):
  150. result = to_offset(shortcut)
  151. assert result == expected