datopy.inspection#

Description

Tools for visualizing matrix and dataframe operations.

Notes

Adapted from [1] to support NumPy arrays, in addition to Pandas data frames.

References

[1]

VanderPlas, J. (2016). Python data science handbook: Essential tools for working with data. O’Reilly Media, Inc.


make_df(
cols: Iterable[Any],
ind: Iterable[Any],
) DataFrame[source]#

Generate a data frame with a simple structure for conducting tests.

Parameters:
  • cols (Iterable[Any]) – An iterable with items representing column names.

  • ind (Iterable[Any]) – An iterable with items representing row names.

Returns:

A pandas data frame of shape (len(ind), len(cols)).

Return type:

pd.DataFrame

Examples

>>> from datopy.inspection import make_df
>>> import pandas as pd
>>> make_df("ABC", [1,2,3])
    A   B   C
1  A1  B1  C1
2  A2  B2  C2
3  A3  B3  C3
>>> make_df([1,2,3], ("A", "B", "C"))
    1   2   3
A  1A  2A  3A
B  1B  2B  3B
C  1C  2C  3C
display(
*args,
globs: dict[str, Any] | None = None,
bold: bool = True,
) None[source]#

Display an informative representation of multiple objects side-by-side.

Parameters:
  • *args (tuple) – Tuple of expressions to evaluate and display.

  • globs (dict[str, Any], default=None) – Global namespace, to give eval() access to nonlocals passed by name.

  • bold (bool, default=True) – Option to enable/disable string styling.

Warning

This function uses eval() to render expressions it receives as strings. Access to variables in the global namespace is controlled by globs. Take care to only pass trusted expressions to the function.

Examples

>>> import numpy as np
>>> import pandas as pd
>>> from datopy.inspection import display, make_df

Data frame example

>>> df1 = make_df('AB', [1, 2]); df2 = make_df('AB', [3, 4])
>>> display('df1', 'df2', 'pd.concat([df1, df2])', globs=globals(), bold=False)

df1
--- (2, 2) ---
    A   B
1  A1  B1
2  A2  B2


df2
--- (2, 2) ---
    A   B
3  A3  B3
4  A4  B4


pd.concat([df1, df2])
--- (4, 2) ---
    A   B
1  A1  B1
2  A2  B2
3  A3  B3
4  A4  B4

Matrix example

>>> A = np.array([[1, 3], [2, 4]]); x = np.array([[0, 1]])
>>> display("A", "x.T", "np.dot(A, x.T)", globs=globals(), bold=False)

A
--- (2, 2) ---
array([[1, 3],
       [2, 4]])


x.T
--- (2, 1) ---
array([[0],
       [1]])


np.dot(A, x.T)
--- (2, 1) ---
array([[3],
       [4]])


Functions

display(*args[, globs, bold])

Display an informative representation of multiple objects side-by-side.

make_df(cols, ind)

Generate a data frame with a simple structure for conducting tests.