Skip to content

Commit

Permalink
CLN: applied jreback's request
Browse files Browse the repository at this point in the history
* Requested in PR pandas-dev#21406
  • Loading branch information
deflatSOCO committed Jul 7, 2018
1 parent 7b13eaf commit 33c20e4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
8 changes: 6 additions & 2 deletions pandas/tests/frame/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,21 +882,25 @@ def test_to_csv_line_terminators(self):
index=['one', 'two', 'three'])

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

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

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

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

with ensure_clean() as path:
# case 3: The default line terminator(=os.linesep)(PR 21406)
df.to_csv(path)
os_linesep = os.linesep.encode('utf-8')
df.to_csv(path) # The default line terminator=os.linesep(PR 21406)

expected = (b',A,B' + os_linesep + b'one,1,4' + os_linesep +
b'two,2,5' + os_linesep + b'three,3,6' + os_linesep)
with open(path, mode='rb') as f:
Expand Down
19 changes: 14 additions & 5 deletions pandas/tests/io/formats/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ def test_to_csv_string_with_lf(self):
}
df = pd.DataFrame(data)

os_linesep = os.linesep.encode('utf-8')

with tm.ensure_clean('lf_test.csv') as path:
# case 1: The default line terminator(=os.linesep)(PR 21406)
os_linesep = os.linesep.encode('utf-8')
expected_noarg = (
b'int,str_lf' + os_linesep +
b'1,abc' + os_linesep +
Expand All @@ -364,6 +364,8 @@ def test_to_csv_string_with_lf(self):
with open(path, 'rb') as f:
assert f.read() == expected_noarg

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

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

os_linesep = os.linesep.encode('utf-8')

with tm.ensure_clean('crlf_test.csv') as path:
# case 1: The default line terminator(=os.linesep)(PR 21406)
os_linesep = os.linesep.encode('utf-8')
expected_noarg = (
b'int,str_crlf' + os_linesep +
b'1,abc' + os_linesep +
Expand All @@ -409,6 +413,8 @@ def test_to_csv_string_with_crlf(self):
with open(path, 'rb') as f:
assert f.read() == expected_noarg

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

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

@pytest.mark.xfail(
os.name == 'nt',
compat.is_platform_windows(),
reason=("Especially in Windows, file stream should not be passed"
"to csv writer without newline='' option."
"(https://docs.python.org/3.6/library/csv.html#csv.writer)"))
Expand Down
22 changes: 18 additions & 4 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2804,7 +2804,21 @@ def skipna_wrapper(x):


def convert_rows_list_to_csv_str(rows_list):
csv_str = ''
for row in rows_list:
csv_str += row + os.linesep
return csv_str
"""
Convert list of rows of csv to a single
csv-formatted string that fits the current OS.
This method is used for creating expected value of to_csv() method.
Parameters
----------
rows_list : list
The list of string. Each element represents the row of csv.
Returns
-------
expected : string
Expected output of to_csv() in current OS
"""
sep = os.linesep
expected = sep.join(rows_list) + sep
return expected

0 comments on commit 33c20e4

Please sign in to comment.