Skip to content

Commit f7353bf

Browse files
committed
fixup
1 parent 7b36ac7 commit f7353bf

File tree

6 files changed

+78
-13
lines changed

6 files changed

+78
-13
lines changed

pandas/core/frame.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,9 @@ def iterrows(self):
734734
iteritems : Iterate over (column name, Series) pairs.
735735
736736
"""
737-
iloc = self.iloc
737+
row_at = self.iloc.__getitem__
738738
for i, k in enumerate(self.index):
739-
yield k, iloc[i]
739+
yield k, row_at(i)
740740

741741
def itertuples(self, index=True, name="Pandas"):
742742
"""
@@ -1979,9 +1979,7 @@ def set_value(self, index, col, value, takeable=False):
19791979
19801980
Returns
19811981
-------
1982-
frame : DataFrame
1983-
If label pair is contained, will be reference to calling DataFrame,
1984-
otherwise a new object
1982+
self : DataFrame
19851983
"""
19861984
warnings.warn("set_value is deprecated and will be removed "
19871985
"in a future release. Please use "

pandas/core/indexing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,10 @@ def setter(item, v):
530530
# set the item, possibly having a dtype change
531531
s._consolidate_inplace()
532532
s = s.copy()
533-
if is_sparse(s):
534-
s.set_value(pi, v, takeable=is_list_like(pi))
535-
else:
536-
s._data = s._data.setitem(indexer=pi, value=v)
533+
# if is_sparse(s):
534+
# s.set_value(pi, v, takeable=is_list_like(pi))
535+
# else:
536+
s._data = s._data.setitem(indexer=pi, value=v) # TODO THIS USE THIS
537537
s._maybe_update_cacher(clear=True)
538538

539539
# reset the sliced object if unique

pandas/core/internals.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,9 @@ def _is_empty_indexer(indexer):
857857
if _is_empty_indexer(indexer):
858858
pass
859859

860+
elif is_sparse(values):
861+
values = values.set_values(indexer, value)
862+
860863
# setting a single element for each dim and with a rhs that could
861864
# be say a list
862865
# GH 6043
@@ -1733,9 +1736,12 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0,
17331736
# use block's copy logic.
17341737
# .values may be an Index which does shallow copy by default
17351738
new_values = self.values if inplace else self.copy().values
1736-
new_values = new_values.to_dense()
17371739
new_values, _, new, _ = self._try_coerce_args(new_values, new)
17381740

1741+
# Cannot modify a SparseArray
1742+
if is_sparse(new_values):
1743+
new_values = new_values.to_dense()
1744+
17391745
if isinstance(new, np.ndarray) and len(new) == len(mask):
17401746
new = new[mask]
17411747

@@ -2754,8 +2760,12 @@ def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
27542760
return self.make_block_same_class(values=values,
27552761
placement=self.mgr_locs)
27562762

2763+
def _can_hold_element(self, element):
2764+
return np.issubdtype(np.asanyarray(element).dtype,
2765+
self.sp_values.dtype)
2766+
27572767
def _try_coerce_result(self, result):
2758-
if not is_sparse(result):
2768+
if np.ndim(result) > 0 and not is_sparse(result):
27592769
result = SparseArray(result, kind=self.kind,
27602770
fill_value=self.fill_value, dtype=self.dtype)
27612771
return result

pandas/core/sparse/array.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,39 @@ def get_values(self, fill=None):
387387
""" return a dense representation """
388388
return self.to_dense(fill=fill)
389389

390+
def set_values(self, indexer, value):
391+
"""
392+
Return new SparseArray with indexed values set to `value`.
393+
394+
Returns
395+
-------
396+
SparseArray
397+
A new sparse array with indexer positions filled with value.
398+
"""
399+
# If indexer is not a single int position, easiest to handle via dense
400+
if not is_scalar(indexer):
401+
values = self.to_dense()
402+
values[indexer] = value
403+
return SparseArray(values, kind=self.kind,
404+
fill_value=self.fill_value)
405+
406+
# If label already in sparse index, just switch the value on a copy
407+
idx = self.sp_index.lookup(indexer)
408+
if idx != -1:
409+
obj = self.copy()
410+
obj.sp_values[idx] = value
411+
return obj
412+
413+
indices = self.sp_index.to_int_index().indices
414+
pos = np.searchsorted(indices, indexer)
415+
416+
indices = np.insert(indices, pos, indexer)
417+
sp_values = np.insert(self.sp_values, pos, value)
418+
419+
return SparseArray(
420+
sp_values, fill_value=self.fill_value,
421+
sparse_index=_make_index(self.sp_index.length, indices, self.kind))
422+
390423
def to_dense(self, fill=None):
391424
"""
392425
Convert SparseArray to a NumPy array.

pandas/core/sparse/frame.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,29 @@ def _get_value(self, index, col, takeable=False):
465465
return series._get_value(index, takeable=takeable)
466466
_get_value.__doc__ = get_value.__doc__
467467

468+
def set_value(self, index, col, value, takeable=False):
469+
"""
470+
Put single value at passed column and index
471+
472+
Parameters
473+
----------
474+
index : row label
475+
col : column label
476+
value : scalar value
477+
takeable : interpret the index/col as indexers, default False
478+
479+
Notes
480+
-----
481+
This method *always* returns a new object for backwards compatibility.
482+
483+
Returns
484+
-------
485+
frame : DataFrame
486+
"""
487+
self = self.copy(deep=False)
488+
return super(SparseDataFrame, self).set_value(index, col, value,
489+
takeable=takeable)
490+
468491
def _slice(self, slobj, axis=0, kind=None):
469492
if axis == 0:
470493
new_index = self.index[slobj]

pandas/core/sparse/series.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
from pandas.core.dtypes.missing import isna, notna
1212
from pandas.core.dtypes.common import is_scalar, is_sparse
13-
from pandas.core.common import _values_from_object, _maybe_match_name
13+
from pandas.core.common import _values_from_object, _maybe_match_name, \
14+
PerformanceWarning
1415

1516
from pandas.compat.numpy import function as nv
1617
from pandas.core.index import Index, _ensure_index, InvalidIndexError
@@ -22,7 +23,6 @@
2223
import pandas.core.common as com
2324
import pandas.core.ops as ops
2425
import pandas._libs.index as _index
25-
from pandas.errors import PerformanceWarning
2626
from pandas.util._decorators import Appender
2727

2828
from pandas.core.sparse.array import (
@@ -603,6 +603,7 @@ def _set_value(self, label, value, takeable=False):
603603
fill_value=self.fill_value)
604604
self._data = SingleBlockManager(values, index)
605605
self._index = index
606+
return self
606607
_set_value.__doc__ = set_value.__doc__
607608

608609
def _set_values(self, key, value):

0 commit comments

Comments
 (0)