|
| 1 | +# DO NOT MERGE _test_get_groups.py WITH ACTUAL PANDAS |
| 2 | +# DO NOT MERGE _test_get_groups.py WITH ACTUAL PANDAS |
| 3 | +# DO NOT MERGE _test_get_groups.py WITH ACTUAL PANDAS |
| 4 | + |
| 5 | +import unittest |
| 6 | + |
| 7 | +from pandas import DataFrame |
| 8 | +from pandas.testing import assert_frame_equal |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +class _test_get_groups(unittest.TestCase): |
| 12 | + def test_discovered_invalid(self): |
| 13 | + df = DataFrame(data={ |
| 14 | + 'A': ['a1', 'a2', None], |
| 15 | + 'B': ['b1', 'b2', 'b1'], |
| 16 | + 'val': [1, 2, 3], |
| 17 | + }) |
| 18 | + grps = df.groupby(by=['A', 'B']) |
| 19 | + self.assertRaises(KeyError, grps.get_group, ('a1', 'b2')) |
| 20 | + |
| 21 | + def test_single_row_valid(self): |
| 22 | + df = DataFrame(data={ |
| 23 | + 'A': ['a1', 'a2', None], |
| 24 | + 'B': ['b1', 'b2', 'b1'], |
| 25 | + 'val': [1, 2, 3], |
| 26 | + }) |
| 27 | + expected = DataFrame(data={ |
| 28 | + 'A': ['a1'], |
| 29 | + 'B': ['b1'], |
| 30 | + 'val': [1,], |
| 31 | + }) |
| 32 | + grps = df.groupby(by=['A', 'B']) |
| 33 | + assert_frame_equal(grps.get_group(('a1', 'b1')), expected) |
| 34 | + |
| 35 | + def test_alternate_invalid(self): |
| 36 | + df = DataFrame(data={ |
| 37 | + 'A': ['a1', 'a2', None], |
| 38 | + 'B': ['b1', 'b2', 'b1'], |
| 39 | + 'val': [1, 2, 3], |
| 40 | + }) |
| 41 | + grps = df.groupby(by=['A', 'B']) |
| 42 | + self.assertRaises(KeyError, grps.get_group, ('a2', 'b1')) |
| 43 | + |
| 44 | + def test_double_row_valid(self): |
| 45 | + df = DataFrame(data={ |
| 46 | + 'A': ['a1', 'a2', None], |
| 47 | + 'B': ['b1', 'b2', 'b1'], |
| 48 | + 'val': [1, 2, 3], |
| 49 | + }) |
| 50 | + expected = DataFrame(data={ |
| 51 | + 'A': ['a1', None], |
| 52 | + 'B': ['b1', 'b1'], |
| 53 | + 'val': [1, 3], |
| 54 | + }) |
| 55 | + grps = df.groupby(by=['B']) |
| 56 | + self.assertEqual(True, np.array_equal(grps.get_group(('b1')).values, expected.values)) |
| 57 | + |
| 58 | + def test_expect_empty(self): |
| 59 | + df = DataFrame(data={ |
| 60 | + 'A': ['a1', 'a2', None], |
| 61 | + 'B': ['b1', 'b2', 'b1'], |
| 62 | + 'val': [1, 2, 3], |
| 63 | + }) |
| 64 | + grps = df.groupby(by=['B']) |
| 65 | + self.assertRaises(KeyError, grps.get_group, ('b3')) |
| 66 | + |
| 67 | + def test_double_row_valid(self): |
| 68 | + df = DataFrame({'Animal': ['Falcon', 'Falcon', |
| 69 | + 'Parrot', 'Parrot'], |
| 70 | + 'Max Speed': [380., 370., 24., 26.], |
| 71 | + 'bullshit': ['a', 'b', 'c', 'd']}) |
| 72 | + expected = DataFrame({'Animal': ['Falcon', 'Falcon'], |
| 73 | + 'Max Speed': [380., 370.], |
| 74 | + 'bullshit': ['a', 'b']}) |
| 75 | + grps = df.groupby(['Animal']) |
| 76 | + assert_frame_equal(grps.get_group(('Falcon')), expected) |
| 77 | + |
| 78 | + def test_multi_group_multi_result(self): |
| 79 | + df = DataFrame([('bird', 'Falconiformes', 389.0), |
| 80 | + ('bird', 'Psittaciformes', 24.0), |
| 81 | + ('mammal', 'Carnivora', 80.2), |
| 82 | + ('mammal', 'Primates', None), |
| 83 | + ('mammal', 'Carnivora', 58)], |
| 84 | + index=['falcon', 'parrot', 'lion', 'monkey', 'leopard'], |
| 85 | + columns=('class', 'order', 'max_speed')) |
| 86 | + expected = DataFrame([('mammal', 'Carnivora', 80.2), |
| 87 | + ('mammal', 'Carnivora', 58)], |
| 88 | + index=['lion', 'leopard'], |
| 89 | + columns=('class', 'order', 'max_speed')) |
| 90 | + grps = df.groupby(['class', 'order']); |
| 91 | + assert_frame_equal(grps.get_group(('mammal','Carnivora')), expected) |
| 92 | + |
| 93 | + def test_single_group_no_tuple(self): |
| 94 | + df = DataFrame([('bird', 'Falconiformes', 389.0), |
| 95 | + ('bird', 'Psittaciformes', 24.0), |
| 96 | + ('mammal', 'Carnivora', 80.2), |
| 97 | + ('mammal', 'Primates', None), |
| 98 | + ('mammal', 'Carnivora', 58)], |
| 99 | + index=['falcon', 'parrot', 'lion', 'monkey', 'leopard'], |
| 100 | + columns=('class', 'order', 'max_speed')) |
| 101 | + expected = DataFrame([('bird', 'Falconiformes', 389.0), |
| 102 | + ('bird', 'Psittaciformes', 24.0)], |
| 103 | + index=['falcon', 'parrot'], |
| 104 | + columns=('class', 'order', 'max_speed')) |
| 105 | + grps = df.groupby(['class']); |
| 106 | + assert_frame_equal(grps.get_group('bird'), expected) |
| 107 | + |
| 108 | +if __name__ == '__main__': |
| 109 | + unittest.main() |
0 commit comments