def mathify_column(x: pd.Series, **kwargs) -> pd.Series: """ Mathify (when one could) a pd.Series iteratively Parameters ---------- x : pd.Series The input series **kwargs: num_digit : int, optional Number of digits after the decimal point represented, by default 3. Integers that are > 1 are ignored. large : int, optional The upper limit until scientific notation; scientific notation is activated if s > 10 ** large; the parameter is ignored if auto_scientific is False, by default 9 na_rep : str, optional Representation of missing values (NaN, nan, None, etc.), by default "---" additional_enclosures : Iterable[Enclosure], optional Additional enclosures to be checked by mathify, by default [] auto_scientific : bool, optional Whether to convert automatically into scientific notation, by default True. Scientific notation is activated if number is outside of [10 ** -num_digit, 10 ** large] Returns ------- pd.Series The output series """ return x.apply(skiperror(lambda y: mathify(y, **kwargs), return_x=True))
def mathify_table(df: pd.DataFrame, texttt_index: bool = False, texttt_column: bool = False, **kwargs) -> pd.DataFrame: """ Mathify a pd.DataFrame iteratively Parameters ---------- df : pd.DataFrame The input DataFrame texttt_index : bool, optional Whether or not to escape index and convert index labels to \\texttt, by default False texttt_column : bool, optional Whether or not to escape index and convert column labels to \\texttt, by default False **kwargs: num_digit : int, optional Number of digits after the decimal point represented, by default 3. Integers that are > 1 are ignored. large : int, optional The upper limit until scientific notation; scientific notation is activated if s > 10 ** large; the parameter is ignored if auto_scientific is False, by default 9 na_rep : str, optional Representation of missing values (NaN, nan, None, etc.), by default "---" additional_enclosures : Iterable[Enclosure], optional Additional enclosures to be checked by mathify, by default [] auto_scientific : bool, optional Whether to convert automatically into scientific notation, by default True. Scientific notation is activated if number is outside of [10 ** -num_digit, 10 ** large] Returns ------- d : pd.DataFrame The output DataFrame """ d = df.apply( skiperror(lambda x: mathify_column(x, **kwargs), return_x=True)) if texttt_index: d.index = [ "\\texttt{{{}}}".format(s.replace("_", "\\_")) if (len(s) > 0 and s[0] != "$" and s[-1] != "$") else s for s in d.index.astype(str) ] if texttt_column: d.columns = [ "\\texttt{{{}}}".format(s.replace("_", "\\_")) if (len(s) > 0 and s[0] != "$" and s[-1] != "$") else s for s in np.array(d.columns).astype(str) ] return d
def test_skiperror(): df = pd.DataFrame({"x": [1, 2, 3, "a"], "y": [1, 2, 3, "b"]}) def func(s): return s + 1 # Verify that applying function causes error with pytest.raises(Exception): df["x"].apply(func) result = df["x"].apply(skiperror(func)) assert (result.values[:-1] == np.array([2, 3, 4])).all() and np.isnan( result.values[-1]) result = df["x"].apply(skiperror(func, return_x=True)) assert (result.values == np.array([2, 3, 4, "a"], dtype=object)).all() result = df["x"].apply(skiperror(func, return_x=False, return_val=5)) assert (result.values == np.array([2, 3, 4, 5])).all()