Skip to content

CLN: replace str.format() with f-string #30363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1740,7 +1740,7 @@ def to_records(self, index=True, column_dtypes=None, index_dtypes=None):
rec.array([(b'a', 1, 0.5 ), (b'b', 2, 0.75)],
dtype=[('I', 'S2'), ('A', '<i8'), ('B', '<f8')])

>>> index_dtypes = "<S{}".format(df.index.str.len().max())
>>> index_dtypes = f"<S{df.index.str.len().max()}"
>>> df.to_records(index_dtypes=index_dtypes)
rec.array([(b'a', 1, 0.5 ), (b'b', 2, 0.75)],
dtype=[('I', 'S1'), ('A', '<i8'), ('B', '<f8')])
Expand Down Expand Up @@ -2340,13 +2340,9 @@ def _sizeof_fmt(num, size_qualifier):
# returns size in human readable format
for x in ["bytes", "KB", "MB", "GB", "TB"]:
if num < 1024.0:
return "{num:3.1f}{size_q} {x}".format(
num=num, size_q=size_qualifier, x=x
)
return f"{num:3.1f}{size_qualifier} {x}"
num /= 1024.0
return "{num:3.1f}{size_q} {pb}".format(
num=num, size_q=size_qualifier, pb="PB"
)
return f"{num:3.1f}{size_qualifier} PB"

if verbose:
_verbose_repr()
Expand All @@ -2359,7 +2355,7 @@ def _sizeof_fmt(num, size_qualifier):
_verbose_repr()

counts = self._data.get_dtype_counts()
dtypes = ["{k}({kk:d})".format(k=k[0], kk=k[1]) for k in sorted(counts.items())]
dtypes = [f"{k[0]}({k[1]:d})" for k in sorted(counts.items())]
lines.append(f"dtypes: {', '.join(dtypes)}")

