Skip to content

Commit

Permalink
Backport PR pandas-dev#38244: REGR: unstack on 'int' dtype prevent fi…
Browse files Browse the repository at this point in the history
…llna to work (pandas-dev#38259)
  • Loading branch information
simonjayhawkins authored Dec 3, 2020
1 parent ee18288 commit 3e92680
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Fixed regressions
- Fixed regression in inplace operations on :class:`Series` with ``ExtensionDtype`` with NumPy dtyped operand (:issue:`37910`)
- Fixed regression in metadata propagation for ``groupby`` iterator (:issue:`37343`)
- Fixed regression in :class:`MultiIndex` constructed from a :class:`DatetimeIndex` not retaining frequency (:issue:`35563`)
- Fixed regression in :meth:`DataFrame.unstack` with columns with integer dtype (:issue:`37115`)
- Fixed regression in indexing on a :class:`Series` with ``CategoricalDtype`` after unpickling (:issue:`37631`)
- Fixed regression in :meth:`DataFrame.groupby` aggregation with out-of-bounds datetime objects in an object-dtype column (:issue:`36003`)
- Fixed regression in ``df.groupby(..).rolling(..)`` with the resulting :class:`MultiIndex` when grouping by a label that is in the index (:issue:`37641`)
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 @@ -1410,7 +1410,7 @@ def _unstack(self, unstacker, fill_value, new_placement):
new_values = new_values.T[mask]
new_placement = new_placement[mask]

blocks = [self.make_block_same_class(new_values, placement=new_placement)]
blocks = [make_block(new_values, placement=new_placement)]
return blocks, mask

def quantile(self, qs, interpolation="linear", axis: int = 0):
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/frame/test_reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,3 +1315,26 @@ def test_stack_positional_level_duplicate_column_names():
expected = pd.DataFrame([[1, 1], [1, 1]], index=new_index, columns=new_columns)

tm.assert_frame_equal(result, expected)


def test_unstack_with_missing_int_cast_to_float():
# https://github.com/pandas-dev/pandas/issues/37115
df = DataFrame(
{"a": ["A", "A", "B"], "b": ["ca", "cb", "cb"], "v": [10] * 3}
).set_index(["a", "b"])

# add another int column to get 2 blocks
df["is_"] = 1
assert len(df._mgr.blocks) == 2

result = df.unstack("b")
result[("is_", "ca")] = result[("is_", "ca")].fillna(0)

expected = DataFrame(
[[10.0, 10.0, 1.0, 1.0], [np.nan, 10.0, 0.0, 1.0]],
index=Index(["A", "B"], dtype="object", name="a"),
columns=MultiIndex.from_tuples(
[("v", "ca"), ("v", "cb"), ("is_", "ca"), ("is_", "cb")], names=[None, "b"],
),
)
tm.assert_frame_equal(result, expected)

0 comments on commit 3e92680

Please sign in to comment.