Skip to content

BUG: assign doesnt cast SparseDataFrame to DataFrame #19178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 12, 2018
Merged
Prev Previous commit
Next Next commit
BUG: Fixes problem with SparseArray coercing to float if index is passed
  • Loading branch information
hexgnu committed Jan 19, 2018
commit 686ef8e8120dd0fd52bd31b3ed65ecbee1ba1b43
5 changes: 2 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2649,7 +2649,7 @@ def insert(self, loc, column, value, allow_duplicates=False):
allow_duplicates=allow_duplicates)

def assign(self, **kwargs):
r"""
"""
Assign new columns to a DataFrame, returning a new object
(a copy) with all the original columns in addition to the new ones.

Expand Down Expand Up @@ -2714,8 +2714,7 @@ def assign(self, **kwargs):
9 10 -0.758542 2.302585
"""

# See GH19163
data = self.copy().to_dense()
data = self.copy()

# do all calculations first...
results = OrderedDict()
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
is_scalar, is_dtype_equal)
from pandas.core.dtypes.cast import (
maybe_convert_platform, maybe_promote,
astype_nansafe, find_common_type)
astype_nansafe, find_common_type, infer_dtype_from)
from pandas.core.dtypes.missing import isna, notna, na_value_for_dtype

import pandas._libs.sparse as splib
Expand Down Expand Up @@ -195,7 +195,7 @@ def __new__(cls, data, sparse_index=None, index=None, kind='integer',
data = np.nan
if not is_scalar(data):
raise Exception("must only pass scalars with an index ")
values = np.empty(len(index), dtype='float64')
values = np.empty(len(index), dtype=infer_dtype_from(data)[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is infer_dtype_from_scalar more appropriate here, since we've validated that data is a scalar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea that's a good point. Since infer_dtype_from just checks is_scalar again.

values.fill(data)
data = values

Expand Down
11 changes: 2 additions & 9 deletions pandas/tests/frame/test_mutate_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import numpy as np
from pandas.compat import PY36

from pandas import DataFrame, Series, Index, MultiIndex
from pandas import DataFrame, Series, Index, MultiIndex, SparseSeries

from pandas.util.testing import assert_frame_equal
from pandas.util.testing import assert_frame_equal, assert_series_equal

import pandas.util.testing as tm

Expand Down Expand Up @@ -55,13 +55,6 @@ def test_assign(self):
result = df.assign(A=lambda x: x.A + x.B)
assert_frame_equal(result, expected)

# SparseDataFrame
# See GH 19163
result = df.to_sparse(fill_value=False).assign(newcol=False)
expected = df.assign(newcol=False)
assert type(result) is DataFrame
assert_frame_equal(expected, result)

def test_assign_multiple(self):
df = DataFrame([[1, 4], [2, 5], [3, 6]], columns=['A', 'B'])
result = df.assign(C=[7, 8, 9], D=df.A, E=lambda x: x.B)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/sparse/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1271,3 +1271,15 @@ def test_quantile_multi(self):

tm.assert_frame_equal(result, dense_expected)
tm.assert_sp_frame_equal(result, sparse_expected)

def test_assign_with_sparse_frame(self):
# GH 19163
df = pd.DataFrame({"a":[1,2,3]})
res = df.to_sparse(fill_value=False).assign(newcol=False)
exp = df.assign(newcol=False).to_sparse(fill_value=False)

tm.assert_sp_frame_equal(res, exp)

for column in res.columns:
assert type(res[column]) is SparseSeries

11 changes: 11 additions & 0 deletions pandas/tests/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ def test_constructor_spindex_dtype(self):
assert arr.dtype == np.int64
assert arr.fill_value == 0

@pytest.mark.parametrize('scalar,dtype', [(False, bool), (0.0, 'float64'), (1, 'int64'), ('z', 'object')])
def test_scalar_with_index_infer_dtype(self, scalar, dtype):
# GH 19163
arr = SparseArray(scalar, index=[1,2,3], fill_value=scalar)
exp = SparseArray([scalar, scalar, scalar], fill_value=scalar)

tm.assert_sp_array_equal(arr, exp)

assert arr.dtype == dtype
assert exp.dtype == dtype

def test_sparseseries_roundtrip(self):
# GH 13999
for kind in ['integer', 'block']:
Expand Down