From bb63b1f35a6edc9dfc8c4b1b75e639a375c5d6b7 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 22 Aug 2019 17:57:14 -0700 Subject: [PATCH 1/4] BUG: SparseDataFrame op incorrectly casting to float --- pandas/core/sparse/frame.py | 8 ++++---- pandas/tests/sparse/frame/test_frame.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pandas/core/sparse/frame.py b/pandas/core/sparse/frame.py index f5add426297a7..3d6ba0b8d9774 100644 --- a/pandas/core/sparse/frame.py +++ b/pandas/core/sparse/frame.py @@ -569,15 +569,15 @@ def _combine_frame(self, other, func, fill_value=None, level=None): ).__finalize__(self) def _combine_match_index(self, other, func, level=None): - new_data = {} if level is not None: raise NotImplementedError("'level' argument is not supported") this, other = self.align(other, join="outer", axis=0, level=level, copy=False) - for col, series in this.items(): - new_data[col] = func(series.values, other.values) + new_data = {} + for col in this.columns: + new_data[col] = func(this[col], other) fill_value = self._get_op_result_fill_value(other, func) @@ -603,7 +603,7 @@ def _combine_match_columns(self, other, func, level=None): new_data = {} for col in left.columns: - new_data[col] = func(left[col], float(right[col])) + new_data[col] = func(left[col], right[col]) return self._constructor( new_data, diff --git a/pandas/tests/sparse/frame/test_frame.py b/pandas/tests/sparse/frame/test_frame.py index ddb50e0897a86..b82d63f95dfdd 100644 --- a/pandas/tests/sparse/frame/test_frame.py +++ b/pandas/tests/sparse/frame/test_frame.py @@ -1487,6 +1487,18 @@ def test_comparison_op_scalar(self): assert isinstance(res, pd.SparseDataFrame) tm.assert_frame_equal(res.to_dense(), df != 0) + def test_add_series_retains_dtype(self): + # SparseDataFrame._combine_match_columns used to incorrectly cast + # to float + d = {0: [2j, 3j], 1: [0, 1]} + sdf = SparseDataFrame(data=d, default_fill_value=1) + result = sdf + sdf[0] + + df = sdf.to_dense() + dresult = df + df[0] + expected = dresult.to_sparse(fill_value=1) + tm.assert_sp_frame_equal(result, expected) + @pytest.mark.filterwarnings("ignore:Sparse:FutureWarning") @pytest.mark.filterwarnings("ignore:DataFrame.to_sparse:FutureWarning") From d72c838c9c2096363de5aff964ac0675c914d866 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 22 Aug 2019 18:48:29 -0700 Subject: [PATCH 2/4] dont test fill_value --- pandas/tests/sparse/frame/test_frame.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/tests/sparse/frame/test_frame.py b/pandas/tests/sparse/frame/test_frame.py index b82d63f95dfdd..bc2d033316467 100644 --- a/pandas/tests/sparse/frame/test_frame.py +++ b/pandas/tests/sparse/frame/test_frame.py @@ -1495,9 +1495,8 @@ def test_add_series_retains_dtype(self): result = sdf + sdf[0] df = sdf.to_dense() - dresult = df + df[0] - expected = dresult.to_sparse(fill_value=1) - tm.assert_sp_frame_equal(result, expected) + expected = df + df[0] + tm.assert_frame_equal(result.to_dense(), expected) @pytest.mark.filterwarnings("ignore:Sparse:FutureWarning") From 97ea4a720dc66413913f1e135fe5db3e4afae4f1 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 22 Aug 2019 19:12:06 -0700 Subject: [PATCH 3/4] explicit consturct --- pandas/tests/sparse/frame/test_frame.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/tests/sparse/frame/test_frame.py b/pandas/tests/sparse/frame/test_frame.py index bc2d033316467..e372e2563e682 100644 --- a/pandas/tests/sparse/frame/test_frame.py +++ b/pandas/tests/sparse/frame/test_frame.py @@ -1498,6 +1498,11 @@ def test_add_series_retains_dtype(self): expected = df + df[0] tm.assert_frame_equal(result.to_dense(), expected) + # Make it explicit to be on the safe side + edata = {0: [4j, 5j], 1: [3j, 1 + 3j]} + expected = DataFrame(edata) + tm.assert_frame_equal(result.to_dense(), expected) + @pytest.mark.filterwarnings("ignore:Sparse:FutureWarning") @pytest.mark.filterwarnings("ignore:DataFrame.to_sparse:FutureWarning") From 52d3f874c169b56f0a5c8f21b0a35a1bede3c08c Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 27 Aug 2019 11:43:46 -0700 Subject: [PATCH 4/4] whatsnew --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 7a10447e3ad40..76aadd2c97b67 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -187,7 +187,7 @@ Reshaping Sparse ^^^^^^ - +- Bug in :class:`SparseDataFrame` arithmetic operations incorrectly casting inputs to float (:issue:`28107`) - -