Skip to content

Commit

Permalink
DOC: Enforce Numpy Docstring Validation for pandas.IntervalIndex.to_t…
Browse files Browse the repository at this point in the history
…uples and pandas.arrays.IntervalArray.to_tuples (#58672)

* DOC: add RT03,SA01 to pandas.IntervalIndex.to_tuples

* DOC: remove RT03,SA01 to pandas.IntervalIndex.to_tuples
  • Loading branch information
tuhinsharma121 committed May 11, 2024
1 parent 81c68dd commit 8500629
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
2 changes: 0 additions & 2 deletions ci/code_checks.sh
Expand Up @@ -79,7 +79,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
-i "pandas.Index PR07" \
-i "pandas.IntervalIndex.left 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 @@ -405,7 +404,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
Expand Up @@ -1668,39 +1668,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

0 comments on commit 8500629

Please sign in to comment.