Skip to content

Commit 7c03432

Browse files
Backport PR #53505 on branch 2.0.x (BUG: reindex with expansion and non-nanosecond dtype) (#53748)
Backport PR #53505: BUG: reindex with expansion and non-nanosecond dtype Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 71c9a17 commit 7c03432

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

doc/source/whatsnew/v2.0.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Fixed regressions
2222
Bug fixes
2323
~~~~~~~~~
2424
- Bug in :func:`RangeIndex.union` when using ``sort=True`` with another :class:`RangeIndex` (:issue:`53490`)
25+
- Bug in :func:`Series.reindex` when expanding a non-nanosecond datetime or timedelta :class:`Series` would not fill with ``NaT`` correctly (:issue:`53497`)
2526
- Bug in :func:`read_csv` when defining ``dtype`` with ``bool[pyarrow]`` for the ``"c"`` and ``"python"`` engines (:issue:`53390`)
2627
- Bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` with ``expand=True`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`53532`)
2728
- Bug in indexing methods (e.g. :meth:`DataFrame.__getitem__`) where taking the entire :class:`DataFrame`/:class:`Series` would raise an ``OverflowError`` when Copy on Write was enabled and the length of the array was over the maximum size a 32-bit integer can hold (:issue:`53616`)

pandas/core/dtypes/cast.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,16 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
583583
If fill_value is a non-scalar and dtype is not object.
584584
"""
585585
orig = fill_value
586+
orig_is_nat = False
586587
if checknull(fill_value):
587588
# https://github.com/pandas-dev/pandas/pull/39692#issuecomment-1441051740
588589
# avoid cache misses with NaN/NaT values that are not singletons
590+
if fill_value is not NA:
591+
try:
592+
orig_is_nat = np.isnat(fill_value)
593+
except TypeError:
594+
pass
595+
589596
fill_value = _canonical_nans.get(type(fill_value), fill_value)
590597

591598
# for performance, we are using a cached version of the actual implementation
@@ -601,8 +608,10 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
601608
# if fill_value is not hashable (required for caching)
602609
dtype, fill_value = _maybe_promote(dtype, fill_value)
603610

604-
if dtype == _dtype_obj and orig is not None:
605-
# GH#51592 restore our potentially non-canonical fill_value
611+
if (dtype == _dtype_obj and orig is not None) or (
612+
orig_is_nat and np.datetime_data(orig)[0] != "ns"
613+
):
614+
# GH#51592,53497 restore our potentially non-canonical fill_value
606615
fill_value = orig
607616
return dtype, fill_value
608617

pandas/tests/series/methods/test_reindex.py

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
NaT,
1111
Period,
1212
PeriodIndex,
13+
RangeIndex,
1314
Series,
1415
Timedelta,
1516
Timestamp,
@@ -422,3 +423,14 @@ def test_reindexing_with_float64_NA_log():
422423
result_log = np.log(s_reindex)
423424
expected_log = Series([0, np.NaN, np.NaN], dtype=Float64Dtype())
424425
tm.assert_series_equal(result_log, expected_log)
426+
427+
428+
@pytest.mark.parametrize("dtype", ["timedelta64", "datetime64"])
429+
def test_reindex_expand_nonnano_nat(dtype):
430+
# GH 53497
431+
ser = Series(np.array([1], dtype=f"{dtype}[s]"))
432+
result = ser.reindex(RangeIndex(2))
433+
expected = Series(
434+
np.array([1, getattr(np, dtype)("nat", "s")], dtype=f"{dtype}[s]")
435+
)
436+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)