diff --git a/pandas/_libs/interval.pyx b/pandas/_libs/interval.pyx index 6bd8693dffebd..c51dc6403ada0 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 f7d30d537b358..3553a411a27f8 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 182b07d57ea49..96b7cf8f97c3f 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 18adb12a9ad72..44bdf478affc4 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 46aab31770fde..83f6051b8423f 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 6f2bb095a014d..32ef3f4fbfed0 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 197ddd999fd37..9390eb47d07ee 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 598680ca6c2de..c8c4310cc0fee 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 e608f82b03ade..9330586bbce68 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 77999d2c166fd..1a493bc58a227 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 4852e498537f2..fe02963e4782d 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):