Skip to content

CLN:F-string formatting #29554

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 32 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4c3c6bd
Finished groupby.pyx
Nov 10, 2019
e64e327
Finished hashing.pyx
Nov 10, 2019
ba7c6be
Finished index.pyx
Nov 10, 2019
590c6a0
Finished internals.pyx
Nov 10, 2019
89bdecc
Finished interval.pyx
Nov 10, 2019
dd3e437
Finished lib.pyx
Nov 10, 2019
6fc5aca
Finished ops.pyx
Nov 10, 2019
3d193fa
Finished parsers.pyx
Nov 10, 2019
b09066b
Finished reduction.pyx
Nov 10, 2019
da6dcf0
Finished sparse.pyx
Nov 10, 2019
2667b09
Finished testing.pyx
Nov 10, 2019
5ff5e40
Finished tslib.pyx
Nov 10, 2019
f6db195
Finished window.pyx
Nov 10, 2019
5fbd795
Finished fixing errors in previous PR
Nov 10, 2019
3290a4a
Merge remote-tracking branch 'upstream/master' into F-string-pyx
Nov 10, 2019
3534311
added the \!r to the strings
Nov 10, 2019
88e7943
Merge remote-tracking branch 'upstream/master' into F-string-pyx
Nov 11, 2019
56d40ac
parsers.pyx
Nov 11, 2019
aef8971
Finished the pyx left overs
Nov 11, 2019
3811395
Update parsers.pyx
ShaharNaveh Nov 11, 2019
515e20e
Update parsers.pyx
ShaharNaveh Nov 11, 2019
10dea5a
Putted the join out of the f-string
Nov 11, 2019
dd53c05
res date format
Nov 11, 2019
10ed985
Reverted the comment in parsers.pyx
Nov 11, 2019
1375f9f
Trying with no percision on intergenrs
Nov 11, 2019
eec10f0
Update tslib.pyx
ShaharNaveh Nov 12, 2019
6820063
Update parsers.pyx
ShaharNaveh Nov 12, 2019
98d440d
Squshing commits
Nov 12, 2019
3a11fd1
git issues
Nov 12, 2019
67d9a17
Merge remote-tracking branch 'upstream/master' into F-string-pyx
Nov 12, 2019
28091a8
Lint errors
Nov 12, 2019
082c5ed
Finnal commit, number one
Nov 12, 2019
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
77 changes: 33 additions & 44 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,7 @@ cdef class TextReader:

if not isinstance(quote_char, (str, bytes)) and quote_char is not None:
dtype = type(quote_char).__name__
raise TypeError('"quotechar" must be string, '
'not {dtype}'.format(dtype=dtype))
raise TypeError(f'"quotechar" must be string, not {dtype}')

if quote_char is None or quote_char == '':
if quoting != QUOTE_NONE:
Expand Down Expand Up @@ -684,9 +683,9 @@ cdef class TextReader:
if ptr == NULL:
if not os.path.exists(source):
raise FileNotFoundError(
ENOENT,
'File {source} does not exist'.format(source=source),
source)
ENOENT,
f'File {source} does not exist',
source)
raise IOError('Initializing from file failed')

self.parser.source = ptr
Expand Down Expand Up @@ -741,8 +740,8 @@ cdef class TextReader:
self.parser.lines < hr):
msg = self.orig_header
if isinstance(msg, list):
msg = "[%s], len of %d," % (
','.join(str(m) for m in msg), len(msg))
joined = ','.join(str(m) for m in msg)
msg = f"[{joined}], len of {len(msg)},"
raise ParserError(
f'Passed header={msg} but only '
f'{self.parser.lines} lines in file')
Expand All @@ -768,10 +767,9 @@ cdef class TextReader:

if name == '':
if self.has_mi_columns:
name = ('Unnamed: {i}_level_{lvl}'
.format(i=i, lvl=level))
name = f'Unnamed: {i}_level_{level}'
else:
name = 'Unnamed: {i}'.format(i=i)
name = f'Unnamed: {i}'
unnamed_count += 1

count = counts.get(name, 0)
Expand Down Expand Up @@ -846,9 +844,9 @@ cdef class TextReader:
passed_count = len(header[0])

# if passed_count > field_count:
# raise ParserError('Column names have %d fields, '
# 'data has %d fields'
# % (passed_count, field_count))
# raise ParserError('Column names have %d fields, '
# 'data has %d fields'
# % (passed_count, field_count))

if (self.has_usecols and self.allow_leading_cols and
not callable(self.usecols)):
Expand Down Expand Up @@ -990,7 +988,7 @@ cdef class TextReader:
cdef _end_clock(self, what):
if self.verbose:
elapsed = time.time() - self.clocks.pop(-1)
print('%s took: %.2f ms' % (what, elapsed * 1000))
print(f'{what} took: {elapsed * 1000:.2f} ms')

def set_noconvert(self, i):
self.noconvert.add(i)
Expand Down Expand Up @@ -1028,11 +1026,9 @@ cdef class TextReader:
(num_cols >= self.parser.line_fields[i]) * num_cols

if self.table_width - self.leading_cols > num_cols:
raise ParserError(
"Too many columns specified: expected {expected} and "
"found {found}"
.format(expected=self.table_width - self.leading_cols,
found=num_cols))
raise ParserError(f"Too many columns specified: expected "
f"{self.table_width - self.leading_cols} "
f"and found {num_cols}")

results = {}
nused = 0
Expand Down Expand Up @@ -1075,9 +1071,9 @@ cdef class TextReader:

