Skip to content

Commit 2e833fa

Browse files
committed
BUG: multi-type sparse slicing fixes and improvements
Types were incorrectly determined when slicing SparseDataFrames with multiple dtypes (such as float and object). Also enables type inference for SparseArrays by default.
1 parent b7abef4 commit 2e833fa

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pandas/core/internals.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4439,7 +4439,10 @@ def _lcd_dtype(l):
44394439
""" find the lowest dtype that can accomodate the given types """
44404440
m = l[0].dtype
44414441
for x in l[1:]:
4442-
if x.dtype.itemsize > m.itemsize:
4442+
# the new dtype must either be wider or a strict subtype
4443+
if (x.dtype.itemsize > m.itemsize or
4444+
(np.issubdtype(m, x.dtype) and
4445+
not np.issubdtype(x.dtype, m))):
44434446
m = x.dtype
44444447
return m
44454448

pandas/tests/frame/test_indexing.py

+8
Original file line numberDiff line numberDiff line change
@@ -2712,6 +2712,14 @@ def test_type_error_multiindex(self):
27122712
result = dg['x', 0]
27132713
assert_series_equal(result, expected)
27142714

2715+
def test_sparse_indexing_single_multitype(self):
2716+
from pandas import SparseDataFrame, SparseSeries
2717+
sdf = SparseDataFrame([[1, 2, 'a'], [4, 5, 'b']])
2718+
tm.assert_sp_series_equal(sdf.iloc[0],
2719+
SparseSeries([1, 2, 'a'], name=0))
2720+
tm.assert_sp_series_equal(sdf.iloc[1],
2721+
SparseSeries([4, 5, 'b'], name=1))
2722+
27152723

27162724
class TestDataFrameIndexingDatetimeWithTZ(tm.TestCase, TestData):
27172725

0 commit comments

Comments
 (0)