Skip to content

Commit 10574df

Browse files
authored
BUG: MultiIndex.factorize raising with length-0 (#62281)
1 parent cc8ffb3 commit 10574df

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ I/O
10051005
- Bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
10061006
- Bug in :meth:`HDFStore.get` was failing to save data of dtype datetime64[s] correctly (:issue:`59004`)
10071007
- Bug in :meth:`HDFStore.select` causing queries on categorical string columns to return unexpected results (:issue:`57608`)
1008+
- Bug in :meth:`MultiIndex.factorize` incorrectly raising on length-0 indexes (:issue:`57517`)
10081009
- Bug in :meth:`read_csv` causing segmentation fault when ``encoding_errors`` is not a string. (:issue:`59059`)
10091010
- Bug in :meth:`read_csv` raising ``TypeError`` when ``index_col`` is specified and ``na_values`` is a dict containing the key ``None``. (:issue:`57547`)
10101011
- Bug in :meth:`read_csv` raising ``TypeError`` when ``nrows`` and ``iterator`` are specified without specifying a ``chunksize``. (:issue:`59079`)

pandas/core/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,11 @@ def factorize(
12981298

12991299
if isinstance(self, ABCMultiIndex):
13001300
# preserve MultiIndex
1301-
uniques = self._constructor(uniques)
1301+
if len(self) == 0:
1302+
# GH#57517
1303+
uniques = self[:0]
1304+
else:
1305+
uniques = self._constructor(uniques)
13021306
else:
13031307
from pandas import Index
13041308

pandas/tests/test_algos.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,16 @@ def test_index_returned(self, index):
582582
expected = expected.normalize()
583583
tm.assert_index_equal(result, expected, exact=True)
584584

585+
def test_factorize_multiindex_empty(self):
586+
# GH#57517
587+
mi = MultiIndex.from_product(
588+
[Index([], name="a", dtype=object), Index([], name="i", dtype="f4")]
589+
)
590+
codes, uniques = mi.factorize()
591+
exp_codes = np.array([], dtype=np.intp)
592+
tm.assert_numpy_array_equal(codes, exp_codes)
593+
tm.assert_index_equal(uniques, mi[:0])
594+
585595
def test_dtype_preservation(self, any_numpy_dtype):
586596
# GH 15442
587597
if any_numpy_dtype in (tm.BYTES_DTYPES + tm.STRING_DTYPES):

0 commit comments

Comments
 (0)