Skip to content

Commit d0afefa

Browse files
authored
BUG: NumericIndex.insert(0, False) casting to int (#38030)
1 parent 27989a6 commit d0afefa

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ Indexing
625625
- Bug in :meth:`DataFrame.xs` ignored ``droplevel=False`` for columns (:issue:`19056`)
626626
- Bug in :meth:`DataFrame.reindex` raising ``IndexingError`` wrongly for empty DataFrame with ``tolerance`` not None or ``method="nearest"`` (:issue:`27315`)
627627
- Bug in indexing on a :class:`Series` or :class:`DataFrame` with a :class:`CategoricalIndex` using listlike indexer that contains elements that are in the index's ``categories`` but not in the index itself failing to raise ``KeyError`` (:issue:`37901`)
628+
- Bug on inserting a boolean label into a :class:`DataFrame` with a numeric :class:`Index` columns incorrectly casting to integer (:issue:`36319`)
628629
- Bug in :meth:`DataFrame.iloc` and :meth:`Series.iloc` aligning objects in ``__setitem__`` (:issue:`22046`)
629630
- Bug in :meth:`DataFrame.loc` did not raise ``KeyError`` when missing combination was given with ``slice(None)`` for remaining levels (:issue:`19556`)
630631
- Bug in :meth:`DataFrame.loc` raising ``TypeError`` when non-integer slice was given to select values from :class:`MultiIndex` (:issue:`25165`, :issue:`24263`)

pandas/core/indexes/numeric.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ def _validate_fill_value(self, value):
122122
raise TypeError
123123
elif isinstance(value, str) or lib.is_complex(value):
124124
raise TypeError
125+
elif is_scalar(value) and isna(value):
126+
if is_valid_nat_for_dtype(value, self.dtype):
127+
value = self._na_value
128+
else:
129+
# NaT, np.datetime64("NaT"), np.timedelta64("NaT")
130+
raise TypeError
125131

126132
return value
127133

@@ -160,13 +166,10 @@ def _is_all_dates(self) -> bool:
160166

161167
@doc(Index.insert)
162168
def insert(self, loc: int, item):
163-
# treat NA values as nans:
164-
if is_scalar(item) and isna(item):
165-
if is_valid_nat_for_dtype(item, self.dtype):
166-
item = self._na_value
167-
else:
168-
# NaT, np.datetime64("NaT"), np.timedelta64("NaT")
169-
return self.astype(object).insert(loc, item)
169+
try:
170+
item = self._validate_fill_value(item)
171+
except TypeError:
172+
return self.astype(object).insert(loc, item)
170173

171174
return super().insert(loc, item)
172175

pandas/tests/frame/indexing/test_setitem.py

+14
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,20 @@ def test_setitem_complete_column_with_array(self):
304304
)
305305
tm.assert_frame_equal(df, expected)
306306

307+
@pytest.mark.parametrize("dtype", ["f8", "i8", "u8"])
308+
def test_setitem_bool_with_numeric_index(self, dtype):
309+
# GH#36319
310+
cols = Index([1, 2, 3], dtype=dtype)
311+
df = DataFrame(np.random.randn(3, 3), columns=cols)
312+
313+
df[False] = ["a", "b", "c"]
314+
315+
expected_cols = Index([1, 2, 3, False], dtype=object)
316+
if dtype == "f8":
317+
expected_cols = Index([1.0, 2.0, 3.0, False], dtype=object)
318+
319+
tm.assert_index_equal(df.columns, expected_cols)
320+
307321

308322
class TestDataFrameSetItemSlicing:
309323
def test_setitem_slice_position(self):

pandas/tests/indexing/test_coercion.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ def test_insert_index_object(self, insert, coerced_val, coerced_dtype):
393393
[
394394
(1, 1, np.int64),
395395
(1.1, 1.1, np.float64),
396-
(False, 0, np.int64),
396+
(False, False, object), # GH#36319
397397
("x", "x", object),
398398
],
399399
)
@@ -409,7 +409,7 @@ def test_insert_index_int64(self, insert, coerced_val, coerced_dtype):
409409
[
410410
(1, 1.0, np.float64),
411411
(1.1, 1.1, np.float64),
412-
(False, 0.0, np.float64),
412+
(False, False, object), # GH#36319
413413
("x", "x", object),
414414
],
415415
)

0 commit comments

Comments
 (0)