-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CLN: String formatting % -> f-strings #29518
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 6 commits
1bd9a95
f773a0b
62c0c74
f97f355
4af85bb
21efbba
3c80e0a
1c3a1b1
6f7ebe0
0478bb1
a2b6a95
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 |
---|---|---|
|
@@ -265,7 +265,7 @@ def __new__( | |
name=None, | ||
fastpath=None, | ||
tupleize_cols=True, | ||
**kwargs | ||
**kwargs, | ||
) -> "Index": | ||
|
||
from .range import RangeIndex | ||
|
@@ -960,14 +960,13 @@ def __repr__(self): | |
data = self._format_data() | ||
attrs = self._format_attrs() | ||
space = self._format_space() | ||
|
||
prepr = (",%s" % space).join("%s=%s" % (k, v) for k, v in attrs) | ||
prepr = f",{space}".join(f"{k}={v}" for k, v in attrs) | ||
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. let's try to minimize what we execute in f-space (better name for this?) |
||
|
||
# no data provided, just attributes | ||
if data is None: | ||
data = "" | ||
|
||
res = "%s(%s%s)" % (klass, data, prepr) | ||
res = f"{klass}({data}{prepr})" | ||
|
||
return res | ||
|
||
|
@@ -1121,13 +1120,13 @@ def _summary(self, name=None): | |
tail = self[-1] | ||
if hasattr(tail, "format") and not isinstance(tail, str): | ||
tail = tail.format() | ||
index_summary = ", %s to %s" % (pprint_thing(head), pprint_thing(tail)) | ||
index_summary = f", {pprint_thing(head)} to {pprint_thing(tail)}" | ||
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. pprint_thing may be unnecessary here now that we're py3-only. can you take a look |
||
else: | ||
index_summary = "" | ||
|
||
if name is None: | ||
name = type(self).__name__ | ||
return "%s: %s entries%s" % (name, len(self), index_summary) | ||
return f"{name}: {len(self)} entries{index_summary}" | ||
|
||
def summary(self, name=None): | ||
""" | ||
|
@@ -1301,7 +1300,7 @@ def _set_names(self, values, level=None): | |
if not is_list_like(values): | ||
raise ValueError("Names must be a list-like") | ||
if len(values) != 1: | ||
raise ValueError("Length of new names must be 1, got %d" % len(values)) | ||
raise ValueError(f"Length of new names must be 1, got {len(values)}") | ||
|
||
# GH 20527 | ||
# All items in 'name' need to be hashable: | ||
|
@@ -1472,8 +1471,8 @@ def _validate_index_level(self, level): | |
if isinstance(level, int): | ||
if level < 0 and level != -1: | ||
raise IndexError( | ||
"Too many levels: Index has only 1 level," | ||
" %d is not a valid level number" % (level,) | ||
f"Too many levels: Index has only 1 level," | ||
f" {level} is not a valid level number" | ||
) | ||
elif level > 0: | ||
raise IndexError( | ||
|
@@ -4561,7 +4560,7 @@ def shift(self, periods=1, freq=None): | |
'2012-03-01'], | ||
dtype='datetime64[ns]', freq='MS') | ||
""" | ||
raise NotImplementedError("Not supported for type %s" % type(self).__name__) | ||
raise NotImplementedError(f"Not supported for type {type(self).__name__}") | ||
|
||
def argsort(self, *args, **kwargs): | ||
""" | ||
|
@@ -5068,8 +5067,8 @@ def get_slice_bound(self, label, side, kind): | |
|
||
if side not in ("left", "right"): | ||
raise ValueError( | ||
"Invalid value for side kwarg," | ||
" must be either 'left' or 'right': %s" % (side,) | ||
f"Invalid value for side kwarg, must be either" | ||
f" 'left' or 'right': {side}" | ||
) | ||
|
||
original_label = label | ||
|
@@ -5623,7 +5622,7 @@ def _trim_front(strings): | |
|
||
def _validate_join_method(method): | ||
if method not in ["left", "right", "inner", "outer"]: | ||
raise ValueError("do not recognize join method %s" % method) | ||
raise ValueError(f"do not recognize join method {method}") | ||
|
||
|
||
def default_index(n): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -496,7 +496,7 @@ def _format_attrs(self): | |
if attrib == "freq": | ||
freq = self.freqstr | ||
if freq is not None: | ||
freq = "'%s'" % freq | ||
freq = f"'{freq}'" | ||
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. do the single quotes become unnecessary with a 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. Assuming you mean {freq!r} here - updated. Agree doesn't seem to be much consistency |
||
attrs.append(("freq", freq)) | ||
return attrs | ||
|
||
|
@@ -686,17 +686,13 @@ def _summary(self, name=None): | |
""" | ||
formatter = self._formatter_func | ||
if len(self) > 0: | ||
index_summary = ", %s to %s" % (formatter(self[0]), formatter(self[-1])) | ||
index_summary = f", {formatter(self[0])} to {formatter(self[-1])}" | ||
else: | ||
index_summary = "" | ||
|
||
if name is None: | ||
name = type(self).__name__ | ||
result = "%s: %s entries%s" % ( | ||
printing.pprint_thing(name), | ||
len(self), | ||
index_summary, | ||
) | ||
result = f"{printing.pprint_thing(name)}: {len(self)} entries{index_summary}" | ||
alimcmaster1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if self.freq: | ||
result += "\nFreq: %s" % self.freqstr | ||
|
||
|
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.
is this a
black
version thing?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.
Weirdly using 19.3b0 locally seems to add this for me - I've reverted
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.
Black code check in CI fail without this - i've added this back in
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.
Personally, I find this quite odd that you have to add a comma at the end.
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.
Related to this - #29607