test_period.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import numpy as np
  2. import pytest
  3. import pandas as pd
  4. from pandas import DataFrame, Period, Series, period_range
  5. from pandas.core.arrays import PeriodArray
  6. import pandas.util.testing as tm
  7. class TestSeriesPeriod(object):
  8. def setup_method(self, method):
  9. self.series = Series(period_range('2000-01-01', periods=10, freq='D'))
  10. def test_auto_conversion(self):
  11. series = Series(list(period_range('2000-01-01', periods=10, freq='D')))
  12. assert series.dtype == 'Period[D]'
  13. series = pd.Series([pd.Period('2011-01-01', freq='D'),
  14. pd.Period('2011-02-01', freq='D')])
  15. assert series.dtype == 'Period[D]'
  16. def test_getitem(self):
  17. assert self.series[1] == pd.Period('2000-01-02', freq='D')
  18. result = self.series[[2, 4]]
  19. exp = pd.Series([pd.Period('2000-01-03', freq='D'),
  20. pd.Period('2000-01-05', freq='D')],
  21. index=[2, 4], dtype='Period[D]')
  22. tm.assert_series_equal(result, exp)
  23. assert result.dtype == 'Period[D]'
  24. def test_isna(self):
  25. # GH 13737
  26. s = Series([pd.Period('2011-01', freq='M'),
  27. pd.Period('NaT', freq='M')])
  28. tm.assert_series_equal(s.isna(), Series([False, True]))
  29. tm.assert_series_equal(s.notna(), Series([True, False]))
  30. def test_fillna(self):
  31. # GH 13737
  32. s = Series([pd.Period('2011-01', freq='M'),
  33. pd.Period('NaT', freq='M')])
  34. res = s.fillna(pd.Period('2012-01', freq='M'))
  35. exp = Series([pd.Period('2011-01', freq='M'),
  36. pd.Period('2012-01', freq='M')])
  37. tm.assert_series_equal(res, exp)
  38. assert res.dtype == 'Period[M]'
  39. def test_dropna(self):
  40. # GH 13737
  41. s = Series([pd.Period('2011-01', freq='M'),
  42. pd.Period('NaT', freq='M')])
  43. tm.assert_series_equal(s.dropna(),
  44. Series([pd.Period('2011-01', freq='M')]))
  45. def test_between(self):
  46. left, right = self.series[[2, 7]]
  47. result = self.series.between(left, right)
  48. expected = (self.series >= left) & (self.series <= right)
  49. tm.assert_series_equal(result, expected)
  50. # ---------------------------------------------------------------------
  51. # NaT support
  52. @pytest.mark.xfail(reason="PeriodDtype Series not supported yet")
  53. def test_NaT_scalar(self):
  54. series = Series([0, 1000, 2000, pd._libs.iNaT], dtype='period[D]')
  55. val = series[3]
  56. assert pd.isna(val)
  57. series[2] = val
  58. assert pd.isna(series[2])
  59. @pytest.mark.xfail(reason="PeriodDtype Series not supported yet")
  60. def test_NaT_cast(self):
  61. result = Series([np.nan]).astype('period[D]')
  62. expected = Series([pd.NaT])
  63. tm.assert_series_equal(result, expected)
  64. def test_set_none(self):
  65. self.series[3] = None
  66. assert self.series[3] is pd.NaT
  67. self.series[3:5] = None
  68. assert self.series[4] is pd.NaT
  69. def test_set_nan(self):
  70. # Do we want to allow this?
  71. self.series[5] = np.nan
  72. assert self.series[5] is pd.NaT
  73. self.series[5:7] = np.nan
  74. assert self.series[6] is pd.NaT
  75. def test_intercept_astype_object(self):
  76. expected = self.series.astype('object')
  77. df = DataFrame({'a': self.series,
  78. 'b': np.random.randn(len(self.series))})
  79. result = df.values.squeeze()
  80. assert (result[:, 0] == expected.values).all()
  81. df = DataFrame({'a': self.series, 'b': ['foo'] * len(self.series)})
  82. result = df.values.squeeze()
  83. assert (result[:, 0] == expected.values).all()
  84. def test_align_series(self, join_type):
  85. rng = period_range('1/1/2000', '1/1/2010', freq='A')
  86. ts = Series(np.random.randn(len(rng)), index=rng)
  87. ts.align(ts[::2], join=join_type)
  88. def test_truncate(self):
  89. # GH 17717
  90. idx1 = pd.PeriodIndex([
  91. pd.Period('2017-09-02'),
  92. pd.Period('2017-09-02'),
  93. pd.Period('2017-09-03')
  94. ])
  95. series1 = pd.Series([1, 2, 3], index=idx1)
  96. result1 = series1.truncate(after='2017-09-02')
  97. expected_idx1 = pd.PeriodIndex([
  98. pd.Period('2017-09-02'),
  99. pd.Period('2017-09-02')
  100. ])
  101. tm.assert_series_equal(result1, pd.Series([1, 2], index=expected_idx1))
  102. idx2 = pd.PeriodIndex([
  103. pd.Period('2017-09-03'),
  104. pd.Period('2017-09-02'),
  105. pd.Period('2017-09-03')
  106. ])
  107. series2 = pd.Series([1, 2, 3], index=idx2)
  108. result2 = series2.sort_index().truncate(after='2017-09-02')
  109. expected_idx2 = pd.PeriodIndex([
  110. pd.Period('2017-09-02')
  111. ])
  112. tm.assert_series_equal(result2, pd.Series([2], index=expected_idx2))
  113. @pytest.mark.parametrize('input_vals', [
  114. [Period('2016-01', freq='M'), Period('2016-02', freq='M')],
  115. [Period('2016-01-01', freq='D'), Period('2016-01-02', freq='D')],
  116. [Period('2016-01-01 00:00:00', freq='H'),
  117. Period('2016-01-01 01:00:00', freq='H')],
  118. [Period('2016-01-01 00:00:00', freq='M'),
  119. Period('2016-01-01 00:01:00', freq='M')],
  120. [Period('2016-01-01 00:00:00', freq='S'),
  121. Period('2016-01-01 00:00:01', freq='S')]
  122. ])
  123. def test_end_time_timevalues(self, input_vals):
  124. # GH 17157
  125. # Check that the time part of the Period is adjusted by end_time
  126. # when using the dt accessor on a Series
  127. input_vals = PeriodArray._from_sequence(np.asarray(input_vals))
  128. s = Series(input_vals)
  129. result = s.dt.end_time
  130. expected = s.apply(lambda x: x.end_time)
  131. tm.assert_series_equal(result, expected)