-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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])] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try/except around this instead is more idomatic There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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])) | ||
|
There was a problem hiding this comment.
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.