test_period.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. import numpy as np
  2. import pytest
  3. from pandas._libs.tslibs.period import IncompatibleFrequency
  4. import pandas.util._test_decorators as td
  5. import pandas as pd
  6. from pandas import (
  7. DataFrame, DatetimeIndex, Index, NaT, Period, PeriodIndex, Series,
  8. date_range, offsets, period_range)
  9. from pandas.util import testing as tm
  10. from ..datetimelike import DatetimeLike
  11. class TestPeriodIndex(DatetimeLike):
  12. _holder = PeriodIndex
  13. def setup_method(self, method):
  14. self.indices = dict(index=tm.makePeriodIndex(10),
  15. index_dec=period_range('20130101', periods=10,
  16. freq='D')[::-1])
  17. self.setup_indices()
  18. def create_index(self):
  19. return period_range('20130101', periods=5, freq='D')
  20. def test_pickle_compat_construction(self):
  21. pass
  22. @pytest.mark.parametrize('freq', ['D', 'M', 'A'])
  23. def test_pickle_round_trip(self, freq):
  24. idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq=freq)
  25. result = tm.round_trip_pickle(idx)
  26. tm.assert_index_equal(result, idx)
  27. def test_where(self):
  28. # This is handled in test_indexing
  29. pass
  30. @pytest.mark.parametrize('use_numpy', [True, False])
  31. @pytest.mark.parametrize('index', [
  32. pd.period_range('2000-01-01', periods=3, freq='D'),
  33. pd.period_range('2001-01-01', periods=3, freq='2D'),
  34. pd.PeriodIndex(['2001-01', 'NaT', '2003-01'], freq='M')])
  35. def test_repeat_freqstr(self, index, use_numpy):
  36. # GH10183
  37. expected = PeriodIndex([p for p in index for _ in range(3)])
  38. result = np.repeat(index, 3) if use_numpy else index.repeat(3)
  39. tm.assert_index_equal(result, expected)
  40. assert result.freqstr == index.freqstr
  41. def test_fillna_period(self):
  42. # GH 11343
  43. idx = pd.PeriodIndex(['2011-01-01 09:00', pd.NaT,
  44. '2011-01-01 11:00'], freq='H')
  45. exp = pd.PeriodIndex(['2011-01-01 09:00', '2011-01-01 10:00',
  46. '2011-01-01 11:00'], freq='H')
  47. tm.assert_index_equal(
  48. idx.fillna(pd.Period('2011-01-01 10:00', freq='H')), exp)
  49. exp = pd.Index([pd.Period('2011-01-01 09:00', freq='H'), 'x',
  50. pd.Period('2011-01-01 11:00', freq='H')], dtype=object)
  51. tm.assert_index_equal(idx.fillna('x'), exp)
  52. exp = pd.Index([pd.Period('2011-01-01 09:00', freq='H'),
  53. pd.Period('2011-01-01', freq='D'),
  54. pd.Period('2011-01-01 11:00', freq='H')], dtype=object)
  55. tm.assert_index_equal(idx.fillna(
  56. pd.Period('2011-01-01', freq='D')), exp)
  57. def test_no_millisecond_field(self):
  58. with pytest.raises(AttributeError):
  59. DatetimeIndex.millisecond
  60. with pytest.raises(AttributeError):
  61. DatetimeIndex([]).millisecond
  62. @pytest.mark.parametrize("sort", [None, False])
  63. def test_difference_freq(self, sort):
  64. # GH14323: difference of Period MUST preserve frequency
  65. # but the ability to union results must be preserved
  66. index = period_range("20160920", "20160925", freq="D")
  67. other = period_range("20160921", "20160924", freq="D")
  68. expected = PeriodIndex(["20160920", "20160925"], freq='D')
  69. idx_diff = index.difference(other, sort)
  70. tm.assert_index_equal(idx_diff, expected)
  71. tm.assert_attr_equal('freq', idx_diff, expected)
  72. other = period_range("20160922", "20160925", freq="D")
  73. idx_diff = index.difference(other, sort)
  74. expected = PeriodIndex(["20160920", "20160921"], freq='D')
  75. tm.assert_index_equal(idx_diff, expected)
  76. tm.assert_attr_equal('freq', idx_diff, expected)
  77. def test_hash_error(self):
  78. index = period_range('20010101', periods=10)
  79. with pytest.raises(TypeError, match=("unhashable type: %r" %
  80. type(index).__name__)):
  81. hash(index)
  82. def test_make_time_series(self):
  83. index = period_range(freq='A', start='1/1/2001', end='12/1/2009')
  84. series = Series(1, index=index)
  85. assert isinstance(series, Series)
  86. def test_shallow_copy_empty(self):
  87. # GH13067
  88. idx = PeriodIndex([], freq='M')
  89. result = idx._shallow_copy()
  90. expected = idx
  91. tm.assert_index_equal(result, expected)
  92. def test_shallow_copy_i8(self):
  93. # GH-24391
  94. pi = period_range("2018-01-01", periods=3, freq="2D")
  95. result = pi._shallow_copy(pi.asi8, freq=pi.freq)
  96. tm.assert_index_equal(result, pi)
  97. def test_shallow_copy_changing_freq_raises(self):
  98. pi = period_range("2018-01-01", periods=3, freq="2D")
  99. with pytest.raises(IncompatibleFrequency, match="are different"):
  100. pi._shallow_copy(pi, freq="H")
  101. def test_dtype_str(self):
  102. pi = pd.PeriodIndex([], freq='M')
  103. assert pi.dtype_str == 'period[M]'
  104. assert pi.dtype_str == str(pi.dtype)
  105. pi = pd.PeriodIndex([], freq='3M')
  106. assert pi.dtype_str == 'period[3M]'
  107. assert pi.dtype_str == str(pi.dtype)
  108. def test_view_asi8(self):
  109. idx = pd.PeriodIndex([], freq='M')
  110. exp = np.array([], dtype=np.int64)
  111. tm.assert_numpy_array_equal(idx.view('i8'), exp)
  112. tm.assert_numpy_array_equal(idx.asi8, exp)
  113. idx = pd.PeriodIndex(['2011-01', pd.NaT], freq='M')
  114. exp = np.array([492, -9223372036854775808], dtype=np.int64)
  115. tm.assert_numpy_array_equal(idx.view('i8'), exp)
  116. tm.assert_numpy_array_equal(idx.asi8, exp)
  117. exp = np.array([14975, -9223372036854775808], dtype=np.int64)
  118. idx = pd.PeriodIndex(['2011-01-01', pd.NaT], freq='D')
  119. tm.assert_numpy_array_equal(idx.view('i8'), exp)
  120. tm.assert_numpy_array_equal(idx.asi8, exp)
  121. def test_values(self):
  122. idx = pd.PeriodIndex([], freq='M')
  123. exp = np.array([], dtype=np.object)
  124. tm.assert_numpy_array_equal(idx.values, exp)
  125. tm.assert_numpy_array_equal(idx.get_values(), exp)
  126. exp = np.array([], dtype=np.int64)
  127. tm.assert_numpy_array_equal(idx._ndarray_values, exp)
  128. idx = pd.PeriodIndex(['2011-01', pd.NaT], freq='M')
  129. exp = np.array([pd.Period('2011-01', freq='M'), pd.NaT], dtype=object)
  130. tm.assert_numpy_array_equal(idx.values, exp)
  131. tm.assert_numpy_array_equal(idx.get_values(), exp)
  132. exp = np.array([492, -9223372036854775808], dtype=np.int64)
  133. tm.assert_numpy_array_equal(idx._ndarray_values, exp)
  134. idx = pd.PeriodIndex(['2011-01-01', pd.NaT], freq='D')
  135. exp = np.array([pd.Period('2011-01-01', freq='D'), pd.NaT],
  136. dtype=object)
  137. tm.assert_numpy_array_equal(idx.values, exp)
  138. tm.assert_numpy_array_equal(idx.get_values(), exp)
  139. exp = np.array([14975, -9223372036854775808], dtype=np.int64)
  140. tm.assert_numpy_array_equal(idx._ndarray_values, exp)
  141. def test_period_index_length(self):
  142. pi = period_range(freq='A', start='1/1/2001', end='12/1/2009')
  143. assert len(pi) == 9
  144. pi = period_range(freq='Q', start='1/1/2001', end='12/1/2009')
  145. assert len(pi) == 4 * 9
  146. pi = period_range(freq='M', start='1/1/2001', end='12/1/2009')
  147. assert len(pi) == 12 * 9
  148. start = Period('02-Apr-2005', 'B')
  149. i1 = period_range(start=start, periods=20)
  150. assert len(i1) == 20
  151. assert i1.freq == start.freq
  152. assert i1[0] == start
  153. end_intv = Period('2006-12-31', 'W')
  154. i1 = period_range(end=end_intv, periods=10)
  155. assert len(i1) == 10
  156. assert i1.freq == end_intv.freq
  157. assert i1[-1] == end_intv
  158. end_intv = Period('2006-12-31', '1w')
  159. i2 = period_range(end=end_intv, periods=10)
  160. assert len(i1) == len(i2)
  161. assert (i1 == i2).all()
  162. assert i1.freq == i2.freq
  163. end_intv = Period('2006-12-31', ('w', 1))
  164. i2 = period_range(end=end_intv, periods=10)
  165. assert len(i1) == len(i2)
  166. assert (i1 == i2).all()
  167. assert i1.freq == i2.freq
  168. try:
  169. period_range(start=start, end=end_intv)
  170. raise AssertionError('Cannot allow mixed freq for start and end')
  171. except ValueError:
  172. pass
  173. end_intv = Period('2005-05-01', 'B')
  174. i1 = period_range(start=start, end=end_intv)
  175. try:
  176. period_range(start=start)
  177. raise AssertionError(
  178. 'Must specify periods if missing start or end')
  179. except ValueError:
  180. pass
  181. # infer freq from first element
  182. i2 = PeriodIndex([end_intv, Period('2005-05-05', 'B')])
  183. assert len(i2) == 2
  184. assert i2[0] == end_intv
  185. i2 = PeriodIndex(np.array([end_intv, Period('2005-05-05', 'B')]))
  186. assert len(i2) == 2
  187. assert i2[0] == end_intv
  188. # Mixed freq should fail
  189. vals = [end_intv, Period('2006-12-31', 'w')]
  190. pytest.raises(ValueError, PeriodIndex, vals)
  191. vals = np.array(vals)
  192. pytest.raises(ValueError, PeriodIndex, vals)
  193. def test_fields(self):
  194. # year, month, day, hour, minute
  195. # second, weekofyear, week, dayofweek, weekday, dayofyear, quarter
  196. # qyear
  197. pi = period_range(freq='A', start='1/1/2001', end='12/1/2005')
  198. self._check_all_fields(pi)
  199. pi = period_range(freq='Q', start='1/1/2001', end='12/1/2002')
  200. self._check_all_fields(pi)
  201. pi = period_range(freq='M', start='1/1/2001', end='1/1/2002')
  202. self._check_all_fields(pi)
  203. pi = period_range(freq='D', start='12/1/2001', end='6/1/2001')
  204. self._check_all_fields(pi)
  205. pi = period_range(freq='B', start='12/1/2001', end='6/1/2001')
  206. self._check_all_fields(pi)
  207. pi = period_range(freq='H', start='12/31/2001', end='1/1/2002 23:00')
  208. self._check_all_fields(pi)
  209. pi = period_range(freq='Min', start='12/31/2001', end='1/1/2002 00:20')
  210. self._check_all_fields(pi)
  211. pi = period_range(freq='S', start='12/31/2001 00:00:00',
  212. end='12/31/2001 00:05:00')
  213. self._check_all_fields(pi)
  214. end_intv = Period('2006-12-31', 'W')
  215. i1 = period_range(end=end_intv, periods=10)
  216. self._check_all_fields(i1)
  217. def _check_all_fields(self, periodindex):
  218. fields = ['year', 'month', 'day', 'hour', 'minute', 'second',
  219. 'weekofyear', 'week', 'dayofweek', 'dayofyear',
  220. 'quarter', 'qyear', 'days_in_month']
  221. periods = list(periodindex)
  222. s = pd.Series(periodindex)
  223. for field in fields:
  224. field_idx = getattr(periodindex, field)
  225. assert len(periodindex) == len(field_idx)
  226. for x, val in zip(periods, field_idx):
  227. assert getattr(x, field) == val
  228. if len(s) == 0:
  229. continue
  230. field_s = getattr(s.dt, field)
  231. assert len(periodindex) == len(field_s)
  232. for x, val in zip(periods, field_s):
  233. assert getattr(x, field) == val
  234. def test_period_set_index_reindex(self):
  235. # GH 6631
  236. df = DataFrame(np.random.random(6))
  237. idx1 = period_range('2011/01/01', periods=6, freq='M')
  238. idx2 = period_range('2013', periods=6, freq='A')
  239. df = df.set_index(idx1)
  240. tm.assert_index_equal(df.index, idx1)
  241. df = df.set_index(idx2)
  242. tm.assert_index_equal(df.index, idx2)
  243. def test_factorize(self):
  244. idx1 = PeriodIndex(['2014-01', '2014-01', '2014-02', '2014-02',
  245. '2014-03', '2014-03'], freq='M')
  246. exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp)
  247. exp_idx = PeriodIndex(['2014-01', '2014-02', '2014-03'], freq='M')
  248. arr, idx = idx1.factorize()
  249. tm.assert_numpy_array_equal(arr, exp_arr)
  250. tm.assert_index_equal(idx, exp_idx)
  251. arr, idx = idx1.factorize(sort=True)
  252. tm.assert_numpy_array_equal(arr, exp_arr)
  253. tm.assert_index_equal(idx, exp_idx)
  254. idx2 = pd.PeriodIndex(['2014-03', '2014-03', '2014-02', '2014-01',
  255. '2014-03', '2014-01'], freq='M')
  256. exp_arr = np.array([2, 2, 1, 0, 2, 0], dtype=np.intp)
  257. arr, idx = idx2.factorize(sort=True)
  258. tm.assert_numpy_array_equal(arr, exp_arr)
  259. tm.assert_index_equal(idx, exp_idx)
  260. exp_arr = np.array([0, 0, 1, 2, 0, 2], dtype=np.intp)
  261. exp_idx = PeriodIndex(['2014-03', '2014-02', '2014-01'], freq='M')
  262. arr, idx = idx2.factorize()
  263. tm.assert_numpy_array_equal(arr, exp_arr)
  264. tm.assert_index_equal(idx, exp_idx)
  265. def test_is_(self):
  266. create_index = lambda: period_range(freq='A', start='1/1/2001',
  267. end='12/1/2009')
  268. index = create_index()
  269. assert index.is_(index)
  270. assert not index.is_(create_index())
  271. assert index.is_(index.view())
  272. assert index.is_(index.view().view().view().view().view())
  273. assert index.view().is_(index)
  274. ind2 = index.view()
  275. index.name = "Apple"
  276. assert ind2.is_(index)
  277. assert not index.is_(index[:])
  278. assert not index.is_(index.asfreq('M'))
  279. assert not index.is_(index.asfreq('A'))
  280. assert not index.is_(index - 2)
  281. assert not index.is_(index - 0)
  282. def test_contains(self):
  283. rng = period_range('2007-01', freq='M', periods=10)
  284. assert Period('2007-01', freq='M') in rng
  285. assert not Period('2007-01', freq='D') in rng
  286. assert not Period('2007-01', freq='2M') in rng
  287. def test_contains_nat(self):
  288. # see gh-13582
  289. idx = period_range('2007-01', freq='M', periods=10)
  290. assert pd.NaT not in idx
  291. assert None not in idx
  292. assert float('nan') not in idx
  293. assert np.nan not in idx
  294. idx = pd.PeriodIndex(['2011-01', 'NaT', '2011-02'], freq='M')
  295. assert pd.NaT in idx
  296. assert None in idx
  297. assert float('nan') in idx
  298. assert np.nan in idx
  299. def test_periods_number_check(self):
  300. with pytest.raises(ValueError):
  301. period_range('2011-1-1', '2012-1-1', 'B')
  302. def test_start_time(self):
  303. # GH 17157
  304. index = period_range(freq='M', start='2016-01-01', end='2016-05-31')
  305. expected_index = date_range('2016-01-01', end='2016-05-31', freq='MS')
  306. tm.assert_index_equal(index.start_time, expected_index)
  307. def test_end_time(self):
  308. # GH 17157
  309. index = period_range(freq='M', start='2016-01-01', end='2016-05-31')
  310. expected_index = date_range('2016-01-01', end='2016-05-31', freq='M')
  311. expected_index = expected_index.shift(1, freq='D').shift(-1, freq='ns')
  312. tm.assert_index_equal(index.end_time, expected_index)
  313. def test_index_duplicate_periods(self):
  314. # monotonic
  315. idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq='A-JUN')
  316. ts = Series(np.random.randn(len(idx)), index=idx)
  317. result = ts[2007]
  318. expected = ts[1:3]
  319. tm.assert_series_equal(result, expected)
  320. result[:] = 1
  321. assert (ts[1:3] == 1).all()
  322. # not monotonic
  323. idx = PeriodIndex([2000, 2007, 2007, 2009, 2007], freq='A-JUN')
  324. ts = Series(np.random.randn(len(idx)), index=idx)
  325. result = ts[2007]
  326. expected = ts[idx == 2007]
  327. tm.assert_series_equal(result, expected)
  328. def test_index_unique(self):
  329. idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq='A-JUN')
  330. expected = PeriodIndex([2000, 2007, 2009], freq='A-JUN')
  331. tm.assert_index_equal(idx.unique(), expected)
  332. assert idx.nunique() == 3
  333. idx = PeriodIndex([2000, 2007, 2007, 2009, 2007], freq='A-JUN',
  334. tz='US/Eastern')
  335. expected = PeriodIndex([2000, 2007, 2009], freq='A-JUN',
  336. tz='US/Eastern')
  337. tm.assert_index_equal(idx.unique(), expected)
  338. assert idx.nunique() == 3
  339. def test_shift(self):
  340. # This is tested in test_arithmetic
  341. pass
  342. @td.skip_if_32bit
  343. def test_ndarray_compat_properties(self):
  344. super(TestPeriodIndex, self).test_ndarray_compat_properties()
  345. def test_negative_ordinals(self):
  346. Period(ordinal=-1000, freq='A')
  347. Period(ordinal=0, freq='A')
  348. idx1 = PeriodIndex(ordinal=[-1, 0, 1], freq='A')
  349. idx2 = PeriodIndex(ordinal=np.array([-1, 0, 1]), freq='A')
  350. tm.assert_index_equal(idx1, idx2)
  351. def test_pindex_fieldaccessor_nat(self):
  352. idx = PeriodIndex(['2011-01', '2011-02', 'NaT',
  353. '2012-03', '2012-04'], freq='D', name='name')
  354. exp = Index([2011, 2011, -1, 2012, 2012], dtype=np.int64, name='name')
  355. tm.assert_index_equal(idx.year, exp)
  356. exp = Index([1, 2, -1, 3, 4], dtype=np.int64, name='name')
  357. tm.assert_index_equal(idx.month, exp)
  358. def test_pindex_qaccess(self):
  359. pi = PeriodIndex(['2Q05', '3Q05', '4Q05', '1Q06', '2Q06'], freq='Q')
  360. s = Series(np.random.rand(len(pi)), index=pi).cumsum()
  361. # Todo: fix these accessors!
  362. assert s['05Q4'] == s[2]
  363. def test_pindex_multiples(self):
  364. with tm.assert_produces_warning(FutureWarning):
  365. pi = PeriodIndex(start='1/1/11', end='12/31/11', freq='2M')
  366. expected = PeriodIndex(['2011-01', '2011-03', '2011-05', '2011-07',
  367. '2011-09', '2011-11'], freq='2M')
  368. tm.assert_index_equal(pi, expected)
  369. assert pi.freq == offsets.MonthEnd(2)
  370. assert pi.freqstr == '2M'
  371. pi = period_range(start='1/1/11', end='12/31/11', freq='2M')
  372. tm.assert_index_equal(pi, expected)
  373. assert pi.freq == offsets.MonthEnd(2)
  374. assert pi.freqstr == '2M'
  375. pi = period_range(start='1/1/11', periods=6, freq='2M')
  376. tm.assert_index_equal(pi, expected)
  377. assert pi.freq == offsets.MonthEnd(2)
  378. assert pi.freqstr == '2M'
  379. def test_iteration(self):
  380. index = period_range(start='1/1/10', periods=4, freq='B')
  381. result = list(index)
  382. assert isinstance(result[0], Period)
  383. assert result[0].freq == index.freq
  384. def test_is_full(self):
  385. index = PeriodIndex([2005, 2007, 2009], freq='A')
  386. assert not index.is_full
  387. index = PeriodIndex([2005, 2006, 2007], freq='A')
  388. assert index.is_full
  389. index = PeriodIndex([2005, 2005, 2007], freq='A')
  390. assert not index.is_full
  391. index = PeriodIndex([2005, 2005, 2006], freq='A')
  392. assert index.is_full
  393. index = PeriodIndex([2006, 2005, 2005], freq='A')
  394. pytest.raises(ValueError, getattr, index, 'is_full')
  395. assert index[:0].is_full
  396. def test_with_multi_index(self):
  397. # #1705
  398. index = date_range('1/1/2012', periods=4, freq='12H')
  399. index_as_arrays = [index.to_period(freq='D'), index.hour]
  400. s = Series([0, 1, 2, 3], index_as_arrays)
  401. assert isinstance(s.index.levels[0], PeriodIndex)
  402. assert isinstance(s.index.values[0][0], Period)
  403. def test_convert_array_of_periods(self):
  404. rng = period_range('1/1/2000', periods=20, freq='D')
  405. periods = list(rng)
  406. result = pd.Index(periods)
  407. assert isinstance(result, PeriodIndex)
  408. def test_append_concat(self):
  409. # #1815
  410. d1 = date_range('12/31/1990', '12/31/1999', freq='A-DEC')
  411. d2 = date_range('12/31/2000', '12/31/2009', freq='A-DEC')
  412. s1 = Series(np.random.randn(10), d1)
  413. s2 = Series(np.random.randn(10), d2)
  414. s1 = s1.to_period()
  415. s2 = s2.to_period()
  416. # drops index
  417. result = pd.concat([s1, s2])
  418. assert isinstance(result.index, PeriodIndex)
  419. assert result.index[0] == s1.index[0]
  420. def test_pickle_freq(self):
  421. # GH2891
  422. prng = period_range('1/1/2011', '1/1/2012', freq='M')
  423. new_prng = tm.round_trip_pickle(prng)
  424. assert new_prng.freq == offsets.MonthEnd()
  425. assert new_prng.freqstr == 'M'
  426. def test_map(self):
  427. # test_map_dictlike generally tests
  428. index = PeriodIndex([2005, 2007, 2009], freq='A')
  429. result = index.map(lambda x: x.ordinal)
  430. exp = Index([x.ordinal for x in index])
  431. tm.assert_index_equal(result, exp)
  432. def test_join_self(self, join_type):
  433. index = period_range('1/1/2000', periods=10)
  434. joined = index.join(index, how=join_type)
  435. assert index is joined
  436. def test_insert(self):
  437. # GH 18295 (test missing)
  438. expected = PeriodIndex(
  439. ['2017Q1', pd.NaT, '2017Q2', '2017Q3', '2017Q4'], freq='Q')
  440. for na in (np.nan, pd.NaT, None):
  441. result = period_range('2017Q1', periods=4, freq='Q').insert(1, na)
  442. tm.assert_index_equal(result, expected)
  443. def test_maybe_convert_timedelta():
  444. pi = PeriodIndex(['2000', '2001'], freq='D')
  445. offset = offsets.Day(2)
  446. assert pi._maybe_convert_timedelta(offset) == 2
  447. assert pi._maybe_convert_timedelta(2) == 2
  448. offset = offsets.BusinessDay()
  449. with pytest.raises(ValueError, match='freq'):
  450. pi._maybe_convert_timedelta(offset)