|
7 | 7 | from numpy import nan
|
8 | 8 | import numpy as np
|
9 | 9 | import pandas as pd
|
10 |
| -from distutils.version import LooseVersion |
11 | 10 |
|
12 | 11 | from pandas import Series, DataFrame, bdate_range, Panel
|
13 |
| -from pandas.core.dtypes.common import ( |
14 |
| - is_bool_dtype, |
15 |
| - is_float_dtype, |
16 |
| - is_object_dtype, |
17 |
| - is_float) |
18 | 12 | from pandas.core.indexes.datetimes import DatetimeIndex
|
19 | 13 | from pandas.tseries.offsets import BDay
|
20 | 14 | from pandas.util import testing as tm
|
21 | 15 | from pandas.compat import lrange
|
22 | 16 | from pandas import compat
|
23 | 17 | from pandas.core.sparse import frame as spf
|
24 |
| -import pandas.util._test_decorators as td |
25 | 18 |
|
26 | 19 | from pandas._libs.sparse import BlockIndex, IntIndex
|
27 | 20 | from pandas.core.sparse.api import SparseSeries, SparseDataFrame, SparseArray
|
@@ -1171,163 +1164,6 @@ def test_notna(self):
|
1171 | 1164 | tm.assert_frame_equal(res.to_dense(), exp)
|
1172 | 1165 |
|
1173 | 1166 |
|
1174 |
| -@td.skip_if_no_scipy |
1175 |
| -@pytest.mark.parametrize('index', [None, list('abc')]) # noqa: F811 |
1176 |
| -@pytest.mark.parametrize('columns', [None, list('def')]) |
1177 |
| -@pytest.mark.parametrize('fill_value', [None, 0, np.nan]) |
1178 |
| -@pytest.mark.parametrize('dtype', [bool, int, float, np.uint16]) |
1179 |
| -def test_from_to_scipy(spmatrix, index, columns, fill_value, dtype): |
1180 |
| - # GH 4343 |
1181 |
| - # Make one ndarray and from it one sparse matrix, both to be used for |
1182 |
| - # constructing frames and comparing results |
1183 |
| - arr = np.eye(3, dtype=dtype) |
1184 |
| - # GH 16179 |
1185 |
| - arr[0, 1] = dtype(2) |
1186 |
| - try: |
1187 |
| - spm = spmatrix(arr) |
1188 |
| - assert spm.dtype == arr.dtype |
1189 |
| - except (TypeError, AssertionError): |
1190 |
| - # If conversion to sparse fails for this spmatrix type and arr.dtype, |
1191 |
| - # then the combination is not currently supported in NumPy, so we |
1192 |
| - # can just skip testing it thoroughly |
1193 |
| - return |
1194 |
| - |
1195 |
| - sdf = pd.SparseDataFrame(spm, index=index, columns=columns, |
1196 |
| - default_fill_value=fill_value) |
1197 |
| - |
1198 |
| - # Expected result construction is kind of tricky for all |
1199 |
| - # dtype-fill_value combinations; easiest to cast to something generic |
1200 |
| - # and except later on |
1201 |
| - rarr = arr.astype(object) |
1202 |
| - rarr[arr == 0] = np.nan |
1203 |
| - expected = pd.SparseDataFrame(rarr, index=index, columns=columns).fillna( |
1204 |
| - fill_value if fill_value is not None else np.nan) |
1205 |
| - |
1206 |
| - # Assert frame is as expected |
1207 |
| - sdf_obj = sdf.astype(object) |
1208 |
| - tm.assert_sp_frame_equal(sdf_obj, expected) |
1209 |
| - tm.assert_frame_equal(sdf_obj.to_dense(), expected.to_dense()) |
1210 |
| - |
1211 |
| - # Assert spmatrices equal |
1212 |
| - assert dict(sdf.to_coo().todok()) == dict(spm.todok()) |
1213 |
| - |
1214 |
| - # Ensure dtype is preserved if possible |
1215 |
| - was_upcast = ((fill_value is None or is_float(fill_value)) and |
1216 |
| - not is_object_dtype(dtype) and |
1217 |
| - not is_float_dtype(dtype)) |
1218 |
| - res_dtype = (bool if is_bool_dtype(dtype) else |
1219 |
| - float if was_upcast else |
1220 |
| - dtype) |
1221 |
| - tm.assert_contains_all(sdf.dtypes, {np.dtype(res_dtype)}) |
1222 |
| - assert sdf.to_coo().dtype == res_dtype |
1223 |
| - |
1224 |
| - # However, adding a str column results in an upcast to object |
1225 |
| - sdf['strings'] = np.arange(len(sdf)).astype(str) |
1226 |
| - assert sdf.to_coo().dtype == np.object_ |
1227 |
| - |
1228 |
| - |
1229 |
| -@td.skip_if_no_scipy |
1230 |
| -@pytest.mark.parametrize('fill_value', [None, 0, np.nan]) # noqa: F811 |
1231 |
| -def test_from_to_scipy_object(spmatrix, fill_value): |
1232 |
| - # GH 4343 |
1233 |
| - dtype = object |
1234 |
| - columns = list('cd') |
1235 |
| - index = list('ab') |
1236 |
| - import scipy |
1237 |
| - if (spmatrix is scipy.sparse.dok_matrix and LooseVersion( |
1238 |
| - scipy.__version__) >= LooseVersion('0.19.0')): |
1239 |
| - pytest.skip("dok_matrix from object does not work in SciPy >= 0.19") |
1240 |
| - |
1241 |
| - # Make one ndarray and from it one sparse matrix, both to be used for |
1242 |
| - # constructing frames and comparing results |
1243 |
| - arr = np.eye(2, dtype=dtype) |
1244 |
| - try: |
1245 |
| - spm = spmatrix(arr) |
1246 |
| - assert spm.dtype == arr.dtype |
1247 |
| - except (TypeError, AssertionError): |
1248 |
| - # If conversion to sparse fails for this spmatrix type and arr.dtype, |
1249 |
| - # then the combination is not currently supported in NumPy, so we |
1250 |
| - # can just skip testing it thoroughly |
1251 |
| - return |
1252 |
| - |
1253 |
| - sdf = pd.SparseDataFrame(spm, index=index, columns=columns, |
1254 |
| - default_fill_value=fill_value) |
1255 |
| - |
1256 |
| - # Expected result construction is kind of tricky for all |
1257 |
| - # dtype-fill_value combinations; easiest to cast to something generic |
1258 |
| - # and except later on |
1259 |
| - rarr = arr.astype(object) |
1260 |
| - rarr[arr == 0] = np.nan |
1261 |
| - expected = pd.SparseDataFrame(rarr, index=index, columns=columns).fillna( |
1262 |
| - fill_value if fill_value is not None else np.nan) |
1263 |
| - |
1264 |
| - # Assert frame is as expected |
1265 |
| - sdf_obj = sdf.astype(object) |
1266 |
| - tm.assert_sp_frame_equal(sdf_obj, expected) |
1267 |
| - tm.assert_frame_equal(sdf_obj.to_dense(), expected.to_dense()) |
1268 |
| - |
1269 |
| - # Assert spmatrices equal |
1270 |
| - assert dict(sdf.to_coo().todok()) == dict(spm.todok()) |
1271 |
| - |
1272 |
| - # Ensure dtype is preserved if possible |
1273 |
| - res_dtype = object |
1274 |
| - tm.assert_contains_all(sdf.dtypes, {np.dtype(res_dtype)}) |
1275 |
| - assert sdf.to_coo().dtype == res_dtype |
1276 |
| - |
1277 |
| - |
1278 |
| -@td.skip_if_no_scipy |
1279 |
| -def test_from_scipy_correct_ordering(spmatrix): |
1280 |
| - # GH 16179 |
1281 |
| - arr = np.arange(1, 5).reshape(2, 2) |
1282 |
| - try: |
1283 |
| - spm = spmatrix(arr) |
1284 |
| - assert spm.dtype == arr.dtype |
1285 |
| - except (TypeError, AssertionError): |
1286 |
| - # If conversion to sparse fails for this spmatrix type and arr.dtype, |
1287 |
| - # then the combination is not currently supported in NumPy, so we |
1288 |
| - # can just skip testing it thoroughly |
1289 |
| - return |
1290 |
| - |
1291 |
| - sdf = pd.SparseDataFrame(spm) |
1292 |
| - expected = pd.SparseDataFrame(arr) |
1293 |
| - tm.assert_sp_frame_equal(sdf, expected) |
1294 |
| - tm.assert_frame_equal(sdf.to_dense(), expected.to_dense()) |
1295 |
| - |
1296 |
| - |
1297 |
| -@td.skip_if_no_scipy |
1298 |
| -def test_from_scipy_fillna(spmatrix): |
1299 |
| - # GH 16112 |
1300 |
| - arr = np.eye(3) |
1301 |
| - arr[1:, 0] = np.nan |
1302 |
| - |
1303 |
| - try: |
1304 |
| - spm = spmatrix(arr) |
1305 |
| - assert spm.dtype == arr.dtype |
1306 |
| - except (TypeError, AssertionError): |
1307 |
| - # If conversion to sparse fails for this spmatrix type and arr.dtype, |
1308 |
| - # then the combination is not currently supported in NumPy, so we |
1309 |
| - # can just skip testing it thoroughly |
1310 |
| - return |
1311 |
| - |
1312 |
| - sdf = pd.SparseDataFrame(spm).fillna(-1.0) |
1313 |
| - |
1314 |
| - # Returning frame should fill all nan values with -1.0 |
1315 |
| - expected = pd.SparseDataFrame({ |
1316 |
| - 0: pd.SparseSeries([1., -1, -1]), |
1317 |
| - 1: pd.SparseSeries([np.nan, 1, np.nan]), |
1318 |
| - 2: pd.SparseSeries([np.nan, np.nan, 1]), |
1319 |
| - }, default_fill_value=-1) |
1320 |
| - |
1321 |
| - # fill_value is expected to be what .fillna() above was called with |
1322 |
| - # We don't use -1 as initial fill_value in expected SparseSeries |
1323 |
| - # construction because this way we obtain "compressed" SparseArrays, |
1324 |
| - # avoiding having to construct them ourselves |
1325 |
| - for col in expected: |
1326 |
| - expected[col].fill_value = -1 |
1327 |
| - |
1328 |
| - tm.assert_sp_frame_equal(sdf, expected) |
1329 |
| - |
1330 |
| - |
1331 | 1167 | class TestSparseDataFrameArithmetic(object):
|
1332 | 1168 |
|
1333 | 1169 | def test_numeric_op_scalar(self):
|
|
0 commit comments