test_scalar_compat.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """
  2. Tests for TimedeltaIndex methods behaving like their Timedelta counterparts
  3. """
  4. import numpy as np
  5. import pytest
  6. from pandas._libs.tslibs.offsets import INVALID_FREQ_ERR_MSG
  7. from pandas import (
  8. Index,
  9. Series,
  10. Timedelta,
  11. TimedeltaIndex,
  12. timedelta_range,
  13. )
  14. import pandas._testing as tm
  15. class TestVectorizedTimedelta:
  16. def test_tdi_total_seconds(self):
  17. # GH#10939
  18. # test index
  19. rng = timedelta_range("1 days, 10:11:12.100123456", periods=2, freq="s")
  20. expt = [
  21. 1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456.0 / 1e9,
  22. 1 * 86400 + 10 * 3600 + 11 * 60 + 13 + 100123456.0 / 1e9,
  23. ]
  24. tm.assert_almost_equal(rng.total_seconds(), Index(expt))
  25. # test Series
  26. ser = Series(rng)
  27. s_expt = Series(expt, index=[0, 1])
  28. tm.assert_series_equal(ser.dt.total_seconds(), s_expt)
  29. # with nat
  30. ser[1] = np.nan
  31. s_expt = Series(
  32. [1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456.0 / 1e9, np.nan],
  33. index=[0, 1],
  34. )
  35. tm.assert_series_equal(ser.dt.total_seconds(), s_expt)
  36. def test_tdi_total_seconds_all_nat(self):
  37. # with both nat
  38. ser = Series([np.nan, np.nan], dtype="timedelta64[ns]")
  39. result = ser.dt.total_seconds()
  40. expected = Series([np.nan, np.nan])
  41. tm.assert_series_equal(result, expected)
  42. def test_tdi_round(self):
  43. td = timedelta_range(start="16801 days", periods=5, freq="30Min")
  44. elt = td[1]
  45. expected_rng = TimedeltaIndex(
  46. [
  47. Timedelta("16801 days 00:00:00"),
  48. Timedelta("16801 days 00:00:00"),
  49. Timedelta("16801 days 01:00:00"),
  50. Timedelta("16801 days 02:00:00"),
  51. Timedelta("16801 days 02:00:00"),
  52. ]
  53. )
  54. expected_elt = expected_rng[1]
  55. tm.assert_index_equal(td.round(freq="H"), expected_rng)
  56. assert elt.round(freq="H") == expected_elt
  57. msg = INVALID_FREQ_ERR_MSG
  58. with pytest.raises(ValueError, match=msg):
  59. td.round(freq="foo")
  60. with pytest.raises(ValueError, match=msg):
  61. elt.round(freq="foo")
  62. msg = "<MonthEnd> is a non-fixed frequency"
  63. with pytest.raises(ValueError, match=msg):
  64. td.round(freq="M")
  65. with pytest.raises(ValueError, match=msg):
  66. elt.round(freq="M")
  67. @pytest.mark.parametrize(
  68. "freq,msg",
  69. [
  70. ("Y", "<YearEnd: month=12> is a non-fixed frequency"),
  71. ("M", "<MonthEnd> is a non-fixed frequency"),
  72. ("foobar", "Invalid frequency: foobar"),
  73. ],
  74. )
  75. def test_tdi_round_invalid(self, freq, msg):
  76. t1 = timedelta_range("1 days", periods=3, freq="1 min 2 s 3 us")
  77. with pytest.raises(ValueError, match=msg):
  78. t1.round(freq)
  79. with pytest.raises(ValueError, match=msg):
  80. # Same test for TimedeltaArray
  81. t1._data.round(freq)
  82. # TODO: de-duplicate with test_tdi_round
  83. def test_round(self):
  84. t1 = timedelta_range("1 days", periods=3, freq="1 min 2 s 3 us")
  85. t2 = -1 * t1
  86. t1a = timedelta_range("1 days", periods=3, freq="1 min 2 s")
  87. t1c = TimedeltaIndex([1, 1, 1], unit="D")
  88. # note that negative times round DOWN! so don't give whole numbers
  89. for freq, s1, s2 in [
  90. ("N", t1, t2),
  91. ("U", t1, t2),
  92. (
  93. "L",
  94. t1a,
  95. TimedeltaIndex(
  96. ["-1 days +00:00:00", "-2 days +23:58:58", "-2 days +23:57:56"]
  97. ),
  98. ),
  99. (
  100. "S",
  101. t1a,
  102. TimedeltaIndex(
  103. ["-1 days +00:00:00", "-2 days +23:58:58", "-2 days +23:57:56"]
  104. ),
  105. ),
  106. ("12T", t1c, TimedeltaIndex(["-1 days", "-1 days", "-1 days"])),
  107. ("H", t1c, TimedeltaIndex(["-1 days", "-1 days", "-1 days"])),
  108. ("d", t1c, TimedeltaIndex([-1, -1, -1], unit="D")),
  109. ]:
  110. r1 = t1.round(freq)
  111. tm.assert_index_equal(r1, s1)
  112. r2 = t2.round(freq)
  113. tm.assert_index_equal(r2, s2)
  114. def test_components(self):
  115. rng = timedelta_range("1 days, 10:11:12", periods=2, freq="s")
  116. rng.components
  117. # with nat
  118. s = Series(rng)
  119. s[1] = np.nan
  120. result = s.dt.components
  121. assert not result.iloc[0].isna().all()
  122. assert result.iloc[1].isna().all()