1
1
from datetime import datetime
2
2
from io import StringIO
3
+ from textwrap import dedent
3
4
4
5
import numpy as np
5
6
import pytest
@@ -169,12 +170,16 @@ def format_func(x):
169
170
return x .strftime ("%Y-%m" )
170
171
171
172
result = x .to_string (formatters = {"months" : format_func })
172
- expected = "months\n 0 2016-01\n 1 2016-02"
173
+ expected = dedent (
174
+ """\
175
+ months
176
+ 0 2016-01
177
+ 1 2016-02"""
178
+ )
173
179
assert result .strip () == expected
174
180
175
181
176
182
def test_to_string_with_datetime64_hourformatter ():
177
-
178
183
x = DataFrame (
179
184
{"hod" : to_datetime (["10:10:10.100" , "12:12:12.120" ], format = "%H:%M:%S.%f" )}
180
185
)
@@ -183,20 +188,37 @@ def format_func(x):
183
188
return x .strftime ("%H:%M" )
184
189
185
190
result = x .to_string (formatters = {"hod" : format_func })
186
- expected = "hod\n 0 10:10\n 1 12:12"
191
+ expected = dedent (
192
+ """\
193
+ hod
194
+ 0 10:10
195
+ 1 12:12"""
196
+ )
187
197
assert result .strip () == expected
188
198
189
199
190
200
def test_to_string_with_formatters_unicode ():
191
201
df = DataFrame ({"c/\u03c3 " : [1 , 2 , 3 ]})
192
202
result = df .to_string (formatters = {"c/\u03c3 " : str })
193
- assert result == " c/\u03c3 \n " + "0 1\n 1 2\n 2 3"
203
+ expected = dedent (
204
+ """\
205
+ c/\u03c3
206
+ 0 1
207
+ 1 2
208
+ 2 3"""
209
+ )
210
+ assert result == expected
194
211
195
212
196
213
def test_to_string_complex_number_trims_zeros ():
197
214
s = Series ([1.000000 + 1.000000j , 1.0 + 1.0j , 1.05 + 1.0j ])
198
215
result = s .to_string ()
199
- expected = "0 1.00+1.00j\n 1 1.00+1.00j\n 2 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
+ )
200
222
assert result == expected
201
223
202
224
@@ -205,9 +227,12 @@ def test_nullable_float_to_string(float_ea_dtype):
205
227
dtype = float_ea_dtype
206
228
s = Series ([0.0 , 1.0 , None ], dtype = dtype )
207
229
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
+ )
211
236
assert result == expected
212
237
213
238
@@ -216,9 +241,12 @@ def test_nullable_int_to_string(any_nullable_int_dtype):
216
241
dtype = any_nullable_int_dtype
217
242
s = Series ([0 , 1 , None ], dtype = dtype )
218
243
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
+ )
222
250
assert result == expected
223
251
224
252
@@ -227,7 +255,10 @@ def test_to_string_na_rep_and_float_format(na_rep):
227
255
# GH 13828
228
256
df = DataFrame ([["A" , 1.2225 ], ["A" , None ]], columns = ["Group" , "Data" ])
229
257
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
+ )
233
264
assert result == expected
0 commit comments