Skip to content

Commit 40ac77c

Browse files
authored
BUG: casting dt64/td64 in DataFrame.reindex (#39759)
1 parent a85b3b3 commit 40ac77c

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ Indexing
335335
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` when setting multiple values to duplicate columns (:issue:`15695`)
336336
- Bug in :meth:`DataFrame.loc`, :meth:`Series.loc`, :meth:`DataFrame.__getitem__` and :meth:`Series.__getitem__` returning incorrect elements for non-monotonic :class:`DatetimeIndex` for string slices (:issue:`33146`)
337337
- Bug in :meth:`DataFrame.reindex` and :meth:`Series.reindex` with timezone aware indexes raising ``TypeError`` for ``method="ffill"`` and ``method="bfill"`` and specified ``tolerance`` (:issue:`38566`)
338+
- Bug in :meth:`DataFrame.reindex` with ``datetime64[ns]`` or ``timedelta64[ns]`` incorrectly casting to integers when the ``fill_value`` requires casting to object dtype (:issue:`39755`)
338339
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` with empty :class:`DataFrame` and specified columns for string indexer and non empty :class:`DataFrame` to set (:issue:`38831`)
339340
- Bug in :meth:`DataFrame.loc.__setitem__` raising ValueError when expanding unique column for :class:`DataFrame` with duplicate columns (:issue:`38521`)
340341
- Bug in :meth:`DataFrame.iloc.__setitem__` and :meth:`DataFrame.loc.__setitem__` with mixed dtypes when setting with a dictionary value (:issue:`38335`)

pandas/core/algorithms.py

+3
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,9 @@ def wrapper(arr, indexer, out, fill_value=np.nan):
13881388

13891389
def _convert_wrapper(f, conv_dtype):
13901390
def wrapper(arr, indexer, out, fill_value=np.nan):
1391+
if conv_dtype == object:
1392+
# GH#39755 avoid casting dt64/td64 to integers
1393+
arr = ensure_wrapped_if_datetimelike(arr)
13911394
arr = arr.astype(conv_dtype)
13921395
f(arr, indexer, out, fill_value=fill_value)
13931396

pandas/tests/frame/methods/test_reindex.py

+35
Original file line numberDiff line numberDiff line change
@@ -933,3 +933,38 @@ def test_reindex_empty(self, src_idx, cat_idx):
933933
result = df.reindex(columns=cat_idx)
934934
expected = DataFrame(index=["K"], columns=cat_idx, dtype="f8")
935935
tm.assert_frame_equal(result, expected)
936+
937+
@pytest.mark.parametrize("dtype", ["m8[ns]", "M8[ns]"])
938+
def test_reindex_datetimelike_to_object(self, dtype):
939+
# GH#39755 dont cast dt64/td64 to ints
940+
mi = MultiIndex.from_product([list("ABCDE"), range(2)])
941+
942+
dti = date_range("2016-01-01", periods=10)
943+
fv = np.timedelta64("NaT", "ns")
944+
if dtype == "m8[ns]":
945+
dti = dti - dti[0]
946+
fv = np.datetime64("NaT", "ns")
947+
948+
ser = Series(dti, index=mi)
949+
ser[::3] = pd.NaT
950+
951+
df = ser.unstack()
952+
953+
index = df.index.append(Index([1]))
954+
columns = df.columns.append(Index(["foo"]))
955+
956+
res = df.reindex(index=index, columns=columns, fill_value=fv)
957+
958+
expected = DataFrame(
959+
{
960+
0: df[0].tolist() + [fv],
961+
1: df[1].tolist() + [fv],
962+
"foo": np.array(["NaT"] * 6, dtype=fv.dtype),
963+
},
964+
index=index,
965+
)
966+
assert (res.dtypes[[0, 1]] == object).all()
967+
assert res.iloc[0, 0] is pd.NaT
968+
assert res.iloc[-1, 0] is fv
969+
assert res.iloc[-1, 1] is fv
970+
tm.assert_frame_equal(res, expected)

0 commit comments

Comments
 (0)