Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ I/O
- Bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
- Bug in :meth:`HDFStore.get` was failing to save data of dtype datetime64[s] correctly (:issue:`59004`)
- Bug in :meth:`HDFStore.select` causing queries on categorical string columns to return unexpected results (:issue:`57608`)
- Bug in :meth:`MultiIndex.factorize` incorrectly raising on length-0 indexes (:issue:`57517`)
- Bug in :meth:`read_csv` causing segmentation fault when ``encoding_errors`` is not a string. (:issue:`59059`)
- Bug in :meth:`read_csv` raising ``TypeError`` when ``index_col`` is specified and ``na_values`` is a dict containing the key ``None``. (:issue:`57547`)
- Bug in :meth:`read_csv` raising ``TypeError`` when ``nrows`` and ``iterator`` are specified without specifying a ``chunksize``. (:issue:`59079`)
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,11 @@ def factorize(

if isinstance(self, ABCMultiIndex):
# preserve MultiIndex
uniques = self._constructor(uniques)
if len(self) == 0:
# GH#57517
uniques = self[:0]
else:
uniques = self._constructor(uniques)
else:
from pandas import Index

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,16 @@ def test_index_returned(self, index):
expected = expected.normalize()
tm.assert_index_equal(result, expected, exact=True)

def test_factorize_multiindex_empty(self):
# GH#57517
mi = MultiIndex.from_product(
[Index([], name="a", dtype=object), Index([], name="i", dtype="f4")]
)
codes, uniques = mi.factorize()
exp_codes = np.array([], dtype=np.intp)
tm.assert_numpy_array_equal(codes, exp_codes)
tm.assert_index_equal(uniques, mi[:0])

def test_dtype_preservation(self, any_numpy_dtype):
# GH 15442
if any_numpy_dtype in (tm.BYTES_DTYPES + tm.STRING_DTYPES):
Expand Down
Loading