test_asof.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # coding=utf-8
  2. import numpy as np
  3. import pytest
  4. from pandas import Series, Timestamp, date_range, isna, notna, offsets
  5. import pandas.util.testing as tm
  6. class TestSeriesAsof():
  7. def test_basic(self):
  8. # array or list or dates
  9. N = 50
  10. rng = date_range('1/1/1990', periods=N, freq='53s')
  11. ts = Series(np.random.randn(N), index=rng)
  12. ts[15:30] = np.nan
  13. dates = date_range('1/1/1990', periods=N * 3, freq='25s')
  14. result = ts.asof(dates)
  15. assert notna(result).all()
  16. lb = ts.index[14]
  17. ub = ts.index[30]
  18. result = ts.asof(list(dates))
  19. assert notna(result).all()
  20. lb = ts.index[14]
  21. ub = ts.index[30]
  22. mask = (result.index >= lb) & (result.index < ub)
  23. rs = result[mask]
  24. assert (rs == ts[lb]).all()
  25. val = result[result.index[result.index >= ub][0]]
  26. assert ts[ub] == val
  27. def test_scalar(self):
  28. N = 30
  29. rng = date_range('1/1/1990', periods=N, freq='53s')
  30. ts = Series(np.arange(N), index=rng)
  31. ts[5:10] = np.NaN
  32. ts[15:20] = np.NaN
  33. val1 = ts.asof(ts.index[7])
  34. val2 = ts.asof(ts.index[19])
  35. assert val1 == ts[4]
  36. assert val2 == ts[14]
  37. # accepts strings
  38. val1 = ts.asof(str(ts.index[7]))
  39. assert val1 == ts[4]
  40. # in there
  41. result = ts.asof(ts.index[3])
  42. assert result == ts[3]
  43. # no as of value
  44. d = ts.index[0] - offsets.BDay()
  45. assert np.isnan(ts.asof(d))
  46. def test_with_nan(self):
  47. # basic asof test
  48. rng = date_range('1/1/2000', '1/2/2000', freq='4h')
  49. s = Series(np.arange(len(rng)), index=rng)
  50. r = s.resample('2h').mean()
  51. result = r.asof(r.index)
  52. expected = Series([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6.],
  53. index=date_range('1/1/2000', '1/2/2000', freq='2h'))
  54. tm.assert_series_equal(result, expected)
  55. r.iloc[3:5] = np.nan
  56. result = r.asof(r.index)
  57. expected = Series([0, 0, 1, 1, 1, 1, 3, 3, 4, 4, 5, 5, 6.],
  58. index=date_range('1/1/2000', '1/2/2000', freq='2h'))
  59. tm.assert_series_equal(result, expected)
  60. r.iloc[-3:] = np.nan
  61. result = r.asof(r.index)
  62. expected = Series([0, 0, 1, 1, 1, 1, 3, 3, 4, 4, 4, 4, 4.],
  63. index=date_range('1/1/2000', '1/2/2000', freq='2h'))
  64. tm.assert_series_equal(result, expected)
  65. def test_periodindex(self):
  66. from pandas import period_range, PeriodIndex
  67. # array or list or dates
  68. N = 50
  69. rng = period_range('1/1/1990', periods=N, freq='H')
  70. ts = Series(np.random.randn(N), index=rng)
  71. ts[15:30] = np.nan
  72. dates = date_range('1/1/1990', periods=N * 3, freq='37min')
  73. result = ts.asof(dates)
  74. assert notna(result).all()
  75. lb = ts.index[14]
  76. ub = ts.index[30]
  77. result = ts.asof(list(dates))
  78. assert notna(result).all()
  79. lb = ts.index[14]
  80. ub = ts.index[30]
  81. pix = PeriodIndex(result.index.values, freq='H')
  82. mask = (pix >= lb) & (pix < ub)
  83. rs = result[mask]
  84. assert (rs == ts[lb]).all()
  85. ts[5:10] = np.nan
  86. ts[15:20] = np.nan
  87. val1 = ts.asof(ts.index[7])
  88. val2 = ts.asof(ts.index[19])
  89. assert val1 == ts[4]
  90. assert val2 == ts[14]
  91. # accepts strings
  92. val1 = ts.asof(str(ts.index[7]))
  93. assert val1 == ts[4]
  94. # in there
  95. assert ts.asof(ts.index[3]) == ts[3]
  96. # no as of value
  97. d = ts.index[0].to_timestamp() - offsets.BDay()
  98. assert isna(ts.asof(d))
  99. def test_errors(self):
  100. s = Series([1, 2, 3],
  101. index=[Timestamp('20130101'),
  102. Timestamp('20130103'),
  103. Timestamp('20130102')])
  104. # non-monotonic
  105. assert not s.index.is_monotonic
  106. with pytest.raises(ValueError):
  107. s.asof(s.index[0])
  108. # subset with Series
  109. N = 10
  110. rng = date_range('1/1/1990', periods=N, freq='53s')
  111. s = Series(np.random.randn(N), index=rng)
  112. with pytest.raises(ValueError):
  113. s.asof(s.index[0], subset='foo')
  114. def test_all_nans(self):
  115. # GH 15713
  116. # series is all nans
  117. result = Series([np.nan]).asof([0])
  118. expected = Series([np.nan])
  119. tm.assert_series_equal(result, expected)
  120. # testing non-default indexes
  121. N = 50
  122. rng = date_range('1/1/1990', periods=N, freq='53s')
  123. dates = date_range('1/1/1990', periods=N * 3, freq='25s')
  124. result = Series(np.nan, index=rng).asof(dates)
  125. expected = Series(np.nan, index=dates)
  126. tm.assert_series_equal(result, expected)
  127. # testing scalar input
  128. date = date_range('1/1/1990', periods=N * 3, freq='25s')[0]
  129. result = Series(np.nan, index=rng).asof(date)
  130. assert isna(result)
  131. # test name is propagated
  132. result = Series(np.nan, index=[1, 2, 3, 4], name='test').asof([4, 5])
  133. expected = Series(np.nan, index=[4, 5], name='test')
  134. tm.assert_series_equal(result, expected)