test_timedelta.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from datetime import timedelta
  2. import numpy as np
  3. import pandas as pd
  4. from pandas import DataFrame, Series
  5. from pandas.core.indexes.timedeltas import timedelta_range
  6. import pandas.util.testing as tm
  7. from pandas.util.testing import assert_frame_equal, assert_series_equal
  8. def test_asfreq_bug():
  9. df = DataFrame(data=[1, 3],
  10. index=[timedelta(), timedelta(minutes=3)])
  11. result = df.resample('1T').asfreq()
  12. expected = DataFrame(data=[1, np.nan, np.nan, 3],
  13. index=timedelta_range('0 day',
  14. periods=4,
  15. freq='1T'))
  16. assert_frame_equal(result, expected)
  17. def test_resample_with_nat():
  18. # GH 13223
  19. index = pd.to_timedelta(['0s', pd.NaT, '2s'])
  20. result = DataFrame({'value': [2, 3, 5]}, index).resample('1s').mean()
  21. expected = DataFrame({'value': [2.5, np.nan, 5.0]},
  22. index=timedelta_range('0 day',
  23. periods=3,
  24. freq='1S'))
  25. assert_frame_equal(result, expected)
  26. def test_resample_as_freq_with_subperiod():
  27. # GH 13022
  28. index = timedelta_range('00:00:00', '00:10:00', freq='5T')
  29. df = DataFrame(data={'value': [1, 5, 10]}, index=index)
  30. result = df.resample('2T').asfreq()
  31. expected_data = {'value': [1, np.nan, np.nan, np.nan, np.nan, 10]}
  32. expected = DataFrame(data=expected_data,
  33. index=timedelta_range('00:00:00',
  34. '00:10:00', freq='2T'))
  35. tm.assert_frame_equal(result, expected)
  36. def test_resample_with_timedeltas():
  37. expected = DataFrame({'A': np.arange(1480)})
  38. expected = expected.groupby(expected.index // 30).sum()
  39. expected.index = pd.timedelta_range('0 days', freq='30T', periods=50)
  40. df = DataFrame({'A': np.arange(1480)}, index=pd.to_timedelta(
  41. np.arange(1480), unit='T'))
  42. result = df.resample('30T').sum()
  43. assert_frame_equal(result, expected)
  44. s = df['A']
  45. result = s.resample('30T').sum()
  46. assert_series_equal(result, expected['A'])
  47. def test_resample_single_period_timedelta():
  48. s = Series(list(range(5)), index=pd.timedelta_range(
  49. '1 day', freq='s', periods=5))
  50. result = s.resample('2s').sum()
  51. expected = Series([1, 5, 4], index=pd.timedelta_range(
  52. '1 day', freq='2s', periods=3))
  53. assert_series_equal(result, expected)
  54. def test_resample_timedelta_idempotency():
  55. # GH 12072
  56. index = pd.timedelta_range('0', periods=9, freq='10L')
  57. series = Series(range(9), index=index)
  58. result = series.resample('10L').mean()
  59. expected = series
  60. assert_series_equal(result, expected)
  61. def test_resample_base_with_timedeltaindex():
  62. # GH 10530
  63. rng = timedelta_range(start='0s', periods=25, freq='s')
  64. ts = Series(np.random.randn(len(rng)), index=rng)
  65. with_base = ts.resample('2s', base=5).mean()
  66. without_base = ts.resample('2s').mean()
  67. exp_without_base = timedelta_range(start='0s', end='25s', freq='2s')
  68. exp_with_base = timedelta_range(start='5s', end='29s', freq='2s')
  69. tm.assert_index_equal(without_base.index, exp_without_base)
  70. tm.assert_index_equal(with_base.index, exp_with_base)
  71. def test_resample_categorical_data_with_timedeltaindex():
  72. # GH #12169
  73. df = DataFrame({'Group_obj': 'A'},
  74. index=pd.to_timedelta(list(range(20)), unit='s'))
  75. df['Group'] = df['Group_obj'].astype('category')
  76. result = df.resample('10s').agg(lambda x: (x.value_counts().index[0]))
  77. expected = DataFrame({'Group_obj': ['A', 'A'],
  78. 'Group': ['A', 'A']},
  79. index=pd.to_timedelta([0, 10], unit='s'))
  80. expected = expected.reindex(['Group_obj', 'Group'], axis=1)
  81. expected['Group'] = expected['Group_obj'].astype('category')
  82. tm.assert_frame_equal(result, expected)
  83. def test_resample_timedelta_values():
  84. # GH 13119
  85. # check that timedelta dtype is preserved when NaT values are
  86. # introduced by the resampling
  87. times = timedelta_range('1 day', '4 day', freq='4D')
  88. df = DataFrame({'time': times}, index=times)
  89. times2 = timedelta_range('1 day', '4 day', freq='2D')
  90. exp = Series(times2, index=times2, name='time')
  91. exp.iloc[1] = pd.NaT
  92. res = df.resample('2D').first()['time']
  93. tm.assert_series_equal(res, exp)
  94. res = df['time'].resample('2D').first()
  95. tm.assert_series_equal(res, exp)