Skip to content

Commit 098c604

Browse files
committed
parametrized multiindex test & fixed the args to index_col
1 parent 18d7055 commit 098c604

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ I/O
796796
- Bug in :func:`read_excel` dropping empty values from single-column spreadsheets (:issue:`39808`)
797797
- Bug in :meth:`DataFrame.to_string` misplacing the truncation column when ``index=False`` (:issue:`40907`)
798798
- Bug in :func:`read_orc` always raising ``AttributeError`` (:issue:`40918`)
799-
- Bug in :func:`read_clipboard` when copying from excel and the first column contains null values (:issue:`41108`)
799+
- Bug in :func:`read_clipboard` copying from an excel file shifts values into the wrong column if there are null values in first column (:issue:`41108`)
800800

801801
Period
802802
^^^^^^

pandas/io/clipboards.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def read_clipboard(sep=r"\s+", **kwargs): # pragma: no cover
6565
# to account for index columns
6666
index_length = len(lines[0]) - len(lines[0].lstrip(" \t"))
6767
if index_length != 0:
68-
kwargs.setdefault("index_col", [0, index_length - 1])
68+
kwargs.setdefault("index_col", list(range(index_length)))
6969

7070
# Edge case where sep is specified to be None, return to default
7171
if sep is None and kwargs.get("delim_whitespace") is None:

pandas/tests/io/test_clipboard.py

+23-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from pandas import (
77
DataFrame,
8-
MultiIndex,
98
get_option,
109
read_clipboard,
1110
)
@@ -257,16 +256,34 @@ def test_infer_excel_with_nulls(self, request, mock_clipboard):
257256
# excel data is parsed correctly
258257
tm.assert_frame_equal(df, df_expected)
259258

260-
def test_infer_excel_with_multiindex(self, request, mock_clipboard):
259+
@pytest.mark.parametrize(
260+
"multiindex",
261+
[
262+
(
263+
"""\t\t\tcol1\tcol2
264+
A\t0\tTrue\t1\tred
265+
A\t1\tTrue\t\tblue
266+
B\t0\tFalse\t2\tgreen""",
267+
[["A", "A", "B"], [0, 1, 0], [True, True, False]],
268+
),
269+
(
270+
"""\t\tcol1\tcol2
271+
A\t0\t1\tred
272+
A\t1\t\tblue
273+
B\t0\t2\tgreen""",
274+
[["A", "A", "B"], [0, 1, 0]],
275+
),
276+
],
277+
)
278+
def test_infer_excel_with_multiindex(self, request, mock_clipboard, multiindex):
261279
# GH41108
262-
text = "\t\tcol1\tcol2\nA\t0\t1\tred\nA\t1\t\tblue\nB\t0\t2\tgreen"
263280

264-
mock_clipboard[request.node.name] = text
281+
# the `.replace()` is because `.dedent()` does not like the leading `\t`
282+
mock_clipboard[request.node.name] = multiindex[0].replace(" ", "")
265283
df = read_clipboard()
266-
multiindex = MultiIndex.from_tuples([("A", 0), ("A", 1), ("B", 0)])
267284
df_expected = DataFrame(
268285
data={"col1": [1, None, 2], "col2": ["red", "blue", "green"]},
269-
index=multiindex,
286+
index=multiindex[1],
270287
)
271288

272289
# excel data is parsed correctly

0 commit comments

Comments
 (0)