9
9
import warnings
10
10
11
11
from pandas .core .dtypes .missing import isna , notna
12
+ from pandas .core .dtypes .common import is_sparse
12
13
13
14
from pandas .compat .numpy import function as nv
14
15
from pandas .core .index import Index , _ensure_index , InvalidIndexError
17
18
from pandas .core import generic
18
19
import pandas .core .common as com
19
20
import pandas .core .ops as ops
20
- import pandas ._libs .index as libindex
21
21
from pandas .util ._decorators import Appender
22
22
23
23
from pandas .core .sparse .array import (
@@ -279,8 +279,13 @@ def __array_wrap__(self, result, context=None):
279
279
else :
280
280
fill_value = self .fill_value
281
281
282
+ # Assume: If result size matches, old sparse index is valid (ok???)
283
+ if np .size (result ) == self .sp_index .npoints :
284
+ sp_index = self .sp_index
285
+ else :
286
+ sp_index = None
282
287
return self ._constructor (result , index = self .index ,
283
- sparse_index = self . sp_index ,
288
+ sparse_index = sp_index ,
284
289
fill_value = fill_value ,
285
290
copy = False ).__finalize__ (self )
286
291
@@ -481,7 +486,7 @@ def set_value(self, label, value, takeable=False):
481
486
482
487
Returns
483
488
-------
484
- series : SparseSeries
489
+ self : SparseSeries
485
490
"""
486
491
warnings .warn ("set_value is deprecated and will be removed "
487
492
"in a future release. Please use "
@@ -490,35 +495,16 @@ def set_value(self, label, value, takeable=False):
490
495
return self ._set_value (label , value , takeable = takeable )
491
496
492
497
def _set_value (self , label , value , takeable = False ):
493
- values = self .to_dense ()
494
-
495
- # if the label doesn't exist, we will create a new object here
496
- # and possibly change the index
497
- new_values = values ._set_value (label , value , takeable = takeable )
498
- if new_values is not None :
499
- values = new_values
500
- new_index = values .index
501
- values = SparseArray (values , fill_value = self .fill_value ,
502
- kind = self .kind )
503
- self ._data = SingleBlockManager (values , new_index )
504
- self ._index = new_index
498
+ self ._data = self ._data .copy ()
499
+ try :
500
+ idx = self .index .get_loc (label )
501
+ except KeyError :
502
+ idx = len (self )
503
+ self ._data .axes [0 ] = self ._data .index .append (Index ([label ]))
504
+ self ._data = self ._data .setitem (indexer = idx , value = value )
505
+ return self
505
506
_set_value .__doc__ = set_value .__doc__
506
507
507
- def _set_values (self , key , value ):
508
-
509
- # this might be inefficient as we have to recreate the sparse array
510
- # rather than setting individual elements, but have to convert
511
- # the passed slice/boolean that's in dense space into a sparse indexer
512
- # not sure how to do that!
513
- if isinstance (key , Series ):
514
- key = key .values
515
-
516
- values = self .values .to_dense ()
517
- values [key ] = libindex .convert_scalar (values , value )
518
- values = SparseArray (values , fill_value = self .fill_value ,
519
- kind = self .kind )
520
- self ._data = SingleBlockManager (values , self .index )
521
-
522
508
def to_dense (self , sparse_only = False ):
523
509
"""
524
510
Convert SparseSeries to a Series.
@@ -544,8 +530,10 @@ def to_dense(self, sparse_only=False):
544
530
index = self .index .take (int_index .indices )
545
531
return Series (self .sp_values , index = index , name = self .name )
546
532
else :
547
- return Series (self .values .to_dense (), index = self .index ,
548
- name = self .name )
533
+ values = self .values
534
+ if is_sparse (values ):
535
+ values = values .to_dense ()
536
+ return Series (values , index = self .index , name = self .name )
549
537
550
538
@property
551
539
def density (self ):
0 commit comments