|
6 | 6 | import pandas as pd
|
7 | 7 | from pandas.util import testing as tm
|
8 | 8 | from pandas.compat import lrange
|
9 |
| -from pandas._libs import tslib |
| 9 | +from pandas._libs import tslib, tslibs |
10 | 10 | from pandas import (PeriodIndex, Series, DatetimeIndex,
|
11 | 11 | period_range, Period)
|
12 | 12 |
|
@@ -310,3 +310,197 @@ def test_take_fill_value(self):
|
310 | 310 |
|
311 | 311 | with pytest.raises(IndexError):
|
312 | 312 | idx.take(np.array([1, -5]))
|
| 313 | + |
| 314 | + def test_get_loc(self): |
| 315 | + # GH 17717 |
| 316 | + p0 = pd.Period('2017-09-01') |
| 317 | + p1 = pd.Period('2017-09-02') |
| 318 | + p2 = pd.Period('2017-09-03') |
| 319 | + |
| 320 | + # get the location of p1/p2 from |
| 321 | + # monotonic increasing PeriodIndex with non-duplicate |
| 322 | + idx0 = pd.PeriodIndex([p0, p1, p2]) |
| 323 | + expected_idx1_p1 = 1 |
| 324 | + expected_idx1_p2 = 2 |
| 325 | + |
| 326 | + assert idx0.get_loc(p1) == expected_idx1_p1 |
| 327 | + assert idx0.get_loc(str(p1)) == expected_idx1_p1 |
| 328 | + assert idx0.get_loc(p2) == expected_idx1_p2 |
| 329 | + assert idx0.get_loc(str(p2)) == expected_idx1_p2 |
| 330 | + |
| 331 | + pytest.raises(tslibs.parsing.DateParseError, idx0.get_loc, 'foo') |
| 332 | + pytest.raises(KeyError, idx0.get_loc, 1.1) |
| 333 | + pytest.raises(TypeError, idx0.get_loc, idx0) |
| 334 | + |
| 335 | + # get the location of p1/p2 from |
| 336 | + # monotonic increasing PeriodIndex with duplicate |
| 337 | + idx1 = pd.PeriodIndex([p1, p1, p2]) |
| 338 | + expected_idx1_p1 = slice(0, 2) |
| 339 | + expected_idx1_p2 = 2 |
| 340 | + |
| 341 | + assert idx1.get_loc(p1) == expected_idx1_p1 |
| 342 | + assert idx1.get_loc(str(p1)) == expected_idx1_p1 |
| 343 | + assert idx1.get_loc(p2) == expected_idx1_p2 |
| 344 | + assert idx1.get_loc(str(p2)) == expected_idx1_p2 |
| 345 | + |
| 346 | + pytest.raises(tslibs.parsing.DateParseError, idx1.get_loc, 'foo') |
| 347 | + pytest.raises(KeyError, idx1.get_loc, 1.1) |
| 348 | + pytest.raises(TypeError, idx1.get_loc, idx1) |
| 349 | + |
| 350 | + # get the location of p1/p2 from |
| 351 | + # non-monotonic increasing/decreasing PeriodIndex with duplicate |
| 352 | + idx2 = pd.PeriodIndex([p2, p1, p2]) |
| 353 | + expected_idx2_p1 = 1 |
| 354 | + expected_idx2_p2 = np.array([True, False, True]) |
| 355 | + |
| 356 | + assert idx2.get_loc(p1) == expected_idx2_p1 |
| 357 | + assert idx2.get_loc(str(p1)) == expected_idx2_p1 |
| 358 | + tm.assert_numpy_array_equal(idx2.get_loc(p2), expected_idx2_p2) |
| 359 | + tm.assert_numpy_array_equal(idx2.get_loc(str(p2)), expected_idx2_p2) |
| 360 | + |
| 361 | + def test_is_monotonic_increasing(self): |
| 362 | + # GH 17717 |
| 363 | + p0 = pd.Period('2017-09-01') |
| 364 | + p1 = pd.Period('2017-09-02') |
| 365 | + p2 = pd.Period('2017-09-03') |
| 366 | + |
| 367 | + idx_inc0 = pd.PeriodIndex([p0, p1, p2]) |
| 368 | + idx_inc1 = pd.PeriodIndex([p0, p1, p1]) |
| 369 | + idx_dec0 = pd.PeriodIndex([p2, p1, p0]) |
| 370 | + idx_dec1 = pd.PeriodIndex([p2, p1, p1]) |
| 371 | + idx = pd.PeriodIndex([p1, p2, p0]) |
| 372 | + |
| 373 | + assert idx_inc0.is_monotonic_increasing |
| 374 | + assert idx_inc1.is_monotonic_increasing |
| 375 | + assert not idx_dec0.is_monotonic_increasing |
| 376 | + assert not idx_dec1.is_monotonic_increasing |
| 377 | + assert not idx.is_monotonic_increasing |
| 378 | + |
| 379 | + def test_is_monotonic_decreasing(self): |
| 380 | + # GH 17717 |
| 381 | + p0 = pd.Period('2017-09-01') |
| 382 | + p1 = pd.Period('2017-09-02') |
| 383 | + p2 = pd.Period('2017-09-03') |
| 384 | + |
| 385 | + idx_inc0 = pd.PeriodIndex([p0, p1, p2]) |
| 386 | + idx_inc1 = pd.PeriodIndex([p0, p1, p1]) |
| 387 | + idx_dec0 = pd.PeriodIndex([p2, p1, p0]) |
| 388 | + idx_dec1 = pd.PeriodIndex([p2, p1, p1]) |
| 389 | + idx = pd.PeriodIndex([p1, p2, p0]) |
| 390 | + |
| 391 | + assert not idx_inc0.is_monotonic_decreasing |
| 392 | + assert not idx_inc1.is_monotonic_decreasing |
| 393 | + assert idx_dec0.is_monotonic_decreasing |
| 394 | + assert idx_dec1.is_monotonic_decreasing |
| 395 | + assert not idx.is_monotonic_decreasing |
| 396 | + |
| 397 | + def test_is_unique(self): |
| 398 | + # GH 17717 |
| 399 | + p0 = pd.Period('2017-09-01') |
| 400 | + p1 = pd.Period('2017-09-02') |
| 401 | + p2 = pd.Period('2017-09-03') |
| 402 | + |
| 403 | + idx0 = pd.PeriodIndex([p0, p1, p2]) |
| 404 | + assert idx0.is_unique |
| 405 | + |
| 406 | + idx1 = pd.PeriodIndex([p1, p1, p2]) |
| 407 | + assert not idx1.is_unique |
| 408 | + |
| 409 | + def test_contains(self): |
| 410 | + # GH 17717 |
| 411 | + p0 = pd.Period('2017-09-01') |
| 412 | + p1 = pd.Period('2017-09-02') |
| 413 | + p2 = pd.Period('2017-09-03') |
| 414 | + p3 = pd.Period('2017-09-04') |
| 415 | + |
| 416 | + ps0 = [p0, p1, p2] |
| 417 | + idx0 = pd.PeriodIndex(ps0) |
| 418 | + |
| 419 | + for p in ps0: |
| 420 | + assert idx0.contains(p) |
| 421 | + assert p in idx0 |
| 422 | + |
| 423 | + assert idx0.contains(str(p)) |
| 424 | + assert str(p) in idx0 |
| 425 | + |
| 426 | + assert idx0.contains('2017-09-01 00:00:01') |
| 427 | + assert '2017-09-01 00:00:01' in idx0 |
| 428 | + |
| 429 | + assert idx0.contains('2017-09') |
| 430 | + assert '2017-09' in idx0 |
| 431 | + |
| 432 | + assert not idx0.contains(p3) |
| 433 | + assert p3 not in idx0 |
| 434 | + |
| 435 | + def test_get_value(self): |
| 436 | + # GH 17717 |
| 437 | + p0 = pd.Period('2017-09-01') |
| 438 | + p1 = pd.Period('2017-09-02') |
| 439 | + p2 = pd.Period('2017-09-03') |
| 440 | + |
| 441 | + idx0 = pd.PeriodIndex([p0, p1, p2]) |
| 442 | + input0 = np.array([1, 2, 3]) |
| 443 | + expected0 = 2 |
| 444 | + |
| 445 | + result0 = idx0.get_value(input0, p1) |
| 446 | + assert result0 == expected0 |
| 447 | + |
| 448 | + idx1 = pd.PeriodIndex([p1, p1, p2]) |
| 449 | + input1 = np.array([1, 2, 3]) |
| 450 | + expected1 = np.array([1, 2]) |
| 451 | + |
| 452 | + result1 = idx1.get_value(input1, p1) |
| 453 | + tm.assert_numpy_array_equal(result1, expected1) |
| 454 | + |
| 455 | + idx2 = pd.PeriodIndex([p1, p2, p1]) |
| 456 | + input2 = np.array([1, 2, 3]) |
| 457 | + expected2 = np.array([1, 3]) |
| 458 | + |
| 459 | + result2 = idx2.get_value(input2, p1) |
| 460 | + tm.assert_numpy_array_equal(result2, expected2) |
| 461 | + |
| 462 | + def test_get_indexer(self): |
| 463 | + # GH 17717 |
| 464 | + p1 = pd.Period('2017-09-01') |
| 465 | + p2 = pd.Period('2017-09-04') |
| 466 | + p3 = pd.Period('2017-09-07') |
| 467 | + |
| 468 | + tp0 = pd.Period('2017-08-31') |
| 469 | + tp1 = pd.Period('2017-09-02') |
| 470 | + tp2 = pd.Period('2017-09-05') |
| 471 | + tp3 = pd.Period('2017-09-09') |
| 472 | + |
| 473 | + idx = pd.PeriodIndex([p1, p2, p3]) |
| 474 | + |
| 475 | + tm.assert_numpy_array_equal(idx.get_indexer(idx), |
| 476 | + np.array([0, 1, 2], dtype=np.intp)) |
| 477 | + |
| 478 | + target = pd.PeriodIndex([tp0, tp1, tp2, tp3]) |
| 479 | + tm.assert_numpy_array_equal(idx.get_indexer(target, 'pad'), |
| 480 | + np.array([-1, 0, 1, 2], dtype=np.intp)) |
| 481 | + tm.assert_numpy_array_equal(idx.get_indexer(target, 'backfill'), |
| 482 | + np.array([0, 1, 2, -1], dtype=np.intp)) |
| 483 | + tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest'), |
| 484 | + np.array([0, 0, 1, 2], dtype=np.intp)) |
| 485 | + |
| 486 | + res = idx.get_indexer(target, 'nearest', |
| 487 | + tolerance=pd.Timedelta('1 day')) |
| 488 | + tm.assert_numpy_array_equal(res, |
| 489 | + np.array([0, 0, 1, -1], dtype=np.intp)) |
| 490 | + |
| 491 | + def test_get_indexer_non_unique(self): |
| 492 | + # GH 17717 |
| 493 | + p1 = pd.Period('2017-09-02') |
| 494 | + p2 = pd.Period('2017-09-03') |
| 495 | + p3 = pd.Period('2017-09-04') |
| 496 | + p4 = pd.Period('2017-09-05') |
| 497 | + |
| 498 | + idx1 = pd.PeriodIndex([p1, p2, p1]) |
| 499 | + idx2 = pd.PeriodIndex([p2, p1, p3, p4]) |
| 500 | + |
| 501 | + result = idx1.get_indexer_non_unique(idx2) |
| 502 | + expected_indexer = np.array([1, 0, 2, -1, -1], dtype=np.int64) |
| 503 | + expected_missing = np.array([2, 3], dtype=np.int64) |
| 504 | + |
| 505 | + tm.assert_numpy_array_equal(result[0], expected_indexer) |
| 506 | + tm.assert_numpy_array_equal(result[1], expected_missing) |
0 commit comments