if memory_usage is None:
Expand Down
79 changes: 31 additions & 48 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ def _single_replace(self, to_replace, method, inplace, limit):
"""
if self.ndim != 1:
raise TypeError(
"cannot replace {0} with method {1} on a {2}".format(
to_replace, method, type(self).__name__
)
f"cannot replace {to_replace} with method {method} on a "
f"{type(self).__name__}"
)

orig_dtype = self.dtype
Expand Down Expand Up @@ -254,7 +253,7 @@ def _validate_dtype(self, dtype):
if dtype.kind == "V":
raise NotImplementedError(
"compound dtypes are not implemented"
" in the {0} constructor".format(type(self).__name__)
f" in the {type(self).__name__} constructor"
)

return dtype
Expand Down Expand Up @@ -396,7 +395,7 @@ def _get_axis_number(cls, axis):
return cls._AXIS_NUMBERS[axis]
except KeyError:
pass
raise ValueError("No axis named {0} for object type {1}".format(axis, cls))
raise ValueError(f"No axis named {axis} for object type {cls}")

@classmethod
def _get_axis_name(cls, axis):
Expand All @@ -409,7 +408,7 @@ def _get_axis_name(cls, axis):
return cls._AXIS_NAMES[axis]
except KeyError:
pass
raise ValueError("No axis named {0} for object type {1}".format(axis, cls))
raise ValueError(f"No axis named {axis} for object type {cls}")

def _get_axis(self, axis):
name = self._get_axis_name(axis)
Expand Down Expand Up @@ -437,7 +436,7 @@ def _get_axis_resolvers(self, axis):
# prefix with 'i' or 'c' depending on the input axis
# e.g., you must do ilevel_0 for the 0th level of an unnamed
# multiiindex
key = "{prefix}level_{i}".format(prefix=prefix, i=i)
key = f"{prefix}level_{i}"
level = i

level_values = axis_index.get_level_values(level)
Expand Down Expand Up @@ -1082,7 +1081,7 @@ def rename(self, *args, **kwargs):
if kwargs:
raise TypeError(
"rename() got an unexpected keyword "
'argument "{0}"'.format(list(kwargs.keys())[0])
f'argument "{list(kwargs.keys())[0]}"'
)

if com.count_not_none(*axes.values()) == 0:
Expand All @@ -1108,7 +1107,7 @@ def rename(self, *args, **kwargs):
missing_labels = [
label for index, label in enumerate(v) if indexer[index] == -1
]
raise KeyError("{} not found in axis".format(missing_labels))
raise KeyError(f"{missing_labels} not found in axis")

result._data = result._data.rename_axis(
f, axis=baxis, copy=copy, level=level
Expand Down Expand Up @@ -1257,7 +1256,7 @@ class name
if kwargs:
raise TypeError(
"rename_axis() got an unexpected keyword "
'argument "{0}"'.format(list(kwargs.keys())[0])
f'argument "{list(kwargs.keys())[0]}"'
)

inplace = validate_bool_kwarg(inplace, "inplace")
Expand Down Expand Up @@ -1461,9 +1460,7 @@ def __neg__(self):
):
arr = operator.neg(values)
else:
raise TypeError(
"Unary negative expects numeric dtype, not {}".format(values.dtype)
)
raise TypeError(f"Unary negative expects numeric dtype, not {values.dtype}")
return self.__array_wrap__(arr)

def __pos__(self):
Expand All @@ -1477,9 +1474,7 @@ def __pos__(self):
):
arr = operator.pos(values)
else:
raise TypeError(
"Unary plus expects numeric dtype, not {}".format(values.dtype)
)
raise TypeError(f"Unary plus expects numeric dtype, not {values.dtype}")
return self.__array_wrap__(arr)

def __invert__(self):
Expand All @@ -1492,10 +1487,8 @@ def __invert__(self):

def __nonzero__(self):
raise ValueError(
"The truth value of a {0} is ambiguous. "
"Use a.empty, a.bool(), a.item(), a.any() or a.all().".format(
type(self).__name__
)
f"The truth value of a {type(self).__name__} is ambiguous. "
"Use a.empty, a.bool(), a.item(), a.any() or a.all()."
)

__bool__ = __nonzero__
Expand All @@ -1519,7 +1512,7 @@ def bool(self):
elif is_scalar(v):
raise ValueError(
"bool cannot act on a non-boolean single element "
"{0}".format(type(self).__name__)
f"{type(self).__name__}"
)

self.__nonzero__()
Expand Down Expand Up @@ -1659,14 +1652,8 @@ def _check_label_or_level_ambiguity(self, key, axis: int = 0) -> None:
)

msg = (
"'{key}' is both {level_article} {level_type} level and "
"{label_article} {label_type} label, which is ambiguous."
).format(
key=key,
level_article=level_article,
level_type=level_type,
label_article=label_article,
label_type=label_type,
f"'{key}' is both {level_article} {level_type} level and "
f"{label_article} {label_type} label, which is ambiguous."
)
raise ValueError(msg)

Expand Down Expand Up @@ -1731,12 +1718,8 @@ def _get_label_or_level_values(self, key: str, axis: int = 0) -> np.ndarray:
label_axis_name = "column" if axis == 0 else "index"
raise ValueError(
(
"The {label_axis_name} label '{key}' "
"is not unique.{multi_message}"
).format(
key=key,
label_axis_name=label_axis_name,
multi_message=multi_message,
f"The {label_axis_name} label '{key}' "
f"is not unique.{multi_message}"
)
)

Expand Down Expand Up @@ -1780,8 +1763,8 @@ def _drop_labels_or_levels(self, keys, axis: int = 0):
raise ValueError(
(
"The following keys are not valid labels or "
"levels for axis {axis}: {invalid_keys}"
).format(axis=axis, invalid_keys=invalid_keys)
f"levels for axis {axis}: {invalid_keys}"
)
)

# Compute levels and labels to drop
Expand Down Expand Up @@ -1998,7 +1981,7 @@ def __setstate__(self, state):
def __repr__(self) -> str:
# string representation based upon iterating over self
# (since, by definition, `PandasContainers` are iterable)
prepr = "[%s]" % ",".join(map(pprint_thing, self))
prepr = f"[{','.join(map(pprint_thing, self))}]"
return f"{type(self).__name__}({prepr})"

def _repr_latex_(self):
Expand Down Expand Up @@ -3946,13 +3929,13 @@ def _drop_axis(self, labels, axis, level=None, errors: str = "raise"):

# GH 18561 MultiIndex.drop should raise if label is absent
if errors == "raise" and indexer.all():
raise KeyError("{} not found in axis".format(labels))
raise KeyError(f"{labels} not found in axis")
else:
indexer = ~axis.isin(labels)
# Check if label doesn't exist along axis
labels_missing = (axis.get_indexer_for(labels) == -1).any()
if errors == "raise" and labels_missing:
raise KeyError("{} not found in axis".format(labels))
raise KeyError(f"{labels} not found in axis")

slicer = [slice(None)] * self.ndim
slicer[self._get_axis_number(axis_name)] = indexer
Expand Down Expand Up @@ -4476,7 +4459,7 @@ def reindex(self, *args, **kwargs):
if kwargs:
raise TypeError(
"reindex() got an unexpected keyword "
'argument "{0}"'.format(list(kwargs.keys())[0])
f'argument "{list(kwargs.keys())[0]}"'
)

self._consolidate_inplace()
Expand Down Expand Up @@ -5997,7 +5980,7 @@ def fillna(
raise TypeError(
'"value" parameter must be a scalar, dict '
"or Series, but you passed a "
'"{0}"'.format(type(value).__name__)
f'"{type(value).__name__}"'
)

new_data = self._data.fillna(
Expand Down Expand Up @@ -6781,9 +6764,9 @@ def interpolate(
if method not in methods and not is_numeric_or_datetime:
raise ValueError(
"Index column must be numeric or datetime type when "
"using {method} method other than linear. "
f"using {method} method other than linear. "
"Try setting a numeric or datetime index column before "
"interpolating.".format(method=method)
"interpolating."
)

if isna(index).any():
Expand Down Expand Up @@ -9205,7 +9188,7 @@ def _tz_convert(ax, tz):
ax = ax.set_levels(new_level, level=level)
else:
if level not in (None, 0, ax.name):
raise ValueError("The level {0} is not valid".format(level))
raise ValueError(f"The level {level} is not valid")
ax = _tz_convert(ax, tz)

result = self._constructor(self._data, copy=copy)
Expand Down Expand Up @@ -9375,7 +9358,7 @@ def _tz_localize(ax, tz, ambiguous, nonexistent):
ax = ax.set_levels(new_level, level=level)
else:
if level not in (None, 0, ax.name):
raise ValueError("The level {0} is not valid".format(level))
raise ValueError(f"The level {level} is not valid")
ax = _tz_localize(ax, tz, ambiguous, nonexistent)

result = self._constructor(self._data, copy=copy)
Expand Down Expand Up @@ -10357,8 +10340,8 @@ def last_valid_index(self):

def _doc_parms(cls):
"""Return a tuple of the doc parms."""
axis_descr = "{%s}" % ", ".join(
"{0} ({1})".format(a, i) for i, a in enumerate(cls._AXIS_ORDERS)
axis_descr = (
f"{{{', '.join(f'{a} ({i})' for i, a in enumerate(cls._AXIS_ORDERS))}}}"
)
name = cls._constructor_sliced.__name__ if cls._AXIS_LEN > 1 else "scalar"
name2 = cls.__name__
Expand Down
18 changes: 9 additions & 9 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def cat_safe(list_of_columns: List, sep: str):
raise TypeError(
"Concatenation requires list-likes containing only "
"strings (or missing values). Offending values found in "
"column {}".format(dtype)
f"column {dtype}"
) from None
return result

Expand Down Expand Up @@ -1350,8 +1350,8 @@ def str_find(arr, sub, start=0, end=None, side="left"):
"""

