diff --git a/ci/code_checks.sh b/ci/code_checks.sh index b3d6c5156c54d..090bb7002e932 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -81,7 +81,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then -i "pandas.IntervalIndex.left GL08" \ -i "pandas.IntervalIndex.length GL08" \ -i "pandas.IntervalIndex.set_closed RT03,SA01" \ - -i "pandas.IntervalIndex.to_tuples RT03,SA01" \ -i "pandas.MultiIndex PR01" \ -i "pandas.MultiIndex.append PR07,SA01" \ -i "pandas.MultiIndex.copy PR07,RT03,SA01" \ @@ -408,7 +407,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then -i "pandas.arrays.IntervalArray.mid SA01" \ -i "pandas.arrays.IntervalArray.right SA01" \ -i "pandas.arrays.IntervalArray.set_closed RT03,SA01" \ - -i "pandas.arrays.IntervalArray.to_tuples RT03,SA01" \ -i "pandas.arrays.NumpyExtensionArray SA01" \ -i "pandas.arrays.SparseArray PR07,SA01" \ -i "pandas.arrays.TimedeltaArray PR07,SA01" \ diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 90138baac324a..e7205090ce9ff 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -1624,39 +1624,51 @@ def __arrow_array__(self, type=None): """ ) - @Appender( - _interval_shared_docs["to_tuples"] - % { - "return_type": ( - "ndarray (if self is IntervalArray) or Index (if self is IntervalIndex)" - ), - "examples": textwrap.dedent( - """\ - - Examples - -------- - For :class:`pandas.IntervalArray`: - - >>> idx = pd.arrays.IntervalArray.from_tuples([(0, 1), (1, 2)]) - >>> idx - - [(0, 1], (1, 2]] - Length: 2, dtype: interval[int64, right] - >>> idx.to_tuples() - array([(0, 1), (1, 2)], dtype=object) - - For :class:`pandas.IntervalIndex`: - - >>> idx = pd.interval_range(start=0, end=2) - >>> idx - IntervalIndex([(0, 1], (1, 2]], dtype='interval[int64, right]') - >>> idx.to_tuples() - Index([(0, 1), (1, 2)], dtype='object') - """ - ), - } - ) def to_tuples(self, na_tuple: bool = True) -> np.ndarray: + """ + Return an ndarray (if self is IntervalArray) or Index \ + (if self is IntervalIndex) of tuples of the form (left, right). + + Parameters + ---------- + na_tuple : bool, default True + If ``True``, return ``NA`` as a tuple ``(nan, nan)``. If ``False``, + just return ``NA`` as ``nan``. + + Returns + ------- + ndarray or Index + An ndarray of tuples representing the intervals + if `self` is an IntervalArray. + An Index of tuples representing the intervals + if `self` is an IntervalIndex. + + See Also + -------- + IntervalArray.to_list : Convert IntervalArray to a list of tuples. + IntervalArray.to_numpy : Convert IntervalArray to a numpy array. + IntervalArray.unique : Find unique intervals in an IntervalArray. + + Examples + -------- + For :class:`pandas.IntervalArray`: + + >>> idx = pd.arrays.IntervalArray.from_tuples([(0, 1), (1, 2)]) + >>> idx + + [(0, 1], (1, 2]] + Length: 2, dtype: interval[int64, right] + >>> idx.to_tuples() + array([(0, 1), (1, 2)], dtype=object) + + For :class:`pandas.IntervalIndex`: + + >>> idx = pd.interval_range(start=0, end=2) + >>> idx + IntervalIndex([(0, 1], (1, 2]], dtype='interval[int64, right]') + >>> idx.to_tuples() + Index([(0, 1), (1, 2)], dtype='object') + """ tuples = com.asarray_tuplesafe(zip(self._left, self._right)) if not na_tuple: # GH 18756