MCPcopy
hub / github.com/pandas-dev/pandas / _maybe_wrap_formatter

Function _maybe_wrap_formatter

pandas/io/formats/style_render.py:1968–2019  ·  view source on GitHub ↗

Allows formatters to be expressed as str, callable or None, where None returns a default formatting function. wraps with na_rep, and precision where they are available.

(
    formatter: BaseFormatter | None = None,
    na_rep: str | None = None,
    precision: int | None = None,
    decimal: str = ".",
    thousands: str | None = None,
    escape: str | None = None,
    hyperlinks: str | None = None,
)

Source from the content-addressed store, hash-verified

1966
1967
1968def _maybe_wrap_formatter(
1969 formatter: BaseFormatter | None = None,
1970 na_rep: str | None = None,
1971 precision: int | None = None,
1972 decimal: str = ".",
1973 thousands: str | None = None,
1974 escape: str | None = None,
1975 hyperlinks: str | None = None,
1976) -> Callable:
1977 """
1978 Allows formatters to be expressed as str, callable or None, where None returns
1979 a default formatting function. wraps with na_rep, and precision where they are
1980 available.
1981 """
1982 # Get initial func from input string, input callable, or from default factory
1983 if isinstance(formatter, str):
1984 func_0 = lambda x: formatter.format(x)
1985 elif callable(formatter):
1986 func_0 = formatter
1987 elif formatter is None:
1988 precision = (
1989 get_option("styler.format.precision") if precision is None else precision
1990 )
1991 func_0 = partial(
1992 _default_formatter, precision=precision, thousands=(thousands is not None)
1993 )
1994 else:
1995 raise TypeError(f"'formatter' expected str or callable, got {type(formatter)}")
1996
1997 # Replace chars if escaping
1998 if escape is not None:
1999 func_1 = lambda x: func_0(_str_escape(x, escape=escape))
2000 else:
2001 func_1 = func_0
2002
2003 # Replace decimals and thousands if non-standard inputs detected
2004 if decimal != "." or (thousands is not None and thousands != ","):
2005 func_2 = _wrap_decimal_thousands(func_1, decimal=decimal, thousands=thousands)
2006 else:
2007 func_2 = func_1
2008
2009 # Render links
2010 if hyperlinks is not None:
2011 func_3 = lambda x: func_2(_render_href(x, format=hyperlinks))
2012 else:
2013 func_3 = func_2
2014
2015 # Replace missing values if na_rep
2016 if na_rep is None:
2017 return func_3
2018 else:
2019 return lambda x: na_rep if (isna(x) is True) else func_3(x)
2020
2021
2022def non_reducing_slice(slice_: Subset):

Callers 3

formatMethod · 0.85
format_indexMethod · 0.85
format_index_namesMethod · 0.85

Calls 7

get_optionFunction · 0.90
isnaFunction · 0.90
_str_escapeFunction · 0.85
_wrap_decimal_thousandsFunction · 0.85
_render_hrefFunction · 0.85
func_2Function · 0.50
formatMethod · 0.45

Tested by

no test coverage detected