tabled.diagnose#
DataFrame and table collection diagnosis utilities.
This module provides flexible tools for analyzing pandas DataFrames and collections
of tables. The core function dataframe_info extracts configurable information
from DataFrames using pluggable info functions.
Key Features:
Configurable info extraction with
dataframe_infoCollection diagnosis with
diagnose_table_collectionExtensible via custom info functions
Backward-compatible
print_dataframe_infofrom cosmodata
Example
>>> import pandas as pd
>>> df = pd.DataFrame({'a': [1, 2, 3], 'b': ['x', 'y', 'z']})
>>> info = dataframe_info(df)
>>> info['shape']
(3, 2)
Register custom info function
>>> def get_memory_usage(df):
... return df.memory_usage(deep=True).sum()
>>> register_info_func('custom_memory', get_memory_usage)
Functions
|
Extract information from a DataFrame using specified info functions. |
List all registered info function names. |
|
|
Print information about a DataFrame. |
|
Register a new info function in the default info functions dictionary. |
|
Returns the list of columns that are scalar (therefore serializable to CSV) |
- tabled.diagnose.dataframe_info(df, info_funcs={'categorical_stats': <function _get_categorical_stats>, 'columns': <function _get_columns>, 'first_row': <function _get_first_row>, 'numeric_stats': <function _get_numeric_stats>, 'sample_rows': <function _get_sample_rows>, 'shape': <function _get_shape>}, *, egress=<class 'dict'>)[source]#
Extract information from a DataFrame using specified info functions.
- Parameters:
- Returns:
Result of egress applied to the info generator
>>> import pandas as pd >>> df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}) >>> info = dataframe_info(df, {'shape': _get_shape}) >>> info['shape'] (3, 2)
- tabled.diagnose.print_dataframe_info(df, exclude_columns=(), *, mode='short', egress=<built-in function print>)[source]#
Print information about a DataFrame.
- Parameters:
df (
DataFrame) – The DataFrame to analyzeexclude_columns (
Union[str,list[str]]) – Columns to exclude from analysismode (
Literal['short','sample','stats']) –Type of information to display
’short’: shape and first row
’sample’: shape, columns, and random rows
’stats’: descriptive statistics
egress (
Optional[Callable[[str],None]]) – Callback function for output (None returns string instead of printing)
- Returns:
The formatted info string when
egressisNoneor falsy; otherwise the result of callingegresson that string.- Raises:
ValueError – If
modeis not one of'short','sample','stats'.
>>> import pandas as pd >>> df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}) >>> info = print_dataframe_info(df, egress=None) >>> 'shape: (3, 2)' in info True
- tabled.diagnose.register_info_func(name, func, *, overwrite=False)[source]#
Register a new info function in the default info functions dictionary.
- Parameters:
- Raises:
ValueError – If
nameis already registered andoverwriteisFalse.- Return type:
Example
>>> def get_memory_usage(df): ... return df.memory_usage(deep=True).sum() >>> register_info_func('test_memory', get_memory_usage)
- tabled.diagnose.scalar_columns(df)[source]#
Returns the list of columns that are scalar (therefore serializable to CSV)
More precisely, this function returns the list of columns that contain only scalar values (e.g., int, float, str, bool, etc.) and can be saved to a CSV file.
Example
>>> import pandas as pd >>> df = pd.DataFrame({ ... 'A': [1, 2, 3], ... 'B': ['x', 'y', 'z'], ... 'C': [{'a': 1}, {'b': 2}, {'c': 3}], # Non-serializable column ... 'D': [[1, 2], [3, 4], [5, 6]] # Non-serializable column ... }) >>> scalar_columns(df) ['A', 'B']
- Return type: