|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import pandas.util.testing as tm |
| 4 | + |
| 5 | + |
| 6 | +class TestPivotTable(tm.TestCase): |
| 7 | + |
| 8 | + _multiprocess_can_split_ = True |
| 9 | + |
| 10 | + def setUp(self): |
| 11 | + self.dense = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar', |
| 12 | + 'foo', 'bar', 'foo', 'foo'], |
| 13 | + 'B': ['one', 'one', 'two', 'three', |
| 14 | + 'two', 'two', 'one', 'three'], |
| 15 | + 'C': np.random.randn(8), |
| 16 | + 'D': np.random.randn(8), |
| 17 | + 'E': [np.nan, np.nan, 1, 2, |
| 18 | + np.nan, 1, np.nan, np.nan]}) |
| 19 | + self.sparse = self.dense.to_sparse() |
| 20 | + |
| 21 | + def test_pivot_table(self): |
| 22 | + res_sparse = pd.pivot_table(self.sparse, index='A', columns='B', |
| 23 | + values='C') |
| 24 | + res_dense = pd.pivot_table(self.dense, index='A', columns='B', |
| 25 | + values='C') |
| 26 | + tm.assert_frame_equal(res_sparse, res_dense) |
| 27 | + |
| 28 | + res_sparse = pd.pivot_table(self.sparse, index='A', columns='B', |
| 29 | + values='E') |
| 30 | + res_dense = pd.pivot_table(self.dense, index='A', columns='B', |
| 31 | + values='E') |
| 32 | + tm.assert_frame_equal(res_sparse, res_dense) |
| 33 | + |
| 34 | + res_sparse = pd.pivot_table(self.sparse, index='A', columns='B', |
| 35 | + values='E', aggfunc='mean') |
| 36 | + res_dense = pd.pivot_table(self.dense, index='A', columns='B', |
| 37 | + values='E', aggfunc='mean') |
| 38 | + tm.assert_frame_equal(res_sparse, res_dense) |
| 39 | + |
| 40 | + # ToDo: sum doesn't handle nan properly |
| 41 | + # res_sparse = pd.pivot_table(self.sparse, index='A', columns='B', |
| 42 | + # values='E', aggfunc='sum') |
| 43 | + # res_dense = pd.pivot_table(self.dense, index='A', columns='B', |
| 44 | + # values='E', aggfunc='sum') |
| 45 | + # tm.assert_frame_equal(res_sparse, res_dense) |
| 46 | + |
| 47 | + def test_pivot_table_multi(self): |
| 48 | + res_sparse = pd.pivot_table(self.sparse, index='A', columns='B', |
| 49 | + values=['D', 'E']) |
| 50 | + res_dense = pd.pivot_table(self.dense, index='A', columns='B', |
| 51 | + values=['D', 'E']) |
| 52 | + tm.assert_frame_equal(res_sparse, res_dense) |
0 commit comments