From b28628826875ee39652fe2039d564d4328746cfa Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Mon, 2 Dec 2019 01:33:54 +0200 Subject: [PATCH] repr() --- pandas/_libs/interval.pyx | 6 +++--- pandas/conftest.py | 4 ++-- pandas/core/accessor.py | 6 +++--- pandas/core/algorithms.py | 6 ++---- pandas/core/arrays/categorical.py | 6 +++--- pandas/core/arrays/numpy_.py | 2 +- pandas/core/computation/align.py | 8 ++++---- pandas/core/computation/eval.py | 11 +++++------ pandas/core/computation/expr.py | 4 +--- pandas/core/computation/expressions.py | 10 ++++------ pandas/core/computation/ops.py | 19 ++++++++----------- 11 files changed, 36 insertions(+), 46 deletions(-) diff --git a/pandas/_libs/interval.pyx b/pandas/_libs/interval.pyx index 6bd8693dffebd6..c51dc6403ada0e 100644 --- a/pandas/_libs/interval.pyx +++ b/pandas/_libs/interval.pyx @@ -179,7 +179,7 @@ cdef class IntervalMixin: When `other` is not closed exactly the same as self. """ if self.closed != other.closed: - msg = f"'{name}.closed' is '{other.closed}', expected '{self.closed}'." + msg = f"'{name}.closed' is {repr(other.closed)}, expected {repr(self.closed)}." raise ValueError(msg) @@ -316,7 +316,7 @@ cdef class Interval(IntervalMixin): not tz_compare(left.tzinfo, right.tzinfo)): # GH 18538 msg = (f"left and right must have the same time zone, got " - f"'{left.tzinfo}' and '{right.tzinfo}'") + f"{repr(left.tzinfo)}' and {repr(right.tzinfo)}") raise ValueError(msg) self.left = left self.right = right @@ -379,7 +379,7 @@ cdef class Interval(IntervalMixin): left, right = self._repr_base() name = type(self).__name__ - repr_str = f'{name}({left!r}, {right!r}, closed={self.closed!r})' + repr_str = f'{name}({repr(left)}, {repr(right)}, closed={repr(self.closed)})' return repr_str def __str__(self) -> str: diff --git a/pandas/conftest.py b/pandas/conftest.py index f7d30d537b358f..3553a411a27f83 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -88,7 +88,7 @@ def spmatrix(request): return getattr(sparse, request.param + "_matrix") -@pytest.fixture(params=[0, 1, "index", "columns"], ids=lambda x: "axis {!r}".format(x)) +@pytest.fixture(params=[0, 1, "index", "columns"], ids=lambda x: f"axis {repr(x)}") def axis(request): """ Fixture for returning the axis numbers of a DataFrame. @@ -99,7 +99,7 @@ def axis(request): axis_frame = axis -@pytest.fixture(params=[0, "index"], ids=lambda x: "axis {!r}".format(x)) +@pytest.fixture(params=[0, "index"], ids=lambda x: f"axis {repr(x)}") def axis_series(request): """ Fixture for returning the axis numbers of a Series. diff --git a/pandas/core/accessor.py b/pandas/core/accessor.py index 182b07d57ea490..96b7cf8f97c3f5 100644 --- a/pandas/core/accessor.py +++ b/pandas/core/accessor.py @@ -183,9 +183,9 @@ def _register_accessor(name, cls): def decorator(accessor): if hasattr(cls, name): warnings.warn( - "registration of accessor {!r} under name {!r} for type " - "{!r} is overriding a preexisting attribute with the same " - "name.".format(accessor, name, cls), + f"registration of accessor {repr(accessor)} under name " + f"{repr(name)} for type {repr(cls)} is overriding a preexisting" + f"attribute with the same name.", UserWarning, stacklevel=2, ) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 18adb12a9ad724..44bdf478affc40 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1194,10 +1194,8 @@ def compute(self, method): dtype = frame[column].dtype if not self.is_valid_dtype_n_method(dtype): raise TypeError( - ( - "Column {column!r} has dtype {dtype}, cannot use method " - "{method!r} with this dtype" - ).format(column=column, dtype=dtype, method=method) + f"Column {repr(column)} has dtype {repr(dtype)}, " + f"cannot use method {repr(method)} with this dtype" ) def get_indexer(current_indexer, other_indexer): diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 46aab31770fde9..83f6051b8423fd 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -1632,7 +1632,7 @@ def sort_values(self, inplace=False, ascending=True, na_position="last"): """ inplace = validate_bool_kwarg(inplace, "inplace") if na_position not in ["last", "first"]: - raise ValueError(f"invalid na_position: {na_position!r}") + raise ValueError(f"invalid na_position: {repr(na_position)}") sorted_idx = nargsort(self, ascending=ascending, na_position=na_position) @@ -1769,8 +1769,8 @@ def fillna(self, value=None, method=None, limit=None): else: raise TypeError( - '"value" parameter must be a scalar, dict ' - f'or Series, but you passed a {type(value).__name__!r}"' + f"'value' parameter must be a scalar, dict " + f"or Series, but you passed a {type(value).__name__}" ) return self._constructor(codes, dtype=self.dtype, fastpath=True) diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index 6f2bb095a014dc..32ef3f4fbfed06 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -45,7 +45,7 @@ def __init__(self, dtype): self._type = dtype.type def __repr__(self) -> str: - return "PandasDtype({!r})".format(self.name) + return f"PandasDtype({repr(self.name)})" @property def numpy_dtype(self): diff --git a/pandas/core/computation/align.py b/pandas/core/computation/align.py index 197ddd999fd374..9390eb47d07eef 100644 --- a/pandas/core/computation/align.py +++ b/pandas/core/computation/align.py @@ -100,10 +100,10 @@ def _align_core(terms): ordm = np.log10(max(1, abs(reindexer_size - term_axis_size))) if ordm >= 1 and reindexer_size >= 10000: w = ( - "Alignment difference on axis {axis} is larger " - "than an order of magnitude on term {term!r}, by " - "more than {ordm:.4g}; performance may suffer" - ).format(axis=axis, term=terms[i].name, ordm=ordm) + f"Alignment difference on axis {axis} is larger " + f"than an order of magnitude on term {repr(terms[i].name)}, " + f"by more than {ordm:.4g}; performance may suffer" + ) warnings.warn(w, category=PerformanceWarning, stacklevel=6) f = partial(ti.reindex, reindexer, axis=axis, copy=False) diff --git a/pandas/core/computation/eval.py b/pandas/core/computation/eval.py index 598680ca6c2de7..c8c4310cc0fee5 100644 --- a/pandas/core/computation/eval.py +++ b/pandas/core/computation/eval.py @@ -47,8 +47,7 @@ def _check_engine(engine): if engine not in _engines: valid = list(_engines.keys()) raise KeyError( - "Invalid engine {engine!r} passed, valid engines are" - " {valid}".format(engine=engine, valid=valid) + f"Invalid engine {repr(engine)} passed, valid engines are {valid}" ) # TODO: validate this in a more general way (thinking of future engines @@ -82,8 +81,8 @@ def _check_parser(parser: str): if parser not in _parsers: raise KeyError( - "Invalid parser {parser!r} passed, valid parsers are" - " {valid}".format(parser=parser, valid=_parsers.keys()) + f"Invalid parser {repr(parser)} passed, " + f"valid parsers are {repr(_parsers.keys())}" ) @@ -93,8 +92,8 @@ def _check_resolvers(resolvers): if not hasattr(resolver, "__getitem__"): name = type(resolver).__name__ raise TypeError( - "Resolver of type {name!r} does not implement " - "the __getitem__ method".format(name=name) + f"Resolver of type {repr(name)} does not " + f"implement the __getitem__ method" ) diff --git a/pandas/core/computation/expr.py b/pandas/core/computation/expr.py index e608f82b03ade6..9330586bbce682 100644 --- a/pandas/core/computation/expr.py +++ b/pandas/core/computation/expr.py @@ -294,9 +294,7 @@ def _node_not_implemented(node_name, cls): """ def f(self, *args, **kwargs): - raise NotImplementedError( - "{name!r} nodes are not implemented".format(name=node_name) - ) + raise NotImplementedError(f"{repr(node_name)} nodes are not implemented") return f diff --git a/pandas/core/computation/expressions.py b/pandas/core/computation/expressions.py index 77999d2c166fde..1a493bc58a2274 100644 --- a/pandas/core/computation/expressions.py +++ b/pandas/core/computation/expressions.py @@ -175,17 +175,15 @@ def _bool_arith_check( if _has_bool_dtype(a) and _has_bool_dtype(b): if op_str in unsupported: warnings.warn( - "evaluating in Python space because the {op!r} " - "operator is not supported by numexpr for " - "the bool dtype, use {alt_op!r} instead".format( - op=op_str, alt_op=unsupported[op_str] - ) + f"evaluating in Python space because the {repr(op_str)} " + f"operator is not supported by numexpr for " + f"the bool dtype, use {repr(unsupported[op_str])} instead" ) return False if op_str in not_allowed: raise NotImplementedError( - "operator {op!r} not implemented for bool dtypes".format(op=op_str) + f"operator {repr(op_str)} not implemented for bool dtypes" ) return True diff --git a/pandas/core/computation/ops.py b/pandas/core/computation/ops.py index 4852e498537f2f..fe02963e4782d8 100644 --- a/pandas/core/computation/ops.py +++ b/pandas/core/computation/ops.py @@ -56,11 +56,12 @@ class UndefinedVariableError(NameError): """ def __init__(self, name, is_local: bool): + base_msg = f"{repr(name)} is not defined" if is_local: - msg = "local variable {0!r} is not defined" + msg = f"local variable {base_msg}" else: - msg = "name {0!r} is not defined" - super().__init__(msg.format(name)) + msg = f"name {base_msg}" + super().__init__(msg) class Term: @@ -143,10 +144,7 @@ def type(self): @property def raw(self) -> str: - return pprint_thing( - "{0}(name={1!r}, type={2})" - "".format(type(self).__name__, self.name, self.type) - ) + return f"{type(self).__name__}(name={repr(self.name)}, type={self.type})" @property def is_datetime(self) -> bool: @@ -374,8 +372,7 @@ def __init__(self, op: str, lhs, rhs, **kwargs): # has to be made a list for python3 keys = list(_binary_ops_dict.keys()) raise ValueError( - "Invalid binary operator {0!r}, valid" - " operators are {1}".format(op, keys) + f"Invalid binary operator {repr(op)}, valid operators are {keys}" ) def __call__(self, env): @@ -548,8 +545,8 @@ def __init__(self, op: str, operand): self.func = _unary_ops_dict[op] except KeyError: raise ValueError( - "Invalid unary operator {0!r}, valid operators " - "are {1}".format(op, _unary_ops_syms) + f"Invalid unary operator {repr(op)}, " + f"valid operators are {_unary_ops_syms}" ) def __call__(self, env):