Skip to content

Commit 5c3114b

Browse files
changes based on simon review and fix linting error that breaks the pr
1 parent 002f12b commit 5c3114b

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2910,7 +2910,7 @@ def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
29102910
29112911
index : bool, default True
29122912
Write row names (index).
2913-
index_label : bool or str or sequence, default None.
2913+
index_label : bool or str or sequence, default None
29142914
If index_label is not explicitly called, False if either header
29152915
or index is set to False; otherwise, True. If index_label is
29162916
explicitly called by allowed types of input, then input will be

pandas/tests/generic/test_frame.py

-37
Original file line numberDiff line numberDiff line change
@@ -270,40 +270,3 @@ def test_deepcopy_empty(self):
270270
empty_frame_copy = deepcopy(empty_frame)
271271

272272
self._compare(empty_frame_copy, empty_frame)
273-
274-
@pytest.mark.skipif(os.name == 'nt',
275-
reason="Windows use \r\n for newline")
276-
@pytest.mark.parametrize("header, index_label, expected", [
277-
(False, True, 'index.name,,\n0,0,0\n1,0,0\n'),
278-
(True, True, 'index.name,0,1\n0,0,0\n1,0,0\n'),
279-
(False, False, '0,0,0\n1,0,0\n'),
280-
(True, False, ',0,1\n0,0,0\n1,0,0\n'),
281-
(False, None, '0,0,0\n1,0,0\n'),
282-
(True, None, 'index.name,0,1\n0,0,0\n1,0,0\n')
283-
])
284-
def test_to_csv_header_single_index(self, header, index_label, expected):
285-
# issue 24546
286-
df = pd.DataFrame(np.zeros((2, 2), dtype=int))
287-
df.index.name = 'index.name'
288-
289-
result = df.to_csv(header=header, index_label=index_label)
290-
assert result == expected
291-
292-
@pytest.mark.skipif(os.name == 'nt',
293-
reason="Windows use \r\n for newline")
294-
@pytest.mark.parametrize("header, index_label, expected", [
295-
(False, True, 'index.name.0,index.name.1,,\na,b,0,0\na,c,0,0\n'),
296-
(True, True, 'index.name.0,index.name.1,0,1\na,b,0,0\na,c,0,0\n'),
297-
(False, False, 'a,b,0,0\na,c,0,0\n'),
298-
(True, False, ',,0,1\na,b,0,0\na,c,0,0\n'),
299-
(False, None, 'a,b,0,0\na,c,0,0\n'),
300-
(True, None, 'index.name.0,index.name.1,0,1\na,b,0,0\na,c,0,0\n')
301-
])
302-
def test_to_csv_header_multi_index(self, header, index_label, expected):
303-
# issue 24546
304-
df = pd.DataFrame(np.zeros((2, 2), dtype=int))
305-
df.index = pd.MultiIndex.from_product([['a'], ['b', 'c']], names=[
306-
'index.name.0', 'index.name.1'])
307-
308-
result = df.to_csv(header=header, index_label=index_label)
309-
assert result == expected

pandas/tests/io/formats/test_to_csv.py

+39
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,42 @@ def test_to_csv_compression(self, compression_only,
561561
result = pd.read_csv(path, index_col=0,
562562
compression=read_compression)
563563
tm.assert_frame_equal(result, df)
564+
565+
@pytest.mark.parametrize("header, index_label, expected_rows", [
566+
(False, True, ['index.name,,', '0,0,0', '1,0,0']),
567+
(True, True, ['index.name,A,B', '0,0,0', '1,0,0']),
568+
(False, False, ['0,0,0', '1,0,0']),
569+
(True, False, [',A,B', '0,0,0', '1,0,0']),
570+
(False, None, ['0,0,0', '1,0,0']),
571+
(True, None, ['index.name,A,B', '0,0,0', '1,0,0'])
572+
])
573+
def test_to_csv_header_single_index(self, header, index_label,
574+
expected_rows):
575+
# issue 24546
576+
df = pd.DataFrame(np.zeros((2, 2), dtype=int))
577+
df.index.name = 'index.name'
578+
df.columns = ['A', 'B']
579+
580+
result = df.to_csv(header=header, index_label=index_label)
581+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
582+
assert result == expected
583+
584+
@pytest.mark.parametrize("header, index_label, expected_rows", [
585+
(False, True, ['index.name.0,index.name.1,,', 'a,b,0,0', 'a,c,0,0']),
586+
(True, True, ['index.name.0,index.name.1,A,B', 'a,b,0,0', 'a,c,0,0']),
587+
(False, False, ['a,b,0,0', 'a,c,0,0']),
588+
(True, False, [',,A,B', 'a,b,0,0', 'a,c,0,0']),
589+
(False, None, ['a,b,0,0', 'a,c,0,0']),
590+
(True, None, ['index.name.0,index.name.1,A,B', 'a,b,0,0', 'a,c,0,0'])
591+
])
592+
def test_to_csv_header_multi_index(self, header, index_label,
593+
expected_rows):
594+
# issue 24546
595+
df = pd.DataFrame(np.zeros((2, 2), dtype=int))
596+
df.columns = ['A', 'B']
597+
df.index = pd.MultiIndex.from_product([['a'], ['b', 'c']], names=[
598+
'index.name.0', 'index.name.1'])
599+
600+
result = df.to_csv(header=header, index_label=index_label)
601+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
602+
assert result == expected

0 commit comments

Comments
 (0)