Skip to content

DEPR: enforce deprecation MultiIndex.is_lexsorted and MultiIndex.lexsort_depth #49260

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 6 commits into from
Oct 23, 2022
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/v0.15.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ API changes
a lexically sorted index will have a better performance. (:issue:`2646`)

.. ipython:: python
:okexcept:
:okwarning:

df = pd.DataFrame({'jim':[0, 0, 1, 1],
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ Removal of prior version deprecations/changes
- Remove arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`)
- Remove argument ``inplace`` from :meth:`MultiIndex.set_levels` and :meth:`MultiIndex.set_codes` (:issue:`35626`)
- Disallow passing positional arguments to :meth:`MultiIndex.set_levels` and :meth:`MultiIndex.set_codes` (:issue:`41485`)
- Removed :meth:`MultiIndex.is_lexsorted` and :meth:`MultiIndex.lexsort_depth` (:issue:`38701`)
- Removed argument ``how`` from :meth:`PeriodIndex.astype`, use :meth:`PeriodIndex.to_timestamp` instead (:issue:`37982`)
- Removed argument ``try_cast`` from :meth:`DataFrame.mask`, :meth:`DataFrame.where`, :meth:`Series.mask` and :meth:`Series.where` (:issue:`38836`)
- Removed argument ``is_copy`` from :meth:`DataFrame.take` and :meth:`Series.take` (:issue:`30615`)
Expand Down
1 change: 0 additions & 1 deletion pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ def pytest_collection_modifyitems(items, config) -> None:
("DataFrame.append", "The frame.append method is deprecated"),
("Series.append", "The series.append method is deprecated"),
("dtypes.common.is_categorical", "is_categorical is deprecated"),
("MultiIndex._is_lexsorted", "MultiIndex.is_lexsorted is deprecated"),
# Docstring divides by zero to show behavior difference
("missing.mask_zero_div_zero", "divide by zero encountered"),
# Docstring demonstrates the call raises a warning
Expand Down
33 changes: 8 additions & 25 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1810,15 +1810,6 @@ def to_flat_index(self) -> Index: # type: ignore[override]
"""
return Index(self._values, tupleize_cols=False)

def is_lexsorted(self) -> bool:
warnings.warn(
"MultiIndex.is_lexsorted is deprecated as a public function, "
"users should use MultiIndex.is_monotonic_increasing instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
return self._is_lexsorted()

def _is_lexsorted(self) -> bool:
"""
Return True if the codes are lexicographically sorted.
Expand All @@ -1832,37 +1823,29 @@ def _is_lexsorted(self) -> bool:
In the below examples, the first level of the MultiIndex is sorted because
a<b<c, so there is no need to look at the next level.

>>> pd.MultiIndex.from_arrays([['a', 'b', 'c'], ['d', 'e', 'f']]).is_lexsorted()
>>> pd.MultiIndex.from_arrays([['a', 'b', 'c'],
... ['d', 'e', 'f']])._is_lexsorted()
True
>>> pd.MultiIndex.from_arrays([['a', 'b', 'c'], ['d', 'f', 'e']]).is_lexsorted()
>>> pd.MultiIndex.from_arrays([['a', 'b', 'c'],
... ['d', 'f', 'e']])._is_lexsorted()
True

In case there is a tie, the lexicographical sorting looks
at the next level of the MultiIndex.

>>> pd.MultiIndex.from_arrays([[0, 1, 1], ['a', 'b', 'c']]).is_lexsorted()
>>> pd.MultiIndex.from_arrays([[0, 1, 1], ['a', 'b', 'c']])._is_lexsorted()
True
>>> pd.MultiIndex.from_arrays([[0, 1, 1], ['a', 'c', 'b']]).is_lexsorted()
>>> pd.MultiIndex.from_arrays([[0, 1, 1], ['a', 'c', 'b']])._is_lexsorted()
False
>>> pd.MultiIndex.from_arrays([['a', 'a', 'b', 'b'],
... ['aa', 'bb', 'aa', 'bb']]).is_lexsorted()
... ['aa', 'bb', 'aa', 'bb']])._is_lexsorted()
True
>>> pd.MultiIndex.from_arrays([['a', 'a', 'b', 'b'],
... ['bb', 'aa', 'aa', 'bb']]).is_lexsorted()
... ['bb', 'aa', 'aa', 'bb']])._is_lexsorted()
False
"""
return self._lexsort_depth == self.nlevels

@property
def lexsort_depth(self) -> int:
warnings.warn(
"MultiIndex.lexsort_depth is deprecated as a public function, "
"users should use MultiIndex.is_monotonic_increasing instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
return self._lexsort_depth

@cache_readonly
def _lexsort_depth(self) -> int:
"""
Expand Down
17 changes: 0 additions & 17 deletions pandas/tests/indexes/multi/test_lexsort.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from pandas import MultiIndex
import pandas._testing as tm


class TestIsLexsorted:
Expand All @@ -22,14 +21,6 @@ def test_is_lexsorted(self):
assert not index._is_lexsorted()
assert index._lexsort_depth == 0

def test_is_lexsorted_deprecation(self):
# GH 32259
with tm.assert_produces_warning(
FutureWarning,
match="MultiIndex.is_lexsorted is deprecated as a public function",
):
MultiIndex.from_arrays([["a", "b", "c"], ["d", "f", "e"]]).is_lexsorted()


class TestLexsortDepth:
def test_lexsort_depth(self):
Expand All @@ -53,11 +44,3 @@ def test_lexsort_depth(self):
levels=levels, codes=[[0, 0, 1, 0, 1, 1], [0, 1, 0, 2, 2, 1]], sortorder=0
)
assert index._lexsort_depth == 0

def test_lexsort_depth_deprecation(self):
# GH 32259
with tm.assert_produces_warning(
FutureWarning,
match="MultiIndex.lexsort_depth is deprecated as a public function",
):
MultiIndex.from_arrays([["a", "b", "c"], ["d", "f", "e"]]).lexsort_depth
4 changes: 1 addition & 3 deletions pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,7 @@ def test_intersection_lexsort_depth(levels1, levels2, codes1, codes2, names):
mi1 = MultiIndex(levels=levels1, codes=codes1, names=names)
mi2 = MultiIndex(levels=levels2, codes=codes2, names=names)
mi_int = mi1.intersection(mi2)

with tm.assert_produces_warning(FutureWarning, match="MultiIndex.lexsort_depth"):
assert mi_int.lexsort_depth == 2
assert mi_int._lexsort_depth == 2


@pytest.mark.parametrize("val", [pd.NA, 100])
Expand Down