Skip to content

Commit 10ae3d0

Browse files
mroeschkeim-vinicius
authored and
im-vinicius
committed
BUG: reindex with expansion and non-nanosecond dtype (pandas-dev#53505)
* BUG: reindex with expansion and non-nanosecond dtype * Restrict to timelike types * Check earlier * handle NA * handle NA * Better check
1 parent 5e78c9d commit 10ae3d0

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
@@ -23,6 +23,7 @@ Fixed regressions
2323
Bug fixes
2424
~~~~~~~~~
2525
- Bug in :func:`RangeIndex.union` when using ``sort=True`` with another :class:`RangeIndex` (:issue:`53490`)
26+
- Bug in :func:`Series.reindex` when expanding a non-nanosecond datetime or timedelta :class:`Series` would not fill with ``NaT`` correctly (:issue:`53497`)
2627
- Bug in :func:`read_csv` when defining ``dtype`` with ``bool[pyarrow]`` for the ``"c"`` and ``"python"`` engines (:issue:`53390`)
2728
- Bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` with ``expand=True`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`53532`)
2829
- 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
@@ -562,9 +562,16 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
562562
If fill_value is a non-scalar and dtype is not object.
563563
"""
564564
orig = fill_value
565+
orig_is_nat = False
565566
if checknull(fill_value):
566567
# https://github.com/pandas-dev/pandas/pull/39692#issuecomment-1441051740
567568
# avoid cache misses with NaN/NaT values that are not singletons
569+
if fill_value is not NA:
570+
try:
571+
orig_is_nat = np.isnat(fill_value)
572+
except TypeError:
573+
pass
574+
568575
fill_value = _canonical_nans.get(type(fill_value), fill_value)
569576

570577
# for performance, we are using a cached version of the actual implementation
@@ -580,8 +587,10 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan):
580587
# if fill_value is not hashable (required for caching)
581588
dtype, fill_value = _maybe_promote(dtype, fill_value)
582589

583-
if dtype == _dtype_obj and orig is not None:
584-
# GH#51592 restore our potentially non-canonical fill_value
590+
if (dtype == _dtype_obj and orig is not None) or (
591+
orig_is_nat and np.datetime_data(orig)[0] != "ns"
592+
):
593+
# GH#51592,53497 restore our potentially non-canonical fill_value
585594
fill_value = orig
586595
return dtype, fill_value
587596

pandas/tests/series/methods/test_reindex.py

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
NaT,
1313
Period,
1414
PeriodIndex,
15+
RangeIndex,
1516
Series,
1617
Timedelta,
1718
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)