Skip to content

Commit efcf4b4

Browse files
BUG: reset_index is passing a bad dtype to NumPy (#35111)
1 parent 7453810 commit efcf4b4

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

pandas/core/frame.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
from pandas.core.dtypes.cast import (
7676
cast_scalar_to_array,
7777
coerce_to_dtypes,
78+
construct_1d_arraylike_from_scalar,
7879
find_common_type,
7980
infer_dtype_from_scalar,
8081
invalidate_string_dtypes,
@@ -109,7 +110,7 @@
109110
needs_i8_conversion,
110111
pandas_dtype,
111112
)
112-
from pandas.core.dtypes.missing import isna, notna
113+
from pandas.core.dtypes.missing import isna, na_value_for_dtype, notna
113114

114115
from pandas.core import algorithms, common as com, nanops, ops
115116
from pandas.core.accessor import CachedAccessor
@@ -4731,8 +4732,11 @@ def _maybe_casted_values(index, labels=None):
47314732
# we can have situations where the whole mask is -1,
47324733
# meaning there is nothing found in labels, so make all nan's
47334734
if mask.all():
4734-
values = np.empty(len(mask), dtype=index.dtype)
4735-
values.fill(np.nan)
4735+
dtype = index.dtype
4736+
fill_value = na_value_for_dtype(dtype)
4737+
values = construct_1d_arraylike_from_scalar(
4738+
fill_value, len(mask), dtype
4739+
)
47364740
else:
47374741
values = values.take(labels)
47384742

pandas/tests/frame/methods/test_reset_index.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import pytest
55

6+
import pandas as pd
67
from pandas import (
78
DataFrame,
89
Index,
@@ -299,9 +300,19 @@ def test_reset_index_range(self):
299300
tm.assert_frame_equal(result, expected)
300301

301302

302-
def test_reset_index_dtypes_on_empty_frame_with_multiindex():
303+
@pytest.mark.parametrize(
304+
"array, dtype",
305+
[
306+
(["a", "b"], object),
307+
(
308+
pd.period_range("12-1-2000", periods=2, freq="Q-DEC"),
309+
pd.PeriodDtype(freq="Q-DEC"),
310+
),
311+
],
312+
)
313+
def test_reset_index_dtypes_on_empty_frame_with_multiindex(array, dtype):
303314
# GH 19602 - Preserve dtype on empty DataFrame with MultiIndex
304-
idx = MultiIndex.from_product([[0, 1], [0.5, 1.0], ["a", "b"]])
315+
idx = MultiIndex.from_product([[0, 1], [0.5, 1.0], array])
305316
result = DataFrame(index=idx)[:0].reset_index().dtypes
306-
expected = Series({"level_0": np.int64, "level_1": np.float64, "level_2": object})
317+
expected = Series({"level_0": np.int64, "level_1": np.float64, "level_2": dtype})
307318
tm.assert_series_equal(result, expected)

pandas/tests/series/methods/test_reset_index.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33

4+
import pandas as pd
45
from pandas import DataFrame, Index, MultiIndex, RangeIndex, Series
56
import pandas._testing as tm
67

@@ -110,11 +111,21 @@ def test_reset_index_drop_errors(self):
110111
s.reset_index("wrong", drop=True)
111112

112113

113-
def test_reset_index_dtypes_on_empty_series_with_multiindex():
114+
@pytest.mark.parametrize(
115+
"array, dtype",
116+
[
117+
(["a", "b"], object),
118+
(
119+
pd.period_range("12-1-2000", periods=2, freq="Q-DEC"),
120+
pd.PeriodDtype(freq="Q-DEC"),
121+
),
122+
],
123+
)
124+
def test_reset_index_dtypes_on_empty_series_with_multiindex(array, dtype):
114125
# GH 19602 - Preserve dtype on empty Series with MultiIndex
115-
idx = MultiIndex.from_product([[0, 1], [0.5, 1.0], ["a", "b"]])
126+
idx = MultiIndex.from_product([[0, 1], [0.5, 1.0], array])
116127
result = Series(dtype=object, index=idx)[:0].reset_index().dtypes
117128
expected = Series(
118-
{"level_0": np.int64, "level_1": np.float64, "level_2": object, 0: object}
129+
{"level_0": np.int64, "level_1": np.float64, "level_2": dtype, 0: object}
119130
)
120131
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)