Skip to content

Commit 46a0f38

Browse files
committed
BUG: Fix pd.Timedelta(None) to return NaT.
Before this commit pd.Timedelta raised ValueError when None is passed. This behavior is inconsistent because * pd.Timestamp(None) and pd.Period(None) return NaT * pd.Timedelta returns NaT if '', 'nat', 'NAT' or np.nan is passed
1 parent b60e42b commit 46a0f38

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -755,3 +755,4 @@ Bug Fixes
755755
- Bug where ``pd.read_gbq()`` could throw ``ImportError: No module named discovery`` as a result of a naming conflict with another python package called apiclient (:issue:`13454`)
756756
- Bug in ``Index.union`` returns an incorrect result with a named empty index (:issue:`13432`)
757757
- Bugs in ``Index.difference`` and ``DataFrame.join`` raise in Python3 when using mixed-integer indexes (:issue:`13432`, :issue:`12814`)
758+
- Bug in ``pd.Timedelta(None)`` raises ``ValueError``. This is different from ``pd.Timestamp(None)`` (:issue:`13687`)

pandas/tseries/tests/test_timedeltas.py

+2
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ def test_construction(self):
188188
self.assertEqual(Timedelta('').value, iNaT)
189189
self.assertEqual(Timedelta('nat').value, iNaT)
190190
self.assertEqual(Timedelta('NAT').value, iNaT)
191+
self.assertEqual(Timedelta(None).value, iNaT)
192+
self.assertEqual(Timedelta(np.nan).value, iNaT)
191193
self.assertTrue(isnull(Timedelta('nat')))
192194

193195
# offset

pandas/tslib.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -2592,10 +2592,10 @@ class Timedelta(_Timedelta):
25922592
25932593
"""
25942594

2595-
def __new__(cls, object value=None, unit=None, **kwargs):
2595+
def __new__(cls, object value=_no_input, unit=None, **kwargs):
25962596
cdef _Timedelta td_base
25972597

2598-
if value is None:
2598+
if value is _no_input:
25992599
if not len(kwargs):
26002600
raise ValueError("cannot construct a Timedelta without a value/unit or descriptive keywords (days,seconds....)")
26012601

0 commit comments

Comments
 (0)