Skip to content

ENH: enable Series.info() #37320

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 34 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a903f32
TST: add series info tests
ivanovmg Oct 7, 2020
e07d6e2
TST: remove test that series has no info
ivanovmg Oct 7, 2020
0990d54
ENH: add method Series.info
ivanovmg Oct 7, 2020
1814795
REF: split tests for frame and series
ivanovmg Oct 7, 2020
4c390a8
REF: param test on frame memory_usage_qualified
ivanovmg Oct 7, 2020
81929e6
Merge branch 'master' into feature/series-info
ivanovmg Oct 21, 2020
824d8d6
ENH: enable series info
ivanovmg Oct 21, 2020
ce68e94
CLN: remove extra parens
ivanovmg Oct 21, 2020
ede6dc4
REF: split series-related tests
ivanovmg Oct 23, 2020
789e03e
DOC: add release note
ivanovmg Oct 23, 2020
f41596d
CLN: merge two lines
ivanovmg Oct 23, 2020
40b71f8
DOC: unify series/frame docstrings, fix indent
ivanovmg Oct 23, 2020
3e71336
REF: to_buffer -> render, unify func signature
ivanovmg Oct 23, 2020
739c62d
Merge branch 'master' into feature/series-info
ivanovmg Oct 23, 2020
e9c5220
DOC: add versionadded tag
ivanovmg Oct 23, 2020
f7cb4f8
Merge branch 'master' into feature/series-info
ivanovmg Nov 4, 2020
def5ed6
Merge branch 'master' into feature/series-info
ivanovmg Nov 5, 2020
dd4205d
Merge branch 'master' into feature/series-info
ivanovmg Nov 7, 2020
e74cdce
DOC: maybe fix empty line problem with df.info
ivanovmg Nov 7, 2020
d41ecf1
DOC: remove trailing period in type
ivanovmg Nov 7, 2020
98d0f55
Merge branch 'master' into feature/series-info
ivanovmg Nov 12, 2020
0d1c5d8
Merge branch 'master' into feature/series-info
ivanovmg Oct 4, 2021
816803e
Fix styling
ivanovmg Oct 4, 2021
9e0198f
Merge branch 'master' into feature/series-info
ivanovmg Nov 29, 2021
1e2aaef
DOC: move whatsnew info to v1.4.0
ivanovmg Nov 29, 2021
688080b
DOC: move docs on Series.info() to io/formats/info.py
ivanovmg Nov 29, 2021
4e87b1a
FIX: newline
ivanovmg Nov 29, 2021
dc999fe
FIX: change versionadded to 1.4.0
ivanovmg Nov 29, 2021
4bb4e40
DOC: extract null_counts_sub for frames only
ivanovmg Nov 29, 2021
f114293
DOC: avoid duplication of kwargs replacement
ivanovmg Nov 29, 2021
16ac96e
DOC: unify newlines/spacing with substitutions
ivanovmg Nov 29, 2021
aac2954
Revert "DOC: unify newlines/spacing with substitutions"
ivanovmg Nov 29, 2021
22303dc
DOC: fix newlines substitutions
ivanovmg Nov 29, 2021
9428a32
DOC: another attempt to fix newline
ivanovmg Nov 30, 2021
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
Prev Previous commit
Next Next commit
Merge branch 'master' into feature/series-info
  • Loading branch information
ivanovmg committed Oct 4, 2021
commit 0d1c5d874cca3cac166f7cc7ef3864513ed6db18
21 changes: 15 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,15 @@
nargsort,
)

from pandas.io.common import get_filepath_or_buffer
from pandas.io.formats import console, format as fmt
from pandas.io.formats.info import BaseInfo, DataFrameInfo
from pandas.io.common import get_handle
from pandas.io.formats import (
console,
format as fmt,
)
from pandas.io.formats.info import (
BaseInfo,
DataFrameInfo,
)
import pandas.plotting

