|
21 | 21 | JOIN_TYPES = ['inner', 'outer', 'left', 'right'] |
22 | 22 |
|
23 | 23 |
|
24 | | -def test_align(test_data): |
25 | | - def _check_align(a, b, how='left', fill=None): |
26 | | - aa, ab = a.align(b, join=how, fill_value=fill) |
27 | | - |
28 | | - join_index = a.index.join(b.index, how=how) |
29 | | - if fill is not None: |
30 | | - diff_a = aa.index.difference(join_index) |
31 | | - diff_b = ab.index.difference(join_index) |
32 | | - if len(diff_a) > 0: |
33 | | - assert (aa.reindex(diff_a) == fill).all() |
34 | | - if len(diff_b) > 0: |
35 | | - assert (ab.reindex(diff_b) == fill).all() |
36 | | - |
37 | | - ea = a.reindex(join_index) |
38 | | - eb = b.reindex(join_index) |
39 | | - |
40 | | - if fill is not None: |
41 | | - ea = ea.fillna(fill) |
42 | | - eb = eb.fillna(fill) |
43 | | - |
44 | | - assert_series_equal(aa, ea) |
45 | | - assert_series_equal(ab, eb) |
46 | | - assert aa.name == 'ts' |
47 | | - assert ea.name == 'ts' |
48 | | - assert ab.name == 'ts' |
49 | | - assert eb.name == 'ts' |
50 | | - |
51 | | - for kind in JOIN_TYPES: |
52 | | - _check_align(test_data.ts[2:], test_data.ts[:-5], how=kind) |
53 | | - _check_align(test_data.ts[2:], test_data.ts[:-5], how=kind, fill=-1) |
54 | | - |
55 | | - # empty left |
56 | | - _check_align(test_data.ts[:0], test_data.ts[:-5], how=kind) |
57 | | - _check_align(test_data.ts[:0], test_data.ts[:-5], how=kind, fill=-1) |
58 | | - |
59 | | - # empty right |
60 | | - _check_align(test_data.ts[:-5], test_data.ts[:0], how=kind) |
61 | | - _check_align(test_data.ts[:-5], test_data.ts[:0], how=kind, fill=-1) |
62 | | - |
63 | | - # both empty |
64 | | - _check_align(test_data.ts[:0], test_data.ts[:0], how=kind) |
65 | | - _check_align(test_data.ts[:0], test_data.ts[:0], how=kind, fill=-1) |
66 | | - |
67 | | - |
68 | | -def test_align_fill_method(test_data): |
69 | | - def _check_align(a, b, how='left', method='pad', limit=None): |
70 | | - aa, ab = a.align(b, join=how, method=method, limit=limit) |
71 | | - |
72 | | - join_index = a.index.join(b.index, how=how) |
73 | | - ea = a.reindex(join_index) |
74 | | - eb = b.reindex(join_index) |
75 | | - |
76 | | - ea = ea.fillna(method=method, limit=limit) |
77 | | - eb = eb.fillna(method=method, limit=limit) |
78 | | - |
79 | | - assert_series_equal(aa, ea) |
80 | | - assert_series_equal(ab, eb) |
81 | | - |
82 | | - for kind in JOIN_TYPES: |
83 | | - for meth in ['pad', 'bfill']: |
84 | | - _check_align(test_data.ts[2:], test_data.ts[:-5], |
85 | | - how=kind, method=meth) |
86 | | - _check_align(test_data.ts[2:], test_data.ts[:-5], |
87 | | - how=kind, method=meth, limit=1) |
88 | | - |
89 | | - # empty left |
90 | | - _check_align(test_data.ts[:0], test_data.ts[:-5], |
91 | | - how=kind, method=meth) |
92 | | - _check_align(test_data.ts[:0], test_data.ts[:-5], |
93 | | - how=kind, method=meth, limit=1) |
94 | | - |
95 | | - # empty right |
96 | | - _check_align(test_data.ts[:-5], test_data.ts[:0], |
97 | | - how=kind, method=meth) |
98 | | - _check_align(test_data.ts[:-5], test_data.ts[:0], |
99 | | - how=kind, method=meth, limit=1) |
100 | | - |
101 | | - # both empty |
102 | | - _check_align(test_data.ts[:0], test_data.ts[:0], |
103 | | - how=kind, method=meth) |
104 | | - _check_align(test_data.ts[:0], test_data.ts[:0], |
105 | | - how=kind, method=meth, limit=1) |
| 24 | +@pytest.mark.parametrize( |
| 25 | + 'first_slice,second_slice', [ |
| 26 | + [[2, None], [None, -5]], |
| 27 | + [[None, 0], [None, -5]], |
| 28 | + [[None, -5], [None, 0]], |
| 29 | + [[None, 0], [None, 0]] |
| 30 | + ]) |
| 31 | +@pytest.mark.parametrize('join_type', JOIN_TYPES) |
| 32 | +@pytest.mark.parametrize('fill', [None, -1]) |
| 33 | +def test_align(test_data, first_slice, second_slice, join_type, fill): |
| 34 | + a = test_data.ts[slice(*first_slice)] |
| 35 | + b = test_data.ts[slice(*second_slice)] |
| 36 | + |
| 37 | + aa, ab = a.align(b, join=join_type, fill_value=fill) |
| 38 | + |
| 39 | + join_index = a.index.join(b.index, how=join_type) |
| 40 | + if fill is not None: |
| 41 | + diff_a = aa.index.difference(join_index) |
| 42 | + diff_b = ab.index.difference(join_index) |
| 43 | + if len(diff_a) > 0: |
| 44 | + assert (aa.reindex(diff_a) == fill).all() |
| 45 | + if len(diff_b) > 0: |
| 46 | + assert (ab.reindex(diff_b) == fill).all() |
| 47 | + |
| 48 | + ea = a.reindex(join_index) |
| 49 | + eb = b.reindex(join_index) |
| 50 | + |
| 51 | + if fill is not None: |
| 52 | + ea = ea.fillna(fill) |
| 53 | + eb = eb.fillna(fill) |
| 54 | + |
| 55 | + assert_series_equal(aa, ea) |
| 56 | + assert_series_equal(ab, eb) |
| 57 | + assert aa.name == 'ts' |
| 58 | + assert ea.name == 'ts' |
| 59 | + assert ab.name == 'ts' |
| 60 | + assert eb.name == 'ts' |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize( |
| 64 | + 'first_slice,second_slice', [ |
| 65 | + [[2, None], [None, -5]], |
| 66 | + [[None, 0], [None, -5]], |
| 67 | + [[None, -5], [None, 0]], |
| 68 | + [[None, 0], [None, 0]] |
| 69 | + ]) |
| 70 | +@pytest.mark.parametrize('join_type', JOIN_TYPES) |
| 71 | +@pytest.mark.parametrize('method', ['pad', 'bfill']) |
| 72 | +@pytest.mark.parametrize('limit', [None, 1]) |
| 73 | +def test_align_fill_method(test_data, |
| 74 | + first_slice, second_slice, |
| 75 | + join_type, method, limit): |
| 76 | + a = test_data.ts[slice(*first_slice)] |
| 77 | + b = test_data.ts[slice(*second_slice)] |
| 78 | + |
| 79 | + aa, ab = a.align(b, join=join_type, method=method, limit=limit) |
| 80 | + |
| 81 | + join_index = a.index.join(b.index, how=join_type) |
| 82 | + ea = a.reindex(join_index) |
| 83 | + eb = b.reindex(join_index) |
| 84 | + |
| 85 | + ea = ea.fillna(method=method, limit=limit) |
| 86 | + eb = eb.fillna(method=method, limit=limit) |
| 87 | + |
| 88 | + assert_series_equal(aa, ea) |
| 89 | + assert_series_equal(ab, eb) |
106 | 90 |
|
107 | 91 |
|
108 | 92 | def test_align_nocopy(test_data): |
@@ -481,3 +465,56 @@ def test_rename(): |
481 | 465 | assert_series_equal(result, expected) |
482 | 466 |
|
483 | 467 | assert result.name == expected.name |
| 468 | + |
| 469 | + |
| 470 | +def test_drop(): |
| 471 | + # unique |
| 472 | + s = Series([1, 2], index=['one', 'two']) |
| 473 | + expected = Series([1], index=['one']) |
| 474 | + result = s.drop(['two']) |
| 475 | + assert_series_equal(result, expected) |
| 476 | + result = s.drop('two', axis='rows') |
| 477 | + assert_series_equal(result, expected) |
| 478 | + |
| 479 | + # non-unique |
| 480 | + # GH 5248 |
| 481 | + s = Series([1, 1, 2], index=['one', 'two', 'one']) |
| 482 | + expected = Series([1, 2], index=['one', 'one']) |
| 483 | + result = s.drop(['two'], axis=0) |
| 484 | + assert_series_equal(result, expected) |
| 485 | + result = s.drop('two') |
| 486 | + assert_series_equal(result, expected) |
| 487 | + |
| 488 | + expected = Series([1], index=['two']) |
| 489 | + result = s.drop(['one']) |
| 490 | + assert_series_equal(result, expected) |
| 491 | + result = s.drop('one') |
| 492 | + assert_series_equal(result, expected) |
| 493 | + |
| 494 | + # single string/tuple-like |
| 495 | + s = Series(range(3), index=list('abc')) |
| 496 | + pytest.raises(KeyError, s.drop, 'bc') |
| 497 | + pytest.raises(KeyError, s.drop, ('a',)) |
| 498 | + |
| 499 | + # errors='ignore' |
| 500 | + s = Series(range(3), index=list('abc')) |
| 501 | + result = s.drop('bc', errors='ignore') |
| 502 | + assert_series_equal(result, s) |
| 503 | + result = s.drop(['a', 'd'], errors='ignore') |
| 504 | + expected = s.iloc[1:] |
| 505 | + assert_series_equal(result, expected) |
| 506 | + |
| 507 | + # bad axis |
| 508 | + pytest.raises(ValueError, s.drop, 'one', axis='columns') |
| 509 | + |
| 510 | + # GH 8522 |
| 511 | + s = Series([2, 3], index=[True, False]) |
| 512 | + assert s.index.is_object() |
| 513 | + result = s.drop(True) |
| 514 | + expected = Series([3], index=[False]) |
| 515 | + assert_series_equal(result, expected) |
| 516 | + |
| 517 | + # GH 16877 |
| 518 | + s = Series([2, 3], index=[0, 1]) |
| 519 | + with tm.assert_raises_regex(KeyError, 'not contained in axis'): |
| 520 | + s.drop([False, True]) |
0 commit comments