Skip to content

Commit

Permalink
BUG: Timedelta.__bool__ (#21485)
Browse files Browse the repository at this point in the history
Closes #21484
  • Loading branch information
TomAugspurger authored and jreback committed Jun 18, 2018
1 parent 89418a3 commit d5a1232
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
9 changes: 6 additions & 3 deletions doc/source/whatsnew/v0.23.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ Bug Fixes
-
-

**Conversion**
**Timedelta**

- Bug in :class:`Timedelta` where non-zero timedeltas shorter than 1 microsecond were considered False (:issue:`21484`)

-
**Conversion**

- Bug in :meth:`Series.nlargest` for signed and unsigned integer dtypes when the minimum value is present (:issue:`21426`)
-

**Indexing**
Expand Down Expand Up @@ -78,6 +81,7 @@ Bug Fixes
-

**Timezones**

- Bug in :class:`Timestamp` and :class:`DatetimeIndex` where passing a :class:`Timestamp` localized after a DST transition would return a datetime before the DST transition (:issue:`20854`)
- Bug in comparing :class:`DataFrame`s with tz-aware :class:`DatetimeIndex` columns with a DST transition that raised a ``KeyError`` (:issue:`19970`)
- Bug in :meth:`DatetimeIndex.shift` where an ``AssertionError`` would raise when shifting across DST (:issue:`8616`)
Expand All @@ -88,5 +92,4 @@ Bug Fixes

**Other**

- Bug in :meth:`Series.nlargest` for signed and unsigned integer dtypes when the minimum value is present (:issue:`21426`)
-
3 changes: 3 additions & 0 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,9 @@ cdef class _Timedelta(timedelta):
def __str__(self):
return self._repr_base(format='long')

def __bool__(self):
return self.value != 0

def isoformat(self):
"""
Format Timedelta as ISO 8601 Duration like
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,17 @@ def test_components(self):
result = s.dt.components
assert not result.iloc[0].isna().all()
assert result.iloc[1].isna().all()


@pytest.mark.parametrize('value, expected', [
(Timedelta('10S'), True),
(Timedelta('-10S'), True),
(Timedelta(10, unit='ns'), True),
(Timedelta(0, unit='ns'), False),
(Timedelta(-10, unit='ns'), True),
(Timedelta(None), True),
(pd.NaT, True),
])
def test_truthiness(value, expected):
# https://github.com/pandas-dev/pandas/issues/21484
assert bool(value) is expected

0 comments on commit d5a1232

Please sign in to comment.