Skip to content

BUG/TST: Empty input arrays in cartesian_product and MultiIndex (#12258) #14151

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

Closed
wants to merge 1 commit into from
Closed
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/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,7 @@ Bug Fixes
- Bug in ``MultiIndex`` slicing where extra elements were returned when level is non-unique (:issue:`12896`)
- Bug in ``.str.replace`` does not raise ``TypeError`` for invalid replacement (:issue:`13438`)
- Bug in ``MultiIndex.from_arrays`` which didn't check for input array lengths matching (:issue:`13599`)
- Bug in ``cartesian_product`` and ``MultiIndex.from_product`` which may raise with empty input arrays (:issue:`12258`)


- Bug in ``pd.read_csv()`` which may cause a segfault or corruption when iterating in large chunks over a stream/file under rare circumstances (:issue:`13703`)
Expand Down
22 changes: 20 additions & 2 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1979,13 +1979,16 @@ def _factorize_from_iterable(values):

Returns
-------
codes : np.array
codes : ndarray
categories : Index
If `values` has a categorical dtype, then `categories` is
a CategoricalIndex keeping the categories and order of `values`.
"""
from pandas.indexes.category import CategoricalIndex

if not is_list_like(values):
raise TypeError("Input must be list-like")

if is_categorical(values):
if isinstance(values, (ABCCategoricalIndex, ABCSeries)):
values = values._values
Expand All @@ -2003,8 +2006,23 @@ def _factorize_from_iterable(values):
def _factorize_from_iterables(iterables):
"""
A higher-level wrapper over `_factorize_from_iterable`.
See `_factorize_from_iterable` for more info.

*This is an internal function*

Parameters
----------
iterables : list-like of list-likes

Returns
-------
codes_tuple : tuple of ndarrays
categories_tuple : tuple of Indexes

Notes
-----
See `_factorize_from_iterable` for more info.
"""
if len(iterables) == 0:
# For consistency, it should return a list of 2 tuples.
return [(), ()]
return lzip(*[_factorize_from_iterable(it) for it in iterables])
63 changes: 63 additions & 0 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,32 @@ def test_from_arrays_index_series_categorical(self):
tm.assert_index_equal(result3.get_level_values(0), idx1)
tm.assert_index_equal(result3.get_level_values(1), idx2)

def test_from_arrays_empty(self):
# 0 levels
with tm.assertRaisesRegexp(
ValueError, "Must pass non-zero number of levels/labels"):
MultiIndex.from_arrays(arrays=[])

# 1 level
result = MultiIndex.from_arrays(arrays=[[]], names=['A'])
expected = Index([], name='A')
tm.assert_index_equal(result, expected)

# N levels
for N in [2, 3]:
arrays = [[]] * N
names = list('ABC')[:N]
result = MultiIndex.from_arrays(arrays=arrays, names=names)
expected = MultiIndex(levels=[np.array([])] * N, labels=[[]] * N,
names=names)
tm.assert_index_equal(result, expected)

def test_from_arrays_invalid_input(self):
invalid_inputs = [1, [1], [1, 2], [[1], 2],
'a', ['a'], ['a', 'b'], [['a'], 'b']]
for i in invalid_inputs:
tm.assertRaises(TypeError, MultiIndex.from_arrays, arrays=i)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not the most descriptive test...
TypeError is raised at different places for different inputs. I couldn't quickly think of anything better and clean enough - but can try to improve it if needed.


def test_from_arrays_different_lengths(self):
# GH13599
idx1 = [1, 2, 3]
Expand Down Expand Up @@ -723,6 +749,43 @@ def test_from_product(self):
tm.assert_index_equal(result, expected)
self.assertEqual(result.names, names)

def test_from_product_empty(self):
# 0 levels
with tm.assertRaisesRegexp(
ValueError, "Must pass non-zero number of levels/labels"):
MultiIndex.from_product([])

# 1 level
result = MultiIndex.from_product([[]], names=['A'])
expected = pd.Float64Index([], name='A')
tm.assert_index_equal(result, expected)

# 2 levels
l1 = [[], ['foo', 'bar', 'baz'], []]
l2 = [[], [], ['a', 'b', 'c']]
names = ['A', 'B']
for first, second in zip(l1, l2):
result = MultiIndex.from_product([first, second], names=names)
expected = MultiIndex(levels=[np.array(first), np.array(second)],
labels=[[], []], names=names)
tm.assert_index_equal(result, expected)

# GH12258
names = ['A', 'B', 'C']
for N in range(4):
lvl2 = lrange(N)
result = MultiIndex.from_product([[], lvl2, []], names=names)
expected = MultiIndex(levels=[np.array(A)
for A in [[], lvl2, []]],
labels=[[], [], []], names=names)
tm.assert_index_equal(result, expected)

def test_from_product_invalid_input(self):
invalid_inputs = [1, [1], [1, 2], [[1], 2],
'a', ['a'], ['a', 'b'], [['a'], 'b']]
for i in invalid_inputs:
tm.assertRaises(TypeError, MultiIndex.from_product, iterables=i)

def test_from_product_datetimeindex(self):
dt_index = date_range('2000-01-01', periods=2)
mi = pd.MultiIndex.from_product([[1, 2], dt_index])
Expand Down
23 changes: 23 additions & 0 deletions pandas/tools/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ def test_datetimeindex(self):
tm.assert_numpy_array_equal(result1, expected1)
tm.assert_numpy_array_equal(result2, expected2)

def test_empty(self):
# product of empty factors
X = [[], [0, 1], []]
Y = [[], [], ['a', 'b', 'c']]
for x, y in zip(X, Y):
expected1 = np.array([], dtype=np.asarray(x).dtype)
expected2 = np.array([], dtype=np.asarray(y).dtype)
result1, result2 = cartesian_product([x, y])
tm.assert_numpy_array_equal(result1, expected1)
tm.assert_numpy_array_equal(result2, expected2)

# empty product (empty input):
result = cartesian_product([])
expected = []
tm.assert_equal(result, expected)

def test_invalid_input(self):
invalid_inputs = [1, [1], [1, 2], [[1], 2],
'a', ['a'], ['a', 'b'], [['a'], 'b']]
msg = "Input must be a list-like of list-likes"
for X in invalid_inputs:
tm.assertRaisesRegexp(TypeError, msg, cartesian_product, X=X)


class TestLocaleUtils(tm.TestCase):

Expand Down
29 changes: 28 additions & 1 deletion pandas/tools/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pandas.types.common import (is_number,
is_numeric_dtype,
is_datetime_or_timedelta_dtype,
is_list_like,
_ensure_object)
from pandas.types.cast import _possibly_downcast_to_dtype

Expand All @@ -24,21 +25,47 @@ def cartesian_product(X):
Numpy version of itertools.product or pandas.compat.product.
Sometimes faster (for large inputs)...

Parameters
----------
X : list-like of list-likes

Returns
-------
product : list of ndarrays

Examples
--------
>>> cartesian_product([list('ABC'), [1, 2]])
[array(['A', 'A', 'B', 'B', 'C', 'C'], dtype='|S1'),
array([1, 2, 1, 2, 1, 2])]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try/except around this instead is more idomatic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't raise:

0/np.array([0,1])
Out[83]: array([ nan,   0.])
/opt/eclipse/plugins/org.python.pydev_5.1.2.201606231256/pysrc/pydevconsole.py:1: RuntimeWarning: invalid value encountered in true_divide

Hence this strange behaviour in #12258.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

See also
--------
itertools.product : Cartesian product of input iterables. Equivalent to
nested for-loops.
pandas.compat.product : An alias for itertools.product.
"""
msg = "Input must be a list-like of list-likes"
if not is_list_like(X):
raise TypeError(msg)
for x in X:
if not is_list_like(x):
raise TypeError(msg)

if len(X) == 0:
return []

lenX = np.fromiter((len(x) for x in X), dtype=int)
cumprodX = np.cumproduct(lenX)

a = np.roll(cumprodX, 1)
a[0] = 1

b = cumprodX[-1] / cumprodX
if cumprodX[-1] != 0:
b = cumprodX[-1] / cumprodX
else:
# if any factor is empty, the cartesian product is empty
b = np.zeros_like(cumprodX)

return [np.tile(np.repeat(np.asarray(com._values_from_object(x)), b[i]),
np.product(a[i]))
Expand Down