Skip to content

Commit 214820d

Browse files
committed
BUG: NumericIndex.insert(0, False) casting to int
1 parent 2c1e981 commit 214820d

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
@@ -619,6 +619,7 @@ Indexing
619619
- 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`)
620620
- Bug in :meth:`DataFrame.iloc` and :meth:`Series.iloc` aligning objects in ``__setitem__`` (:issue:`22046`)
621621
- Bug in :meth:`DataFrame.loc` did not raise ``KeyError`` when missing combination was given with ``slice(None)`` for remaining levels (:issue:`19556`)
622+
- Bug on inserting a boolean label into a :class:`DataFrame` with a numeric :class:`Index` columns incorrectly casting to integer (:issue:`36319`)
622623

623624
Missing
624625
^^^^^^^

pandas/core/indexes/numeric.py

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

128134
return value
129135

@@ -162,13 +168,10 @@ def _is_all_dates(self) -> bool:
162168

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

173176
return super().insert(loc, item)
174177

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)