Skip to content

BUG: Index.is_monotonic... incorrectly caching Index.is_unique when first value is NaT #55755

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 2 commits into from
Oct 30, 2023
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ Datetimelike
^^^^^^^^^^^^
- Bug in :func:`concat` raising ``AttributeError`` when concatenating all-NA DataFrame with :class:`DatetimeTZDtype` dtype DataFrame. (:issue:`52093`)
- Bug in :meth:`DatetimeIndex.union` returning object dtype for tz-aware indexes with the same timezone but different units (:issue:`55238`)
- Bug in :meth:`Index.is_monotonic_increasing` and :meth:`Index.is_monotonic_decreasing` always caching :meth:`Index.is_unique` as ``True`` when first value in index is ``NaT`` (:issue:`55755`)
- Bug in :meth:`Index.view` to a datetime64 dtype with non-supported resolution incorrectly raising (:issue:`55710`)
- Bug in :meth:`Tick.delta` with very large ticks raising ``OverflowError`` instead of ``OutOfBoundsTimedelta`` (:issue:`55503`)
- Bug in adding or subtracting a :class:`Week` offset to a ``datetime64`` :class:`Series`, :class:`Index`, or :class:`DataFrame` column with non-nanosecond resolution returning incorrect results (:issue:`55583`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ def is_monotonic(ndarray[numeric_object_t, ndim=1] arr, bint timelike):
return True, True, True

if timelike and <int64_t>arr[0] == NPY_NAT:
return False, False, True
return False, False, False

if numeric_object_t is not object:
with nogil:
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,20 @@ def test_is_monotonic_na(self, index):
assert index._is_strictly_monotonic_increasing is False
assert index._is_strictly_monotonic_decreasing is False

@pytest.mark.parametrize("dtype", ["f8", "m8[ns]", "M8[us]"])
@pytest.mark.parametrize("unique_first", [True, False])
def test_is_monotonic_unique_na(self, dtype, unique_first):
# GH 55755
index = Index([None, 1, 1], dtype=dtype)
if unique_first:
assert index.is_unique is False
assert index.is_monotonic_increasing is False
assert index.is_monotonic_decreasing is False
else:
assert index.is_monotonic_increasing is False
assert index.is_monotonic_decreasing is False
assert index.is_unique is False

def test_int_name_format(self, frame_or_series):
index = Index(["a", "b", "c"], name=0)
result = frame_or_series(list(range(3)), index=index)
Expand Down