Skip to content

Commit 5be95b4

Browse files
authored
Merge branch 'main' into DOC46504
2 parents 3af8584 + 9a2d9ea commit 5be95b4

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ MultiIndex
485485
- Bug in :meth:`DataFrame.loc` raising when slicing a :class:`MultiIndex` with a negative step size and slicing a non-int labeled index level (:issue:`46156`)
486486
- Bug in :meth:`Series.to_numpy` where multiindexed Series could not be converted to numpy arrays when an ``na_value`` was supplied (:issue:`45774`)
487487
- Bug in :class:`MultiIndex.equals` not commutative when only one side has extension array dtype (:issue:`46026`)
488-
-
488+
- Bug in :meth:`MultiIndex.from_tuples` cannot construct Index of empty tuples (:issue:`45608`)
489489

490490
I/O
491491
^^^

pandas/core/indexes/multi.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,18 @@ def from_tuples(
546546
tuples = list(tuples)
547547
tuples = cast(Collection[Tuple[Hashable, ...]], tuples)
548548

549+
# handling the empty tuple cases
550+
if len(tuples) and all(isinstance(e, tuple) and not e for e in tuples):
551+
codes = [np.zeros(len(tuples))]
552+
levels = [Index(com.asarray_tuplesafe(tuples, dtype=np.dtype("object")))]
553+
return cls(
554+
levels=levels,
555+
codes=codes,
556+
sortorder=sortorder,
557+
names=names,
558+
verify_integrity=False,
559+
)
560+
549561
arrays: list[Sequence[Hashable]]
550562
if len(tuples) == 0:
551563
if names is None:

pandas/tests/indexes/base_class/test_constructors.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,11 @@ def test_constructor_cast(self):
4040
msg = "could not convert string to float"
4141
with pytest.raises(ValueError, match=msg):
4242
Index(["a", "b", "c"], dtype=float)
43+
44+
@pytest.mark.parametrize("tuple_list", [[()], [(), ()]])
45+
def test_construct_empty_tuples(self, tuple_list):
46+
# GH #45608
47+
result = Index(tuple_list)
48+
expected = MultiIndex.from_tuples(tuple_list)
49+
50+
tm.assert_index_equal(result, expected)

pandas/tests/test_downstream.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import numpy as np
99
import pytest
1010

11+
from pandas.compat import is_platform_windows
1112
import pandas.util._test_decorators as td
1213

1314
import pandas as pd
@@ -225,6 +226,13 @@ def test_pandas_datareader():
225226

226227
# importing from pandas, Cython import warning
227228
@pytest.mark.filterwarnings("ignore:can't resolve:ImportWarning")
229+
@pytest.mark.xfail(
230+
is_platform_windows(),
231+
raises=ImportError,
232+
reason="ImportError: the 'read_file' function requires the 'fiona' package, "
233+
"but it is not installed or does not import correctly",
234+
strict=False,
235+
)
228236
def test_geopandas():
229237

230238
geopandas = import_module("geopandas")

0 commit comments

Comments
 (0)