Skip to content

BUG: GH11847 Unstack with mixed dtypes coerces everything to object #14053

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 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.19.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ Bug Fixes


- Explicit check in ``to_stata`` and ``StataWriter`` for out-of-range values when writing doubles (:issue:`14618`)

- Bug in ``unstack()`` if called with a list of column(s) as an argument, regardless of the dtypes of all columns, they get coerced to ``object`` (:issue:`11847`)
6 changes: 4 additions & 2 deletions pandas/core/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ def _unstack_multiple(data, clocs):
verify_integrity=False)

if isinstance(data, Series):
dummy = Series(data.values, index=dummy_index)
dummy = data.copy()
dummy.index = dummy_index
unstacked = dummy.unstack('__placeholder__')
new_levels = clevels
new_names = cnames
Expand All @@ -292,7 +293,8 @@ def _unstack_multiple(data, clocs):

return result

dummy = DataFrame(data.values, index=dummy_index, columns=data.columns)
dummy = data.copy()
dummy.index = dummy_index

unstacked = dummy.unstack('__placeholder__')
if isinstance(unstacked, Series):
Expand Down
40 changes: 40 additions & 0 deletions pandas/tests/frame/test_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,46 @@ def test_unstack_fill_frame_categorical(self):
index=list('xyz'))
assert_frame_equal(result, expected)

def test_unstack_preserve_dtypes(self):
# Checks fix for #11847
Copy link
Contributor

Choose a reason for hiding this comment

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

test with more dtypes

Copy link
Contributor

Choose a reason for hiding this comment

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

use more types as the index

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 write a 1-liner that this is checking for a list of unstacks is the same as as repeated unstacking.

Copy link
Member

Choose a reason for hiding this comment

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

@jreback the test examples are not repeated unstacking, just comparing 'index' with ['index']

df = pd.DataFrame(dict(state=['IL', 'MI', 'NC'],
index=['a', 'b', 'c'],
some_categories=pd.Series(['a', 'b', 'c']
).astype('category'),
A=np.random.rand(3),
B=1,
C='foo',
D=pd.Timestamp('20010102'),
E=pd.Series([1.0, 50.0, 100.0]
).astype('float32'),
F=pd.Series([3.0, 4.0, 5.0]).astype('float64'),
G=False,
H=pd.Series([1, 200, 923442], dtype='int8')))

def unstack_and_compare(df, column_name):
unstacked1 = df.unstack([column_name])
unstacked2 = df.unstack(column_name)
assert_frame_equal(unstacked1, unstacked2)

df1 = df.set_index(['state', 'index'])
unstack_and_compare(df1, 'index')

df1 = df.set_index(['state', 'some_categories'])
unstack_and_compare(df1, 'some_categories')

df1 = df.set_index(['F', 'C'])
unstack_and_compare(df1, 'F')

df1 = df.set_index(['G', 'B', 'state'])
unstack_and_compare(df1, 'B')

df1 = df.set_index(['E', 'A'])
unstack_and_compare(df1, 'E')

df1 = df.set_index(['state', 'index'])
s = df1['A']
unstack_and_compare(s, 'index')

def test_stack_ints(self):
columns = MultiIndex.from_tuples(list(itertools.product(range(3),
repeat=3)))
Expand Down