|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import pandas.util.testing as tm |
| 4 | + |
| 5 | + |
| 6 | +class TestSparseDataFrameMultitype(tm.TestCase): |
| 7 | + def setUp(self): |
| 8 | + super(TestSparseDataFrameMultitype, self).setUp() |
| 9 | + self.string_series = pd.SparseSeries(['a', 'b', 'c']) |
| 10 | + self.int_series = pd.SparseSeries([1, 2, 3]) |
| 11 | + self.float_series = pd.SparseSeries([1.1, 1.2, 1.3]) |
| 12 | + self.object_series = pd.SparseSeries([[], {}, set()]) |
| 13 | + self.sdf = pd.SparseDataFrame({ |
| 14 | + 'string': self.string_series, |
| 15 | + 'int': self.int_series, |
| 16 | + 'float': self.float_series, |
| 17 | + 'object': self.object_series, |
| 18 | + }) |
| 19 | + self.cols = ['string', 'int', 'float', 'object'] |
| 20 | + self.sdf = self.sdf[self.cols] |
| 21 | + |
| 22 | + def test_basic_dtypes(self): |
| 23 | + for _, row in self.sdf.iterrows(): |
| 24 | + self.assertEqual(row.dtype, object) |
| 25 | + tm.assert_sp_series_equal(self.sdf['string'], self.string_series, |
| 26 | + check_names=False) |
| 27 | + tm.assert_sp_series_equal(self.sdf['int'], self.int_series, |
| 28 | + check_names=False) |
| 29 | + tm.assert_sp_series_equal(self.sdf['float'], self.float_series, |
| 30 | + check_names=False) |
| 31 | + tm.assert_sp_series_equal(self.sdf['object'], self.object_series, |
| 32 | + check_names=False) |
| 33 | + |
| 34 | + def test_indexing_single(self): |
| 35 | + tm.assert_sp_series_equal(self.sdf.iloc[0], |
| 36 | + pd.SparseSeries(['a', 1, 1.1, []], |
| 37 | + index=self.cols), |
| 38 | + check_names=False) |
| 39 | + tm.assert_sp_series_equal(self.sdf.iloc[1], |
| 40 | + pd.SparseSeries(['b', 2, 1.2, {}], |
| 41 | + index=self.cols), |
| 42 | + check_names=False) |
| 43 | + tm.assert_sp_series_equal(self.sdf.iloc[2], |
| 44 | + pd.SparseSeries(['c', 3, 1.3, set()], |
| 45 | + index=self.cols), |
| 46 | + check_names=False) |
| 47 | + |
| 48 | + def test_indexing_multiple(self): |
| 49 | + tm.assert_sp_frame_equal(self.sdf, self.sdf[:]) |
| 50 | + tm.assert_sp_frame_equal(self.sdf, self.sdf.loc[:]) |
| 51 | + tm.assert_sp_frame_equal(self.sdf.iloc[[1, 2]], |
| 52 | + pd.SparseDataFrame({ |
| 53 | + 'string': self.string_series.iloc[[1, 2]], |
| 54 | + 'int': self.int_series.iloc[[1, 2]], |
| 55 | + 'float': self.float_series.iloc[[1, 2]], |
| 56 | + 'object': self.object_series.iloc[[1, 2]] |
| 57 | + }, index=[1, 2])[self.cols]) |
| 58 | + tm.assert_sp_frame_equal(self.sdf[['int', 'string']], |
| 59 | + pd.SparseDataFrame({ |
| 60 | + 'int': self.int_series, |
| 61 | + 'string': self.string_series, |
| 62 | + })) |
| 63 | + |
| 64 | + |
| 65 | +class TestSparseSeriesMultitype(tm.TestCase): |
| 66 | + def setUp(self): |
| 67 | + super(TestSparseSeriesMultitype, self).setUp() |
| 68 | + self.index = ['string', 'int', 'float', 'object'] |
| 69 | + self.ss = pd.SparseSeries(['a', 1, 1.1, []], |
| 70 | + index=self.index) |
| 71 | + |
| 72 | + def test_indexing_single(self): |
| 73 | + for i, idx in enumerate(self.index): |
| 74 | + self.assertEqual(self.ss.iloc[i], self.ss[idx]) |
| 75 | + self.assertEqual(type(self.ss.iloc[i]), |
| 76 | + type(self.ss[idx])) |
| 77 | + self.assertEqual(self.ss['string'], 'a') |
| 78 | + self.assertEqual(self.ss['int'], 1) |
| 79 | + self.assertEqual(self.ss['float'], 1.1) |
| 80 | + self.assertEqual(self.ss['object'], []) |
| 81 | + |
| 82 | + def test_indexing_multiple(self): |
| 83 | + tm.assert_sp_series_equal(self.ss.loc[['string', 'int']], |
| 84 | + pd.SparseSeries(['a', 1], |
| 85 | + index=['string', 'int'])) |
| 86 | + tm.assert_sp_series_equal(self.ss.loc[['string', 'object']], |
| 87 | + pd.SparseSeries(['a', []], |
| 88 | + index=['string', 'object'])) |
0 commit comments