if TYPE_CHECKING:
Expand Down Expand Up @@ -3041,14 +3047,17 @@ def to_xml(
is used. By default, the setting in
``pandas.options.display.max_info_columns`` is used."""
),
null_counts_sub=dedent(
show_counts_sub=dedent(
"""\
null_counts : bool, optional
show_counts : bool, optional
Whether to show the non-null counts. By default, this is shown
only if the DataFrame is smaller than
``pandas.options.display.max_info_rows`` and
``pandas.options.display.max_info_columns``. A value of True always
shows the counts, and False never shows the counts."""
shows the counts, and False never shows the counts.
null_counts : bool, optional
.. deprecated:: 1.2.0
Use show_counts instead."""
),
examples_sub=dedent(
"""\
Expand Down
23 changes: 13 additions & 10 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@
from pandas.core.tools.datetimes import to_datetime

import pandas.io.formats.format as fmt
from pandas.io.formats.info import BaseInfo, SeriesInfo
from pandas.io.formats.info import (
BaseInfo,
SeriesInfo,
)
import pandas.plotting

if TYPE_CHECKING:
Expand Down Expand Up @@ -4890,9 +4893,9 @@ def replace(
klass="Series",
type_sub="",
max_cols_sub="",
null_counts_sub=dedent(
show_counts_sub=dedent(
"""\
null_counts : bool, default True
show_counts : bool, default True
Whether to show the non-null counts."""
),
examples_sub=dedent(
Expand Down Expand Up @@ -4965,20 +4968,20 @@ def replace(
@doc(BaseInfo.render)
def info(
self,
verbose: Optional[bool] = None,
buf: Optional[IO[str]] = None,
max_cols: Optional[int] = None,
memory_usage: Optional[Union[bool, str]] = None,
null_counts: bool = True,
verbose: bool | None = None,
buf: IO[str] | None = None,
max_cols: int | None = None,
memory_usage: bool | str | None = None,
show_counts: bool = True,
) -> None:
return SeriesInfo(self, memory_usage).render(
buf=buf,
max_cols=max_cols,
verbose=verbose,
show_counts=null_counts,
show_counts=show_counts,
)

def _replace_single(self, to_replace, method, inplace, limit):
def _replace_single(self, to_replace, method: str, inplace: bool, limit):
"""
Replaces values in a Series using the fill method specified when no
replacement value is given in the replace method
Expand Down
78 changes: 37 additions & 41 deletions pandas/io/formats/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from pandas._config import get_option

from pandas._typing import Dtype, FrameOrSeriesUnion
from pandas._typing import Dtype

from pandas.core.indexes.api import Index

Expand Down Expand Up @@ -105,20 +105,22 @@ class BaseInfo(ABC):

Parameters
----------
data : FrameOrSeriesUnion
data : DataFrame or Series
Either dataframe or series.
memory_usage : bool or str, optional
If "deep", introspect the data deeply by interrogating object dtypes
for system-level memory consumption, and include it in the returned
values.
"""

data: FrameOrSeriesUnion
data: DataFrame | Series
memory_usage: Union[bool, str]

@property
@abstractmethod
def dtypes(self) -> Iterable[Dtype]:
"""Dtypes.
"""
Dtypes.

Returns
-------
Expand Down Expand Up @@ -209,7 +211,7 @@ def render(
consume the same memory amount for corresponding dtypes. With deep
memory introspection, a real memory usage calculation is performed
at the cost of computational resources.
%(null_counts_sub)s
%(show_counts_sub)s

Returns
-------
Expand All @@ -233,10 +235,10 @@ class DataFrameInfo(BaseInfo):

def __init__(
self,
data: "DataFrame",
memory_usage: Optional[Union[bool, str]] = None,
data: DataFrame,
memory_usage: bool | str | None = None,
):
self.data: "DataFrame" = data
self.data: DataFrame = data
self.memory_usage = _initialize_memory_usage(memory_usage)

@property
Expand All @@ -245,7 +247,8 @@ def dtype_counts(self) -> Mapping[str, int]:

@property
def dtypes(self) -> Iterable[Dtype]:
"""Dtypes.
"""
Dtypes.

Returns
-------
Expand All @@ -256,7 +259,8 @@ def dtypes(self) -> Iterable[Dtype]:

@property
def ids(self) -> Index:
"""Column names.
"""
Column names.

Returns
-------
Expand Down Expand Up @@ -286,10 +290,10 @@ def memory_usage_bytes(self) -> int:
def render(
self,
*,
buf: Optional[IO[str]],
max_cols: Optional[int],
verbose: Optional[bool],
show_counts: Optional[bool],
buf: IO[str] | None,
max_cols: int | None,
verbose: bool | None,
show_counts: bool | None,
) -> None:
printer = DataFrameInfoPrinter(
info=self,
Expand All @@ -307,19 +311,19 @@ class SeriesInfo(BaseInfo):

def __init__(
self,
data: "Series",
memory_usage: Optional[Union[bool, str]] = None,
data: Series,
memory_usage: bool | str | None = None,
):
self.data: "Series" = data
self.data: Series = data
self.memory_usage = _initialize_memory_usage(memory_usage)

def render(
self,
*,
buf: Optional[IO[str]],
max_cols: Optional[int],
verbose: Optional[bool],
show_counts: Optional[bool],
buf: IO[str] | None = None,
max_cols: int | None = None,
verbose: bool | None = None,
show_counts: bool | None = None,
) -> None:
if max_cols is not None:
raise ValueError(
Expand Down Expand Up @@ -368,7 +372,7 @@ class InfoPrinterAbstract:
Class for printing dataframe or series info.
"""

def to_buffer(self, buf: Optional[IO[str]] = None) -> None:
def to_buffer(self, buf: IO[str] | None = None) -> None:
"""Save dataframe info into buffer."""
table_builder = self._create_table_builder()
lines = table_builder.get_lines()
Expand All @@ -377,7 +381,7 @@ def to_buffer(self, buf: Optional[IO[str]] = None) -> None:
fmt.buffer_put_lines(buf, lines)

@abstractmethod
def _create_table_builder(self) -> "TableBuilderAbstract":
def _create_table_builder(self) -> TableBuilderAbstract:
"""Create instance of table builder."""


Expand Down Expand Up @@ -441,7 +445,7 @@ def _initialize_show_counts(self, show_counts: bool | None) -> bool:
else:
return show_counts

def _create_table_builder(self) -> "DataFrameTableBuilder":
def _create_table_builder(self) -> DataFrameTableBuilder:
"""
Create instance of table builder based on verbosity and display settings.
"""
Expand Down Expand Up @@ -478,15 +482,15 @@ class SeriesInfoPrinter(InfoPrinterAbstract):
def __init__(
self,
info: SeriesInfo,
verbose: Optional[bool] = None,
show_counts: Optional[bool] = None,
verbose: bool | None = None,
show_counts: bool | None = None,
):
self.info = info
self.data = info.data
self.verbose = verbose
self.show_counts = self._initialize_show_counts(show_counts)

def _create_table_builder(self) -> "SeriesTableBuilder":
def _create_table_builder(self) -> SeriesTableBuilder:
"""
Create instance of table builder based on verbosity.
"""
Expand All @@ -510,15 +514,15 @@ class TableBuilderAbstract(ABC):
Abstract builder for info table.
"""

_lines: List[str]
_lines: list[str]
info: BaseInfo

@abstractmethod
def get_lines(self) -> list[str]:
"""Product in a form of list of lines (strings)."""

@property
def data(self) -> FrameOrSeriesUnion:
def data(self) -> DataFrame | Series:
return self.info.data

@property
Expand Down Expand Up @@ -571,15 +575,6 @@ class DataFrameTableBuilder(TableBuilderAbstract):
Instance of DataFrameInfo.
"""

def __init__(self, *, info: DataFrameInfo):
self.info: DataFrameInfo = info

Parameters
----------
info : DataFrameInfo.
Instance of DataFrameInfo.
"""

def __init__(self, *, info: DataFrameInfo):
self.info: DataFrameInfo = info

Expand Down Expand Up @@ -673,7 +668,8 @@ def _get_body_column_widths(self) -> Sequence[int]:
return [max(len(x) for x in col) for col in strcols]

def _gen_rows(self) -> Iterator[Sequence[str]]:
"""Generator function yielding rows content.
"""
Generator function yielding rows content.

Each element represents a row comprising a sequence of strings.
"""
Expand Down Expand Up @@ -893,9 +889,9 @@ def _gen_rows_with_counts(self) -> Iterator[Sequence[str]]:
)


def _get_dataframe_dtype_counts(df: "DataFrame") -> Mapping[str, int]:
def _get_dataframe_dtype_counts(df: DataFrame) -> Mapping[str, int]:
"""
Create mapping between datatypes and their number of occurences.
Create mapping between datatypes and their number of occurrences.
"""
# groupby dtype.name to collect e.g. Categorical columns
return df.dtypes.value_counts().groupby(lambda x: x.name).sum()
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.