Skip to content

Commit cb04ad7

Browse files
REGR: unstack on 'int' dtype prevent fillna to work (#38244)
1 parent 73d0d34 commit cb04ad7

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-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
@@ -1542,7 +1542,7 @@ def _unstack(self, unstacker, fill_value, new_placement):
15421542
new_values = new_values.T[mask]
15431543
new_placement = new_placement[mask]
15441544

1545-
blocks = [self.make_block_same_class(new_values, placement=new_placement)]
1545+
blocks = [make_block(new_values, placement=new_placement)]
15461546
return blocks, mask
15471547

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

pandas/tests/frame/test_stack_unstack.py

+27
Original file line numberDiff line numberDiff line change
@@ -1880,3 +1880,30 @@ def test_unstack_group_index_overflow(self):
18801880
s = Series(np.arange(1000), index=index)
18811881
result = s.unstack(4)
18821882
assert result.shape == (500, 2)
1883+
1884+
def test_unstack_with_missing_int_cast_to_float(self):
1885+
# https://github.com/pandas-dev/pandas/issues/37115
1886+
df = DataFrame(
1887+
{
1888+
"a": ["A", "A", "B"],
1889+
"b": ["ca", "cb", "cb"],
1890+
"v": [10] * 3,
1891+
}
1892+
).set_index(["a", "b"])
1893+
1894+
# add another int column to get 2 blocks
1895+
df["is_"] = 1
1896+
assert len(df._mgr.blocks) == 2
1897+
1898+
result = df.unstack("b")
1899+
result[("is_", "ca")] = result[("is_", "ca")].fillna(0)
1900+
1901+
expected = DataFrame(
1902+
[[10.0, 10.0, 1.0, 1.0], [np.nan, 10.0, 0.0, 1.0]],
1903+
index=Index(["A", "B"], dtype="object", name="a"),
1904+
columns=MultiIndex.from_tuples(
1905+
[("v", "ca"), ("v", "cb"), ("is_", "ca"), ("is_", "cb")],
1906+
names=[None, "b"],
1907+
),
1908+
)
1909+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)