Skip to content

DOC: Enforce Numpy Docstring Validation for pandas.IntervalIndex.to_tuples and pandas.arrays.IntervalArray.to_tuples #58672

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

Merged
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
2 changes: 0 additions & 2 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down Expand Up @@ -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" \
Expand Down
76 changes: 44 additions & 32 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
<IntervalArray>
[(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
<IntervalArray>
[(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
Expand Down
Loading