Skip to content

Commit e8310f8

Browse files
committed
s unit for all-NaT entries
1 parent f5016a7 commit e8310f8

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ def array_to_timedelta64(
497497
errors=errors,
498498
creso=state.creso,
499499
)
500-
elif state.creso == NPY_DATETIMEUNIT.NPY_FR_GENERIC:
500+
elif creso == NPY_DATETIMEUNIT.NPY_FR_GENERIC:
501501
# i.e. we never encountered anything non-NaT, default to "s". This
502502
# ensures that insert and concat-like operations with NaT
503503
# do not upcast units
@@ -507,9 +507,10 @@ def array_to_timedelta64(
507507
# a second pass.
508508
abbrev = npy_unit_to_abbrev(state.creso)
509509
result = iresult.view(f"m8[{abbrev}]")
510-
511-
abbrev = npy_unit_to_abbrev(creso)
512-
return result.view(f"m8[{abbrev}]")
510+
else:
511+
abbrev = npy_unit_to_abbrev(creso)
512+
result = result.view(f"m8[{abbrev}]")
513+
return result
513514

514515

515516
@cython.cpow(True)

pandas/tests/tools/test_to_timedelta.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727

2828

2929
class TestTimedeltas:
30+
def test_to_timedelta_all_nat_unit(self):
31+
# With all-NaT entries, we get "s" unit
32+
result = to_timedelta([None])
33+
assert result.unit == "s"
34+
35+
result = TimedeltaIndex([None])
36+
assert result.unit == "s"
37+
3038
def test_to_timedelta_month_raises(self):
3139
obj = np.timedelta64(1, "M")
3240

0 commit comments

Comments
 (0)