Skip to content

TST: fix test creating invalid CategoricalBlock #32519

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 5 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,10 @@ def __init__(self, values, placement, ndim=None):
values = self._maybe_coerce_values(values)
super().__init__(values, placement, ndim)

if self.ndim == 2 and len(self.mgr_locs) != 1:
Copy link
Contributor

Choose a reason for hiding this comment

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

woa, why is this here? shouldn't this be in a higher level block construction?

Copy link
Member Author

Choose a reason for hiding this comment

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

either goes here or in NonconsolidateableMixin (which should be merged into ExtensionBlock anyway)

Copy link
Contributor

Choose a reason for hiding this comment

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

no this is not what i mean. how can we possibly get here when passing a Categorical? and if you can't except by directly constructing a manager, then this should be an assertion error.

Copy link
Member Author

Choose a reason for hiding this comment

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

then this should be an assertion error.

sure, will update

# TODO(2DEA): check unnecessary with 2D EAs
raise AssertionError("block.size != values.size")

def _maybe_coerce_values(self, values):
"""
Unbox to an extension array.
Expand Down
13 changes: 8 additions & 5 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,13 @@ def _compare(old_mgr, new_mgr):
assert new_mgr.get("g").dtype == np.float64
assert new_mgr.get("h").dtype == np.float16

def test_invalid_ea_block(self):
with pytest.raises(AssertionError, match="block.size != values.size"):
create_mgr("a: category; b: category")

with pytest.raises(AssertionError, match="block.size != values.size"):
create_mgr("a: category2; b: category2")

def test_interleave(self):

# self
Expand All @@ -552,14 +559,10 @@ def test_interleave(self):
# will be converted according the actual dtype of the underlying
mgr = create_mgr("a: category")
assert mgr.as_array().dtype == "i8"
mgr = create_mgr("a: category; b: category")
assert mgr.as_array().dtype == "i8"
mgr = create_mgr("a: category; b: category2")
assert mgr.as_array().dtype == "object"
mgr = create_mgr("a: category2")
assert mgr.as_array().dtype == "object"
mgr = create_mgr("a: category2; b: category2")
assert mgr.as_array().dtype == "object"

# combinations
mgr = create_mgr("a: f8")
Expand Down Expand Up @@ -702,7 +705,7 @@ def test_equals(self):
"a:i8;b:f8", # basic case
"a:i8;b:f8;c:c8;d:b", # many types
"a:i8;e:dt;f:td;g:string", # more types
"a:i8;b:category;c:category2;d:category2", # categories
"a:i8;b:category;c:category2", # categories
"c:sparse;d:sparse_na;b:f8", # sparse
],
)
Expand Down