-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
CLN: use f-string for JSON related files #30430
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
Changes from 3 commits
b9d2f40
e98cdfc
bbf017c
175fdb9
e6781a1
58b6249
d31f074
c197561
b5eb58e
8ee5fba
9313d2a
0a9ca0c
7bb78f7
a76220b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -309,7 +309,7 @@ def _recursive_extract(data, path, seen_meta, level=0): | |
raise KeyError( | ||
"Try running with " | ||
"errors='ignore' as key " | ||
"{err} is not always present".format(err=e) | ||
f"{e} is not always present" | ||
) | ||
meta_vals[key].append(meta_val) | ||
records.extend(recs) | ||
|
@@ -319,7 +319,7 @@ def _recursive_extract(data, path, seen_meta, level=0): | |
result = DataFrame(records) | ||
|
||
if record_prefix is not None: | ||
result = result.rename(columns=lambda x: "{p}{c}".format(p=record_prefix, c=x)) | ||
result = result.rename(columns=lambda x: f"{record_prefix}{x}") | ||
|
||
# Data types, a problem | ||
for k, v in meta_vals.items(): | ||
|
@@ -328,8 +328,7 @@ def _recursive_extract(data, path, seen_meta, level=0): | |
|
||
if k in result: | ||
raise ValueError( | ||
"Conflicting metadata name {name}, " | ||
"need distinguishing prefix ".format(name=k) | ||
f"Conflicting metadata name {k}, " "need distinguishing prefix " | ||
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. @jyscao an extra pair of parentheses got in here and needs to be removed. this sometimes happens when running 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. By parentheses, I'm guessing you mean extra pair of |
||
) | ||
result[k] = np.array(v, dtype=object).repeat(lengths) | ||
return result | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,7 +105,7 @@ def test_frame_non_unique_index(self, orient): | |
@pytest.mark.parametrize("orient", ["index", "columns"]) | ||
def test_frame_non_unique_index_raises(self, orient): | ||
df = DataFrame([["a", "b"], ["c", "d"]], index=[1, 1], columns=["x", "y"]) | ||
msg = "DataFrame index must be unique for orient='{}'".format(orient) | ||
msg = f"DataFrame index must be unique for orient='{orient}'" | ||
with pytest.raises(ValueError, match=msg): | ||
df.to_json(orient=orient) | ||
|
||
|
@@ -142,7 +142,7 @@ def test_frame_non_unique_columns(self, orient, data): | |
def test_frame_non_unique_columns_raises(self, orient): | ||
df = DataFrame([["a", "b"], ["c", "d"]], index=[1, 2], columns=["x", "x"]) | ||
|
||
msg = "DataFrame columns must be unique for orient='{}'".format(orient) | ||
msg = f"DataFrame columns must be unique for orient='{orient}'" | ||
with pytest.raises(ValueError, match=msg): | ||
df.to_json(orient=orient) | ||
|
||
|
@@ -225,13 +225,11 @@ def test_roundtrip_str_axes(self, orient, convert_axes, numpy, dtype): | |
def test_roundtrip_categorical(self, orient, convert_axes, numpy): | ||
# TODO: create a better frame to test with and improve coverage | ||
if orient in ("index", "columns"): | ||
pytest.xfail( | ||
"Can't have duplicate index values for orient '{}')".format(orient) | ||
) | ||
pytest.xfail(f"Can't have duplicate index values for orient '{orient}')") | ||
|
||
data = self.categorical.to_json(orient=orient) | ||
if numpy and orient in ("records", "values"): | ||
pytest.xfail("Orient {} is broken with numpy=True".format(orient)) | ||
pytest.xfail(f"Orient {orient} is broken with numpy=True") | ||
|
||
result = pd.read_json( | ||
data, orient=orient, convert_axes=convert_axes, numpy=numpy | ||
|
@@ -399,7 +397,7 @@ def test_frame_infinity(self, orient, inf, dtype): | |
def test_frame_to_json_float_precision(self, value, precision, expected_val): | ||
df = pd.DataFrame([dict(a_float=value)]) | ||
encoded = df.to_json(double_precision=precision) | ||
assert encoded == '{{"a_float":{{"0":{}}}}}'.format(expected_val) | ||
assert encoded == f'{{"a_float":{{"0":{expected_val}}}}}' | ||
|
||
def test_frame_to_json_except(self): | ||
df = DataFrame([1, 2, 3]) | ||
|
@@ -593,7 +591,7 @@ def __str__(self) -> str: | |
|
||
# verify the proper conversion of printable content | ||
df_printable = DataFrame({"A": [binthing.hexed]}) | ||
assert df_printable.to_json() == '{{"A":{{"0":"{hex}"}}}}'.format(hex=hexed) | ||
assert df_printable.to_json() == f'{{"A":{{"0":"{hexed}"}}}}' | ||
|
||
# check if non-printable content throws appropriate Exception | ||
df_nonprintable = DataFrame({"A": [binthing]}) | ||
|
@@ -607,19 +605,18 @@ def __str__(self) -> str: | |
df_mixed.to_json() | ||
|
||
# default_handler should resolve exceptions for non-string types | ||
assert df_nonprintable.to_json( | ||
default_handler=str | ||
) == '{{"A":{{"0":"{hex}"}}}}'.format(hex=hexed) | ||
assert df_mixed.to_json( | ||
default_handler=str | ||
) == '{{"A":{{"0":"{hex}"}},"B":{{"0":1}}}}'.format(hex=hexed) | ||
assert ( | ||
df_nonprintable.to_json(default_handler=str) == f'{{"A":{{"0":"{hexed}"}}}}' | ||
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. pls split into lines:
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. Done. I also split the test below this one into 3 lines. |
||
) | ||
assert ( | ||
df_mixed.to_json(default_handler=str) | ||
== f'{{"A":{{"0":"{hexed}"}},"B":{{"0":1}}}}' | ||
) | ||
|
||
def test_label_overflow(self): | ||
# GH14256: buffer length not checked when writing label | ||
df = pd.DataFrame({"bar" * 100000: [1], "foo": [1337]}) | ||
assert df.to_json() == '{{"{bar}":{{"0":1}},"foo":{{"0":1337}}}}'.format( | ||
bar=("bar" * 100000) | ||
) | ||
assert df.to_json() == f'{{"{"bar" * 10}":{{"0":1}},"foo":{{"0":1337}}}}' | ||
jyscao marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_series_non_unique_index(self): | ||
s = Series(["a", "b"], index=[1, 1]) | ||
|
@@ -1431,7 +1428,7 @@ def test_read_timezone_information(self): | |
) | ||
def test_timedelta_as_label(self, date_format, key): | ||
df = pd.DataFrame([[1]], columns=[pd.Timedelta("1D")]) | ||
expected = '{{"{key}":{{"0":1}}}}'.format(key=key) | ||
expected = f'{{"{key}":{{"0":1}}}}' | ||
result = df.to_json(date_format=date_format) | ||
|
||
assert result == expected | ||
|
@@ -1460,17 +1457,17 @@ def test_to_json_indent(self, indent): | |
|
||
result = df.to_json(indent=indent) | ||
spaces = " " * indent | ||
expected = """{{ | ||
{spaces}"a":{{ | ||
{spaces}{spaces}"0":"foo", | ||
{spaces}{spaces}"1":"baz" | ||
{spaces}}}, | ||
{spaces}"b":{{ | ||
{spaces}{spaces}"0":"bar", | ||
{spaces}{spaces}"1":"qux" | ||
{spaces}}} | ||
}}""".format( | ||
spaces=spaces | ||
expected = ( | ||
"{\n" | ||
f'{spaces}"a":{{\n' | ||
f'{spaces}{spaces}"0":"foo",\n' | ||
f'{spaces}{spaces}"1":"baz"\n' | ||
f"{spaces}}},\n" | ||
f'{spaces}"b":{{\n' | ||
f'{spaces}{spaces}"0":"bar",\n' | ||
f'{spaces}{spaces}"1":"qux"\n' | ||
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. Can we just use double quotes opposed to mixing? 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. the outer quotes all use 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. Now I remember, the quotes actually became mixed as a result of running Changing all to outer quotes to Running black again as the CI is prompting me to do will once again result in mixed quotes. So please let me know what's the best practise here: consistent quoting or black-style? 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. Just keep the original triple quoted string as is and prefix with an 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. Good point, I changed it back |
||
f"{spaces}}}\n" | ||
"}" | ||
) | ||
|
||
assert result == expected | ||
|
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.
Not sure if pprint_thing is still needed - can you check?
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.
I'm unfamiliar with the specifics of
pprint_thing
and where and when it should be used. Could you let me know why you suspect it might no longer be needed, and I'll investigate.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.
pprint_thing is partially left over from py2/py3 compat, but partially for pretty-pretting nested objects. In tthis case,
bad_keys
is defined a couple lines up and is a str, so pprint_thing is definitely not needed here.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.
Thanks for the clarification. I removed both the import and the call to
pprint_thing
in that module