Skip to content

Commit 0625e99

Browse files
authored
BUG: pickle Index[object] preserve dtype (#43188)
1 parent 4ec87eb commit 0625e99

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ I/O
377377
- Bug in :func:`read_fwf`, where difference in lengths of ``colspecs`` and ``names`` was not raising ``ValueError`` (:issue:`40830`)
378378
- Bug in :func:`Series.to_json` and :func:`DataFrame.to_json` where some attributes were skipped when serialising plain Python objects to JSON (:issue:`42768`, :issue:`33043`)
379379
- Column headers are dropped when constructing a :class:`DataFrame` from a sqlalchemy's ``Row`` object (:issue:`40682`)
380+
- Bug in unpickling a :class:`Index` with object dtype incorrectly inferring numeric dtypes (:issue:`43188`)
380381
-
381382

382383
Period

pandas/core/indexes/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ def _new_Index(cls, d):
242242
# GH#23752 "labels" kwarg has been replaced with "codes"
243243
d["codes"] = d.pop("labels")
244244

245+
elif "dtype" not in d and "data" in d:
246+
# Prevent Index.__new__ from conducting inference;
247+
# "data" key not in RangeIndex
248+
d["dtype"] = d["data"].dtype
245249
return cls.__new__(cls, **d)
246250

247251

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pandas import Index
2+
import pandas._testing as tm
3+
4+
5+
def test_pickle_preserves_object_dtype():
6+
# GH#43188, GH#43155 don't infer numeric dtype
7+
index = Index([1, 2, 3], dtype=object)
8+
9+
result = tm.round_trip_pickle(index)
10+
assert result.dtype == object
11+
tm.assert_index_equal(index, result)

0 commit comments

Comments
 (0)