Skip to content

Commit c74de79

Browse files
jyscaojbrockmendel
authored andcommitted
CLN: use f-string for JSON related files (#30430)
* CLN: use f-string for JSON related files * Apply black style * Missed one... * Use double-quotes for expected in test_to_json_indent * Add the f in f-string * Use correct multiplier (100k not 10) for "bar" * Add back missing closing paren * Use single quote as the outer quotes * Remove pprint_thing usage and import * Keep use f""" for expected * Split two tests into 3 lines each * Remove paren around string * Remove extra pair "" in string * Move kwargs.get onto own line
1 parent 658840d commit c74de79

File tree

9 files changed

+42
-66
lines changed

9 files changed

+42
-66
lines changed

pandas/io/json/_json.py

+7-19
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
infer_compression,
2626
stringify_path,
2727
)
28-
from pandas.io.formats.printing import pprint_thing
2928
from pandas.io.parsers import _validate_integer
3029

3130
from ._normalize import convert_to_line_delimits
@@ -175,10 +174,7 @@ class SeriesWriter(Writer):
175174

176175
def _format_axes(self):
177176
if not self.obj.index.is_unique and self.orient == "index":
178-
raise ValueError(
179-
"Series index must be unique for orient="
180-
"'{orient}'".format(orient=self.orient)
181-
)
177+
raise ValueError(f"Series index must be unique for orient='{self.orient}'")
182178

183179
def _write(
184180
self,
@@ -214,17 +210,15 @@ def _format_axes(self):
214210
"""
215211
if not self.obj.index.is_unique and self.orient in ("index", "columns"):
216212
raise ValueError(
217-
"DataFrame index must be unique for orient="
218-
"'{orient}'.".format(orient=self.orient)
213+
f"DataFrame index must be unique for orient='{self.orient}'."
219214
)
220215
if not self.obj.columns.is_unique and self.orient in (
221216
"index",
222217
"columns",
223218
"records",
224219
):
225220
raise ValueError(
226-
"DataFrame columns must be unique for orient="
227-
"'{orient}'.".format(orient=self.orient)
221+
f"DataFrame columns must be unique for orient='{self.orient}'."
228222
)
229223

230224
def _write(
@@ -290,8 +284,8 @@ def __init__(
290284
if date_format != "iso":
291285
msg = (
292286
"Trying to write with `orient='table'` and "
293-
"`date_format='{fmt}'`. Table Schema requires dates "
294-
"to be formatted with `date_format='iso'`".format(fmt=date_format)
287+
f"`date_format='{date_format}'`. Table Schema requires dates "
288+
"to be formatted with `date_format='iso'`"
295289
)
296290
raise ValueError(msg)
297291

@@ -828,9 +822,7 @@ def __init__(
828822
if date_unit is not None:
829823
date_unit = date_unit.lower()
830824
if date_unit not in self._STAMP_UNITS:
831-
raise ValueError(
832-
"date_unit must be one of {units}".format(units=self._STAMP_UNITS)
833-
)
825+
raise ValueError(f"date_unit must be one of {self._STAMP_UNITS}")
834826
self.min_stamp = self._MIN_STAMPS[date_unit]
835827
else:
836828
self.min_stamp = self._MIN_STAMPS["s"]
@@ -850,11 +842,7 @@ def check_keys_split(self, decoded):
850842
bad_keys = set(decoded.keys()).difference(set(self._split_keys))
851843
if bad_keys:
852844
bad_keys = ", ".join(bad_keys)
853-
raise ValueError(
854-
"JSON data had unexpected key(s): {bad_keys}".format(
855-
bad_keys=pprint_thing(bad_keys)
856-
)
857-
)
845+
raise ValueError(f"JSON data had unexpected key(s): {bad_keys}")
858846

859847
def parse(self):
860848

pandas/io/json/_normalize.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def _recursive_extract(data, path, seen_meta, level=0):
309309
raise KeyError(
310310
"Try running with "
311311
"errors='ignore' as key "
312-
"{err} is not always present".format(err=e)
312+
f"{e} is not always present"
313313
)
314314
meta_vals[key].append(meta_val)
315315
records.extend(recs)
@@ -319,7 +319,7 @@ def _recursive_extract(data, path, seen_meta, level=0):
319319
result = DataFrame(records)
320320

321321
if record_prefix is not None:
322-
result = result.rename(columns=lambda x: "{p}{c}".format(p=record_prefix, c=x))
322+
result = result.rename(columns=lambda x: f"{record_prefix}{x}")
323323

324324
# Data types, a problem
325325
for k, v in meta_vals.items():
@@ -328,8 +328,7 @@ def _recursive_extract(data, path, seen_meta, level=0):
328328

329329
if k in result:
330330
raise ValueError(
331-
"Conflicting metadata name {name}, "
332-
"need distinguishing prefix ".format(name=k)
331+
f"Conflicting metadata name {k}, need distinguishing prefix "
333332
)
334333
result[k] = np.array(v, dtype=object).repeat(lengths)
335334
return result

pandas/io/json/_table_schema.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def set_default_names(data):
8989
data = data.copy()
9090
if data.index.nlevels > 1:
9191
names = [
92-
name if name is not None else "level_{}".format(i)
92+
name if name is not None else f"level_{i}"
9393
for i, name in enumerate(data.index.names)
9494
]
9595
data.index.names = names
@@ -175,7 +175,7 @@ def convert_json_field_to_pandas_type(field):
175175
return "timedelta64"
176176
elif typ == "datetime":
177177
if field.get("tz"):
178-
return "datetime64[ns, {tz}]".format(tz=field["tz"])
178+
return f"datetime64[ns, {field['tz']}]"
179179
else:
180180
return "datetime64[ns]"
181181
elif typ == "any":
@@ -186,7 +186,7 @@ def convert_json_field_to_pandas_type(field):
186186
else:
187187
return "object"
188188

189-
raise ValueError("Unsupported or invalid field type: {}".format(typ))
189+
raise ValueError(f"Unsupported or invalid field type: {typ}")
190190

191191

192192
def build_table_schema(data, index=True, primary_key=None, version=True):

pandas/tests/extension/json/test_json.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,15 @@ def assert_series_equal(self, left, right, **kwargs):
9393
tm.assert_series_equal(left, right, **kwargs)
9494

9595
def assert_frame_equal(self, left, right, *args, **kwargs):
96+
obj_type = kwargs.get("obj", "DataFrame")
9697
tm.assert_index_equal(
9798
left.columns,
9899
right.columns,
99100
exact=kwargs.get("check_column_type", "equiv"),
100101
check_names=kwargs.get("check_names", True),
101102
check_exact=kwargs.get("check_exact", False),
102103
check_categorical=kwargs.get("check_categorical", True),
103-
obj="{obj}.columns".format(obj=kwargs.get("obj", "DataFrame")),
104+
obj=f"{obj_type}.columns",
104105
)
105106

106107
jsons = (left.dtypes == "json").index

pandas/tests/io/json/test_compression.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,7 @@ def test_to_json_compression(compression_only, read_infer, to_infer):
9090
compression = compression_only
9191

9292
if compression == "zip":
93-
pytest.skip(
94-
"{compression} is not supported "
95-
"for to_csv".format(compression=compression)
96-
)
93+
pytest.skip(f"{compression} is not supported for to_csv")
9794

9895
# We'll complete file extension subsequently.
9996
filename = "test."

pandas/tests/io/json/test_json_table_schema.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def test_convert_json_field_to_pandas_type(self, inp, exp):
513513
def test_convert_json_field_to_pandas_type_raises(self, inp):
514514
field = {"type": inp}
515515
with pytest.raises(
516-
ValueError, match=("Unsupported or invalid field type: {}".format(inp))
516+
ValueError, match=f"Unsupported or invalid field type: {inp}"
517517
):
518518
convert_json_field_to_pandas_type(field)
519519

pandas/tests/io/json/test_pandas.py

+19-23
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_frame_non_unique_index(self, orient):
105105
@pytest.mark.parametrize("orient", ["index", "columns"])
106106
def test_frame_non_unique_index_raises(self, orient):
107107
df = DataFrame([["a", "b"], ["c", "d"]], index=[1, 1], columns=["x", "y"])
108-
msg = "DataFrame index must be unique for orient='{}'".format(orient)
108+
msg = f"DataFrame index must be unique for orient='{orient}'"
109109
with pytest.raises(ValueError, match=msg):
110110
df.to_json(orient=orient)
111111

@@ -142,7 +142,7 @@ def test_frame_non_unique_columns(self, orient, data):
142142
def test_frame_non_unique_columns_raises(self, orient):
143143
df = DataFrame([["a", "b"], ["c", "d"]], index=[1, 2], columns=["x", "x"])
144144

145-
msg = "DataFrame columns must be unique for orient='{}'".format(orient)
145+
msg = f"DataFrame columns must be unique for orient='{orient}'"
146146
with pytest.raises(ValueError, match=msg):
147147
df.to_json(orient=orient)
148148

@@ -225,13 +225,11 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype):
225225
def test_roundtrip_categorical(self, orient, convert_axes, numpy):
226226
# TODO: create a better frame to test with and improve coverage
227227
if orient in ("index", "columns"):
228-
pytest.xfail(
229-
"Can't have duplicate index values for orient '{}')".format(orient)
230-
)
228+
pytest.xfail(f"Can't have duplicate index values for orient '{orient}')")
231229

232230
data = self.categorical.to_json(orient=orient)
233231
if numpy and orient in ("records", "values"):
234-
pytest.xfail("Orient {} is broken with numpy=True".format(orient))
232+
pytest.xfail(f"Orient {orient} is broken with numpy=True")
235233

236234
result = pd.read_json(
237235
data, orient=orient, convert_axes=convert_axes, numpy=numpy
@@ -399,7 +397,7 @@ def test_frame_infinity(self, orient, inf, dtype):
399397
def test_frame_to_json_float_precision(self, value, precision, expected_val):
400398
df = pd.DataFrame([dict(a_float=value)])
401399
encoded = df.to_json(double_precision=precision)
402-
assert encoded == '{{"a_float":{{"0":{}}}}}'.format(expected_val)
400+
assert encoded == f'{{"a_float":{{"0":{expected_val}}}}}'
403401

404402
def test_frame_to_json_except(self):
405403
df = DataFrame([1, 2, 3])
@@ -593,7 +591,7 @@ def __str__(self) -> str:
593591

594592
# verify the proper conversion of printable content
595593
df_printable = DataFrame({"A": [binthing.hexed]})
596-
assert df_printable.to_json() == '{{"A":{{"0":"{hex}"}}}}'.format(hex=hexed)
594+
assert df_printable.to_json() == f'{{"A":{{"0":"{hexed}"}}}}'
597595

598596
# check if non-printable content throws appropriate Exception
599597
df_nonprintable = DataFrame({"A": [binthing]})
@@ -607,19 +605,19 @@ def __str__(self) -> str:
607605
df_mixed.to_json()
608606

609607
# default_handler should resolve exceptions for non-string types
610-
assert df_nonprintable.to_json(
611-
default_handler=str
612-
) == '{{"A":{{"0":"{hex}"}}}}'.format(hex=hexed)
613-
assert df_mixed.to_json(
614-
default_handler=str
615-
) == '{{"A":{{"0":"{hex}"}},"B":{{"0":1}}}}'.format(hex=hexed)
608+
result = df_nonprintable.to_json(default_handler=str)
609+
expected = f'{{"A":{{"0":"{hexed}"}}}}'
610+
assert result == expected
611+
assert (
612+
df_mixed.to_json(default_handler=str)
613+
== f'{{"A":{{"0":"{hexed}"}},"B":{{"0":1}}}}'
614+
)
616615

617616
def test_label_overflow(self):
618617
# GH14256: buffer length not checked when writing label
619-
df = pd.DataFrame({"bar" * 100000: [1], "foo": [1337]})
620-
assert df.to_json() == '{{"{bar}":{{"0":1}},"foo":{{"0":1337}}}}'.format(
621-
bar=("bar" * 100000)
622-
)
618+
result = pd.DataFrame({"bar" * 100000: [1], "foo": [1337]}).to_json()
619+
expected = f'{{"{"bar" * 100000}":{{"0":1}},"foo":{{"0":1337}}}}'
620+
assert result == expected
623621

624622
def test_series_non_unique_index(self):
625623
s = Series(["a", "b"], index=[1, 1])
@@ -1431,7 +1429,7 @@ def test_read_timezone_information(self):
14311429
)
14321430
def test_timedelta_as_label(self, date_format, key):
14331431
df = pd.DataFrame([[1]], columns=[pd.Timedelta("1D")])
1434-
expected = '{{"{key}":{{"0":1}}}}'.format(key=key)
1432+
expected = f'{{"{key}":{{"0":1}}}}'
14351433
result = df.to_json(date_format=date_format)
14361434

14371435
assert result == expected
@@ -1460,7 +1458,7 @@ def test_to_json_indent(self, indent):
14601458

14611459
result = df.to_json(indent=indent)
14621460
spaces = " " * indent
1463-
expected = """{{
1461+
expected = f"""{{
14641462
{spaces}"a":{{
14651463
{spaces}{spaces}"0":"foo",
14661464
{spaces}{spaces}"1":"baz"
@@ -1469,9 +1467,7 @@ def test_to_json_indent(self, indent):
14691467
{spaces}{spaces}"0":"bar",
14701468
{spaces}{spaces}"1":"qux"
14711469
{spaces}}}
1472-
}}""".format(
1473-
spaces=spaces
1474-
)
1470+
}}"""
14751471

14761472
assert result == expected
14771473

pandas/tests/io/json/test_readlines.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ def test_readjson_chunks_closes(chunksize):
134134
reader.read()
135135
assert (
136136
reader.open_stream.closed
137-
), "didn't close stream with \
138-
chunksize = {chunksize}".format(
139-
chunksize=chunksize
140-
)
137+
), f"didn't close stream with chunksize = {chunksize}"
141138

142139

143140
@pytest.mark.parametrize("chunksize", [0, -1, 2.2, "foo"])
@@ -170,9 +167,7 @@ def test_readjson_chunks_multiple_empty_lines(chunksize):
170167
test = pd.read_json(j, lines=True, chunksize=chunksize)
171168
if chunksize is not None:
172169
test = pd.concat(test)
173-
tm.assert_frame_equal(
174-
orig, test, obj="chunksize: {chunksize}".format(chunksize=chunksize)
175-
)
170+
tm.assert_frame_equal(orig, test, obj=f"chunksize: {chunksize}")
176171

177172

178173
def test_readjson_unicode(monkeypatch):

pandas/tests/io/json/test_ujson.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -362,21 +362,21 @@ def test_encode_date_conversion(self):
362362
)
363363
def test_encode_time_conversion_basic(self, test):
364364
output = ujson.encode(test)
365-
expected = '"{iso}"'.format(iso=test.isoformat())
365+
expected = f'"{test.isoformat()}"'
366366
assert expected == output
367367

