Skip to content

CLN: str.format -> f-strings for io/sas #30409

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 4 commits into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 13 additions & 14 deletions pandas/io/sas/sas7bdat.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def _get_properties(self):
if buf in const.encoding_names:
self.file_encoding = const.encoding_names[buf]
else:
self.file_encoding = "unknown (code={name!s})".format(name=buf)
self.file_encoding = f"unknown (code={buf})"

# Get platform information
buf = self._read_bytes(const.platform_offset, const.platform_length)
Expand Down Expand Up @@ -293,8 +293,8 @@ def _read_bytes(self, offset, length):
buf = self._path_or_buf.read(length)
if len(buf) < length:
self.close()
msg = "Unable to read {:d} bytes from file position {:d}."
raise ValueError(msg.format(length, offset))
msg = f"Unable to read {length:d} bytes from file position {offset:d}."
raise ValueError(msg)
return buf
else:
if offset + length > len(self._cached_page):
Expand Down Expand Up @@ -457,12 +457,9 @@ def _process_columnsize_subheader(self, offset, length):
self.column_count = self._read_int(offset, int_len)
if self.col_count_p1 + self.col_count_p2 != self.column_count:
print(
"Warning: column count mismatch ({p1} + {p2} != "
"{column_count})\n".format(
p1=self.col_count_p1,
p2=self.col_count_p2,
column_count=self.column_count,
)
f"Warning: column count mismatch ({self.col_count_p1} + "
f"{self.col_count_p2} != "
f"{self.column_count})\n"
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for follow-up, this probably should use warnings rather than print


# Unknown purpose
Expand Down Expand Up @@ -672,8 +669,12 @@ def _read_next_page(self):
return True
elif len(self._cached_page) != self._page_length:
self.close()
msg = "failed to read complete page from file (read {:d} of {:d} bytes)"
raise ValueError(msg.format(len(self._cached_page), self._page_length))
msg = (
"failed to read complete page from file (read "
f"{len(self._cached_page):d} of "
f"{self._page_length:d} bytes)"
)
raise ValueError(msg)

self._read_page_header()
page_type = self._current_page_type
Expand Down Expand Up @@ -725,8 +726,6 @@ def _chunk_to_dataframe(self):
js += 1
else:
self.close()
raise ValueError(
"unknown column type {type}".format(type=self._column_types[j])
)
raise ValueError(f"unknown column type {self._column_types[j]}")

return rslt
4 changes: 2 additions & 2 deletions pandas/io/sas/sas_xport.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ def _read_header(self):
fl = field["field_length"]
if field["ntype"] == "numeric" and ((fl < 2) or (fl > 8)):
self.close()
msg = "Floating field width {0} is not between 2 and 8."
raise TypeError(msg.format(fl))
msg = f"Floating field width {fl} is not between 2 and 8."
raise TypeError(msg)

for k, v in field.items():
try:
Expand Down
18 changes: 8 additions & 10 deletions pandas/tests/io/sas/test_sas7bdat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def setup_method(self, datapath):
self.data = []
self.test_ix = [list(range(1, 16)), [16]]
for j in 1, 2:
fname = os.path.join(self.dirpath, "test_sas7bdat_{j}.csv".format(j=j))
fname = os.path.join(self.dirpath, f"test_sas7bdat_{j}.csv")
df = pd.read_csv(fname)
epoch = pd.datetime(1960, 1, 1)
t1 = pd.to_timedelta(df["Column4"], unit="d")
Expand All @@ -38,15 +38,15 @@ def test_from_file(self):
for j in 0, 1:
df0 = self.data[j]
for k in self.test_ix[j]:
fname = os.path.join(self.dirpath, "test{k}.sas7bdat".format(k=k))
fname = os.path.join(self.dirpath, f"test{k}.sas7bdat")
df = pd.read_sas(fname, encoding="utf-8")
tm.assert_frame_equal(df, df0)

def test_from_buffer(self):
for j in 0, 1:
df0 = self.data[j]
for k in self.test_ix[j]:
fname = os.path.join(self.dirpath, "test{k}.sas7bdat".format(k=k))
fname = os.path.join(self.dirpath, f"test{k}.sas7bdat")
with open(fname, "rb") as f:
byts = f.read()
buf = io.BytesIO(byts)
Expand All @@ -61,7 +61,7 @@ def test_from_iterator(self):
for j in 0, 1:
df0 = self.data[j]
for k in self.test_ix[j]:
fname = os.path.join(self.dirpath, "test{k}.sas7bdat".format(k=k))
fname = os.path.join(self.dirpath, f"test{k}.sas7bdat")
rdr = pd.read_sas(fname, iterator=True, encoding="utf-8")
df = rdr.read(2)
tm.assert_frame_equal(df, df0.iloc[0:2, :])
Expand All @@ -73,7 +73,7 @@ def test_path_pathlib(self):
for j in 0, 1:
df0 = self.data[j]
for k in self.test_ix[j]:
fname = Path(os.path.join(self.dirpath, "test{k}.sas7bdat".format(k=k)))
fname = Path(os.path.join(self.dirpath, f"test{k}.sas7bdat"))
df = pd.read_sas(fname, encoding="utf-8")
tm.assert_frame_equal(df, df0)

Expand All @@ -84,9 +84,7 @@ def test_path_localpath(self):
for j in 0, 1:
df0 = self.data[j]
for k in self.test_ix[j]:
fname = LocalPath(
os.path.join(self.dirpath, "test{k}.sas7bdat".format(k=k))
)
fname = LocalPath(os.path.join(self.dirpath, f"test{k}.sas7bdat"))
df = pd.read_sas(fname, encoding="utf-8")
tm.assert_frame_equal(df, df0)

Expand All @@ -95,7 +93,7 @@ def test_iterator_loop(self):
for j in 0, 1:
for k in self.test_ix[j]:
for chunksize in 3, 5, 10, 11:
fname = os.path.join(self.dirpath, "test{k}.sas7bdat".format(k=k))
fname = os.path.join(self.dirpath, f"test{k}.sas7bdat")
rdr = pd.read_sas(fname, chunksize=10, encoding="utf-8")
y = 0
for x in rdr:
Expand All @@ -106,7 +104,7 @@ def test_iterator_loop(self):
def test_iterator_read_too_much(self):
# github #14734
k = self.test_ix[0][0]
fname = os.path.join(self.dirpath, "test{k}.sas7bdat".format(k=k))
fname = os.path.join(self.dirpath, f"test{k}.sas7bdat")
rdr = pd.read_sas(fname, format="sas7bdat", iterator=True, encoding="utf-8")
d1 = rdr.read(rdr.row_count + 20)
rdr.close()
Expand Down