File tree 3 files changed +12
-2
lines changed
3 files changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -170,7 +170,7 @@ Numeric
170
170
Conversion
171
171
^^^^^^^^^^
172
172
- 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 `)
174
174
175
175
Strings
176
176
^^^^^^^
Original file line number Diff line number Diff line change @@ -1051,8 +1051,12 @@ def _make_na_block(
1051
1051
nb = NumpyBlock (vals , placement , ndim = 2 )
1052
1052
return nb
1053
1053
1054
- if fill_value is None :
1054
+ if fill_value is None or fill_value is np . nan :
1055
1055
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 )
1056
1060
1057
1061
shape = (len (placement ), self .shape [1 ])
1058
1062
Original file line number Diff line number Diff line change @@ -1018,6 +1018,12 @@ def test_reindex_with_nans(self):
1018
1018
expected = df .iloc [[1 ]]
1019
1019
tm .assert_frame_equal (result , expected )
1020
1020
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
+
1021
1027
def test_reindex_multi (self ):
1022
1028
df = DataFrame (np .random .default_rng (2 ).standard_normal ((3 , 3 )))
1023
1029
You can’t perform that action at this time.
0 commit comments