Skip to content

Commit 052c775

Browse files
mroeschkepmhatre1
authored andcommitted
PERF: reindex default fill_value dtype (pandas-dev#57272)
1 parent 8ba3bda commit 052c775

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

doc/source/whatsnew/v3.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ Numeric
170170
Conversion
171171
^^^^^^^^^^
172172
- Bug in :meth:`Series.astype` might modify read-only array inplace when casting to a string dtype (:issue:`57212`)
173-
-
173+
- Bug in :meth:`Series.reindex` not maintaining ``float32`` type when a ``reindex`` introduces a missing value (:issue:`45857`)
174174

175175
Strings
176176
^^^^^^^

pandas/core/internals/managers.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1051,8 +1051,12 @@ def _make_na_block(
10511051
nb = NumpyBlock(vals, placement, ndim=2)
10521052
return nb
10531053

1054-
if fill_value is None:
1054+
if fill_value is None or fill_value is np.nan:
10551055
fill_value = np.nan
1056+
# GH45857 avoid unnecessary upcasting
1057+
dtype = interleaved_dtype([blk.dtype for blk in self.blocks])
1058+
if dtype is not None and np.issubdtype(dtype.type, np.floating):
1059+
fill_value = dtype.type(fill_value)
10561060

10571061
shape = (len(placement), self.shape[1])
10581062

pandas/tests/frame/methods/test_reindex.py

+6
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,12 @@ def test_reindex_with_nans(self):
10181018
expected = df.iloc[[1]]
10191019
tm.assert_frame_equal(result, expected)
10201020

1021+
def test_reindex_without_upcasting(self):
1022+
# GH45857
1023+
df = DataFrame(np.zeros((10, 10), dtype=np.float32))
1024+
result = df.reindex(columns=np.arange(5, 15))
1025+
assert result.dtypes.eq(np.float32).all()
1026+
10211027
def test_reindex_multi(self):
10221028
df = DataFrame(np.random.default_rng(2).standard_normal((3, 3)))
10231029

0 commit comments

Comments
 (0)