368368
def test_encode_time_conversion_pytz(self):
369369
# see gh-11473: to_json segfaults with timezone-aware datetimes
370370
test = datetime.time(10, 12, 15, 343243, pytz.utc)
371371
output = ujson.encode(test)
372-
expected = '"{iso}"'.format(iso=test.isoformat())
372+
expected = f'"{test.isoformat()}"'
373373
assert expected == output
374374

375375
def test_encode_time_conversion_dateutil(self):
376376
# see gh-11473: to_json segfaults with timezone-aware datetimes
377377
test = datetime.time(10, 12, 15, 343243, dateutil.tz.tzutc())
378378
output = ujson.encode(test)
379-
expected = '"{iso}"'.format(iso=test.isoformat())
379+
expected = f'"{test.isoformat()}"'
380380
assert expected == output
381381

382382
@pytest.mark.parametrize(
@@ -580,7 +580,7 @@ class Nested:
580580
def test_decode_number_with_32bit_sign_bit(self, val):
581581
# Test that numbers that fit within 32 bits but would have the
582582
# sign bit set (2**31 <= x < 2**32) are decoded properly.
583-
doc = '{{"id": {val}}}'.format(val=val)
583+
doc = f'{{"id": {val}}}'
584584
assert ujson.decode(doc)["id"] == val
585585

586586
def test_encode_big_escape(self):

0 commit comments

Comments
 (0)