|
12 | 12 |
|
13 | 13 | from pandas.types.missing import isnull, notnull |
14 | 14 | from pandas.types.cast import _maybe_upcast, _find_common_type |
15 | | -from pandas.types.common import _ensure_platform_int |
| 15 | +from pandas.types.common import _ensure_platform_int, is_scipy_sparse |
16 | 16 |
|
17 | 17 | from pandas.core.common import _try_sort |
18 | 18 | from pandas.compat.numpy import function as nv |
|
29 | 29 | from pandas.util.decorators import Appender |
30 | 30 | import pandas.core.ops as ops |
31 | 31 |
|
32 | | -try: |
33 | | - from scipy.sparse import spmatrix # noqa |
34 | | -except ImportError: |
35 | | - spmatrix = type('mock spmatrix', (), {}) |
36 | 32 |
|
37 | 33 | _shared_doc_kwargs = dict(klass='SparseDataFrame') |
38 | 34 |
|
@@ -102,7 +98,7 @@ def __init__(self, data=None, index=None, columns=None, default_kind=None, |
102 | 98 | elif isinstance(data, BlockManager): |
103 | 99 | mgr = self._init_mgr(data, axes=dict(index=index, columns=columns), |
104 | 100 | dtype=dtype, copy=copy) |
105 | | - elif isinstance(data, spmatrix): |
| 101 | + elif is_scipy_sparse(data): |
106 | 102 | mgr = self._init_spmatrix(data, index, columns, dtype=dtype) |
107 | 103 | elif data is None: |
108 | 104 | data = DataFrame() |
@@ -175,19 +171,21 @@ def _init_dict(self, data, index, columns, dtype=None): |
175 | 171 | return to_manager(sdict, columns, index) |
176 | 172 |
|
177 | 173 | def _init_matrix(self, data, index, columns, dtype=None): |
| 174 | + """ Init self from ndarray or list of lists """ |
178 | 175 | data = _prep_ndarray(data, copy=False) |
179 | 176 | index, columns = self._prep_index(data, index, columns) |
180 | 177 | data = dict([(idx, data[:, i]) for i, idx in enumerate(columns)]) |
181 | 178 | return self._init_dict(data, index, columns, dtype) |
182 | 179 |
|
183 | 180 | def _init_spmatrix(self, data, index, columns, dtype=None): |
| 181 | + """ Init self from scipy.sparse matrix """ |
184 | 182 | index, columns = self._prep_index(data, index, columns) |
185 | 183 | data = data.tocoo(copy=False) |
186 | 184 | N = len(index) |
187 | 185 | bindex = np.arange(N, dtype=np.int32) |
188 | 186 |
|
189 | 187 | sdict = {} |
190 | | - values = Series(data.data, index=data.row) |
| 188 | + values = Series(data.data, index=data.row, copy=False) |
191 | 189 | for col, rowvals in values.groupby(data.col): |
192 | 190 | blocs, blens = get_blocks(bindex[rowvals.index]) |
193 | 191 | sdict[columns[col]] = SparseSeries( |
@@ -217,49 +215,12 @@ def _prep_index(self, data, index, columns): |
217 | 215 | (len(index), N)) |
218 | 216 | return index, columns |
219 | 217 |
|
220 | | - def as_matrix(self, columns=None, sparse=False): |
221 | | - """ |
222 | | - Convert the frame to its Numpy-array or SciPy sparse COO matrix |
223 | | - representation. |
224 | | -
|
225 | | - Parameters |
226 | | - ---------- |
227 | | - columns : list, optional, default=None |
228 | | - If None, return all columns. Otherwise, returns specified columns. |
229 | | - sparse : bool, optional, default=True |
230 | | - If True, return an instance of scipy.sparse.coo_matrix instead |
231 | | - of ndarray. If False, the result values array will be DENSE. |
232 | | -
|
233 | | - Returns |
234 | | - ------- |
235 | | - values : ndarray or scipy.sparse.spmatrix |
236 | | - If the caller is heterogeneous and contains booleans or objects, |
237 | | - the result will be of dtype=object. See Notes. |
238 | | -
|
239 | | - Notes |
240 | | - ----- |
241 | | - The dtype will be the lowest-common-denominator type (implicit |
242 | | - upcasting); that is to say if the dtypes (even of numeric types) |
243 | | - are mixed, the one that accommodates all will be chosen. |
244 | | -
|
245 | | - e.g. If the dtypes are float16 and float32, dtype will be upcast to |
246 | | - float32. By numpy.find_common_type convention, mixing int64 and |
247 | | - and uint64 will result in a float64 dtype. |
248 | | -
|
249 | | - See Also |
250 | | - -------- |
251 | | - pandas.SparseDataFrame.to_coo |
252 | | - """ |
253 | | - if sparse: |
254 | | - subdf = self if columns is None else self[columns] |
255 | | - return subdf.to_coo() |
256 | | - |
257 | | - return super(SparseDataFrame, self).as_matrix(columns=columns) |
258 | | - |
259 | 218 | def to_coo(self): |
260 | 219 | """ |
261 | 220 | Convert the frame to its SciPy sparse COO matrix representation. |
262 | 221 |
|
| 222 | + .. versionadded:: 0.20.0 |
| 223 | +
|
263 | 224 | Returns |
264 | 225 | ------- |
265 | 226 | coo_matrix : scipy.sparse.spmatrix |
|
0 commit comments