Skip to content

Commit 5cd2036

Browse files
author
MomIsBestFriend
committed
repr()
1 parent f5c102f commit 5cd2036

File tree

11 files changed

+37
-47
lines changed

11 files changed

+37
-47
lines changed

pandas/_libs/interval.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ cdef class IntervalMixin:
179179
When `other` is not closed exactly the same as self.
180180
"""
181181
if self.closed != other.closed:
182-
msg = f"'{name}.closed' is '{other.closed}', expected '{self.closed}'."
183-
raise ValueError(msg)
182+
raise ValueError(f"'{name}.closed' is {repr(other.closed)}, "
183+
f"expected {repr(self.closed)}.")
184184

185185

186186
cdef _interval_like(other):
@@ -316,7 +316,7 @@ cdef class Interval(IntervalMixin):
316316
not tz_compare(left.tzinfo, right.tzinfo)):
317317
# GH 18538
318318
msg = (f"left and right must have the same time zone, got "
319-
f"'{left.tzinfo}' and '{right.tzinfo}'")
319+
f"{repr(left.tzinfo)}' and {repr(right.tzinfo)}")
320320
raise ValueError(msg)
321321
self.left = left
322322
self.right = right
@@ -379,7 +379,7 @@ cdef class Interval(IntervalMixin):
379379

380380
left, right = self._repr_base()
381381
name = type(self).__name__
382-
repr_str = f'{name}({left!r}, {right!r}, closed={self.closed!r})'
382+
repr_str = f'{name}({repr(left)}, {repr(right)}, closed={repr(self.closed)})'
383383
return repr_str
384384

385385
def __str__(self) -> str:

pandas/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def spmatrix(request):
8888
return getattr(sparse, request.param + "_matrix")
8989

9090

91-
@pytest.fixture(params=[0, 1, "index", "columns"], ids=lambda x: "axis {!r}".format(x))
91+
@pytest.fixture(params=[0, 1, "index", "columns"], ids=lambda x: f"axis {repr(x)}")
9292
def axis(request):
9393
"""
9494
Fixture for returning the axis numbers of a DataFrame.
@@ -99,7 +99,7 @@ def axis(request):
9999
axis_frame = axis
100100

101101

102-
@pytest.fixture(params=[0, "index"], ids=lambda x: "axis {!r}".format(x))
102+
@pytest.fixture(params=[0, "index"], ids=lambda x: f"axis {repr(x)}")
103103
def axis_series(request):
104104
"""
105105
Fixture for returning the axis numbers of a Series.

pandas/core/accessor.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ def _register_accessor(name, cls):
183183
def decorator(accessor):
184184
if hasattr(cls, name):
185185
warnings.warn(
186-
"registration of accessor {!r} under name {!r} for type "
187-
"{!r} is overriding a preexisting attribute with the same "
188-
"name.".format(accessor, name, cls),
186+
f"registration of accessor {repr(accessor)} under name "
187+
f"{repr(name)} for type {repr(cls)} is overriding a preexisting"
188+
f"attribute with the same name.",
189189
UserWarning,
190190
stacklevel=2,
191191
)

pandas/core/algorithms.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1194,10 +1194,8 @@ def compute(self, method):
11941194
dtype = frame[column].dtype
11951195
if not self.is_valid_dtype_n_method(dtype):
11961196
raise TypeError(
1197-
(
1198-
"Column {column!r} has dtype {dtype}, cannot use method "
1199-
"{method!r} with this dtype"
1200-
).format(column=column, dtype=dtype, method=method)
1197+
f"Column {repr(column)} has dtype {dtype}, "
1198+
f"cannot use method {repr(method)} with this dtype"
12011199
)
12021200

12031201
def get_indexer(current_indexer, other_indexer):

pandas/core/arrays/categorical.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1632,7 +1632,7 @@ def sort_values(self, inplace=False, ascending=True, na_position="last"):
16321632
"""
16331633
inplace = validate_bool_kwarg(inplace, "inplace")
16341634
if na_position not in ["last", "first"]:
1635-
raise ValueError(f"invalid na_position: {na_position!r}")
1635+
raise ValueError(f"invalid na_position: {repr(na_position)}")
16361636

16371637
sorted_idx = nargsort(self, ascending=ascending, na_position=na_position)
16381638

@@ -1769,8 +1769,8 @@ def fillna(self, value=None, method=None, limit=None):
17691769

17701770
else:
17711771
raise TypeError(
1772-
'"value" parameter must be a scalar, dict '
1773-
f'or Series, but you passed a {type(value).__name__!r}"'
1772+
f"'value' parameter must be a scalar, dict "
1773+
f"or Series, but you passed a {type(value).__name__}"
17741774
)
17751775

17761776
return self._constructor(codes, dtype=self.dtype, fastpath=True)

pandas/core/arrays/numpy_.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, dtype):
4545
self._type = dtype.type
4646

4747
def __repr__(self) -> str:
48-
return "PandasDtype({!r})".format(self.name)
48+
return f"PandasDtype({repr(self.name)})"
4949

5050
@property
5151
def numpy_dtype(self):

