Skip to content

Commit 3e92680

Browse files
Backport PR #38244: REGR: unstack on 'int' dtype prevent fillna to work (#38259)
1 parent ee18288 commit 3e92680

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/whatsnew/v1.1.5.rst

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Fixed regressions
2020
- Fixed regression in inplace operations on :class:`Series` with ``ExtensionDtype`` with NumPy dtyped operand (:issue:`37910`)
2121
- Fixed regression in metadata propagation for ``groupby`` iterator (:issue:`37343`)
2222
- Fixed regression in :class:`MultiIndex` constructed from a :class:`DatetimeIndex` not retaining frequency (:issue:`35563`)
23+
- Fixed regression in :meth:`DataFrame.unstack` with columns with integer dtype (:issue:`37115`)
2324
- Fixed regression in indexing on a :class:`Series` with ``CategoricalDtype`` after unpickling (:issue:`37631`)
2425
- Fixed regression in :meth:`DataFrame.groupby` aggregation with out-of-bounds datetime objects in an object-dtype column (:issue:`36003`)
2526
- Fixed regression in ``df.groupby(..).rolling(..)`` with the resulting :class:`MultiIndex` when grouping by a label that is in the index (:issue:`37641`)

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ def _unstack(self, unstacker, fill_value, new_placement):
14101410
new_values = new_values.T[mask]
14111411
new_placement = new_placement[mask]
14121412

1413-
blocks = [self.make_block_same_class(new_values, placement=new_placement)]
1413+
blocks = [make_block(new_values, placement=new_placement)]
14141414
return blocks, mask
14151415

14161416
def quantile(self, qs, interpolation="linear", axis: int = 0):

pandas/tests/frame/test_reshape.py

+23
Original file line numberDiff line numberDiff line change
@@ -1315,3 +1315,26 @@ def test_stack_positional_level_duplicate_column_names():
13151315
expected = pd.DataFrame([[1, 1], [1, 1]], index=new_index, columns=new_columns)
13161316

13171317
tm.assert_frame_equal(result, expected)
1318+
1319+
1320+
def test_unstack_with_missing_int_cast_to_float():
1321+
# https://github.com/pandas-dev/pandas/issues/37115
1322+
df = DataFrame(
1323+
{"a": ["A", "A", "B"], "b": ["ca", "cb", "cb"], "v": [10] * 3}
1324+
).set_index(["a", "b"])
1325+
1326+
# add another int column to get 2 blocks
1327+
df["is_"] = 1
1328+
assert len(df._mgr.blocks) == 2
1329+
1330+
result = df.unstack("b")
1331+
result[("is_", "ca")] = result[("is_", "ca")].fillna(0)
1332+
1333+
expected = DataFrame(
1334+
[[10.0, 10.0, 1.0, 1.0], [np.nan, 10.0, 0.0, 1.0]],
1335+
index=Index(["A", "B"], dtype="object", name="a"),
1336+
columns=MultiIndex.from_tuples(
1337+
[("v", "ca"), ("v", "cb"), ("is_", "ca"), ("is_", "cb")], names=[None, "b"],
1338+
),
1339+
)
1340+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)