Skip to content

Commit

Permalink
Rename summarylevel to describelevel (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
coroa authored May 8, 2023
1 parent 6200560 commit 9178001
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Changelog
=========

* :py:func:`~core.describelevel` superseedes the as-of-now deprecated
:py:func:`~core.summarylevel` :pull:`11`

v0.2.4 (2023-05-03)
------------------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions src/pandas_indexing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
alignlevel,
alignlevels,
assignlevel,
describelevel,
dropnalevel,
index_names,
projectlevel,
Expand Down
4 changes: 2 additions & 2 deletions src/pandas_indexing/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
from .core import (
Data,
assignlevel,
describelevel,
dropnalevel,
projectlevel,
semijoin,
summarylevel,
uniquelevel,
)
from .utils import Axis
Expand All @@ -38,7 +38,7 @@ def __init__(self, pandas_obj):
self._obj = pandas_obj

def __repr__(self):
return summarylevel(self._obj)
return describelevel(self._obj, as_str=True)

def assign(
self,
Expand Down
37 changes: 26 additions & 11 deletions src/pandas_indexing/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import Any, Literal, Optional, Sequence, TypeVar, Union

import numpy as np
from deprecated import deprecated
from pandas import DataFrame, Index, MultiIndex, Series
from pandas.core.indexes.frozen import FrozenList

Expand Down Expand Up @@ -230,7 +231,7 @@ def uniquelevel(
return projectlevel(index, levels).unique()


def _summarylevel(index: Index, n: int = 80) -> str:
def _describelevel(index: Index, n: int = 80) -> str:
def name(l):
return "<unnamed>" if l is None else l

Expand All @@ -241,34 +242,48 @@ def name(l):
)


def summarylevel(index_or_data: Union[DataFrame, Series, Index], n: int = 80):
def describelevel(
index_or_data: Union[DataFrame, Series, Index], n: int = 80, as_str: bool = False
) -> Optional[str]:
"""
Summarize index levels.
Describe index levels.
Parameters
----------
index_or_data : Index|Series|DataFrame
Index, Series or DataFrame of which to summarize index levels
Index, Series or DataFrame of which to describe index levels
n : int, default 80
The maximum line length
as_str : bool, default False
Whether to return as string or print, instead
Returns
-------
summary : str
description : str, optional
if print is False
See also
--------
pandas.DataFrame.describe
"""
if isinstance(index_or_data, DataFrame):
index_desc = _summarylevel(index_or_data.index, n=n)
columns_desc = _summarylevel(index_or_data.columns, n=n)
return f"Index:\n{index_desc}\n\nColumns:\n{columns_desc}"
index_desc = _describelevel(index_or_data.index, n=n)
columns_desc = _describelevel(index_or_data.columns, n=n)
description = f"Index:\n{index_desc}\n\nColumns:\n{columns_desc}"
else:
if isinstance(index_or_data, Series):
index_or_data = index_or_data.index
description = f"Index:\n{_describelevel(index_or_data, n=n)}"

if as_str:
return description

print(description)

if isinstance(index_or_data, Series):
index_or_data = index_or_data.index

return f"Index:\n{_summarylevel(index_or_data, n=n)}"
summarylevel = deprecated(
describelevel, reason="Use describelevel instead", version="v0.2.5"
)


def index_names(s, raise_on_index=False):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

from pandas_indexing.core import (
assignlevel,
describelevel,
dropnalevel,
projectlevel,
summarylevel,
uniquelevel,
)

Expand Down Expand Up @@ -139,9 +139,9 @@ def test_uniquelevel(mdf, midx):
assert_index_equal(uniquelevel(midx, ["str", "num"]), midx)


def test_summarylevel(mdf, midx):
def test_describelevel(mdf, midx):
assert (
summarylevel(mdf)
describelevel(mdf, as_str=True)
== dedent(
"""
Index:
Expand All @@ -155,7 +155,7 @@ def test_summarylevel(mdf, midx):
)

assert (
summarylevel(midx)
describelevel(midx, as_str=True)
== dedent(
"""
Index:
Expand Down

0 comments on commit 9178001

Please sign in to comment.