Skip to content

Commit 1603426

Browse files
committed
parametrized multiindex test & fixed the args to index_col
1 parent efab5a8 commit 1603426

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ I/O
925925
- Bug in :func:`read_csv` and :func:`read_table` misinterpreting arguments when ``sys.setprofile`` had been previously called (:issue:`41069`)
926926
- Bug in the conversion from pyarrow to pandas (e.g. for reading Parquet) with nullable dtypes and a pyarrow array whose data buffer size is not a multiple of dtype size (:issue:`40896`)
927927
- Bug in :func:`read_excel` would raise an error when pandas could not determine the file type, even when user specified the ``engine`` argument (:issue:`41225`)
928-
- Bug in :func:`read_clipboard` when copying from excel and the first column contains null values (:issue:`41108`)
928+
- 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`)
929929

930930
Period
931931
^^^^^^

pandas/io/clipboards.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 23 additions & 6 deletions
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)