Skip to content

BUG: unstack fails for mixed dtype subset #7462

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 2 commits 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
1 change: 1 addition & 0 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,4 @@ Bug Fixes

- Bug in non-monotonic ``Index.union`` may preserve ``name`` incorrectly (:issue:`7458`)
- Bug in ``DatetimeIndex.intersection`` doesn't preserve timezone (:issue:`4690`)
- Bug in ``unstack`` when columns contained mixed dtypes (:issue:`7405`).
5 changes: 5 additions & 0 deletions pandas/core/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ def get_new_index(self):

# construct the new index
if len(self.new_index_levels) == 1:
result_labels = result_labels[0]
new_index = self.new_index_levels[0]

if len(new_index) != len(result_labels):
new_index = new_index.take(result_labels)

new_index.name = self.new_index_names[0]
else:
new_index = MultiIndex(levels=self.new_index_levels,
Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -11706,6 +11706,38 @@ def test_unstack_non_unique_index_names(self):
with tm.assertRaises(ValueError):
df.T.stack('c1')

def test_unstack_mixed_dtypes(self):
n = 5
df = DataFrame({'A': ['a'] * n,
'B': pd.date_range('20120101', periods=n),
'C': np.zeros(n),
'D': np.zeros(n, dtype='i8')}).set_index(list("AB"))
expected = df.unstack(0).iloc[:3]
result = df.iloc[:3].unstack(0)
tm.assert_frame_equal(result, expected)

def test_unstack_mixed_dtypes_different_method(self):
n = 5
df = DataFrame({'A': ['a'] * n,
'B': pd.date_range('20120101', periods=n),
'C': np.zeros(n),
'D': np.zeros(n, dtype='i8')}).set_index(list("AB"))
result = df.iloc[:3].unstack(0)
other = df.iloc[:3].reset_index().set_index(list("AB")).unstack(0)
tm.assert_frame_equal(result, other)

def test_unstack_mixed_dtypes_with_mi_and_nat_different_method(self):
n = 5
B = pd.date_range('20120101', periods=n - 1).tolist()
B.insert(3, pd.NaT)
df = DataFrame({'A': ['a'] * n,
'B': B,
'C': np.zeros(n),
'D': np.zeros(n, dtype='i8')}).set_index(list("AB"))
result = df.iloc[:3].unstack(0)
other = df.iloc[:3].reset_index().set_index(list("AB")).unstack(0)
tm.assert_frame_equal(result, other)

def test_repr_with_mi_nat(self):
df = DataFrame({'X': [1, 2]},
index=[[pd.NaT, pd.Timestamp('20130101')], ['a', 'b']])
Expand Down