Skip to content

Commit d8f2f7d

Browse files
authored
TST: improve readability using dedent (#37718)
1 parent 87e554d commit d8f2f7d

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

pandas/tests/io/formats/test_to_string.py

+45-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from datetime import datetime
22
from io import StringIO
3+
from textwrap import dedent
34

45
import numpy as np
56
import pytest
@@ -169,12 +170,16 @@ def format_func(x):
169170
return x.strftime("%Y-%m")
170171

171172
result = x.to_string(formatters={"months": format_func})
172-
expected = "months\n0 2016-01\n1 2016-02"
173+
expected = dedent(
174+
"""\
175+
months
176+
0 2016-01
177+
1 2016-02"""
178+
)
173179
assert result.strip() == expected
174180

175181

176182
def test_to_string_with_datetime64_hourformatter():
177-
178183
x = DataFrame(
179184
{"hod": to_datetime(["10:10:10.100", "12:12:12.120"], format="%H:%M:%S.%f")}
180185
)
@@ -183,20 +188,37 @@ def format_func(x):
183188
return x.strftime("%H:%M")
184189

185190
result = x.to_string(formatters={"hod": format_func})
186-
expected = "hod\n0 10:10\n1 12:12"
191+
expected = dedent(
192+
"""\
193+
hod
194+
0 10:10
195+
1 12:12"""
196+
)
187197
assert result.strip() == expected
188198

189199

190200
def test_to_string_with_formatters_unicode():
191201
df = DataFrame({"c/\u03c3": [1, 2, 3]})
192202
result = df.to_string(formatters={"c/\u03c3": str})
193-
assert result == " c/\u03c3\n" + "0 1\n1 2\n2 3"
203+
expected = dedent(
204+
"""\
205+
c/\u03c3
206+
0 1
207+
1 2
208+
2 3"""
209+
)
210+
assert result == expected
194211

195212

196213
def test_to_string_complex_number_trims_zeros():
197214
s = Series([1.000000 + 1.000000j, 1.0 + 1.0j, 1.05 + 1.0j])
198215
result = s.to_string()
199-
expected = "0 1.00+1.00j\n1 1.00+1.00j\n2 1.05+1.00j"
216+
expected = dedent(
217+
"""\
218+
0 1.00+1.00j
219+
1 1.00+1.00j
220+
2 1.05+1.00j"""
221+
)
200222
assert result == expected
201223

202224

@@ -205,9 +227,12 @@ def test_nullable_float_to_string(float_ea_dtype):
205227
dtype = float_ea_dtype
206228
s = Series([0.0, 1.0, None], dtype=dtype)
207229
result = s.to_string()
208-
expected = """0 0.0
209-
1 1.0
210-
2 <NA>"""
230+
expected = dedent(
231+
"""\
232+
0 0.0
233+
1 1.0
234+
2 <NA>"""
235+
)
211236
assert result == expected
212237

213238

@@ -216,9 +241,12 @@ def test_nullable_int_to_string(any_nullable_int_dtype):
216241
dtype = any_nullable_int_dtype
217242
s = Series([0, 1, None], dtype=dtype)
218243
result = s.to_string()
219-
expected = """0 0
220-
1 1
221-
2 <NA>"""
244+
expected = dedent(
245+
"""\
246+
0 0
247+
1 1
248+
2 <NA>"""
249+
)
222250
assert result == expected
223251

224252

@@ -227,7 +255,10 @@ def test_to_string_na_rep_and_float_format(na_rep):
227255
# GH 13828
228256
df = DataFrame([["A", 1.2225], ["A", None]], columns=["Group", "Data"])
229257
result = df.to_string(na_rep=na_rep, float_format="{:.2f}".format)
230-
expected = f""" Group Data
231-
0 A 1.22
232-
1 A {na_rep}"""
258+
expected = dedent(
259+
f"""\
260+
Group Data
261+
0 A 1.22
262+
1 A {na_rep}"""
263+
)
233264
assert result == expected

0 commit comments

Comments
 (0)