Skip to content

BUG: Timedelta.__bool__ #21485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add 0S here

(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