Skip to content

BUG: SparseDataFrame construction with lists not coercing to dtype (GH 15682) #15834

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ Bug Fixes
- Bug in ``pd.concat()`` in which concatting with an empty dataframe with ``join='inner'`` was being improperly handled (:issue:`15328`)
- Bug in ``groupby.agg()`` incorrectly localizing timezone on ``datetime`` (:issue:`15426`, :issue:`10668`, :issue:`13046`)


- Bug in ``SparseDataFrame`` construction with lists not coercing to dtype (:issue:`15682`)

- Bug in ``.read_csv()`` with ``parse_dates`` when multiline headers are specified (:issue:`15376`)
- Bug in ``groupby.transform()`` that would coerce the resultant dtypes back to the original (:issue:`10972`, :issue:`11444`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def _init_dict(self, data, index, columns, dtype=None):
v = [v.get(i, nan) for i in index]

v = sp_maker(v)
sdict[k] = v
sdict[k] = v.astype(np.dtype(dtype)) if dtype is not None else v
Copy link
Contributor

Choose a reason for hiding this comment

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

you would do this in sp_maker, also don't use if/else on a single line (hard to read)

Copy link
Author

Choose a reason for hiding this comment

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

done


# TODO: figure out how to handle this case, all nan's?
# add in any other columns we want to have (completeness)
Expand Down
22 changes: 14 additions & 8 deletions pandas/tests/sparse/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@


class TestSparseDataFrame(tm.TestCase, SharedWithSparse):

klass = SparseDataFrame

def setUp(self):
Expand Down Expand Up @@ -758,7 +757,8 @@ def test_sparse_frame_fillna_limit(self):
def test_rename(self):
# just check this works
renamed = self.frame.rename(index=str) # noqa
renamed = self.frame.rename(columns=lambda x: '%s%d' % (x, len(x))) # noqa
Copy link
Contributor

Choose a reason for hiding this comment

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

can you compare these results (as no tests before).

Copy link
Author

Choose a reason for hiding this comment

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

didn't understand

Copy link
Author

Choose a reason for hiding this comment

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

ok, just figured it out, added the right test on my last commit! :)

renamed = self.frame.rename(
columns=lambda x: '%s%d' % (x, len(x))) # noqa

def test_corr(self):
res = self.frame.corr()
Expand Down Expand Up @@ -967,7 +967,6 @@ def _check(frame, orig):
def test_shift(self):

def _check(frame, orig):

shifted = frame.shift(0)
exp = orig.shift(0)
tm.assert_frame_equal(shifted.to_dense(), exp)
Expand Down Expand Up @@ -1060,7 +1059,7 @@ def test_sparse_pow_issue(self):
df = SparseDataFrame({'A': [nan, 0, 1]})

# note that 2 ** df works fine, also df ** 1
result = 1**df
result = 1 ** df

r1 = result.take([0], 1)['A']
r2 = result['A']
Expand Down Expand Up @@ -1126,7 +1125,7 @@ def test_isnotnull(self):
tm.assert_frame_equal(res.to_dense(), exp)


@pytest.mark.parametrize('index', [None, list('ab')]) # noqa: F811
@pytest.mark.parametrize('index', [None, list('ab')]) # noqa: F811
@pytest.mark.parametrize('columns', [None, list('cd')])
@pytest.mark.parametrize('fill_value', [None, 0, np.nan])
@pytest.mark.parametrize('dtype', [bool, int, float, np.uint16])
Expand Down Expand Up @@ -1180,7 +1179,7 @@ def test_from_to_scipy(spmatrix, index, columns, fill_value, dtype):
tm.assert_equal(sdf.to_coo().dtype, np.object_)


@pytest.mark.parametrize('fill_value', [None, 0, np.nan]) # noqa: F811
@pytest.mark.parametrize('fill_value', [None, 0, np.nan]) # noqa: F811
def test_from_to_scipy_object(spmatrix, fill_value):
# GH 4343
dtype = object
Expand Down Expand Up @@ -1226,7 +1225,6 @@ def test_from_to_scipy_object(spmatrix, fill_value):


class TestSparseDataFrameArithmetic(tm.TestCase):

Copy link
Contributor

Choose a reason for hiding this comment

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

try to turn off this removing blank lines like this

Copy link
Author

Choose a reason for hiding this comment

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

just put it back, sorry

def test_numeric_op_scalar(self):
df = pd.DataFrame({'A': [nan, nan, 0, 1, ],
'B': [0, 1, 2, nan],
Expand Down Expand Up @@ -1255,7 +1253,6 @@ def test_comparison_op_scalar(self):


class TestSparseDataFrameAnalytics(tm.TestCase):

def setUp(self):
self.data = {'A': [nan, nan, nan, 0, 1, 2, 3, 4, 5, 6],
'B': [0, 1, 2, nan, nan, nan, 3, 4, 5, 6],
Expand Down Expand Up @@ -1299,3 +1296,12 @@ def test_numpy_func_call(self):
'std', 'min', 'max']
for func in funcs:
getattr(np, func)(self.frame)

def test_type_coercion_at_construction(self):
# GH 15682
df = pd.SparseDataFrame(
{'a': [1, 0, 0], 'b': [0, 1, 0], 'c': [0, 0, 1]}, dtype='uint8',
default_fill_value=0)
result = df.dtypes[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

create the result and use assert_sp_frame_equal

Copy link
Contributor

Choose a reason for hiding this comment

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

you can use the comparison from the issue (IOW construct both ways and compare)

Copy link
Contributor

Choose a reason for hiding this comment

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

move this test where other test_constructor tests are

Copy link
Author

Choose a reason for hiding this comment

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

done

expected = np.dtype('uint8')
assert result == expected