diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 744cde95cb668..6b57bf82eafbc 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -211,9 +211,10 @@ 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"{repr(valid_limit_directions)}, " + f"got {repr(limit_direction)}." ) if limit_area is not None: diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index a2a40bbf93604..b2d3cc752d22d 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 {repr(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 853a638bdb277..d688bdfd603a6 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -542,8 +542,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 object " + f"of type {repr(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 d671fff568891..4e173b1851219 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1194,9 +1194,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..fb9859719c4e1 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/series.py b/pandas/core/series.py index a8232f137f3ef..b8eb2728cc681 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2944,7 +2944,7 @@ def _try_kind_sort(arr): sortedIdx[n:] = idx[good][argsorted] sortedIdx[:n] = idx[bad] else: - raise ValueError("invalid na_position: {!r}".format(na_position)) + raise ValueError(f"invalid na_position: {repr(na_position)}") result = self._constructor(arr[sortedIdx], index=self.index[sortedIdx]) diff --git a/pandas/core/sorting.py b/pandas/core/sorting.py index 82eb93dd4c879..ed9ef23132683 100644 --- a/pandas/core/sorting.py +++ b/pandas/core/sorting.py @@ -208,7 +208,7 @@ def lexsort_indexer(keys, orders=None, na_position="last"): cat = Categorical(key, ordered=True) if na_position not in ["last", "first"]: - raise ValueError("invalid na_position: {!r}".format(na_position)) + raise ValueError(f"invalid na_position: {repr(na_position)}") n = len(cat.categories) codes = cat.codes.copy() @@ -264,7 +264,7 @@ def nargsort(items, kind="quicksort", ascending: bool = True, na_position="last" elif na_position == "first": indexer = np.concatenate([nan_idx, indexer]) else: - raise ValueError("invalid na_position: {!r}".format(na_position)) + raise ValueError(f"invalid na_position: {repr(na_position)}") return indexer diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 137c37f938dfa..a822f8418b2db 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -1932,13 +1932,10 @@ def _forbid_nonstring_types(func): @wraps(func) def wrapper(self, *args, **kwargs): if self._inferred_dtype not in allowed_types: - msg = ( - "Cannot use .str.{name} with values of inferred dtype " - "{inf_type!r}.".format( - name=func_name, inf_type=self._inferred_dtype - ) + raise TypeError( + f"Cannot use .str.{func_name} with values of " + f"inferred dtype {repr(self._inferred_dtype)}." ) - raise TypeError(msg) return func(self, *args, **kwargs) wrapper.__name__ = func_name 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