Skip to content

Commit 1a86a50

Browse files
author
gcerri
committed
Add option to DataFrame.info for structured output
1 parent aa134bb commit 1a86a50

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

pandas/core/frame.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3529,17 +3529,21 @@ def info(
35293529
max_cols: int | None = None,
35303530
memory_usage: bool | str | None = None,
35313531
show_counts: bool | None = None,
3532+
return_dict: bool | None = None,
35323533
) -> None:
35333534
info = DataFrameInfo(
35343535
data=self,
35353536
memory_usage=memory_usage,
35363537
)
3537-
info.render(
3538+
info_return = info.render(
35383539
buf=buf,
35393540
max_cols=max_cols,
35403541
verbose=verbose,
35413542
show_counts=show_counts,
3543+
return_dict=return_dict,
35423544
)
3545+
if return_dict:
3546+
return info_return
35433547

35443548
def memory_usage(self, index: bool = True, deep: bool = False) -> Series:
35453549
"""

pandas/io/formats/info.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,22 +494,47 @@ def non_null_counts(self) -> Series:
494494
def memory_usage_bytes(self) -> int:
495495
deep = self.memory_usage == "deep"
496496
return self.data.memory_usage(index=True, deep=deep).sum()
497-
497+
498+
def to_dict(self) -> dict:
499+
"""Return DataFrame info as a dictionary."""
500+
return {
501+
'Column summary': self._get_column_summary(),
502+
'Memory usage': self.memory_usage_bytes,
503+
'Index type': type(self.data.index).__name__,
504+
'Index entries': len(self.data.index),
505+
}
506+
507+
def _get_column_summary(self) -> list[dict]:
508+
"""Return a DataFrame summarizing columns."""
509+
return [
510+
{
511+
'#': i,
512+
'Column': col,
513+
'Non-Null Count': self.data[col].notna().sum(),
514+
'Dtype': self.data[col].dtype
515+
}
516+
for i, col in enumerate(self.ids)
517+
]
518+
498519
def render(
499520
self,
500521
*,
501522
buf: WriteBuffer[str] | None,
502523
max_cols: int | None,
503524
verbose: bool | None,
504525
show_counts: bool | None,
526+
return_dict: bool | None,
505527
) -> None:
506-
printer = _DataFrameInfoPrinter(
507-
info=self,
508-
max_cols=max_cols,
509-
verbose=verbose,
510-
show_counts=show_counts,
511-
)
512-
printer.to_buffer(buf)
528+
if return_dict:
529+
return self.to_dict()
530+
else:
531+
printer = _DataFrameInfoPrinter(
532+
info=self,
533+
max_cols=max_cols,
534+
verbose=verbose,
535+
show_counts=show_counts,
536+
)
537+
printer.to_buffer(buf)
513538

514539

515540
class SeriesInfo(_BaseInfo):

0 commit comments

Comments
 (0)