|
28 | 28 |
|
29 | 29 |
|
30 | 30 | class TestSparseDataFrame(tm.TestCase, SharedWithSparse):
|
31 |
| - |
32 | 31 | klass = SparseDataFrame
|
33 | 32 |
|
34 | 33 | def setUp(self):
|
@@ -237,6 +236,18 @@ def test_constructor_nan_dataframe(self):
|
237 | 236 | dtype=float)
|
238 | 237 | tm.assert_sp_frame_equal(result, expected)
|
239 | 238 |
|
| 239 | + def test_type_coercion_at_construction(self): |
| 240 | + # GH 15682 |
| 241 | + result = pd.SparseDataFrame( |
| 242 | + {'a': [1, 0, 0], 'b': [0, 1, 0], 'c': [0, 0, 1]}, dtype='uint8', |
| 243 | + default_fill_value=0) |
| 244 | + expected = pd.SparseDataFrame( |
| 245 | + {'a': pd.SparseSeries([1, 0, 0], dtype='uint8'), |
| 246 | + 'b': pd.SparseSeries([0, 1, 0], dtype='uint8'), |
| 247 | + 'c': pd.SparseSeries([0, 0, 1], dtype='uint8')}, |
| 248 | + default_fill_value=0) |
| 249 | + tm.assert_sp_frame_equal(result, expected) |
| 250 | + |
240 | 251 | def test_dtypes(self):
|
241 | 252 | df = DataFrame(np.random.randn(10000, 4))
|
242 | 253 | df.loc[:9998] = np.nan
|
@@ -756,9 +767,18 @@ def test_sparse_frame_fillna_limit(self):
|
756 | 767 | tm.assert_frame_equal(result, expected)
|
757 | 768 |
|
758 | 769 | def test_rename(self):
|
759 |
| - # just check this works |
760 |
| - renamed = self.frame.rename(index=str) # noqa |
761 |
| - renamed = self.frame.rename(columns=lambda x: '%s%d' % (x, len(x))) # noqa |
| 770 | + result = self.frame.rename(index=str) |
| 771 | + expected = SparseDataFrame(self.data, index=self.dates.strftime( |
| 772 | + "%Y-%m-%d %H:%M:%S")) |
| 773 | + tm.assert_sp_frame_equal(result, expected) |
| 774 | + |
| 775 | + result = self.frame.rename(columns=lambda x: '%s%d' % (x, len(x))) |
| 776 | + data = {'A1': [nan, nan, nan, 0, 1, 2, 3, 4, 5, 6], |
| 777 | + 'B1': [0, 1, 2, nan, nan, nan, 3, 4, 5, 6], |
| 778 | + 'C1': np.arange(10, dtype=np.float64), |
| 779 | + 'D1': [0, 1, 2, 3, 4, 5, nan, nan, nan, nan]} |
| 780 | + expected = SparseDataFrame(data, index=self.dates) |
| 781 | + tm.assert_sp_frame_equal(result, expected) |
762 | 782 |
|
763 | 783 | def test_corr(self):
|
764 | 784 | res = self.frame.corr()
|
@@ -967,7 +987,6 @@ def _check(frame, orig):
|
967 | 987 | def test_shift(self):
|
968 | 988 |
|
969 | 989 | def _check(frame, orig):
|
970 |
| - |
971 | 990 | shifted = frame.shift(0)
|
972 | 991 | exp = orig.shift(0)
|
973 | 992 | tm.assert_frame_equal(shifted.to_dense(), exp)
|
@@ -1060,7 +1079,7 @@ def test_sparse_pow_issue(self):
|
1060 | 1079 | df = SparseDataFrame({'A': [nan, 0, 1]})
|
1061 | 1080 |
|
1062 | 1081 | # note that 2 ** df works fine, also df ** 1
|
1063 |
| - result = 1**df |
| 1082 | + result = 1 ** df |
1064 | 1083 |
|
1065 | 1084 | r1 = result.take([0], 1)['A']
|
1066 | 1085 | r2 = result['A']
|
@@ -1126,7 +1145,7 @@ def test_isnotnull(self):
|
1126 | 1145 | tm.assert_frame_equal(res.to_dense(), exp)
|
1127 | 1146 |
|
1128 | 1147 |
|
1129 |
| -@pytest.mark.parametrize('index', [None, list('ab')]) # noqa: F811 |
| 1148 | +@pytest.mark.parametrize('index', [None, list('ab')]) # noqa: F811 |
1130 | 1149 | @pytest.mark.parametrize('columns', [None, list('cd')])
|
1131 | 1150 | @pytest.mark.parametrize('fill_value', [None, 0, np.nan])
|
1132 | 1151 | @pytest.mark.parametrize('dtype', [bool, int, float, np.uint16])
|
@@ -1180,7 +1199,7 @@ def test_from_to_scipy(spmatrix, index, columns, fill_value, dtype):
|
1180 | 1199 | tm.assert_equal(sdf.to_coo().dtype, np.object_)
|
1181 | 1200 |
|
1182 | 1201 |
|
1183 |
| -@pytest.mark.parametrize('fill_value', [None, 0, np.nan]) # noqa: F811 |
| 1202 | +@pytest.mark.parametrize('fill_value', [None, 0, np.nan]) # noqa: F811 |
1184 | 1203 | def test_from_to_scipy_object(spmatrix, fill_value):
|
1185 | 1204 | # GH 4343
|
1186 | 1205 | dtype = object
|
@@ -1255,7 +1274,6 @@ def test_comparison_op_scalar(self):
|
1255 | 1274 |
|
1256 | 1275 |
|
1257 | 1276 | class TestSparseDataFrameAnalytics(tm.TestCase):
|
1258 |
| - |
1259 | 1277 | def setUp(self):
|
1260 | 1278 | self.data = {'A': [nan, nan, nan, 0, 1, 2, 3, 4, 5, 6],
|
1261 | 1279 | 'B': [0, 1, 2, nan, nan, nan, 3, 4, 5, 6],
|
|
0 commit comments