test_tools.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. from datetime import time, timedelta
  2. import numpy as np
  3. import pytest
  4. from pandas._libs.tslib import iNaT
  5. import pandas as pd
  6. from pandas import Series, TimedeltaIndex, isna, to_timedelta
  7. import pandas.util.testing as tm
  8. from pandas.util.testing import assert_series_equal
  9. class TestTimedeltas(object):
  10. def test_to_timedelta(self):
  11. def conv(v):
  12. return v.astype('m8[ns]')
  13. d1 = np.timedelta64(1, 'D')
  14. assert (to_timedelta('1 days 06:05:01.00003', box=False) ==
  15. conv(d1 + np.timedelta64(6 * 3600 + 5 * 60 + 1, 's') +
  16. np.timedelta64(30, 'us')))
  17. assert (to_timedelta('15.5us', box=False) ==
  18. conv(np.timedelta64(15500, 'ns')))
  19. # empty string
  20. result = to_timedelta('', box=False)
  21. assert result.astype('int64') == iNaT
  22. result = to_timedelta(['', ''])
  23. assert isna(result).all()
  24. # pass thru
  25. result = to_timedelta(np.array([np.timedelta64(1, 's')]))
  26. expected = pd.Index(np.array([np.timedelta64(1, 's')]))
  27. tm.assert_index_equal(result, expected)
  28. # ints
  29. result = np.timedelta64(0, 'ns')
  30. expected = to_timedelta(0, box=False)
  31. assert result == expected
  32. # Series
  33. expected = Series([timedelta(days=1), timedelta(days=1, seconds=1)])
  34. result = to_timedelta(Series(['1d', '1days 00:00:01']))
  35. tm.assert_series_equal(result, expected)
  36. # with units
  37. result = TimedeltaIndex([np.timedelta64(0, 'ns'), np.timedelta64(
  38. 10, 's').astype('m8[ns]')])
  39. expected = to_timedelta([0, 10], unit='s')
  40. tm.assert_index_equal(result, expected)
  41. # single element conversion
  42. v = timedelta(seconds=1)
  43. result = to_timedelta(v, box=False)
  44. expected = np.timedelta64(timedelta(seconds=1))
  45. assert result == expected
  46. v = np.timedelta64(timedelta(seconds=1))
  47. result = to_timedelta(v, box=False)
  48. expected = np.timedelta64(timedelta(seconds=1))
  49. assert result == expected
  50. # arrays of various dtypes
  51. arr = np.array([1] * 5, dtype='int64')
  52. result = to_timedelta(arr, unit='s')
  53. expected = TimedeltaIndex([np.timedelta64(1, 's')] * 5)
  54. tm.assert_index_equal(result, expected)
  55. arr = np.array([1] * 5, dtype='int64')
  56. result = to_timedelta(arr, unit='m')
  57. expected = TimedeltaIndex([np.timedelta64(1, 'm')] * 5)
  58. tm.assert_index_equal(result, expected)
  59. arr = np.array([1] * 5, dtype='int64')
  60. result = to_timedelta(arr, unit='h')
  61. expected = TimedeltaIndex([np.timedelta64(1, 'h')] * 5)
  62. tm.assert_index_equal(result, expected)
  63. arr = np.array([1] * 5, dtype='timedelta64[s]')
  64. result = to_timedelta(arr)
  65. expected = TimedeltaIndex([np.timedelta64(1, 's')] * 5)
  66. tm.assert_index_equal(result, expected)
  67. arr = np.array([1] * 5, dtype='timedelta64[D]')
  68. result = to_timedelta(arr)
  69. expected = TimedeltaIndex([np.timedelta64(1, 'D')] * 5)
  70. tm.assert_index_equal(result, expected)
  71. # Test with lists as input when box=false
  72. expected = np.array(np.arange(3) * 1000000000, dtype='timedelta64[ns]')
  73. result = to_timedelta(range(3), unit='s', box=False)
  74. tm.assert_numpy_array_equal(expected, result)
  75. result = to_timedelta(np.arange(3), unit='s', box=False)
  76. tm.assert_numpy_array_equal(expected, result)
  77. result = to_timedelta([0, 1, 2], unit='s', box=False)
  78. tm.assert_numpy_array_equal(expected, result)
  79. # Tests with fractional seconds as input:
  80. expected = np.array(
  81. [0, 500000000, 800000000, 1200000000], dtype='timedelta64[ns]')
  82. result = to_timedelta([0., 0.5, 0.8, 1.2], unit='s', box=False)
  83. tm.assert_numpy_array_equal(expected, result)
  84. def test_to_timedelta_invalid(self):
  85. # bad value for errors parameter
  86. msg = "errors must be one of"
  87. with pytest.raises(ValueError, match=msg):
  88. to_timedelta(['foo'], errors='never')
  89. # these will error
  90. pytest.raises(ValueError, lambda: to_timedelta([1, 2], unit='foo'))
  91. pytest.raises(ValueError, lambda: to_timedelta(1, unit='foo'))
  92. # time not supported ATM
  93. pytest.raises(ValueError, lambda: to_timedelta(time(second=1)))
  94. assert to_timedelta(time(second=1), errors='coerce') is pd.NaT
  95. pytest.raises(ValueError, lambda: to_timedelta(['foo', 'bar']))
  96. tm.assert_index_equal(TimedeltaIndex([pd.NaT, pd.NaT]),
  97. to_timedelta(['foo', 'bar'], errors='coerce'))
  98. tm.assert_index_equal(TimedeltaIndex(['1 day', pd.NaT, '1 min']),
  99. to_timedelta(['1 day', 'bar', '1 min'],
  100. errors='coerce'))
  101. # gh-13613: these should not error because errors='ignore'
  102. invalid_data = 'apple'
  103. assert invalid_data == to_timedelta(invalid_data, errors='ignore')
  104. invalid_data = ['apple', '1 days']
  105. tm.assert_numpy_array_equal(
  106. np.array(invalid_data, dtype=object),
  107. to_timedelta(invalid_data, errors='ignore'))
  108. invalid_data = pd.Index(['apple', '1 days'])
  109. tm.assert_index_equal(invalid_data, to_timedelta(
  110. invalid_data, errors='ignore'))
  111. invalid_data = Series(['apple', '1 days'])
  112. tm.assert_series_equal(invalid_data, to_timedelta(
  113. invalid_data, errors='ignore'))
  114. def test_to_timedelta_via_apply(self):
  115. # GH 5458
  116. expected = Series([np.timedelta64(1, 's')])
  117. result = Series(['00:00:01']).apply(to_timedelta)
  118. tm.assert_series_equal(result, expected)
  119. result = Series([to_timedelta('00:00:01')])
  120. tm.assert_series_equal(result, expected)
  121. def test_to_timedelta_on_missing_values(self):
  122. # GH5438
  123. timedelta_NaT = np.timedelta64('NaT')
  124. actual = pd.to_timedelta(Series(['00:00:01', np.nan]))
  125. expected = Series([np.timedelta64(1000000000, 'ns'),
  126. timedelta_NaT], dtype='<m8[ns]')
  127. assert_series_equal(actual, expected)
  128. actual = pd.to_timedelta(Series(['00:00:01', pd.NaT]))
  129. assert_series_equal(actual, expected)
  130. actual = pd.to_timedelta(np.nan)
  131. assert actual.value == timedelta_NaT.astype('int64')
  132. actual = pd.to_timedelta(pd.NaT)
  133. assert actual.value == timedelta_NaT.astype('int64')
  134. def test_to_timedelta_float(self):
  135. # https://github.com/pandas-dev/pandas/issues/25077
  136. arr = np.arange(0, 1, 1e-6)[-10:]
  137. result = pd.to_timedelta(arr, unit='s')
  138. expected_asi8 = np.arange(999990000, int(1e9), 1000, dtype='int64')
  139. tm.assert_numpy_array_equal(result.asi8, expected_asi8)