if conv:
if col_dtype is not None:
warnings.warn(("Both a converter and dtype were specified "
"for column {0} - only the converter will "
"be used").format(name), ParserWarning,
warnings.warn((f"Both a converter and dtype were specified "
f"for column {name} - only the converter will "
f"be used"), ParserWarning,
stacklevel=5)
results[i] = _apply_converter(conv, self.parser, i, start, end,
self.c_encoding)
Expand Down Expand Up @@ -1118,7 +1114,7 @@ cdef class TextReader:
col_res = _maybe_upcast(col_res)

if col_res is None:
raise ParserError('Unable to parse column {i}'.format(i=i))
raise ParserError(f'Unable to parse column {i}')

results[i] = col_res

Expand Down Expand Up @@ -1178,12 +1174,9 @@ cdef class TextReader:
col_res = col_res.astype(col_dtype)
if (col_res != col_res_orig).any():
raise ValueError(
"cannot safely convert passed user dtype of "
"{col_dtype} for {col_res} dtyped data in "
"column {column}".format(
col_dtype=col_dtype,
col_res=col_res_orig.dtype.name,
column=i))
f"cannot safely convert passed user dtype of "
f"{col_dtype} for {col_res_orig.dtype.name} dtyped data in "
f"column {i}")

return col_res, na_count

Expand Down Expand Up @@ -1216,9 +1209,9 @@ cdef class TextReader:
dtype=dtype)
except NotImplementedError:
raise NotImplementedError(
"Extension Array: {ea} must implement "
"_from_sequence_of_strings in order "
"to be used in parser methods".format(ea=array_type))
f"Extension Array: {array_type} must implement "
f"_from_sequence_of_strings in order "
f"to be used in parser methods")

return result, na_count

Expand All @@ -1228,8 +1221,7 @@ cdef class TextReader:
end, na_filter, na_hashset)
if user_dtype and na_count is not None:
if na_count > 0:
raise ValueError("Integer column has NA values in "
"column {column}".format(column=i))
raise ValueError(f"Integer column has NA values in column {i}")
except OverflowError:
result = _try_uint64(self.parser, i, start, end,
na_filter, na_hashset)
Expand All @@ -1253,8 +1245,7 @@ cdef class TextReader:
self.true_set, self.false_set)
if user_dtype and na_count is not None:
if na_count > 0:
raise ValueError("Bool column has NA values in "
"column {column}".format(column=i))
raise ValueError(f"Bool column has NA values in column {i}")
return result, na_count

elif dtype.kind == 'S':
Expand All @@ -1270,8 +1261,7 @@ cdef class TextReader:
elif dtype.kind == 'U':
width = dtype.itemsize
if width > 0:
raise TypeError("the dtype {dtype} is not "
"supported for parsing".format(dtype=dtype))
raise TypeError(f"the dtype {dtype} is not supported for parsing")

# unicode variable width
return self._string_convert(i, start, end, na_filter,
Expand All @@ -1280,12 +1270,11 @@ cdef class TextReader:
return self._string_convert(i, start, end, na_filter,
na_hashset)
elif is_datetime64_dtype(dtype):
raise TypeError("the dtype {dtype} is not supported "
"for parsing, pass this column "
"using parse_dates instead".format(dtype=dtype))
raise TypeError(f"the dtype {dtype} is not supported "
f"for parsing, pass this column "
f"using parse_dates instead")
else:
raise TypeError("the dtype {dtype} is not "
"supported for parsing".format(dtype=dtype))
raise TypeError(f"the dtype {dtype} is not supported for parsing")

cdef _string_convert(self, Py_ssize_t i, int64_t start, int64_t end,
bint na_filter, kh_str_starts_t *na_hashset):
Expand Down Expand Up @@ -2132,7 +2121,7 @@ cdef raise_parser_error(object base, parser_t *parser):
Py_XDECREF(type)
raise old_exc

message = '{base}. C error: '.format(base=base)
message = f'{base}. C error: '
if parser.error_msg != NULL:
message += parser.error_msg.decode('utf-8')
else:
Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/testing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ cpdef assert_almost_equal(a, b,
# case for zero
if abs(fa) < 1e-5:
if not decimal_almost_equal(fa, fb, decimal):
assert False, ('(very low values) expected %.5f but '
'got %.5f, with decimal %d' % (fb, fa, decimal))
assert False, (f'(very low values) expected {fb:.5f} '
f'but got {fa:.5f}, with decimal {decimal}')
else:
if not decimal_almost_equal(1, fb / fa, decimal):
assert False, ('expected %.5f but got %.5f, '
'with decimal %d' % (fb, fa, decimal))
assert False, (f'expected {fb:.5f} but got {fa:.5f}, '
f'with decimal {decimal}')
return True

raise AssertionError(f"{a} != {b}")
14 changes: 5 additions & 9 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,16 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None,
elif basic_format:

dt64_to_dtstruct(val, &dts)
res = '%d-%.2d-%.2d %.2d:%.2d:%.2d' % (dts.year,
dts.month,
dts.day,
dts.hour,
dts.min,
dts.sec)
res = (f'{dts.year}-{dts.month}-{dts.day} '
f'{dts.hour}-{dts.min}-{dts.sec}')

if show_ns:
ns = dts.ps // 1000
res += '.%.9d' % (ns + 1000 * dts.us)
res += f'{ns + 1000 * dts.us}'
elif show_us:
res += '.%.6d' % dts.us
res += f'{dts.us}'
elif show_ms:
res += '.%.3d' % (dts.us /1000)
res += f'{dts.us /1000}'

result[i] = res

Expand Down