-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Convert to fstring #30094
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
Closed
Closed
Convert to fstring #30094
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1209,8 +1209,7 @@ def period_format(int64_t value, int freq, object fmt=None): | |
elif freq_group == 4000: # WK | ||
left = period_asfreq(value, freq, 6000, 0) | ||
right = period_asfreq(value, freq, 6000, 1) | ||
return '%s/%s' % (period_format(left, 6000), | ||
period_format(right, 6000)) | ||
return f"{period_format(left, 6000)}/{period_format(right, 6000)}" | ||
elif (freq_group == 5000 # BUS | ||
or freq_group == 6000): # DAY | ||
fmt = b'%Y-%m-%d' | ||
|
@@ -1227,7 +1226,7 @@ def period_format(int64_t value, int freq, object fmt=None): | |
elif freq_group == 12000: # NANOSEC | ||
fmt = b'%Y-%m-%d %H:%M:%S.%n' | ||
else: | ||
raise ValueError(f'Unknown freq: {freq}') | ||
raise ValueError(f"Unknown freq: {freq}") | ||
|
||
return _period_strftime(value, freq, fmt) | ||
|
||
|
@@ -1275,15 +1274,15 @@ cdef object _period_strftime(int64_t value, int freq, object fmt): | |
if i == 0: | ||
repl = str(quarter) | ||
elif i == 1: # %f, 2-digit year | ||
repl = f'{(year % 100):02d}' | ||
repl = f"{(year % 100):02d}" | ||
elif i == 2: | ||
repl = str(year) | ||
elif i == 3: | ||
repl = f'{(value % 1_000):03d}' | ||
repl = f"{(value % 1_000):03d}" | ||
elif i == 4: | ||
repl = f'{(value % 1_000_000):06d}' | ||
repl = f"{(value % 1_000_000):06d}" | ||
elif i == 5: | ||
repl = f'{(value % 1_000_000_000):09d}' | ||
repl = f"{(value % 1_000_000_000):09d}" | ||
|
||
result = result.replace(str_extra_fmts[i], repl) | ||
|
||
|
@@ -1391,7 +1390,7 @@ def get_period_field_arr(int code, int64_t[:] arr, int freq): | |
|
||
func = _get_accessor_func(code) | ||
if func is NULL: | ||
raise ValueError(f'Unrecognized period code: {code}') | ||
raise ValueError(f"Unrecognized period code: {code}") | ||
|
||
sz = len(arr) | ||
out = np.empty(sz, dtype=np.int64) | ||
|
@@ -1455,7 +1454,7 @@ def extract_ordinals(ndarray[object] values, freq): | |
ordinals[i] = p.ordinal | ||
|
||
if p.freqstr != freqstr: | ||
msg = DIFFERENT_FREQ.format(cls="PeriodIndex", | ||
msg = DIFFERENT_FREQ(cls="PeriodIndex", | ||
own_freq=freqstr, | ||
other_freq=p.freqstr) | ||
raise IncompatibleFrequency(msg) | ||
|
@@ -1549,8 +1548,9 @@ cdef int64_t[:] localize_dt64arr_to_period(int64_t[:] stamps, | |
return result | ||
|
||
|
||
DIFFERENT_FREQ = ("Input has different freq={other_freq} " | ||
"from {cls}(freq={own_freq})") | ||
|
||
DIFFERENT_FREQ = lambda other_freq, cls, own_freq: f"Input has different freq={other_freq} " \ | ||
f"from {cls}(freq={own_freq})" | ||
|
||
|
||
class IncompatibleFrequency(ValueError): | ||
|
@@ -1578,8 +1578,8 @@ cdef class _Period: | |
freq = to_offset(freq) | ||
|
||
if freq.n <= 0: | ||
raise ValueError(f'Frequency must be positive, because it ' | ||
f'represents span: {freq.freqstr}') | ||
raise ValueError(f"Frequency must be positive, because it " | ||
f"represents span: {freq.freqstr}") | ||
|
||
return freq | ||
|
||
|
@@ -1598,7 +1598,7 @@ cdef class _Period: | |
def __richcmp__(self, other, op): | ||
if is_period_object(other): | ||
if other.freq != self.freq: | ||
msg = DIFFERENT_FREQ.format(cls=type(self).__name__, | ||
msg = DIFFERENT_FREQ(cls=type(self).__name__, | ||
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. You need an f string? 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. same deal as abowe in frequencies.pyx file. DIFFERENT_FREQ is now a lambda function |
||
own_freq=self.freqstr, | ||
other_freq=other.freqstr) | ||
raise IncompatibleFrequency(msg) | ||
|
@@ -1613,8 +1613,8 @@ cdef class _Period: | |
return NotImplemented | ||
elif op == Py_NE: | ||
return NotImplemented | ||
raise TypeError(f'Cannot compare type {type(self).__name__} ' | ||
f'with type {type(other).__name__}') | ||
raise TypeError(f"Cannot compare type {type(self).__name__} " | ||
f"with type {type(other).__name__}") | ||
|
||
def __hash__(self): | ||
return hash((self.ordinal, self.freqstr)) | ||
|
@@ -1632,15 +1632,15 @@ cdef class _Period: | |
if nanos % offset_nanos == 0: | ||
ordinal = self.ordinal + (nanos // offset_nanos) | ||
return Period(ordinal=ordinal, freq=self.freq) | ||
raise IncompatibleFrequency(f'Input cannot be converted to ' | ||
f'Period(freq={self.freqstr})') | ||
raise IncompatibleFrequency(f"Input cannot be converted to " | ||
f"Period(freq={self.freqstr})") | ||
elif util.is_offset_object(other): | ||
freqstr = other.rule_code | ||
base = get_base_alias(freqstr) | ||
if base == self.freq.rule_code: | ||
ordinal = self.ordinal + other.n | ||
return Period(ordinal=ordinal, freq=self.freq) | ||
msg = DIFFERENT_FREQ.format(cls=type(self).__name__, | ||
msg = DIFFERENT_FREQ(cls=type(self).__name__, | ||
own_freq=self.freqstr, | ||
other_freq=other.freqstr) | ||
raise IncompatibleFrequency(msg) | ||
|
@@ -1684,7 +1684,7 @@ cdef class _Period: | |
return Period(ordinal=ordinal, freq=self.freq) | ||
elif is_period_object(other): | ||
if other.freq != self.freq: | ||
msg = DIFFERENT_FREQ.format(cls=type(self).__name__, | ||
msg = DIFFERENT_FREQ(cls=type(self).__name__, | ||
own_freq=self.freqstr, | ||
other_freq=other.freqstr) | ||
raise IncompatibleFrequency(msg) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This looks like a typo?
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.
Hi,
In order to converting str.format() to f string I changed INVALID_FREQ_ERR_MSG definition from predefined string to lambda function. Now error message is defined in one place and is callable without .format().
Current definition:
INVALID_FREQ_ERR_MSG = lambda arg: f"Invalid frequency {arg}"
So now message is called as a function:
INVALID_FREQ_ERR_MSG(arg_str)