Skip to content

Commit 327d559

Browse files
authored
BUG: Index.is_monotonic... incorrectly caching Index.is_unique when first value is NaT (#55755)
* Index.is_montonic_increasing incorrectly setting is_unique when first value is NaT * add test and whatsnew
1 parent a4aa699 commit 327d559

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ Datetimelike
323323
^^^^^^^^^^^^
324324
- Bug in :func:`concat` raising ``AttributeError`` when concatenating all-NA DataFrame with :class:`DatetimeTZDtype` dtype DataFrame. (:issue:`52093`)
325325
- Bug in :meth:`DatetimeIndex.union` returning object dtype for tz-aware indexes with the same timezone but different units (:issue:`55238`)
326+
- 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`)
326327
- Bug in :meth:`Index.view` to a datetime64 dtype with non-supported resolution incorrectly raising (:issue:`55710`)
327328
- Bug in :meth:`Tick.delta` with very large ticks raising ``OverflowError`` instead of ``OutOfBoundsTimedelta`` (:issue:`55503`)
328329
- 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`)

pandas/_libs/algos.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ def is_monotonic(ndarray[numeric_object_t, ndim=1] arr, bint timelike):
814814
return True, True, True
815815

816816
if timelike and <int64_t>arr[0] == NPY_NAT:
817-
return False, False, True
817+
return False, False, False
818818

819819
if numeric_object_t is not object:
820820
with nogil:

pandas/tests/indexes/test_base.py

+14
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,20 @@ def test_is_monotonic_na(self, index):
14541454
assert index._is_strictly_monotonic_increasing is False
14551455
assert index._is_strictly_monotonic_decreasing is False
14561456

1457+
@pytest.mark.parametrize("dtype", ["f8", "m8[ns]", "M8[us]"])
1458+
@pytest.mark.parametrize("unique_first", [True, False])
1459+
def test_is_monotonic_unique_na(self, dtype, unique_first):
1460+
# GH 55755
1461+
index = Index([None, 1, 1], dtype=dtype)
1462+
if unique_first:
1463+
assert index.is_unique is False
1464+
assert index.is_monotonic_increasing is False
1465+
assert index.is_monotonic_decreasing is False
1466+
else:
1467+
assert index.is_monotonic_increasing is False
1468+
assert index.is_monotonic_decreasing is False
1469+
assert index.is_unique is False
1470+
14571471
def test_int_name_format(self, frame_or_series):
14581472
index = Index(["a", "b", "c"], name=0)
14591473
result = frame_or_series(list(range(3)), index=index)

0 commit comments

Comments
 (0)