pandas/core/computation/align.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ def _align_core(terms):
100100
ordm = np.log10(max(1, abs(reindexer_size - term_axis_size)))
101101
if ordm >= 1 and reindexer_size >= 10000:
102102
w = (
103-
"Alignment difference on axis {axis} is larger "
104-
"than an order of magnitude on term {term!r}, by "
105-
"more than {ordm:.4g}; performance may suffer"
106-
).format(axis=axis, term=terms[i].name, ordm=ordm)
103+
f"Alignment difference on axis {axis} is larger "
104+
f"than an order of magnitude on term {repr(terms[i].name)}, "
105+
f"by more than {ordm:.4g}; performance may suffer"
106+
)
107107
warnings.warn(w, category=PerformanceWarning, stacklevel=6)
108108

109109
f = partial(ti.reindex, reindexer, axis=axis, copy=False)

pandas/core/computation/eval.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ def _check_engine(engine):
4747
if engine not in _engines:
4848
valid = list(_engines.keys())
4949
raise KeyError(
50-
"Invalid engine {engine!r} passed, valid engines are"
51-
" {valid}".format(engine=engine, valid=valid)
50+
f"Invalid engine {repr(engine)} passed, valid engines are {valid}"
5251
)
5352

5453
# TODO: validate this in a more general way (thinking of future engines
@@ -82,8 +81,8 @@ def _check_parser(parser: str):
8281

8382
if parser not in _parsers:
8483
raise KeyError(
85-
"Invalid parser {parser!r} passed, valid parsers are"
86-
" {valid}".format(parser=parser, valid=_parsers.keys())
84+
f"Invalid parser {repr(parser)} passed, "
85+
f"valid parsers are {_parsers.keys()}"
8786
)
8887

8988

@@ -93,8 +92,8 @@ def _check_resolvers(resolvers):
9392
if not hasattr(resolver, "__getitem__"):
9493
name = type(resolver).__name__
9594
raise TypeError(
96-
"Resolver of type {name!r} does not implement "
97-
"the __getitem__ method".format(name=name)
95+
f"Resolver of type {repr(name)} does not "
96+
f"implement the __getitem__ method"
9897
)
9998

10099

pandas/core/computation/expr.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,7 @@ def _node_not_implemented(node_name, cls):
294294
"""
295295

296296
def f(self, *args, **kwargs):
297-
raise NotImplementedError(
298-
"{name!r} nodes are not implemented".format(name=node_name)
299-
)
297+
raise NotImplementedError(f"{repr(node_name)} nodes are not implemented")
300298

301299
return f
302300

pandas/core/computation/expressions.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,15 @@ def _bool_arith_check(
175175
if _has_bool_dtype(a) and _has_bool_dtype(b):
176176
if op_str in unsupported:
177177
warnings.warn(
178-
"evaluating in Python space because the {op!r} "
179-
"operator is not supported by numexpr for "
180-
"the bool dtype, use {alt_op!r} instead".format(
181-
op=op_str, alt_op=unsupported[op_str]
182-
)
178+
f"evaluating in Python space because the {repr(op_str)} "
179+
f"operator is not supported by numexpr for "
180+
f"the bool dtype, use {repr(unsupported[op_str])} instead"
183181
)
184182
return False
185183

186184
if op_str in not_allowed:
187185
raise NotImplementedError(
188-
"operator {op!r} not implemented for bool dtypes".format(op=op_str)
186+
f"operator {repr(op_str)} not implemented for bool dtypes"
189187
)
190188
return True
191189

pandas/core/computation/ops.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ class UndefinedVariableError(NameError):
5656
"""
5757

5858
def __init__(self, name, is_local: bool):
59+
base_msg = f"{repr(name)} is not defined"
5960
if is_local:
60-
msg = "local variable {0!r} is not defined"
61+
msg = f"local variable {base_msg}"
6162
else:
62-
msg = "name {0!r} is not defined"
63-
super().__init__(msg.format(name))
63+
msg = f"name {base_msg}"
64+
super().__init__(msg)
6465

6566

6667
class Term:
@@ -143,10 +144,7 @@ def type(self):
143144

144145
@property
145146
def raw(self) -> str:
146-
return pprint_thing(
147-
"{0}(name={1!r}, type={2})"
148-
"".format(type(self).__name__, self.name, self.type)
149-
)
147+
return f"{type(self).__name__}(name={repr(self.name)}, type={self.type})"
150148

151149
@property
152150
def is_datetime(self) -> bool:
@@ -374,8 +372,7 @@ def __init__(self, op: str, lhs, rhs, **kwargs):
374372
# has to be made a list for python3
375373
keys = list(_binary_ops_dict.keys())
376374
raise ValueError(
377-
"Invalid binary operator {0!r}, valid"
378-
" operators are {1}".format(op, keys)
375+
f"Invalid binary operator {repr(op)}, valid operators are {keys}"
379376
)
380377

381378
def __call__(self, env):
@@ -548,8 +545,8 @@ def __init__(self, op: str, operand):
548545
self.func = _unary_ops_dict[op]
549546
except KeyError:
550547
raise ValueError(
551-
"Invalid unary operator {0!r}, valid operators "
552-
"are {1}".format(op, _unary_ops_syms)
548+
f"Invalid unary operator {repr(op)}, "
549+
f"valid operators are {_unary_ops_syms}"
553550
)
554551

555552
def __call__(self, env):

0 commit comments

Comments
 (0)