Skip to content

Commit f4f74f9

Browse files
rockgjreback
authored andcommitted
PEP: pandas/sparse cleanup
Author: rockg <[email protected]> Closes #12116 from rockg/pep8-sparse and squashes the following commits: 0902bb8 [rockg] PEP: pandas/sparse cleanup
1 parent b26fac8 commit f4f74f9

File tree

9 files changed

+456
-499
lines changed

9 files changed

+456
-499
lines changed

pandas/sparse/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pylint: disable=W0611
2-
2+
# flake8: noqa
33
from pandas.sparse.array import SparseArray
44
from pandas.sparse.list import SparseList
55
from pandas.sparse.series import SparseSeries, SparseTimeSeries

pandas/sparse/array.py

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
import pandas.core.ops as ops
2020

2121

22-
def _arith_method(op, name, str_rep=None, default_axis=None,
23-
fill_zeros=None, **eval_kwargs):
22+
def _arith_method(op, name, str_rep=None, default_axis=None, fill_zeros=None,
23+
**eval_kwargs):
2424
"""
2525
Wrapper function for Series arithmetic operations, to avoid
2626
code duplication.
2727
"""
28+
2829
def wrapper(self, other):
2930
if isinstance(other, np.ndarray):
3031
if len(self) != len(other):
@@ -37,14 +38,14 @@ def wrapper(self, other):
3738
else:
3839
return _sparse_array_op(self, other, op, name)
3940
elif np.isscalar(other):
40-
new_fill_value = op(np.float64(self.fill_value),
41-
np.float64(other))
41+
new_fill_value = op(np.float64(self.fill_value), np.float64(other))
4242

4343
return SparseArray(op(self.sp_values, other),
4444
sparse_index=self.sp_index,
4545
fill_value=new_fill_value)
4646
else: # pragma: no cover
4747
raise TypeError('operation with %s not supported' % type(other))
48+
4849
if name.startswith("__"):
4950
name = name[2:-2]
5051
wrapper.__name__ = name
@@ -74,44 +75,38 @@ def _sparse_array_op(left, right, op, name):
7475

7576
def _sparse_nanop(this, other, name):
7677
sparse_op = getattr(splib, 'sparse_nan%s' % name)
77-
result, result_index = sparse_op(this.sp_values,
78-
this.sp_index,
79-
other.sp_values,
80-
other.sp_index)
78+
result, result_index = sparse_op(this.sp_values, this.sp_index,
79+
other.sp_values, other.sp_index)
8180

8281
return result, result_index
8382

8483

8584
def _sparse_fillop(this, other, name):
8685
sparse_op = getattr(splib, 'sparse_%s' % name)
87-
result, result_index = sparse_op(this.sp_values,
88-
this.sp_index,
89-
this.fill_value,
90-
other.sp_values,
91-
other.sp_index,
92-
other.fill_value)
86+
result, result_index = sparse_op(this.sp_values, this.sp_index,
87+
this.fill_value, other.sp_values,
88+
other.sp_index, other.fill_value)
9389

9490
return result, result_index
9591

9692

9793
class SparseArray(PandasObject, np.ndarray):
98-
9994
"""Data structure for labeled, sparse floating point data
10095
101-
Parameters
102-
----------
103-
data : {array-like, Series, SparseSeries, dict}
104-
kind : {'block', 'integer'}
105-
fill_value : float
106-
Defaults to NaN (code for missing)
107-
sparse_index : {BlockIndex, IntIndex}, optional
108-
Only if you have one. Mainly used internally
109-
110-
Notes
111-
-----
112-
SparseArray objects are immutable via the typical Python means. If you
113-
must change values, convert to dense, make your changes, then convert back
114-
to sparse
96+
Parameters
97+
----------
98+
data : {array-like, Series, SparseSeries, dict}
99+
kind : {'block', 'integer'}
100+
fill_value : float
101+
Defaults to NaN (code for missing)
102+
sparse_index : {BlockIndex, IntIndex}, optional
103+
Only if you have one. Mainly used internally
104+
105+
Notes
106+
-----
107+
SparseArray objects are immutable via the typical Python means. If you
108+
must change values, convert to dense, make your changes, then convert back
109+
to sparse
115110
"""
116111
__array_priority__ = 15
117112
_typ = 'array'
@@ -120,9 +115,8 @@ class SparseArray(PandasObject, np.ndarray):
120115
sp_index = None
121116
fill_value = None
122117

123-
def __new__(
124-
cls, data, sparse_index=None, index=None, kind='integer', fill_value=None,
125-
dtype=np.float64, copy=False):
118+
def __new__(cls, data, sparse_index=None, index=None, kind='integer',
119+
fill_value=None, dtype=np.float64, copy=False):
126120

