test_timedelta.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. from datetime import timedelta
  2. import numpy as np
  3. import pytest
  4. import pandas as pd
  5. from pandas import (
  6. DataFrame,
  7. Index,
  8. Int64Index,
  9. Series,
  10. Timedelta,
  11. TimedeltaIndex,
  12. date_range,
  13. timedelta_range,
  14. )
  15. import pandas._testing as tm
  16. from ..datetimelike import DatetimeLike
  17. randn = np.random.randn
  18. class TestTimedeltaIndex(DatetimeLike):
  19. _holder = TimedeltaIndex
  20. @pytest.fixture
  21. def indices(self):
  22. return tm.makeTimedeltaIndex(10)
  23. def create_index(self):
  24. return pd.to_timedelta(range(5), unit="d") + pd.offsets.Hour(1)
  25. def test_numeric_compat(self):
  26. # Dummy method to override super's version; this test is now done
  27. # in test_arithmetic.py
  28. pass
  29. def test_shift(self):
  30. pass # this is handled in test_arithmetic.py
  31. def test_pickle_compat_construction(self):
  32. pass
  33. def test_fillna_timedelta(self):
  34. # GH 11343
  35. idx = pd.TimedeltaIndex(["1 day", pd.NaT, "3 day"])
  36. exp = pd.TimedeltaIndex(["1 day", "2 day", "3 day"])
  37. tm.assert_index_equal(idx.fillna(pd.Timedelta("2 day")), exp)
  38. exp = pd.TimedeltaIndex(["1 day", "3 hour", "3 day"])
  39. idx.fillna(pd.Timedelta("3 hour"))
  40. exp = pd.Index(
  41. [pd.Timedelta("1 day"), "x", pd.Timedelta("3 day")], dtype=object
  42. )
  43. tm.assert_index_equal(idx.fillna("x"), exp)
  44. def test_isin(self):
  45. index = tm.makeTimedeltaIndex(4)
  46. result = index.isin(index)
  47. assert result.all()
  48. result = index.isin(list(index))
  49. assert result.all()
  50. tm.assert_almost_equal(
  51. index.isin([index[2], 5]), np.array([False, False, True, False])
  52. )
  53. def test_factorize(self):
  54. idx1 = TimedeltaIndex(["1 day", "1 day", "2 day", "2 day", "3 day", "3 day"])
  55. exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp)
  56. exp_idx = TimedeltaIndex(["1 day", "2 day", "3 day"])
  57. arr, idx = idx1.factorize()
  58. tm.assert_numpy_array_equal(arr, exp_arr)
  59. tm.assert_index_equal(idx, exp_idx)
  60. arr, idx = idx1.factorize(sort=True)
  61. tm.assert_numpy_array_equal(arr, exp_arr)
  62. tm.assert_index_equal(idx, exp_idx)
  63. # freq must be preserved
  64. idx3 = timedelta_range("1 day", periods=4, freq="s")
  65. exp_arr = np.array([0, 1, 2, 3], dtype=np.intp)
  66. arr, idx = idx3.factorize()
  67. tm.assert_numpy_array_equal(arr, exp_arr)
  68. tm.assert_index_equal(idx, idx3)
  69. def test_join_self(self, join_type):
  70. index = timedelta_range("1 day", periods=10)
  71. joined = index.join(index, how=join_type)
  72. tm.assert_index_equal(index, joined)
  73. def test_does_not_convert_mixed_integer(self):
  74. df = tm.makeCustomDataframe(
  75. 10,
  76. 10,
  77. data_gen_f=lambda *args, **kwargs: randn(),
  78. r_idx_type="i",
  79. c_idx_type="td",
  80. )
  81. str(df)
  82. cols = df.columns.join(df.index, how="outer")
  83. joined = cols.join(df.columns)
  84. assert cols.dtype == np.dtype("O")
  85. assert cols.dtype == joined.dtype
  86. tm.assert_index_equal(cols, joined)
  87. def test_sort_values(self):
  88. idx = TimedeltaIndex(["4d", "1d", "2d"])
  89. ordered = idx.sort_values()
  90. assert ordered.is_monotonic
  91. ordered = idx.sort_values(ascending=False)
  92. assert ordered[::-1].is_monotonic
  93. ordered, dexer = idx.sort_values(return_indexer=True)
  94. assert ordered.is_monotonic
  95. tm.assert_numpy_array_equal(dexer, np.array([1, 2, 0]), check_dtype=False)
  96. ordered, dexer = idx.sort_values(return_indexer=True, ascending=False)
  97. assert ordered[::-1].is_monotonic
  98. tm.assert_numpy_array_equal(dexer, np.array([0, 2, 1]), check_dtype=False)
  99. def test_argmin_argmax(self):
  100. idx = TimedeltaIndex(["1 day 00:00:05", "1 day 00:00:01", "1 day 00:00:02"])
  101. assert idx.argmin() == 1
  102. assert idx.argmax() == 0
  103. def test_misc_coverage(self):
  104. rng = timedelta_range("1 day", periods=5)
  105. result = rng.groupby(rng.days)
  106. assert isinstance(list(result.values())[0][0], Timedelta)
  107. idx = TimedeltaIndex(["3d", "1d", "2d"])
  108. assert not idx.equals(list(idx))
  109. non_td = Index(list("abc"))
  110. assert not idx.equals(list(non_td))
  111. def test_map(self):
  112. # test_map_dictlike generally tests
  113. rng = timedelta_range("1 day", periods=10)
  114. f = lambda x: x.days
  115. result = rng.map(f)
  116. exp = Int64Index([f(x) for x in rng])
  117. tm.assert_index_equal(result, exp)
  118. def test_pass_TimedeltaIndex_to_index(self):
  119. rng = timedelta_range("1 days", "10 days")
  120. idx = Index(rng, dtype=object)
  121. expected = Index(rng.to_pytimedelta(), dtype=object)
  122. tm.assert_numpy_array_equal(idx.values, expected.values)
  123. def test_pickle(self):
  124. rng = timedelta_range("1 days", periods=10)
  125. rng_p = tm.round_trip_pickle(rng)
  126. tm.assert_index_equal(rng, rng_p)
  127. def test_hash_error(self):
  128. index = timedelta_range("1 days", periods=10)
  129. with pytest.raises(
  130. TypeError, match=(f"unhashable type: {repr(type(index).__name__)}")
  131. ):
  132. hash(index)
  133. def test_append_join_nondatetimeindex(self):
  134. rng = timedelta_range("1 days", periods=10)
  135. idx = Index(["a", "b", "c", "d"])
  136. result = rng.append(idx)
  137. assert isinstance(result[0], Timedelta)
  138. # it works
  139. rng.join(idx, how="outer")
  140. def test_append_numpy_bug_1681(self):
  141. td = timedelta_range("1 days", "10 days", freq="2D")
  142. a = DataFrame()
  143. c = DataFrame({"A": "foo", "B": td}, index=td)
  144. str(c)
  145. result = a.append(c)
  146. assert (result["B"] == td).all()
  147. def test_delete_doesnt_infer_freq(self):
  148. # GH#30655 behavior matches DatetimeIndex
  149. tdi = pd.TimedeltaIndex(["1 Day", "2 Days", None, "3 Days", "4 Days"])
  150. result = tdi.delete(2)
  151. assert result.freq is None
  152. def test_fields(self):
  153. rng = timedelta_range("1 days, 10:11:12.100123456", periods=2, freq="s")
  154. tm.assert_index_equal(rng.days, Index([1, 1], dtype="int64"))
  155. tm.assert_index_equal(
  156. rng.seconds,
  157. Index([10 * 3600 + 11 * 60 + 12, 10 * 3600 + 11 * 60 + 13], dtype="int64"),
  158. )
  159. tm.assert_index_equal(
  160. rng.microseconds, Index([100 * 1000 + 123, 100 * 1000 + 123], dtype="int64")
  161. )
  162. tm.assert_index_equal(rng.nanoseconds, Index([456, 456], dtype="int64"))
  163. msg = "'TimedeltaIndex' object has no attribute '{}'"
  164. with pytest.raises(AttributeError, match=msg.format("hours")):
  165. rng.hours
  166. with pytest.raises(AttributeError, match=msg.format("minutes")):
  167. rng.minutes
  168. with pytest.raises(AttributeError, match=msg.format("milliseconds")):
  169. rng.milliseconds
  170. # with nat
  171. s = Series(rng)
  172. s[1] = np.nan
  173. tm.assert_series_equal(s.dt.days, Series([1, np.nan], index=[0, 1]))
  174. tm.assert_series_equal(
  175. s.dt.seconds, Series([10 * 3600 + 11 * 60 + 12, np.nan], index=[0, 1])
  176. )
  177. # preserve name (GH15589)
  178. rng.name = "name"
  179. assert rng.days.name == "name"
  180. def test_freq_conversion(self):
  181. # doc example
  182. # series
  183. td = Series(date_range("20130101", periods=4)) - Series(
  184. date_range("20121201", periods=4)
  185. )
  186. td[2] += timedelta(minutes=5, seconds=3)
  187. td[3] = np.nan
  188. result = td / np.timedelta64(1, "D")
  189. expected = Series([31, 31, (31 * 86400 + 5 * 60 + 3) / 86400.0, np.nan])
  190. tm.assert_series_equal(result, expected)
  191. result = td.astype("timedelta64[D]")
  192. expected = Series([31, 31, 31, np.nan])
  193. tm.assert_series_equal(result, expected)
  194. result = td / np.timedelta64(1, "s")
  195. expected = Series([31 * 86400, 31 * 86400, 31 * 86400 + 5 * 60 + 3, np.nan])
  196. tm.assert_series_equal(result, expected)
  197. result = td.astype("timedelta64[s]")
  198. tm.assert_series_equal(result, expected)
  199. # tdi
  200. td = TimedeltaIndex(td)
  201. result = td / np.timedelta64(1, "D")
  202. expected = Index([31, 31, (31 * 86400 + 5 * 60 + 3) / 86400.0, np.nan])
  203. tm.assert_index_equal(result, expected)
  204. result = td.astype("timedelta64[D]")
  205. expected = Index([31, 31, 31, np.nan])
  206. tm.assert_index_equal(result, expected)
  207. result = td / np.timedelta64(1, "s")
  208. expected = Index([31 * 86400, 31 * 86400, 31 * 86400 + 5 * 60 + 3, np.nan])
  209. tm.assert_index_equal(result, expected)
  210. result = td.astype("timedelta64[s]")
  211. tm.assert_index_equal(result, expected)
  212. @pytest.mark.parametrize("unit", ["Y", "y", "M"])
  213. def test_unit_m_y_raises(self, unit):
  214. msg = "Units 'M' and 'Y' are no longer supported"
  215. with pytest.raises(ValueError, match=msg):
  216. TimedeltaIndex([1, 3, 7], unit)
  217. class TestTimeSeries:
  218. def test_series_box_timedelta(self):
  219. rng = timedelta_range("1 day 1 s", periods=5, freq="h")
  220. s = Series(rng)
  221. assert isinstance(s[1], Timedelta)
  222. assert isinstance(s.iat[2], Timedelta)