if not isinstance(sub, str):
msg = "expected a string object, not {0}"
raise TypeError(msg.format(type(sub).__name__))
msg = f"expected a string object, not {type(sub).__name__}"
raise TypeError(msg)

if side == "left":
method = "find"
Expand All @@ -1370,8 +1370,8 @@ def str_find(arr, sub, start=0, end=None, side="left"):

def str_index(arr, sub, start=0, end=None, side="left"):
if not isinstance(sub, str):
msg = "expected a string object, not {0}"
raise TypeError(msg.format(type(sub).__name__))
msg = f"expected a string object, not {type(sub).__name__}"
raise TypeError(msg)

if side == "left":
method = "index"
Expand Down Expand Up @@ -1442,15 +1442,15 @@ def str_pad(arr, width, side="left", fillchar=" "):
dtype: object
"""
if not isinstance(fillchar, str):
msg = "fillchar must be a character, not {0}"
raise TypeError(msg.format(type(fillchar).__name__))
msg = f"fillchar must be a character, not {type(fillchar).__name__}"
raise TypeError(msg)

if len(fillchar) != 1:
raise TypeError("fillchar must be a character, not str")

if not is_integer(width):
msg = "width must be of integer type, not {0}"
raise TypeError(msg.format(type(width).__name__))
msg = f"width must be of integer type, not {type(width).__name__}"
raise TypeError(msg)

if side == "left":
f = lambda x: x.rjust(width, fillchar)
Expand Down