from __future__ import annotations from typing import TYPE_CHECKING import numpy as np from pandas.core.dtypes.common import is_list_like if TYPE_CHECKING: from pandas._typing import NumpyIndexT def cartesian_product(X) -> list[np.ndarray]: """ Numpy version of itertools.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=' NumpyIndexT: """ Index compat for np.tile. Notes ----- Does not support multi-dimensional `num`. """ if isinstance(arr, np.ndarray): return np.tile(arr, num) # Otherwise we have an Index taker = np.tile(np.arange(len(arr)), num) return arr.take(taker)