diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 9f4f7445509c9..5b9e37bd54bad 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -211,9 +211,9 @@ def interpolate_1d( valid_limit_directions = ["forward", "backward", "both"] limit_direction = limit_direction.lower() if limit_direction not in valid_limit_directions: - msg = "Invalid limit_direction: expecting one of {valid!r}, got {invalid!r}." raise ValueError( - msg.format(valid=valid_limit_directions, invalid=limit_direction) + f"Invalid limit_direction: expecting one of " + f"{valid_limit_directions}, got '{limit_direction}'." ) if limit_area is not None: diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 9e436ec4c6c13..77e9f0b44c0cb 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -60,8 +60,10 @@ def __call__(self, f): def _f(*args, **kwargs): obj_iter = itertools.chain(args, kwargs.values()) if any(self.check(obj) for obj in obj_iter): - msg = "reduction operation {name!r} not allowed for this dtype" - raise TypeError(msg.format(name=f.__name__.replace("nan", ""))) + f_name = f.__name__.replace("nan", "") + raise TypeError( + f"reduction operation '{f_name}' not allowed for this dtype" + ) try: with np.errstate(invalid="ignore"): return f(*args, **kwargs) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index 8319db9f07ad8..cea70012b47ea 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -507,8 +507,8 @@ def _get_concat_axis(self) -> Index: for i, x in enumerate(self.objs): if not isinstance(x, Series): raise TypeError( - "Cannot concatenate type 'Series' " - "with object of type {type!r}".format(type=type(x).__name__) + f"Cannot concatenate type 'Series' with " + f"object of type '{type(x).__name__}'" ) if x.name is not None: names[i] = x.name diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 726a59ca8e008..0fb029c8429a6 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1197,9 +1197,7 @@ def _validate_specification(self): ) ) if not common_cols.is_unique: - raise MergeError( - "Data columns not unique: {common!r}".format(common=common_cols) - ) + raise MergeError(f"Data columns not unique: {repr(common_cols)}") self.left_on = self.right_on = common_cols elif self.on is not None: if self.left_on is not None or self.right_on is not None: diff --git a/pandas/core/reshape/tile.py b/pandas/core/reshape/tile.py index bfaa49dd576dc..ceb4e3290ff75 100644 --- a/pandas/core/reshape/tile.py +++ b/pandas/core/reshape/tile.py @@ -376,9 +376,8 @@ def _bins_to_cuts( if len(unique_bins) < len(bins) and len(bins) != 2: if duplicates == "raise": raise ValueError( - "Bin edges must be unique: {bins!r}.\nYou " - "can drop duplicate edges by setting " - "the 'duplicates' kwarg".format(bins=bins) + f"Bin edges must be unique: {repr(bins)}.\n" + f"You can drop duplicate edges by setting the 'duplicates' kwarg" ) else: bins = unique_bins diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 040a28ea2ee8c..d4d8be90402b7 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1933,8 +1933,8 @@ def _forbid_nonstring_types(func): def wrapper(self, *args, **kwargs): if self._inferred_dtype not in allowed_types: msg = ( - f"Cannot use .str.{func_name} with values of inferred dtype " - f"{repr(self._inferred_dtype)}." + f"Cannot use .str.{func_name} with values of " + f"inferred dtype '{self._inferred_dtype}'." ) raise TypeError(msg) return func(self, *args, **kwargs) diff --git a/pandas/io/formats/css.py b/pandas/io/formats/css.py index cd58ef51d1b65..6bc5f56bb5f7f 100644 --- a/pandas/io/formats/css.py +++ b/pandas/io/formats/css.py @@ -173,7 +173,7 @@ def __call__(self, declarations_str, inherited=None): def size_to_pt(self, in_val, em_pt=None, conversions=UNIT_RATIOS): def _error(): - warnings.warn("Unhandled size: {val!r}".format(val=in_val), CSSWarning) + warnings.warn(f"Unhandled size: {repr(in_val)}", CSSWarning) return self.size_to_pt("1!!default", conversions=conversions) try: @@ -252,7 +252,6 @@ def parse(self, declarations_str): yield prop, val else: warnings.warn( - "Ill-formatted attribute: expected a colon " - "in {decl!r}".format(decl=decl), + f"Ill-formatted attribute: expected a colon in {repr(decl)}", CSSWarning, ) diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 0413dcf18d04a..5d8573c1341f5 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -321,7 +321,7 @@ def color_to_excel(self, val): try: return self.NAMED_COLORS[val] except KeyError: - warnings.warn("Unhandled color format: {val!r}".format(val=val), CSSWarning) + warnings.warn(f"Unhandled color format: {repr(val)}", CSSWarning) def build_number_format(self, props): return {"format_code": props.get("number-format")} diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index ebe86a7f535cb..a45045991e879 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -627,29 +627,25 @@ def _apply(self, func, axis=0, subset=None, **kwargs): result = func(data, **kwargs) if not isinstance(result, pd.DataFrame): raise TypeError( - "Function {func!r} must return a DataFrame when " - "passed to `Styler.apply` with axis=None".format(func=func) + f"Function {repr(func)} must return a DataFrame when " + f"passed to `Styler.apply` with axis=None" ) if not ( result.index.equals(data.index) and result.columns.equals(data.columns) ): - msg = ( - "Result of {func!r} must have identical index and " - "columns as the input".format(func=func) + raise ValueError( + f"Result of {repr(func)} must have identical " + f"index and columns as the input" ) - raise ValueError(msg) result_shape = result.shape expected_shape = self.data.loc[subset].shape if result_shape != expected_shape: - msg = ( - "Function {func!r} returned the wrong shape.\n" - "Result has shape: {res}\n" - "Expected shape: {expect}".format( - func=func, res=result.shape, expect=expected_shape - ) + raise ValueError( + f"Function {repr(func)} returned the wrong shape.\n" + f"Result has shape: {result.shape}\n" + f"Expected shape: {expected_shape}" ) - raise ValueError(msg) self._update_ctx(result) return self