Skip to content

Commit

Permalink
repr() (pandas-dev#29950)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaharNaveh authored and proost committed Dec 19, 2019
1 parent 7df9202 commit 7b9d1a7
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 44 deletions.
7 changes: 2 additions & 5 deletions pandas/core/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _resolve_name(self):
if self.side == "left":
# Note: The behavior of __new__ ensures that self.name is a str here
if self.name not in self.env.queryables:
raise NameError("name {name!r} is not defined".format(name=self.name))
raise NameError(f"name {repr(self.name)} is not defined")
return self.name

# resolve the rhs (and allow it to be None)
Expand Down Expand Up @@ -431,10 +431,7 @@ def visit_Subscript(self, node, **kwargs):
try:
return self.const_type(value[slobj], self.env)
except TypeError:
raise ValueError(
"cannot subscript {value!r} with "
"{slobj!r}".format(value=value, slobj=slobj)
)
raise ValueError(f"cannot subscript {repr(value)} with {repr(slobj)}")

def visit_Attribute(self, node, **kwargs):
attr = node.attr
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1861,8 +1861,10 @@ def _validate_date_like_dtype(dtype) -> None:
except ValueError as e:
raise TypeError("{error}".format(error=e))
if typ != "generic" and typ != "ns":
msg = "{name!r} is too specific of a frequency, try passing {type!r}"
raise ValueError(msg.format(name=dtype.name, type=dtype.type.__name__))
raise ValueError(
f"{repr(dtype.name)} is too specific of a frequency, "
f"try passing {repr(dtype.type.__name__)}"
)


def pandas_dtype(dtype):
Expand Down
17 changes: 8 additions & 9 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ def _from_values_or_dtype(
if dtype == "category":
dtype = CategoricalDtype(categories, ordered)
else:
msg = "Unknown dtype {dtype!r}"
raise ValueError(msg.format(dtype=dtype))
raise ValueError(f"Unknown dtype {repr(dtype)}")
elif categories is not None or ordered is not None:
raise ValueError(
"Cannot specify `categories` or `ordered` together with `dtype`."
Expand Down Expand Up @@ -512,8 +511,9 @@ def validate_categories(categories, fastpath: bool = False):
from pandas.core.indexes.base import Index

if not fastpath and not is_list_like(categories):
msg = "Parameter 'categories' must be list-like, was {!r}"
raise TypeError(msg.format(categories))
raise TypeError(
f"Parameter 'categories' must be list-like, was {repr(categories)}"
)
elif not isinstance(categories, ABCIndexClass):
categories = Index(categories, tupleize_cols=False)

Expand Down Expand Up @@ -549,11 +549,10 @@ def update_dtype(
# dtype='category' should not change anything
return self
elif not self.is_dtype(dtype):
msg = (
"a CategoricalDtype must be passed to perform an update, "
"got {dtype!r}"
).format(dtype=dtype)
raise ValueError(msg)
raise ValueError(
f"a CategoricalDtype must be passed to perform an update, "
f"got {repr(dtype)}"
)
else:
# from here on, dtype is a CategoricalDtype
dtype = cast(CategoricalDtype, dtype)
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8123,10 +8123,9 @@ def isin(self, values):
else:
if not is_list_like(values):
raise TypeError(
"only list-like or dict-like objects are "
"allowed to be passed to DataFrame.isin(), "
"you passed a "
"{0!r}".format(type(values).__name__)
f"only list-like or dict-like objects are allowed "
f"to be passed to DataFrame.isin(), "
f"you passed a {repr(type(values).__name__)}"
)
return DataFrame(
algorithms.isin(self.values.ravel(), values).reshape(self.shape),
Expand Down
17 changes: 7 additions & 10 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1864,8 +1864,8 @@ def _drop_labels_or_levels(self, keys, axis=0):

def __hash__(self):
raise TypeError(
"{0!r} objects are mutable, thus they cannot be"
" hashed".format(type(self).__name__)
f"{repr(type(self).__name__)} objects are mutable, "
f"thus they cannot be hashed"
)

def __iter__(self):
Expand Down Expand Up @@ -6567,11 +6567,9 @@ def replace(
or is_dict_like(regex)
):
raise TypeError(
"'regex' must be a string or a compiled "
"regular expression or a list or dict of "
"strings or regular expressions, you "
"passed a"
" {0!r}".format(type(regex).__name__)
f"'regex' must be a string or a compiled regular expression "
f"or a list or dict of strings or regular expressions, "
f"you passed a {repr(type(regex).__name__)}"
)
return self.replace(
regex, value, inplace=inplace, limit=limit, regex=True
Expand All @@ -6597,10 +6595,9 @@ def replace(
to_replace=to_replace, value=value, inplace=inplace, regex=regex
)
else:
msg = ('Invalid "to_replace" type: ' "{0!r}").format(
type(to_replace).__name__
raise TypeError(
f'Invalid "to_replace" type: {repr(type(to_replace).__name__)}'
)
raise TypeError(msg) # pragma: no cover

if inplace:
self._update_inplace(new_data)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def groups(self):

def __repr__(self) -> str:
attrs_list = (
f"{attr_name}={getattr(self, attr_name)!r}"
f"{attr_name}={repr(getattr(self, attr_name))}"
for attr_name in self._attributes
if getattr(self, attr_name) is not None
)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def _format_attrs(self):
if attrib == "freq":
freq = self.freqstr
if freq is not None:
freq = f"{freq!r}"
freq = repr(freq)
attrs.append(("freq", freq))
return attrs

Expand Down
8 changes: 2 additions & 6 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ def _get_next_label(label):
elif is_float_dtype(dtype):
return np.nextafter(label, np.infty)
else:
raise TypeError(
"cannot determine next label for type {typ!r}".format(typ=type(label))
)
raise TypeError(f"cannot determine next label for type {repr(type(label))}")


def _get_prev_label(label):
Expand All @@ -99,9 +97,7 @@ def _get_prev_label(label):
elif is_float_dtype(dtype):
return np.nextafter(label, -np.infty)
else:
raise TypeError(
"cannot determine next label for type {typ!r}".format(typ=type(label))
)
raise TypeError(f"cannot determine next label for type {repr(type(label))}")


def _get_interval_closed_bounds(interval):
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/internals/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ def __init__(self, block, shape, indexers=None):
self.shape = shape

def __repr__(self) -> str:
return "{name}({block!r}, {indexers})".format(
name=type(self).__name__, block=self.block, indexers=self.indexers
)
return f"{type(self).__name__}({repr(self.block)}, {self.indexers})"

@cache_readonly
def needs_filling(self):
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1905,9 +1905,7 @@ def _compare_or_regex_search(a, b, regex=False):
type_names[1] = "ndarray(dtype={dtype})".format(dtype=b.dtype)

raise TypeError(
"Cannot compare types {a!r} and {b!r}".format(
a=type_names[0], b=type_names[1]
)
f"Cannot compare types {repr(type_names[0])} and {repr(type_names[1])}"
)
return result

Expand Down

0 comments on commit 7b9d1a7

Please sign in to comment.