Skip to content

Bug fix - cannot construct Index of empty tuples #46238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
b8644cb
cannot construct Index of empty tuples
Mar 5, 2022
83cd66a
test case failed
Mar 6, 2022
ae1414f
make change
Mar 8, 2022
3033614
Merge branch 'main' of https://github.com/weikhor/pandas into bug_fix…
Mar 8, 2022
2d15d49
change tuple
Mar 8, 2022
298fced
Merge branch 'main' of https://github.com/weikhor/pandas into bug_fix…
Mar 9, 2022
7ef7c86
multi index
Mar 9, 2022
ccd5479
multi index
Mar 9, 2022
6915cc1
change to index
Mar 10, 2022
33de53c
Merge branch 'main' of https://github.com/weikhor/pandas into bug_fix…
Mar 12, 2022
44adae2
from tuples
Mar 12, 2022
1283e97
merge
Mar 12, 2022
79bd8ea
ensure function return multiindex type
Mar 12, 2022
4fdae77
Incompatible types in assignment
Mar 12, 2022
1ee00a1
mypy
Mar 12, 2022
4dcc06d
check tuple
Mar 12, 2022
0323e0e
condition
Mar 12, 2022
fff543f
Merge branch 'main' into bug_fix_cannot_construct_Index_of_empty_tuples
Mar 12, 2022
e138c96
Merge branch 'main' of https://github.com/weikhor/pandas into bug_fix…
Mar 14, 2022
a8c3ff6
Merge branch 'bug_fix_cannot_construct_Index_of_empty_tuples' of http…
Mar 15, 2022
b8f2536
Multi index
Mar 15, 2022
68765e6
Update multi.py
Mar 15, 2022
4a329fd
Update multi.py
Mar 15, 2022
5f4e717
Update test_constructors.py
Mar 15, 2022
1b23a4c
test
Mar 15, 2022
560e4c2
Update multi.py
Mar 15, 2022
bcbed46
Update multi.py
Mar 15, 2022
8f2d3a5
pre-commit
Mar 15, 2022
9c60f32
pre commit
Mar 16, 2022
5f1d3bd
pre commit
Mar 16, 2022
9d6bf35
Merge branch 'main' into bug_fix_cannot_construct_Index_of_empty_tuples
Mar 16, 2022
ca1198d
Merge branch 'main' into bug_fix_cannot_construct_Index_of_empty_tuples
Mar 17, 2022
f3a190a
Merge branch 'main' of https://github.com/weikhor/pandas into bug_fix…
Mar 17, 2022
a792791
form tuples
Mar 17, 2022
0f84944
form tuples
Mar 17, 2022
0936405
Merge branch 'main' of https://github.com/weikhor/pandas into bug_fix…
Mar 18, 2022
7bd3ed1
Update multi.py
Mar 18, 2022
315808a
resolve comment
Mar 18, 2022
9184467
resolve comment
Mar 18, 2022
af6f067
multi
Mar 19, 2022
d4c499b
multi
Mar 19, 2022
71a1064
np zero
Mar 19, 2022
b5d3f39
Merge branch 'main' into bug_fix_cannot_construct_Index_of_empty_tuples
Mar 19, 2022
590a404
Merge branch 'main' of https://github.com/weikhor/pandas into bug_fix…
Mar 19, 2022
9215fc7
tuples
Mar 19, 2022
9612e53
Merge branch 'bug_fix_cannot_construct_Index_of_empty_tuples' of http…
Mar 19, 2022
4798b4c
tuple len
Mar 19, 2022
a5c84e8
Merge branch 'main' of https://github.com/weikhor/pandas into bug_fix…
Mar 24, 2022
e1430d4
Merge branch 'main' into bug_fix_cannot_construct_Index_of_empty_tuples
Mar 24, 2022
79f00b4
tuple
Mar 24, 2022
7960403
Merge branch 'main' into bug_fix_cannot_construct_Index_of_empty_tuples
Mar 24, 2022
3e6a00a
testing
Mar 24, 2022
e725d03
types
Mar 24, 2022
9ec6d9f
Merge branch 'main' into bug_fix_cannot_construct_Index_of_empty_tuples
Mar 24, 2022
70eb658
whatsnew
Mar 25, 2022
7879a19
Merge branch 'main' into bug_fix_cannot_construct_Index_of_empty_tuples
Mar 25, 2022
3f15379
Update v1.5.0.rst
Mar 26, 2022
65dc2aa
Merge branch 'main' into bug_fix_cannot_construct_Index_of_empty_tuples
Mar 26, 2022
e0db784
Merge branch 'main' into bug_fix_cannot_construct_Index_of_empty_tuples
Mar 27, 2022
7b2a788
Merge branch 'main' into bug_fix_cannot_construct_Index_of_empty_tuples
Mar 28, 2022
dbb45c9
Merge branch 'main' into bug_fix_cannot_construct_Index_of_empty_tuples
jreback Mar 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ MultiIndex
- 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`)
- Bug in :meth:`Series.to_numpy` where multiindexed Series could not be converted to numpy arrays when an ``na_value`` was supplied (:issue:`45774`)
- Bug in :class:`MultiIndex.equals` not commutative when only one side has extension array dtype (:issue:`46026`)
-
- Bug in :meth:`MultiIndex.from_tuples` cannot construct Index of empty tuples (:issue:`45608`)

I/O
^^^
Expand Down
12 changes: 12 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,18 @@ def from_tuples(
tuples = list(tuples)
tuples = cast(Collection[Tuple[Hashable, ...]], tuples)

# handling the empty tuple cases
if len(tuples) and all(isinstance(e, tuple) and not e for e in tuples):
codes = [np.zeros(len(tuples))]
levels = [Index(com.asarray_tuplesafe(tuples, dtype=np.dtype("object")))]
return cls(
levels=levels,
codes=codes,
sortorder=sortorder,
names=names,
verify_integrity=False,
)

arrays: list[Sequence[Hashable]]
if len(tuples) == 0:
if names is None:
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/base_class/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ def test_constructor_cast(self):
msg = "could not convert string to float"
with pytest.raises(ValueError, match=msg):
Index(["a", "b", "c"], dtype=float)

@pytest.mark.parametrize("tuple_list", [[()], [(), ()]])
def test_construct_empty_tuples(self, tuple_list):
# GH #45608
result = Index(tuple_list)
expected = MultiIndex.from_tuples(tuple_list)

tm.assert_index_equal(result, expected)