-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
format replaced with f-strings #29701
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,9 +173,7 @@ def _set_grouper(self, obj: FrameOrSeries, sort: bool = False): | |
ax = self._grouper.take(obj.index) | ||
else: | ||
if key not in obj._info_axis: | ||
raise KeyError( | ||
"The grouper name {key} is not found".format(key=key) | ||
) | ||
raise KeyError(f"The grouper name {key} is not found") | ||
ax = Index(obj[key], name=key) | ||
|
||
else: | ||
|
@@ -191,9 +189,7 @@ def _set_grouper(self, obj: FrameOrSeries, sort: bool = False): | |
|
||
else: | ||
if level not in (0, ax.name): | ||
raise ValueError( | ||
"The level {level} is not valid".format(level=level) | ||
) | ||
raise ValueError(f"The level {level} is not valid") | ||
|
||
# possibly sort | ||
if (self.sort or sort) and not ax.is_monotonic: | ||
|
@@ -212,13 +208,13 @@ def groups(self): | |
|
||
def __repr__(self) -> str: | ||
attrs_list = ( | ||
"{name}={val!r}".format(name=attr_name, val=getattr(self, attr_name)) | ||
f"{attr_name}={getattr(self, attr_name)!r}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you do this in two steps so as to not have the getattr inside the fstring There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jbrockmendel Not really sure how to do this in two steps since attr_name is generated from a list comprehension. Do I just drop list comprehension usage in that case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this usage seems ok to me |
||
for attr_name in self._attributes | ||
if getattr(self, attr_name) is not None | ||
) | ||
attrs = ", ".join(attrs_list) | ||
cls_name = self.__class__.__name__ | ||
return "{cls}({attrs})".format(cls=cls_name, attrs=attrs) | ||
return f"{cls_name}({attrs})" | ||
|
||
|
||
class Grouping: | ||
|
@@ -280,9 +276,7 @@ def __init__( | |
if level is not None: | ||
if not isinstance(level, int): | ||
if level not in index.names: | ||
raise AssertionError( | ||
"Level {level} not in index".format(level=level) | ||
) | ||
raise AssertionError(f"Level {level} not in index") | ||
level = index.names.index(level) | ||
|
||
if self.name is None: | ||
|
@@ -350,17 +344,16 @@ def __init__( | |
): | ||
if getattr(self.grouper, "ndim", 1) != 1: | ||
t = self.name or str(type(self.grouper)) | ||
raise ValueError("Grouper for '{t}' not 1-dimensional".format(t=t)) | ||
raise ValueError(f"Grouper for '{t}' not 1-dimensional") | ||
self.grouper = self.index.map(self.grouper) | ||
if not ( | ||
hasattr(self.grouper, "__len__") | ||
and len(self.grouper) == len(self.index) | ||
): | ||
grper = pprint_thing(self.grouper) | ||
errmsg = ( | ||
"Grouper result violates len(labels) == " | ||
"len(data)\nresult: {grper}".format( | ||
grper=pprint_thing(self.grouper) | ||
) | ||
f"len(data)\nresult: {grper}" | ||
) | ||
self.grouper = None # Try for sanity | ||
raise AssertionError(errmsg) | ||
|
@@ -375,7 +368,7 @@ def __init__( | |
self.grouper = self.grouper.astype("timedelta64[ns]") | ||
|
||
def __repr__(self) -> str: | ||
return "Grouping({name})".format(name=self.name) | ||
return f"Grouping({self.name})" | ||
|
||
def __iter__(self): | ||
return iter(self.indices) | ||
|
@@ -500,11 +493,7 @@ def get_grouper( | |
|
||
if isinstance(level, str): | ||
if obj.index.name != level: | ||
raise ValueError( | ||
"level name {level} is not the name of the index".format( | ||
level=level | ||
) | ||
) | ||
raise ValueError(f"level name {level} is not the name of the index") | ||
elif level > 0 or level < -1: | ||
raise ValueError("level > 0 or level < -1 only valid with MultiIndex") | ||
|
||
|
@@ -636,12 +625,8 @@ def is_in_obj(gpr) -> bool: | |
|
||
if is_categorical_dtype(gpr) and len(gpr) != obj.shape[axis]: | ||
raise ValueError( | ||
( | ||
"Length of grouper ({len_gpr}) and axis ({len_axis})" | ||
" must be same length".format( | ||
len_gpr=len(gpr), len_axis=obj.shape[axis] | ||
) | ||
) | ||
f"Length of grouper ({len(gpr)}) and axis ({obj.shape[axis]})" | ||
" must be same length" | ||
) | ||
|
||
# create the Grouping | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@datapythonista do we have a preferred usage for cases where this line needs the "f" and the preceeding does not? I could imagine we might want to put the "f" on all of them anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is correct - should just keep it on lines where needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @WillAyd, my preference is to just have the
f
were it's needed. Even in strings splitted in different lines.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As described in pep-498-concatenating-strings, regular strings are concatenated at compile time, and f-strings are concatenated at run time.
I think what we should not concatenate regular strings with f-strings because this could lead to unexpected results.