Skip to content

Commit

Permalink
CLN: modernize string formatting (pandas-dev#22553)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and victor committed Sep 30, 2018
1 parent d2d7b4a commit 48d97c7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
5 changes: 3 additions & 2 deletions pandas/_libs/algos_common_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ cpdef map_indices_{{name}}(ndarray[{{c_type}}] index):

Better to do this with Cython because of the enormous speed boost.
"""
cdef Py_ssize_t i, length
cdef dict result = {}
cdef:
Py_ssize_t i, length
dict result = {}

length = len(index)

Expand Down
41 changes: 22 additions & 19 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ cdef extern from "Python.h":

import numpy as np
cimport numpy as cnp
from numpy cimport ndarray, uint8_t, uint64_t, int64_t
from numpy cimport ndarray, uint8_t, uint64_t, int64_t, float64_t
cnp.import_array()

from util cimport UINT64_MAX, INT64_MAX, INT64_MIN
Expand Down Expand Up @@ -694,7 +694,7 @@ cdef class TextReader:
if ptr == NULL:
if not os.path.exists(source):
raise compat.FileNotFoundError(
'File %s does not exist' % source)
'File {source} does not exist'.format(source=source))
raise IOError('Initializing from file failed')

self.parser.source = ptr
Expand Down Expand Up @@ -772,9 +772,10 @@ cdef class TextReader:

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

count = counts.get(name, 0)
Expand Down Expand Up @@ -849,8 +850,8 @@ cdef class TextReader:
# 'data has %d fields'
# % (passed_count, field_count))

if self.has_usecols and self.allow_leading_cols and \
not callable(self.usecols):
if (self.has_usecols and self.allow_leading_cols and
not callable(self.usecols)):
nuse = len(self.usecols)
if nuse == passed_count:
self.leading_cols = 0
Expand Down Expand Up @@ -1027,17 +1028,19 @@ cdef class TextReader:

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

results = {}
nused = 0
for i in range(self.table_width):
if i < self.leading_cols:
# Pass through leading columns always
name = i
elif self.usecols and not callable(self.usecols) and \
nused == len(self.usecols):
elif (self.usecols and not callable(self.usecols) and
nused == len(self.usecols)):
# Once we've gathered all requested columns, stop. GH5766
break
else:
Expand Down Expand Up @@ -1103,7 +1106,7 @@ cdef class TextReader:
col_res = _maybe_upcast(col_res)

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

results[i] = col_res

Expand Down Expand Up @@ -1222,8 +1225,8 @@ cdef class TextReader:
elif dtype.kind == 'U':
width = dtype.itemsize
if width > 0:
raise TypeError("the dtype %s is not "
"supported for parsing" % dtype)
raise TypeError("the dtype {dtype} is not "
"supported for parsing".format(dtype=dtype))

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

cdef _string_convert(self, Py_ssize_t i, int64_t start, int64_t end,
bint na_filter, kh_str_t *na_hashset):
Expand Down Expand Up @@ -2058,7 +2061,7 @@ cdef kh_float64_t* kset_float64_from_list(values) except NULL:
khiter_t k
kh_float64_t *table
int ret = 0
cnp.float64_t val
float64_t val
object value

table = kh_init_float64()
Expand Down Expand Up @@ -2101,7 +2104,7 @@ cdef raise_parser_error(object base, parser_t *parser):
Py_XDECREF(type)
raise old_exc

message = '%s. C error: ' % base
message = '{base}. C error: '.format(base=base)
if parser.error_msg != NULL:
if PY3:
message += parser.error_msg.decode('utf-8')
Expand Down

0 comments on commit 48d97c7

Please sign in to comment.