127121
if index is not None:
128122
if data is None:
@@ -164,7 +158,8 @@ def __new__(
164158
subarr = np.asarray(values, dtype=dtype)
165159

166160
# if we have a bool type, make sure that we have a bool fill_value
167-
if (dtype is not None and issubclass(dtype.type, np.bool_)) or (data is not None and lib.is_bool_array(subarr)):
161+
if ((dtype is not None and issubclass(dtype.type, np.bool_)) or
162+
(data is not None and lib.is_bool_array(subarr))):
168163
if np.isnan(fill_value) or not fill_value:
169164
fill_value = False
170165
else:
@@ -284,9 +279,9 @@ def __getitem__(self, key):
284279
else:
285280
if isinstance(key, SparseArray):
286281
key = np.asarray(key)
287-
if hasattr(key,'__len__') and len(self) != len(key):
282+
if hasattr(key, '__len__') and len(self) != len(key):
288283
indices = self.sp_index
289-
if hasattr(indices,'to_int_index'):
284+
if hasattr(indices, 'to_int_index'):
290285
indices = indices.to_int_index()
291286
data_slice = self.values.take(indices.indices)[key]
292287
else:
@@ -355,7 +350,8 @@ def __setitem__(self, key, value):
355350
# if com.is_integer(key):
356351
# self.values[key] = value
357352
# else:
358-
# raise Exception("SparseArray does not support seting non-scalars via setitem")
353+
# raise Exception("SparseArray does not support seting non-scalars
354+
# via setitem")
359355
raise TypeError(
360356
"SparseArray does not support item assignment via setitem")
361357

@@ -364,16 +360,17 @@ def __setslice__(self, i, j, value):
364360
i = 0
365361
if j < 0:
366362
j = 0
367-
slobj = slice(i, j)
363+
slobj = slice(i, j) # noqa
368364

369365
# if not np.isscalar(value):
370-
# raise Exception("SparseArray does not support seting non-scalars via slices")
366+
# raise Exception("SparseArray does not support seting non-scalars
367+
# via slices")
371368

372-
#x = self.values
373-
#x[slobj] = value
374-
#self.values = x
375-
raise TypeError(
376-
"SparseArray does not support item assignment via slices")
369+
# x = self.values
370+
# x[slobj] = value
371+
# self.values = x
372+
raise TypeError("SparseArray does not support item assignment via "
373+
"slices")
377374

378375
def astype(self, dtype=None):
379376
"""
@@ -394,8 +391,7 @@ def copy(self, deep=True):
394391
else:
395392
values = self.sp_values
396393
return SparseArray(values, sparse_index=self.sp_index,
397-
dtype=self.dtype,
398-
fill_value=self.fill_value)
394+
dtype=self.dtype, fill_value=self.fill_value)
399395

400396
def count(self):
401397
"""
@@ -453,8 +449,7 @@ def cumsum(self, axis=0, dtype=None, out=None):
453449
if com.notnull(self.fill_value):
454450
return self.to_dense().cumsum()
455451
# TODO: what if sp_values contains NaN??
456-
return SparseArray(self.sp_values.cumsum(),
457-
sparse_index=self.sp_index,
452+
return SparseArray(self.sp_values.cumsum(), sparse_index=self.sp_index,
458453
fill_value=self.fill_value)
459454

460455
def mean(self, axis=None, dtype=None, out=None):
@@ -485,8 +480,8 @@ def _maybe_to_dense(obj):
485480

486481
def _maybe_to_sparse(array):
487482
if isinstance(array, com.ABCSparseSeries):
488-
array = SparseArray(
489-
array.values, sparse_index=array.sp_index, fill_value=array.fill_value, copy=True)
483+
array = SparseArray(array.values, sparse_index=array.sp_index,
484+
fill_value=array.fill_value, copy=True)
490485
if not isinstance(array, SparseArray):
491486
array = com._values_from_object(array)
492487
return array
@@ -538,15 +533,15 @@ def make_sparse(arr, kind='block', fill_value=nan):
538533
sparsified_values = arr[mask]
539534
return sparsified_values, index
540535

541-
ops.add_special_arithmetic_methods(SparseArray,
542-
arith_method=_arith_method,
543-
use_numexpr=False)
544536

537+
ops.add_special_arithmetic_methods(SparseArray, arith_method=_arith_method,
538+
use_numexpr=False)
545539

546540

547541
def _concat_compat(to_concat, axis=0):
548542
"""
549-
provide concatenation of an sparse/dense array of arrays each of which is a single dtype
543+
provide concatenation of an sparse/dense array of arrays each of which is a
544+
single dtype
550545
551546
Parameters
552547
----------
@@ -570,10 +565,10 @@ def convert_sparse(x, axis):
570565
typs = com.get_dtype_kinds(to_concat)
571566

572567
# we have more than one type here, so densify and regular concat
573-
to_concat = [ convert_sparse(x, axis) for x in to_concat ]
574-
result = np.concatenate(to_concat,axis=axis)
568+
to_concat = [convert_sparse(x, axis) for x in to_concat]
569+
result = np.concatenate(to_concat, axis=axis)
575570

576-
if not len(typs-set(['sparse','f','i'])):
571+
if not len(typs - set(['sparse', 'f', 'i'])):
577572

578573
# we can remain sparse
579574
result = SparseArray(result.ravel())

0 commit comments

Comments
 (0)