test_timedelta.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. """ test the scalar Timedelta """
  2. from datetime import timedelta
  3. import numpy as np
  4. import pytest
  5. from pandas._libs.tslibs import NaT, iNaT
  6. import pandas.compat as compat
  7. import pandas as pd
  8. from pandas import (
  9. Series, Timedelta, TimedeltaIndex, timedelta_range, to_timedelta)
  10. import pandas.util.testing as tm
  11. class TestTimedeltaArithmetic(object):
  12. def test_arithmetic_overflow(self):
  13. with pytest.raises(OverflowError):
  14. pd.Timestamp('1700-01-01') + pd.Timedelta(13 * 19999, unit='D')
  15. with pytest.raises(OverflowError):
  16. pd.Timestamp('1700-01-01') + timedelta(days=13 * 19999)
  17. def test_array_timedelta_floordiv(self):
  18. # https://github.com/pandas-dev/pandas/issues/19761
  19. ints = pd.date_range('2012-10-08', periods=4, freq='D').view('i8')
  20. msg = r"Use 'array // timedelta.value'"
  21. with tm.assert_produces_warning(FutureWarning) as m:
  22. result = ints // pd.Timedelta(1, unit='s')
  23. assert msg in str(m[0].message)
  24. expected = np.array([1349654400, 1349740800, 1349827200, 1349913600],
  25. dtype='i8')
  26. tm.assert_numpy_array_equal(result, expected)
  27. def test_ops_error_str(self):
  28. # GH 13624
  29. td = Timedelta('1 day')
  30. for left, right in [(td, 'a'), ('a', td)]:
  31. with pytest.raises(TypeError):
  32. left + right
  33. with pytest.raises(TypeError):
  34. left > right
  35. assert not left == right
  36. assert left != right
  37. def test_ops_notimplemented(self):
  38. class Other(object):
  39. pass
  40. other = Other()
  41. td = Timedelta('1 day')
  42. assert td.__add__(other) is NotImplemented
  43. assert td.__sub__(other) is NotImplemented
  44. assert td.__truediv__(other) is NotImplemented
  45. assert td.__mul__(other) is NotImplemented
  46. assert td.__floordiv__(other) is NotImplemented
  47. def test_unary_ops(self):
  48. td = Timedelta(10, unit='d')
  49. # __neg__, __pos__
  50. assert -td == Timedelta(-10, unit='d')
  51. assert -td == Timedelta('-10d')
  52. assert +td == Timedelta(10, unit='d')
  53. # __abs__, __abs__(__neg__)
  54. assert abs(td) == td
  55. assert abs(-td) == td
  56. assert abs(-td) == Timedelta('10d')
  57. class TestTimedeltaComparison(object):
  58. def test_compare_tick(self, tick_classes):
  59. cls = tick_classes
  60. off = cls(4)
  61. td = off.delta
  62. assert isinstance(td, Timedelta)
  63. assert td == off
  64. assert not td != off
  65. assert td <= off
  66. assert td >= off
  67. assert not td < off
  68. assert not td > off
  69. assert not td == 2 * off
  70. assert td != 2 * off
  71. assert td <= 2 * off
  72. assert td < 2 * off
  73. assert not td >= 2 * off
  74. assert not td > 2 * off
  75. def test_comparison_object_array(self):
  76. # analogous to GH#15183
  77. td = Timedelta('2 days')
  78. other = Timedelta('3 hours')
  79. arr = np.array([other, td], dtype=object)
  80. res = arr == td
  81. expected = np.array([False, True], dtype=bool)
  82. assert (res == expected).all()
  83. # 2D case
  84. arr = np.array([[other, td],
  85. [td, other]],
  86. dtype=object)
  87. res = arr != td
  88. expected = np.array([[True, False], [False, True]], dtype=bool)
  89. assert res.shape == expected.shape
  90. assert (res == expected).all()
  91. def test_compare_timedelta_ndarray(self):
  92. # GH11835
  93. periods = [Timedelta('0 days 01:00:00'), Timedelta('0 days 01:00:00')]
  94. arr = np.array(periods)
  95. result = arr[0] > arr
  96. expected = np.array([False, False])
  97. tm.assert_numpy_array_equal(result, expected)
  98. @pytest.mark.skip(reason="GH#20829 is reverted until after 0.24.0")
  99. def test_compare_custom_object(self):
  100. """
  101. Make sure non supported operations on Timedelta returns NonImplemented
  102. and yields to other operand (GH#20829).
  103. """
  104. class CustomClass(object):
  105. def __init__(self, cmp_result=None):
  106. self.cmp_result = cmp_result
  107. def generic_result(self):
  108. if self.cmp_result is None:
  109. return NotImplemented
  110. else:
  111. return self.cmp_result
  112. def __eq__(self, other):
  113. return self.generic_result()
  114. def __gt__(self, other):
  115. return self.generic_result()
  116. t = Timedelta('1s')
  117. assert not (t == "string")
  118. assert not (t == 1)
  119. assert not (t == CustomClass())
  120. assert not (t == CustomClass(cmp_result=False))
  121. assert t < CustomClass(cmp_result=True)
  122. assert not (t < CustomClass(cmp_result=False))
  123. assert t == CustomClass(cmp_result=True)
  124. @pytest.mark.parametrize("val", ["string", 1])
  125. def test_compare_unknown_type(self, val):
  126. # GH20829
  127. t = Timedelta('1s')
  128. with pytest.raises(TypeError):
  129. t >= val
  130. with pytest.raises(TypeError):
  131. t > val
  132. with pytest.raises(TypeError):
  133. t <= val
  134. with pytest.raises(TypeError):
  135. t < val
  136. class TestTimedeltas(object):
  137. @pytest.mark.parametrize("unit, value, expected", [
  138. ('us', 9.999, 9999), ('ms', 9.999999, 9999999),
  139. ('s', 9.999999999, 9999999999)])
  140. def test_rounding_on_int_unit_construction(self, unit, value, expected):
  141. # GH 12690
  142. result = Timedelta(value, unit=unit)
  143. assert result.value == expected
  144. result = Timedelta(str(value) + unit)
  145. assert result.value == expected
  146. def test_total_seconds_scalar(self):
  147. # see gh-10939
  148. rng = Timedelta('1 days, 10:11:12.100123456')
  149. expt = 1 * 86400 + 10 * 3600 + 11 * 60 + 12 + 100123456. / 1e9
  150. tm.assert_almost_equal(rng.total_seconds(), expt)
  151. rng = Timedelta(np.nan)
  152. assert np.isnan(rng.total_seconds())
  153. def test_conversion(self):
  154. for td in [Timedelta(10, unit='d'),
  155. Timedelta('1 days, 10:11:12.012345')]:
  156. pydt = td.to_pytimedelta()
  157. assert td == Timedelta(pydt)
  158. assert td == pydt
  159. assert (isinstance(pydt, timedelta) and not isinstance(
  160. pydt, Timedelta))
  161. assert td == np.timedelta64(td.value, 'ns')
  162. td64 = td.to_timedelta64()
  163. assert td64 == np.timedelta64(td.value, 'ns')
  164. assert td == td64
  165. assert isinstance(td64, np.timedelta64)
  166. # this is NOT equal and cannot be roundtriped (because of the nanos)
  167. td = Timedelta('1 days, 10:11:12.012345678')
  168. assert td != td.to_pytimedelta()
  169. def test_freq_conversion(self):
  170. # truediv
  171. td = Timedelta('1 days 2 hours 3 ns')
  172. result = td / np.timedelta64(1, 'D')
  173. assert result == td.value / float(86400 * 1e9)
  174. result = td / np.timedelta64(1, 's')
  175. assert result == td.value / float(1e9)
  176. result = td / np.timedelta64(1, 'ns')
  177. assert result == td.value
  178. # floordiv
  179. td = Timedelta('1 days 2 hours 3 ns')
  180. result = td // np.timedelta64(1, 'D')
  181. assert result == 1
  182. result = td // np.timedelta64(1, 's')
  183. assert result == 93600
  184. result = td // np.timedelta64(1, 'ns')
  185. assert result == td.value
  186. def test_fields(self):
  187. def check(value):
  188. # that we are int/long like
  189. assert isinstance(value, (int, compat.long))
  190. # compat to datetime.timedelta
  191. rng = to_timedelta('1 days, 10:11:12')
  192. assert rng.days == 1
  193. assert rng.seconds == 10 * 3600 + 11 * 60 + 12
  194. assert rng.microseconds == 0
  195. assert rng.nanoseconds == 0
  196. pytest.raises(AttributeError, lambda: rng.hours)
  197. pytest.raises(AttributeError, lambda: rng.minutes)
  198. pytest.raises(AttributeError, lambda: rng.milliseconds)
  199. # GH 10050
  200. check(rng.days)
  201. check(rng.seconds)
  202. check(rng.microseconds)
  203. check(rng.nanoseconds)
  204. td = Timedelta('-1 days, 10:11:12')
  205. assert abs(td) == Timedelta('13:48:48')
  206. assert str(td) == "-1 days +10:11:12"
  207. assert -td == Timedelta('0 days 13:48:48')
  208. assert -Timedelta('-1 days, 10:11:12').value == 49728000000000
  209. assert Timedelta('-1 days, 10:11:12').value == -49728000000000
  210. rng = to_timedelta('-1 days, 10:11:12.100123456')
  211. assert rng.days == -1
  212. assert rng.seconds == 10 * 3600 + 11 * 60 + 12
  213. assert rng.microseconds == 100 * 1000 + 123
  214. assert rng.nanoseconds == 456
  215. pytest.raises(AttributeError, lambda: rng.hours)
  216. pytest.raises(AttributeError, lambda: rng.minutes)
  217. pytest.raises(AttributeError, lambda: rng.milliseconds)
  218. # components
  219. tup = pd.to_timedelta(-1, 'us').components
  220. assert tup.days == -1
  221. assert tup.hours == 23
  222. assert tup.minutes == 59
  223. assert tup.seconds == 59
  224. assert tup.milliseconds == 999
  225. assert tup.microseconds == 999
  226. assert tup.nanoseconds == 0
  227. # GH 10050
  228. check(tup.days)
  229. check(tup.hours)
  230. check(tup.minutes)
  231. check(tup.seconds)
  232. check(tup.milliseconds)
  233. check(tup.microseconds)
  234. check(tup.nanoseconds)
  235. tup = Timedelta('-1 days 1 us').components
  236. assert tup.days == -2
  237. assert tup.hours == 23
  238. assert tup.minutes == 59
  239. assert tup.seconds == 59
  240. assert tup.milliseconds == 999
  241. assert tup.microseconds == 999
  242. assert tup.nanoseconds == 0
  243. def test_iso_conversion(self):
  244. # GH #21877
  245. expected = Timedelta(1, unit='s')
  246. assert to_timedelta('P0DT0H0M1S') == expected
  247. def test_nat_converters(self):
  248. result = to_timedelta('nat', box=False)
  249. assert result.dtype.kind == 'm'
  250. assert result.astype('int64') == iNaT
  251. result = to_timedelta('nan', box=False)
  252. assert result.dtype.kind == 'm'
  253. assert result.astype('int64') == iNaT
  254. @pytest.mark.parametrize('units, np_unit',
  255. [(['Y', 'y'], 'Y'),
  256. (['M'], 'M'),
  257. (['W', 'w'], 'W'),
  258. (['D', 'd', 'days', 'day', 'Days', 'Day'], 'D'),
  259. (['m', 'minute', 'min', 'minutes', 't',
  260. 'Minute', 'Min', 'Minutes', 'T'], 'm'),
  261. (['s', 'seconds', 'sec', 'second',
  262. 'S', 'Seconds', 'Sec', 'Second'], 's'),
  263. (['ms', 'milliseconds', 'millisecond', 'milli',
  264. 'millis', 'l', 'MS', 'Milliseconds',
  265. 'Millisecond', 'Milli', 'Millis', 'L'], 'ms'),
  266. (['us', 'microseconds', 'microsecond', 'micro',
  267. 'micros', 'u', 'US', 'Microseconds',
  268. 'Microsecond', 'Micro', 'Micros', 'U'], 'us'),
  269. (['ns', 'nanoseconds', 'nanosecond', 'nano',
  270. 'nanos', 'n', 'NS', 'Nanoseconds',
  271. 'Nanosecond', 'Nano', 'Nanos', 'N'], 'ns')])
  272. @pytest.mark.parametrize('wrapper', [np.array, list, pd.Index])
  273. def test_unit_parser(self, units, np_unit, wrapper):
  274. # validate all units, GH 6855, GH 21762
  275. for unit in units:
  276. # array-likes
  277. expected = TimedeltaIndex([np.timedelta64(i, np_unit)
  278. for i in np.arange(5).tolist()])
  279. result = to_timedelta(wrapper(range(5)), unit=unit)
  280. tm.assert_index_equal(result, expected)
  281. result = TimedeltaIndex(wrapper(range(5)), unit=unit)
  282. tm.assert_index_equal(result, expected)
  283. if unit == 'M':
  284. # M is treated as minutes in string repr
  285. expected = TimedeltaIndex([np.timedelta64(i, 'm')
  286. for i in np.arange(5).tolist()])
  287. str_repr = ['{}{}'.format(x, unit) for x in np.arange(5)]
  288. result = to_timedelta(wrapper(str_repr))
  289. tm.assert_index_equal(result, expected)
  290. result = TimedeltaIndex(wrapper(str_repr))
  291. tm.assert_index_equal(result, expected)
  292. # scalar
  293. expected = Timedelta(np.timedelta64(2, np_unit).astype(
  294. 'timedelta64[ns]'))
  295. result = to_timedelta(2, unit=unit)
  296. assert result == expected
  297. result = Timedelta(2, unit=unit)
  298. assert result == expected
  299. if unit == 'M':
  300. expected = Timedelta(np.timedelta64(2, 'm').astype(
  301. 'timedelta64[ns]'))
  302. result = to_timedelta('2{}'.format(unit))
  303. assert result == expected
  304. result = Timedelta('2{}'.format(unit))
  305. assert result == expected
  306. def test_numeric_conversions(self):
  307. assert Timedelta(0) == np.timedelta64(0, 'ns')
  308. assert Timedelta(10) == np.timedelta64(10, 'ns')
  309. assert Timedelta(10, unit='ns') == np.timedelta64(10, 'ns')
  310. assert Timedelta(10, unit='us') == np.timedelta64(10, 'us')
  311. assert Timedelta(10, unit='ms') == np.timedelta64(10, 'ms')
  312. assert Timedelta(10, unit='s') == np.timedelta64(10, 's')
  313. assert Timedelta(10, unit='d') == np.timedelta64(10, 'D')
  314. def test_timedelta_conversions(self):
  315. assert (Timedelta(timedelta(seconds=1)) ==
  316. np.timedelta64(1, 's').astype('m8[ns]'))
  317. assert (Timedelta(timedelta(microseconds=1)) ==
  318. np.timedelta64(1, 'us').astype('m8[ns]'))
  319. assert (Timedelta(timedelta(days=1)) ==
  320. np.timedelta64(1, 'D').astype('m8[ns]'))
  321. def test_round(self):
  322. t1 = Timedelta('1 days 02:34:56.789123456')
  323. t2 = Timedelta('-1 days 02:34:56.789123456')
  324. for (freq, s1, s2) in [('N', t1, t2),
  325. ('U', Timedelta('1 days 02:34:56.789123000'),
  326. Timedelta('-1 days 02:34:56.789123000')),
  327. ('L', Timedelta('1 days 02:34:56.789000000'),
  328. Timedelta('-1 days 02:34:56.789000000')),
  329. ('S', Timedelta('1 days 02:34:57'),
  330. Timedelta('-1 days 02:34:57')),
  331. ('2S', Timedelta('1 days 02:34:56'),
  332. Timedelta('-1 days 02:34:56')),
  333. ('5S', Timedelta('1 days 02:34:55'),
  334. Timedelta('-1 days 02:34:55')),
  335. ('T', Timedelta('1 days 02:35:00'),
  336. Timedelta('-1 days 02:35:00')),
  337. ('12T', Timedelta('1 days 02:36:00'),
  338. Timedelta('-1 days 02:36:00')),
  339. ('H', Timedelta('1 days 03:00:00'),
  340. Timedelta('-1 days 03:00:00')),
  341. ('d', Timedelta('1 days'),
  342. Timedelta('-1 days'))]:
  343. r1 = t1.round(freq)
  344. assert r1 == s1
  345. r2 = t2.round(freq)
  346. assert r2 == s2
  347. # invalid
  348. for freq in ['Y', 'M', 'foobar']:
  349. pytest.raises(ValueError, lambda: t1.round(freq))
  350. t1 = timedelta_range('1 days', periods=3, freq='1 min 2 s 3 us')
  351. t2 = -1 * t1
  352. t1a = timedelta_range('1 days', periods=3, freq='1 min 2 s')
  353. t1c = pd.TimedeltaIndex([1, 1, 1], unit='D')
  354. # note that negative times round DOWN! so don't give whole numbers
  355. for (freq, s1, s2) in [('N', t1, t2),
  356. ('U', t1, t2),
  357. ('L', t1a,
  358. TimedeltaIndex(['-1 days +00:00:00',
  359. '-2 days +23:58:58',
  360. '-2 days +23:57:56'],
  361. dtype='timedelta64[ns]',
  362. freq=None)
  363. ),
  364. ('S', t1a,
  365. TimedeltaIndex(['-1 days +00:00:00',
  366. '-2 days +23:58:58',
  367. '-2 days +23:57:56'],
  368. dtype='timedelta64[ns]',
  369. freq=None)
  370. ),
  371. ('12T', t1c,
  372. TimedeltaIndex(['-1 days',
  373. '-1 days',
  374. '-1 days'],
  375. dtype='timedelta64[ns]',
  376. freq=None)
  377. ),
  378. ('H', t1c,
  379. TimedeltaIndex(['-1 days',
  380. '-1 days',
  381. '-1 days'],
  382. dtype='timedelta64[ns]',
  383. freq=None)
  384. ),
  385. ('d', t1c,
  386. pd.TimedeltaIndex([-1, -1, -1], unit='D')
  387. )]:
  388. r1 = t1.round(freq)
  389. tm.assert_index_equal(r1, s1)
  390. r2 = t2.round(freq)
  391. tm.assert_index_equal(r2, s2)
  392. # invalid
  393. for freq in ['Y', 'M', 'foobar']:
  394. pytest.raises(ValueError, lambda: t1.round(freq))
  395. def test_contains(self):
  396. # Checking for any NaT-like objects
  397. # GH 13603
  398. td = to_timedelta(range(5), unit='d') + pd.offsets.Hour(1)
  399. for v in [pd.NaT, None, float('nan'), np.nan]:
  400. assert not (v in td)
  401. td = to_timedelta([pd.NaT])
  402. for v in [pd.NaT, None, float('nan'), np.nan]:
  403. assert (v in td)
  404. def test_identity(self):
  405. td = Timedelta(10, unit='d')
  406. assert isinstance(td, Timedelta)
  407. assert isinstance(td, timedelta)
  408. def test_short_format_converters(self):
  409. def conv(v):
  410. return v.astype('m8[ns]')
  411. assert Timedelta('10') == np.timedelta64(10, 'ns')
  412. assert Timedelta('10ns') == np.timedelta64(10, 'ns')
  413. assert Timedelta('100') == np.timedelta64(100, 'ns')
  414. assert Timedelta('100ns') == np.timedelta64(100, 'ns')
  415. assert Timedelta('1000') == np.timedelta64(1000, 'ns')
  416. assert Timedelta('1000ns') == np.timedelta64(1000, 'ns')
  417. assert Timedelta('1000NS') == np.timedelta64(1000, 'ns')
  418. assert Timedelta('10us') == np.timedelta64(10000, 'ns')
  419. assert Timedelta('100us') == np.timedelta64(100000, 'ns')
  420. assert Timedelta('1000us') == np.timedelta64(1000000, 'ns')
  421. assert Timedelta('1000Us') == np.timedelta64(1000000, 'ns')
  422. assert Timedelta('1000uS') == np.timedelta64(1000000, 'ns')
  423. assert Timedelta('1ms') == np.timedelta64(1000000, 'ns')
  424. assert Timedelta('10ms') == np.timedelta64(10000000, 'ns')
  425. assert Timedelta('100ms') == np.timedelta64(100000000, 'ns')
  426. assert Timedelta('1000ms') == np.timedelta64(1000000000, 'ns')
  427. assert Timedelta('-1s') == -np.timedelta64(1000000000, 'ns')
  428. assert Timedelta('1s') == np.timedelta64(1000000000, 'ns')
  429. assert Timedelta('10s') == np.timedelta64(10000000000, 'ns')
  430. assert Timedelta('100s') == np.timedelta64(100000000000, 'ns')
  431. assert Timedelta('1000s') == np.timedelta64(1000000000000, 'ns')
  432. assert Timedelta('1d') == conv(np.timedelta64(1, 'D'))
  433. assert Timedelta('-1d') == -conv(np.timedelta64(1, 'D'))
  434. assert Timedelta('1D') == conv(np.timedelta64(1, 'D'))
  435. assert Timedelta('10D') == conv(np.timedelta64(10, 'D'))
  436. assert Timedelta('100D') == conv(np.timedelta64(100, 'D'))
  437. assert Timedelta('1000D') == conv(np.timedelta64(1000, 'D'))
  438. assert Timedelta('10000D') == conv(np.timedelta64(10000, 'D'))
  439. # space
  440. assert Timedelta(' 10000D ') == conv(np.timedelta64(10000, 'D'))
  441. assert Timedelta(' - 10000D ') == -conv(np.timedelta64(10000, 'D'))
  442. # invalid
  443. with pytest.raises(ValueError):
  444. Timedelta('1foo')
  445. with pytest.raises(ValueError):
  446. Timedelta('foo')
  447. def test_full_format_converters(self):
  448. def conv(v):
  449. return v.astype('m8[ns]')
  450. d1 = np.timedelta64(1, 'D')
  451. assert Timedelta('1days') == conv(d1)
  452. assert Timedelta('1days,') == conv(d1)
  453. assert Timedelta('- 1days,') == -conv(d1)
  454. assert Timedelta('00:00:01') == conv(np.timedelta64(1, 's'))
  455. assert Timedelta('06:00:01') == conv(np.timedelta64(6 * 3600 + 1, 's'))
  456. assert Timedelta('06:00:01.0') == conv(
  457. np.timedelta64(6 * 3600 + 1, 's'))
  458. assert Timedelta('06:00:01.01') == conv(np.timedelta64(
  459. 1000 * (6 * 3600 + 1) + 10, 'ms'))
  460. assert (Timedelta('- 1days, 00:00:01') ==
  461. conv(-d1 + np.timedelta64(1, 's')))
  462. assert (Timedelta('1days, 06:00:01') ==
  463. conv(d1 + np.timedelta64(6 * 3600 + 1, 's')))
  464. assert (Timedelta('1days, 06:00:01.01') ==
  465. conv(d1 + np.timedelta64(1000 * (6 * 3600 + 1) + 10, 'ms')))
  466. # invalid
  467. with pytest.raises(ValueError):
  468. Timedelta('- 1days, 00')
  469. def test_overflow(self):
  470. # GH 9442
  471. s = Series(pd.date_range('20130101', periods=100000, freq='H'))
  472. s[0] += pd.Timedelta('1s 1ms')
  473. # mean
  474. result = (s - s.min()).mean()
  475. expected = pd.Timedelta((pd.TimedeltaIndex((s - s.min())).asi8 / len(s)
  476. ).sum())
  477. # the computation is converted to float so
  478. # might be some loss of precision
  479. assert np.allclose(result.value / 1000, expected.value / 1000)
  480. # sum
  481. pytest.raises(ValueError, lambda: (s - s.min()).sum())
  482. s1 = s[0:10000]
  483. pytest.raises(ValueError, lambda: (s1 - s1.min()).sum())
  484. s2 = s[0:1000]
  485. result = (s2 - s2.min()).sum()
  486. def test_pickle(self):
  487. v = Timedelta('1 days 10:11:12.0123456')
  488. v_p = tm.round_trip_pickle(v)
  489. assert v == v_p
  490. def test_timedelta_hash_equality(self):
  491. # GH 11129
  492. v = Timedelta(1, 'D')
  493. td = timedelta(days=1)
  494. assert hash(v) == hash(td)
  495. d = {td: 2}
  496. assert d[v] == 2
  497. tds = timedelta_range('1 second', periods=20)
  498. assert all(hash(td) == hash(td.to_pytimedelta()) for td in tds)
  499. # python timedeltas drop ns resolution
  500. ns_td = Timedelta(1, 'ns')
  501. assert hash(ns_td) != hash(ns_td.to_pytimedelta())
  502. def test_implementation_limits(self):
  503. min_td = Timedelta(Timedelta.min)
  504. max_td = Timedelta(Timedelta.max)
  505. # GH 12727
  506. # timedelta limits correspond to int64 boundaries
  507. assert min_td.value == np.iinfo(np.int64).min + 1
  508. assert max_td.value == np.iinfo(np.int64).max
  509. # Beyond lower limit, a NAT before the Overflow
  510. assert (min_td - Timedelta(1, 'ns')) is NaT
  511. with pytest.raises(OverflowError):
  512. min_td - Timedelta(2, 'ns')
  513. with pytest.raises(OverflowError):
  514. max_td + Timedelta(1, 'ns')
  515. # Same tests using the internal nanosecond values
  516. td = Timedelta(min_td.value - 1, 'ns')
  517. assert td is NaT
  518. with pytest.raises(OverflowError):
  519. Timedelta(min_td.value - 2, 'ns')
  520. with pytest.raises(OverflowError):
  521. Timedelta(max_td.value + 1, 'ns')
  522. def test_total_seconds_precision(self):
  523. # GH 19458
  524. assert Timedelta('30S').total_seconds() == 30.0
  525. assert Timedelta('0').total_seconds() == 0.0
  526. assert Timedelta('-2S').total_seconds() == -2.0
  527. assert Timedelta('5.324S').total_seconds() == 5.324
  528. assert (Timedelta('30S').total_seconds() - 30.0) < 1e-20
  529. assert (30.0 - Timedelta('30S').total_seconds()) < 1e-20
  530. def test_timedelta_arithmetic(self):
  531. data = pd.Series(['nat', '32 days'], dtype='timedelta64[ns]')
  532. deltas = [timedelta(days=1), Timedelta(1, unit='D')]
  533. for delta in deltas:
  534. result_method = data.add(delta)
  535. result_operator = data + delta
  536. expected = pd.Series(['nat', '33 days'], dtype='timedelta64[ns]')
  537. tm.assert_series_equal(result_operator, expected)
  538. tm.assert_series_equal(result_method, expected)
  539. result_method = data.sub(delta)
  540. result_operator = data - delta
  541. expected = pd.Series(['nat', '31 days'], dtype='timedelta64[ns]')
  542. tm.assert_series_equal(result_operator, expected)
  543. tm.assert_series_equal(result_method, expected)
  544. # GH 9396
  545. result_method = data.div(delta)
  546. result_operator = data / delta
  547. expected = pd.Series([np.nan, 32.], dtype='float64')
  548. tm.assert_series_equal(result_operator, expected)
  549. tm.assert_series_equal(result_method, expected)
  550. def test_apply_to_timedelta(self):
  551. timedelta_NaT = pd.to_timedelta('NaT')
  552. list_of_valid_strings = ['00:00:01', '00:00:02']
  553. a = pd.to_timedelta(list_of_valid_strings)
  554. b = Series(list_of_valid_strings).apply(pd.to_timedelta)
  555. # Can't compare until apply on a Series gives the correct dtype
  556. # assert_series_equal(a, b)
  557. list_of_strings = ['00:00:01', np.nan, pd.NaT, timedelta_NaT]
  558. # TODO: unused?
  559. a = pd.to_timedelta(list_of_strings) # noqa
  560. b = Series(list_of_strings).apply(pd.to_timedelta) # noqa
  561. # Can't compare until apply on a Series gives the correct dtype
  562. # assert_series_equal(a, b)
  563. def test_components(self):
  564. rng = timedelta_range('1 days, 10:11:12', periods=2, freq='s')
  565. rng.components
  566. # with nat
  567. s = Series(rng)
  568. s[1] = np.nan
  569. result = s.dt.components
  570. assert not result.iloc[0].isna().all()
  571. assert result.iloc[1].isna().all()
  572. @pytest.mark.parametrize('value, expected', [
  573. (Timedelta('10S'), True),
  574. (Timedelta('-10S'), True),
  575. (Timedelta(10, unit='ns'), True),
  576. (Timedelta(0, unit='ns'), False),
  577. (Timedelta(-10, unit='ns'), True),
  578. (Timedelta(None), True),
  579. (pd.NaT, True),
  580. ])
  581. def test_truthiness(value, expected):
  582. # https://github.com/pandas-dev/pandas/issues/21484
  583. assert bool(value) is expected