19
19
import pandas .core .ops as ops
20
20
21
21
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 ):
24
24
"""
25
25
Wrapper function for Series arithmetic operations, to avoid
26
26
code duplication.
27
27
"""
28
+
28
29
def wrapper (self , other ):
29
30
if isinstance (other , np .ndarray ):
30
31
if len (self ) != len (other ):
@@ -37,14 +38,14 @@ def wrapper(self, other):
37
38
else :
38
39
return _sparse_array_op (self , other , op , name )
39
40
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 ))
42
42
43
43
return SparseArray (op (self .sp_values , other ),
44
44
sparse_index = self .sp_index ,
45
45
fill_value = new_fill_value )
46
46
else : # pragma: no cover
47
47
raise TypeError ('operation with %s not supported' % type (other ))
48
+
48
49
if name .startswith ("__" ):
49
50
name = name [2 :- 2 ]
50
51
wrapper .__name__ = name
@@ -74,44 +75,38 @@ def _sparse_array_op(left, right, op, name):
74
75
75
76
def _sparse_nanop (this , other , name ):
76
77
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 )
81
80
82
81
return result , result_index
83
82
84
83
85
84
def _sparse_fillop (this , other , name ):
86
85
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 )
93
89
94
90
return result , result_index
95
91
96
92
97
93
class SparseArray (PandasObject , np .ndarray ):
98
-
99
94
"""Data structure for labeled, sparse floating point data
100
95
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
115
110
"""
116
111
__array_priority__ = 15
117
112
_typ = 'array'
@@ -120,9 +115,8 @@ class SparseArray(PandasObject, np.ndarray):
120
115
sp_index = None
121
116
fill_value = None
122
117
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 ):
126
120
127
121
if index is not None :
128
122
if data is None :
@@ -164,7 +158,8 @@ def __new__(
164
158
subarr = np .asarray (values , dtype = dtype )
165
159
166
160
# 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 ))):
168
163
if np .isnan (fill_value ) or not fill_value :
169
164
fill_value = False
170
165
else :
@@ -284,9 +279,9 @@ def __getitem__(self, key):
284
279
else :
285
280
if isinstance (key , SparseArray ):
286
281
key = np .asarray (key )
287
- if hasattr (key ,'__len__' ) and len (self ) != len (key ):
282
+ if hasattr (key , '__len__' ) and len (self ) != len (key ):
288
283
indices = self .sp_index
289
- if hasattr (indices ,'to_int_index' ):
284
+ if hasattr (indices , 'to_int_index' ):
290
285
indices = indices .to_int_index ()
291
286
data_slice = self .values .take (indices .indices )[key ]
292
287
else :
@@ -355,7 +350,8 @@ def __setitem__(self, key, value):
355
350
# if com.is_integer(key):
356
351
# self.values[key] = value
357
352
# 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")
359
355
raise TypeError (
360
356
"SparseArray does not support item assignment via setitem" )
361
357
@@ -364,16 +360,17 @@ def __setslice__(self, i, j, value):
364
360
i = 0
365
361
if j < 0 :
366
362
j = 0
367
- slobj = slice (i , j )
363
+ slobj = slice (i , j ) # noqa
368
364
369
365
# 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")
371
368
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" )
377
374
378
375
def astype (self , dtype = None ):
379
376
"""
@@ -394,8 +391,7 @@ def copy(self, deep=True):
394
391
else :
395
392
values = self .sp_values
396
393
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 )
399
395
400
396
def count (self ):
401
397
"""
@@ -453,8 +449,7 @@ def cumsum(self, axis=0, dtype=None, out=None):
453
449
if com .notnull (self .fill_value ):
454
450
return self .to_dense ().cumsum ()
455
451
# 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 ,
458
453
fill_value = self .fill_value )
459
454
460
455
def mean (self , axis = None , dtype = None , out = None ):
@@ -485,8 +480,8 @@ def _maybe_to_dense(obj):
485
480
486
481
def _maybe_to_sparse (array ):
487
482
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 )
490
485
if not isinstance (array , SparseArray ):
491
486
array = com ._values_from_object (array )
492
487
return array
@@ -538,15 +533,15 @@ def make_sparse(arr, kind='block', fill_value=nan):
538
533
sparsified_values = arr [mask ]
539
534
return sparsified_values , index
540
535
541
- ops .add_special_arithmetic_methods (SparseArray ,
542
- arith_method = _arith_method ,
543
- use_numexpr = False )
544
536
537
+ ops .add_special_arithmetic_methods (SparseArray , arith_method = _arith_method ,
538
+ use_numexpr = False )
545
539
546
540
547
541
def _concat_compat (to_concat , axis = 0 ):
548
542
"""
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
550
545
551
546
Parameters
552
547
----------
@@ -570,10 +565,10 @@ def convert_sparse(x, axis):
570
565
typs = com .get_dtype_kinds (to_concat )
571
566
572
567
# 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 )
575
570
576
- if not len (typs - set (['sparse' ,'f' ,'i' ])):
571
+ if not len (typs - set (['sparse' , 'f' , 'i' ])):
577
572
578
573
# we can remain sparse
579
574
result = SparseArray (result .ravel ())
0 commit comments