Skip to content

Commit 33c20e4

Browse files
committed
CLN: applied jreback's request
* Requested in PR pandas-dev#21406
1 parent 7b13eaf commit 33c20e4

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

pandas/tests/frame/test_to_csv.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -882,21 +882,25 @@ def test_to_csv_line_terminators(self):
882882
index=['one', 'two', 'three'])
883883

884884
with ensure_clean() as path:
885+
# case 1: CRLF as line terminator
885886
df.to_csv(path, line_terminator='\r\n')
886887

887888
expected = (b',A,B\r\none,1,4\r\ntwo,2,5\r\nthree,3,6\r\n')
888889
with open(path, mode='rb') as f:
889890
assert f.read() == expected
890891

892+
with ensure_clean() as path:
893+
# case 2: LF as line terminator
891894
df.to_csv(path, line_terminator='\n')
892895

893896
expected = (b',A,B\none,1,4\ntwo,2,5\nthree,3,6\n')
894897
with open(path, mode='rb') as f:
895898
assert f.read() == expected
896899

900+
with ensure_clean() as path:
901+
# case 3: The default line terminator(=os.linesep)(PR 21406)
902+
df.to_csv(path)
897903
os_linesep = os.linesep.encode('utf-8')
898-
df.to_csv(path) # The default line terminator=os.linesep(PR 21406)
899-
900904
expected = (b',A,B' + os_linesep + b'one,1,4' + os_linesep +
901905
b'two,2,5' + os_linesep + b'three,3,6' + os_linesep)
902906
with open(path, mode='rb') as f:

pandas/tests/io/formats/test_to_csv.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,9 @@ def test_to_csv_string_with_lf(self):
350350
}
351351
df = pd.DataFrame(data)
352352

353-
os_linesep = os.linesep.encode('utf-8')
354-
355353
with tm.ensure_clean('lf_test.csv') as path:
354+
# case 1: The default line terminator(=os.linesep)(PR 21406)
355+
os_linesep = os.linesep.encode('utf-8')
356356
expected_noarg = (
357357
b'int,str_lf' + os_linesep +
358358
b'1,abc' + os_linesep +
@@ -364,6 +364,8 @@ def test_to_csv_string_with_lf(self):
364364
with open(path, 'rb') as f:
365365
assert f.read() == expected_noarg
366366

367+
with tm.ensure_clean('lf_test.csv') as path:
368+
# case 2: LF as line terminator
367369
expected_lf = (
368370
b'int,str_lf\n'
369371
b'1,abc\n'
@@ -375,6 +377,8 @@ def test_to_csv_string_with_lf(self):
375377
with open(path, 'rb') as f:
376378
assert f.read() == expected_lf
377379

380+
with tm.ensure_clean('lf_test.csv') as path:
381+
# case 3: CRLF as line terminator
378382
# 'line_terminator' should not change inner element
379383
expected_crlf = (
380384
b'int,str_lf\r\n'
@@ -395,9 +399,9 @@ def test_to_csv_string_with_crlf(self):
395399
}
396400
df = pd.DataFrame(data)
397401

398-
os_linesep = os.linesep.encode('utf-8')
399-
400402
with tm.ensure_clean('crlf_test.csv') as path:
403+
# case 1: The default line terminator(=os.linesep)(PR 21406)
404+
os_linesep = os.linesep.encode('utf-8')
401405
expected_noarg = (
402406
b'int,str_crlf' + os_linesep +
403407
b'1,abc' + os_linesep +
@@ -409,6 +413,8 @@ def test_to_csv_string_with_crlf(self):
409413
with open(path, 'rb') as f:
410414
assert f.read() == expected_noarg
411415

416+
with tm.ensure_clean('crlf_test.csv') as path:
417+
# case 2: LF as line terminator
412418
expected_lf = (
413419
b'int,str_crlf\n'
414420
b'1,abc\n'
@@ -420,6 +426,9 @@ def test_to_csv_string_with_crlf(self):
420426
with open(path, 'rb') as f:
421427
assert f.read() == expected_lf
422428

429+
with tm.ensure_clean('crlf_test.csv') as path:
430+
# case 3: CRLF as line terminator
431+
# 'line_terminator' should not change inner element
423432
expected_crlf = (
424433
b'int,str_crlf\r\n'
425434
b'1,abc\r\n'
@@ -446,7 +455,7 @@ def test_to_csv_stdout_file(self):
446455
assert not sys.stdout.closed
447456

448457
@pytest.mark.xfail(
449-
os.name == 'nt',
458+
compat.is_platform_windows(),
450459
reason=("Especially in Windows, file stream should not be passed"
451460
"to csv writer without newline='' option."
452461
"(https://docs.python.org/3.6/library/csv.html#csv.writer)"))

pandas/util/testing.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -2804,7 +2804,21 @@ def skipna_wrapper(x):
28042804

28052805

28062806
def convert_rows_list_to_csv_str(rows_list):
2807-
csv_str = ''
2808-
for row in rows_list:
2809-
csv_str += row + os.linesep
2810-
return csv_str
2807+
"""
2808+
Convert list of rows of csv to a single
2809+
csv-formatted string that fits the current OS.
2810+
This method is used for creating expected value of to_csv() method.
2811+
2812+
Parameters
2813+
----------
2814+
rows_list : list
2815+
The list of string. Each element represents the row of csv.
2816+
2817+
Returns
2818+
-------
2819+
expected : string
2820+
Expected output of to_csv() in current OS
2821+
"""
2822+
sep = os.linesep
2823+
expected = sep.join(rows_list) + sep
2824+
return expected

0 commit comments

Comments
 (0)