Skip to content

Commit f9ac037

Browse files
committed
BUG: fix setting dataframe column to a sparse array
1 parent 9df3ead commit f9ac037

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/v0.15.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -910,3 +910,4 @@ Bug Fixes
910910
- Bug where ``col_space`` was ignored in ``DataFrame.to_string()`` when ``header=False`` (:issue:`8230`).
911911
- Bug with ``DatetimeIndex.asof`` incorrectly matching partial strings and returning the wrong date (:issue:`8245`).
912912
- Bug in plotting methods modifying the global matplotlib rcParams (:issue:`8242`).
913+
- Bug in ``DataFrame.__setitem__`` that caused errors when setting a dataframe column to a sparse array (:issue:`8131`)

pandas/core/frame.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from pandas.compat import(range, zip, lrange, lmap, lzip, StringIO, u,
4444
OrderedDict, raise_with_traceback)
4545
from pandas import compat
46+
from pandas.sparse.array import SparseArray
4647
from pandas.util.decorators import deprecate, Appender, Substitution, \
4748
deprecate_kwarg
4849

@@ -2164,8 +2165,8 @@ def reindexer(value):
21642165
value = np.repeat(value, len(self.index)).astype(dtype)
21652166
value = com._possibly_cast_to_datetime(value, dtype)
21662167

2167-
# return categoricals directly
2168-
if isinstance(value, Categorical):
2168+
# return unconsolidatables directly
2169+
if isinstance(value, (Categorical, SparseArray)):
21692170
return value
21702171

21712172
# broadcast across multiple columns if necessary

pandas/tests/test_frame.py

+14
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,20 @@ def test_getitem_ix_float_duplicates(self):
17911791
expect = df.iloc[[1, -1], 0]
17921792
tm.assert_series_equal(df.loc[0.2, 'a'], expect)
17931793

1794+
def test_setitem_with_sparse_value(self):
1795+
# GH8131
1796+
df = pd.DataFrame({'c_1':['a', 'b', 'c'], 'n_1': [1., 2., 3.]})
1797+
sp_series = pd.Series([0, 0, 1]).to_sparse(fill_value=0)
1798+
df['new_column'] = sp_series
1799+
tm.assert_series_equal(df['new_column'], sp_series)
1800+
1801+
def test_setitem_with_unaligned_sparse_value(self):
1802+
df = pd.DataFrame({'c_1':['a', 'b', 'c'], 'n_1': [1., 2., 3.]})
1803+
sp_series = (pd.Series([0, 0, 1], index=[2, 1, 0])
1804+
.to_sparse(fill_value=0))
1805+
df['new_column'] = sp_series
1806+
tm.assert_series_equal(df['new_column'], pd.Series([1, 0, 0]))
1807+
17941808

17951809
_seriesd = tm.getSeriesData()
17961810
_tsd = tm.getTimeSeriesData()

0 commit comments

Comments
 (0)