Skip to content

PERF: reindex default fill_value dtype #47281

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 9 commits into from
Closed
8 changes: 7 additions & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,10 +821,16 @@ def _make_na_block(

if fill_value is None:
fill_value = np.nan
# GH45857 avoid unnecessary upcasting
dtype = interleaved_dtype([blk.dtype for blk in self.blocks])
if dtype is not None and np.issubdtype(dtype.type, np.floating):
fill_value = dtype.type(fill_value)

dtype, fill_value = infer_dtype_from_scalar(fill_value)

block_shape = list(self.shape)
block_shape[0] = len(placement)

dtype, fill_value = infer_dtype_from_scalar(fill_value)
# error: Argument "dtype" to "empty" has incompatible type "Union[dtype,
# ExtensionDtype]"; expected "Union[dtype, None, type, _SupportsDtype, str,
# Tuple[Any, int], Tuple[Any, Union[int, Sequence[int]]], List[Any], _DtypeDict,
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/frame/methods/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,12 @@ def test_reindex_with_nans(self):
expected = df.iloc[[1]]
tm.assert_frame_equal(result, expected)

def test_reindex_without_upcasting(self):
# GH45857
df = DataFrame(np.zeros((10, 10), dtype=np.float32))
result = df.reindex(columns=np.arange(5, 15))
assert result.dtypes.eq(np.float32).all()

def test_reindex_multi(self):
df = DataFrame(np.random.randn(3, 3))

Expand Down