Skip to content

Preserve EA dtype in DataFrame.stack #23285

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 16 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/master' into ea-stack
  • Loading branch information
TomAugspurger committed Nov 7, 2018
commit 88f08c7f39c14eb98e69fd2f30760a407841765a
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ update the ``ExtensionDtype._metadata`` tuple to match the signature of your
- :func:`ExtensionArray.isna` is allowed to return an ``ExtensionArray`` (:issue:`22325`).
- Support for reduction operations such as ``sum``, ``mean`` via opt-in base class method override (:issue:`22762`)
- :meth:`DataFrame.stack` no longer converts to object dtype for DataFrames where each column has the same extension dtype. The output Series will have the same dtype as the columns (:issue:`23077`).
- :meth:`Series.unstack` and :meth:`DataFrame.unstack` no longer convert extension arrays to object-dtype ndarrays. Each column in the output ``DataFrame`` will now have the same dtype as the input (:issue:`23077`).
- Bug when grouping :meth:`Dataframe.groupby()` and aggregating on ``ExtensionArray`` it was not returning the actual ``ExtensionArray`` dtype (:issue:`23227`).

.. _whatsnew_0240.api.incompatibilities:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def _astype(self, dtype, copy=False, errors='raise', values=None,

if klass is None:
if is_sparse(self.values):
# Series[Sparse].astype(object) is sparse.
# special case sparse, Series[Sparse].astype(object) is sparse
klass = ExtensionBlock
elif is_object_dtype(dtype):
klass = ObjectBlock
Expand Down
43 changes: 43 additions & 0 deletions pandas/tests/extension/base/reshaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,46 @@ def test_stack(self, data, columns):

result = result.astype(object)
self.assert_equal(result, expected)

@pytest.mark.parametrize("index", [
# Two levels, uniform.
pd.MultiIndex.from_product(([['A', 'B'], ['a', 'b']]),
names=['a', 'b']),

# non-uniform
pd.MultiIndex.from_tuples([('A', 'a'), ('A', 'b'), ('B', 'b')]),

# three levels, non-uniform
pd.MultiIndex.from_product([('A', 'B'), ('a', 'b', 'c'), (0, 1, 2)]),
pd.MultiIndex.from_tuples([
('A', 'a', 1),
('A', 'b', 0),
('A', 'a', 0),
('B', 'a', 0),
('B', 'c', 1),
]),
])
@pytest.mark.parametrize("obj", ["series", "frame"])
def test_unstack(self, data, index, obj):
data = data[:len(index)]
if obj == "series":
ser = pd.Series(data, index=index)
else:
ser = pd.DataFrame({"A": data, "B": data}, index=index)

n = index.nlevels
levels = list(range(n))
# [0, 1, 2]
# [(0,), (1,), (2,), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
combinations = itertools.chain.from_iterable(
itertools.permutations(levels, i) for i in range(1, n)
)

for level in combinations:
result = ser.unstack(level=level)
assert all(isinstance(result[col].values, type(data))
for col in result.columns)
expected = ser.astype(object).unstack(level=level)
result = result.astype(object)

self.assert_frame_equal(result, expected)
6 changes: 6 additions & 0 deletions pandas/tests/extension/json/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ def test_stack(self):
rows since we consider `{}` NA, but `.astype(object)` doesn't.
"""

@pytest.mark.xfail(reason="dict for NA", strict=True)
def test_unstack(self, data, index):
# The base test has NaN for the expected NA value.
# this matches otherwise
return super().test_unstack(data, index)


class TestGetitem(BaseJSON, base.BaseGetitemTests):
pass
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/frame/test_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,21 @@ def test_stack_preserve_categorical_dtype_values(self):
index=index)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize('level', [0, 1])
def test_unstack_mixed_extension_types(self, level):
index = pd.MultiIndex.from_tuples([('A', 0), ('A', 1), ('B', 1)],
names=['a', 'b'])
df = pd.DataFrame({"A": pd.core.arrays.integer_array([0, 1, None]),
"B": pd.Categorical(['a', 'a', 'b'])}, index=index)

result = df.unstack(level=level)
expected = df.astype(object).unstack(level=level)

expected_dtypes = pd.Series([df.A.dtype] * 2 + [df.B.dtype] * 2,
index=result.columns)
tm.assert_series_equal(result.dtypes, expected_dtypes)
tm.assert_frame_equal(result.astype(object), expected)

@pytest.mark.parametrize("level", [0, 'baz'])
def test_unstack_swaplevel_sortlevel(self, level):
# GH 20994
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.