Skip to content

Commit cc25182

Browse files
committed
Separate the example section from _interval_shared_docs
1 parent e7b865c commit cc25182

File tree

2 files changed

+127
-91
lines changed

2 files changed

+127
-91
lines changed

pandas/core/arrays/interval.py

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def _from_factorized(cls, values, original):
222222
values = values.astype(original.dtype.subtype)
223223
return cls(values, closed=original.closed)
224224

225-
_interval_shared_docs['from_breaks'] = """
225+
_interval_shared_docs['from_breaks'] = textwrap.dedent("""\
226226
Construct an %(klass)s from an array of splits.
227227
228228
Parameters
@@ -249,23 +249,28 @@ def _from_factorized(cls, values, original):
249249
%(klass)s.from_arrays : Construct from a left and right array.
250250
%(klass)s.from_tuples : Construct from a sequence of tuples.
251251
252-
Examples
253-
--------
254-
>>> pd.%(qualname)s.from_breaks([0, 1, 2, 3])
255-
%(klass)s([(0, 1], (1, 2], (2, 3]],
256-
closed='right',
257-
dtype='interval[int64]')
258-
"""
252+
%(examples)s\
253+
""")
259254

260255
@classmethod
261-
@Appender(_interval_shared_docs['from_breaks'] % _shared_docs_kwargs)
256+
@Appender(_interval_shared_docs['from_breaks'] % dict(
257+
klass='IntervalArray',
258+
examples=textwrap.dedent("""\
259+
Examples
260+
--------
261+
>>> pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3])
262+
<IntervalArray>
263+
[(0, 1], (1, 2], (2, 3]]
264+
Length: 3, closed: right, dtype: interval[int64]
265+
"""),
266+
))
262267
def from_breaks(cls, breaks, closed='right', copy=False, dtype=None):
263268
breaks = maybe_convert_platform_interval(breaks)
264269

265270
return cls.from_arrays(breaks[:-1], breaks[1:], closed, copy=copy,
266271
dtype=dtype)
267272

268-
_interval_shared_docs['from_arrays'] = """
273+
_interval_shared_docs['from_arrays'] = textwrap.dedent("""
269274
Construct from two arrays defining the left and right bounds.
270275
271276
Parameters
@@ -311,16 +316,19 @@ def from_breaks(cls, breaks, closed='right', copy=False, dtype=None):
311316
using an unsupported type for `left` or `right`. At the moment,
312317
'category', 'object', and 'string' subtypes are not supported.
313318
314-
Examples
315-
--------
316-
>>> %(klass)s.from_arrays([0, 1, 2], [1, 2, 3])
317-
%(klass)s([(0, 1], (1, 2], (2, 3]],
318-
closed='right',
319-
dtype='interval[int64]')
320-
"""
319+
%(examples)s\
320+
""")
321321

322322
@classmethod
323-
@Appender(_interval_shared_docs['from_arrays'] % _shared_docs_kwargs)
323+
@Appender(_interval_shared_docs['from_arrays'] % dict(
324+
klass='IntervalArray',
325+
examples=textwrap.dedent("""\
326+
>>> pd.arrays.IntervalArray.from_arrays([0, 1, 2], [1, 2, 3])
327+
<IntervalArray>
328+
[(0, 1], (1, 2], (2, 3]]
329+
Length: 3, closed: right, dtype: interval[int64]
330+
"""),
331+
))
324332
def from_arrays(cls, left, right, closed='right', copy=False, dtype=None):
325333
left = maybe_convert_platform_interval(left)
326334
right = maybe_convert_platform_interval(right)
@@ -370,7 +378,7 @@ def from_arrays(cls, left, right, closed='right', copy=False, dtype=None):
370378
closed='right', dtype='interval[int64]')
371379
"""
372380

373-
_interval_shared_docs['from_tuples'] = """
381+
_interval_shared_docs['from_tuples'] = textwrap.dedent("""\
374382
Construct an %(klass)s from an array-like of tuples
375383
376384
Parameters
@@ -399,15 +407,21 @@ def from_arrays(cls, left, right, closed='right', copy=False, dtype=None):
399407
%(klass)s.from_breaks : Construct an %(klass)s from an array of
400408
splits.
401409
402-
Examples
403-
--------
404-
>>> pd.%(qualname)s.from_tuples([(0, 1), (1, 2)])
405-
%(klass)s([(0, 1], (1, 2]],
406-
closed='right', dtype='interval[int64]')
407-
"""
410+
%(examples)s\
411+
""")
408412

409413
@classmethod
410-
@Appender(_interval_shared_docs['from_tuples'] % _shared_docs_kwargs)
414+
@Appender(_interval_shared_docs['from_tuples'] % dict(
415+
klass='IntervalArray',
416+
examples=textwrap.dedent("""\
417+
Examples
418+
--------
419+
>>> pd.arrays.IntervalArray.from_tuples([(0, 1), (1, 2)])
420+
<IntervalArray>
421+
[(0, 1], (1, 2]]
422+
Length: 2, closed: right, dtype: interval[int64]
423+
"""),
424+
))
411425
def from_tuples(cls, data, closed='right', copy=False, dtype=None):
412426
if len(data):
413427
left, right = [], []
@@ -886,7 +900,7 @@ def closed(self):
886900
"""
887901
return self._closed
888902

889-
_interval_shared_docs['set_closed'] = """
903+
_interval_shared_docs['set_closed'] = textwrap.dedent("""\
890904
Return an %(klass)s identical to the current one, but closed on the
891905
specified side
892906
@@ -902,20 +916,25 @@ def closed(self):
902916
-------
903917
new_index : %(klass)s
904918
919+
%(examples)s\
920+
""")
921+
922+
@Appender(_interval_shared_docs['set_closed'] % dict(
923+
klass='IntervalArray',
924+
examples=textwrap.dedent("""\
905925
Examples
906926
--------
907-
>>> index = pd.interval_range(0, 3)
927+
>>> index = pd.arrays.IntervalArray.from_breaks(range(4))
908928
>>> index
909-
IntervalIndex([(0, 1], (1, 2], (2, 3]],
910-
closed='right',
911-
dtype='interval[int64]')
929+
<IntervalArray>
930+
[(0, 1], (1, 2], (2, 3]]
931+
Length: 3, closed: right, dtype: interval[int64]
912932
>>> index.set_closed('both')
913-
IntervalIndex([[0, 1], [1, 2], [2, 3]],
914-
closed='both',
915-
dtype='interval[int64]')
916-
"""
917-
918-
@Appender(_interval_shared_docs['set_closed'] % _shared_docs_kwargs)
933+
<IntervalArray>
934+
[[0, 1], [1, 2], [2, 3]]
935+
Length: 3, closed: both, dtype: interval[int64]
936+
"""),
937+
))
919938
def set_closed(self, closed):
920939
if closed not in _VALID_CLOSED:
921940
msg = "invalid option for 'closed': {closed}"
@@ -1028,7 +1047,7 @@ def repeat(self, repeats, axis=None):
10281047
right_repeat = self.right.repeat(repeats)
10291048
return self._shallow_copy(left=left_repeat, right=right_repeat)
10301049

1031-
_interval_shared_docs['overlaps'] = """
1050+
_interval_shared_docs['overlaps'] = textwrap.dedent("""
10321051
Check elementwise if an Interval overlaps the values in the %(klass)s.
10331052
10341053
Two intervals overlap if they share a common point, including closed
@@ -1039,7 +1058,7 @@ def repeat(self, repeats, axis=None):
10391058
10401059
Parameters
10411060
----------
1042-
other : Interval
1061+
other : %(klass)s
10431062
Interval to check against for an overlap.
10441063
10451064
Returns
@@ -1053,11 +1072,7 @@ def repeat(self, repeats, axis=None):
10531072
10541073
Examples
10551074
--------
1056-
>>> intervals = pd.%(qualname)s.from_tuples([(0, 1), (1, 3), (2, 4)])
1057-
>>> intervals
1058-
<%(klass)s>
1059-
[(0, 1], (1, 3], (2, 4]]
1060-
Length: 3, closed: right, dtype: interval[int64]
1075+
%(examples)s
10611076
10621077
>>> intervals.overlaps(pd.Interval(0.5, 1.5))
10631078
array([ True, True, False])
@@ -1071,9 +1086,20 @@ def repeat(self, repeats, axis=None):
10711086
10721087
>>> intervals.overlaps(pd.Interval(1, 2, closed='right'))
10731088
array([False, True, False])
1074-
"""
10751089
1076-
@Appender(_interval_shared_docs['overlaps'] % _shared_docs_kwargs)
1090+
""")
1091+
1092+
@Appender(_interval_shared_docs['overlaps'] % dict(
1093+
klass='IntervalArray',
1094+
examples=textwrap.dedent("""\
1095+
>>> intervals = pd.arrays.IntervalArray.from_tuples([(0, 1), (1, 3),\
1096+
(2, 4)])
1097+
>>> intervals
1098+
<IntervalArray>
1099+
[(0, 1], (1, 3], (2, 4]]
1100+
Length: 3, closed: right, dtype: interval[int64]
1101+
"""),
1102+
))
10771103
def overlaps(self, other):
10781104
if isinstance(other, (IntervalArray, ABCIntervalIndex)):
10791105
raise NotImplementedError

pandas/core/indexes/interval.py

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,17 @@ def _simple_new(cls, array, name, closed=None):
206206
return result
207207

208208
@classmethod
209-
@Appender(_interval_shared_docs['from_breaks'] % _index_doc_kwargs)
209+
@Appender(_interval_shared_docs['from_breaks'] % dict(
210+
klass='IntervalIndex',
211+
examples=textwrap.dedent("""\
212+
Examples
213+
--------
214+
>>> pd.IntervalIndex.from_breaks([0, 1, 2, 3])
215+
IntervalIndex([(0, 1], (1, 2], (2, 3]],
216+
closed='right',
217+
dtype='interval[int64]')
218+
"""),
219+
))
210220
def from_breaks(cls, breaks, closed='right', name=None, copy=False,
211221
dtype=None):
212222
with rewrite_exception("IntervalArray", cls.__name__):
@@ -215,7 +225,17 @@ def from_breaks(cls, breaks, closed='right', name=None, copy=False,
215225
return cls._simple_new(array, name=name)
216226

217227
@classmethod
218-
@Appender(_interval_shared_docs['from_arrays'] % _index_doc_kwargs)
228+
@Appender(_interval_shared_docs['from_arrays'] % dict(
229+
klass='IntervalIndex',
230+
examples=textwrap.dedent("""\
231+
Examples
232+
--------
233+
>>> pd.IntervalIndex.from_arrays([0, 1, 2], [1, 2, 3])
234+
IntervalIndex([(0, 1], (1, 2], (2, 3]],
235+
closed='right',
236+
dtype='interval[int64]')
237+
"""),
238+
))
219239
def from_arrays(cls, left, right, closed='right', name=None, copy=False,
220240
dtype=None):
221241
with rewrite_exception("IntervalArray", cls.__name__):
@@ -239,7 +259,17 @@ def from_intervals(cls, data, closed=None, name=None, copy=False,
239259
return cls._simple_new(array, name=name)
240260

241261
@classmethod
242-
@Appender(_interval_shared_docs['from_tuples'] % _index_doc_kwargs)
262+
@Appender(_interval_shared_docs['from_tuples'] % dict(
263+
klass='IntervalIndex',
264+
examples=textwrap.dedent("""\
265+
Examples
266+
--------
267+
>>> pd.IntervalIndex.from_tuples([(0, 1), (1, 2)])
268+
IntervalIndex([(0, 1], (1, 2]],
269+
closed='right',
270+
dtype='interval[int64]')
271+
"""),
272+
))
243273
def from_tuples(cls, data, closed='right', name=None, copy=False,
244274
dtype=None):
245275
with rewrite_exception("IntervalArray", cls.__name__):
@@ -356,7 +386,22 @@ def closed(self):
356386
"""
357387
return self._data._closed
358388

359-
@Appender(_interval_shared_docs['set_closed'] % _index_doc_kwargs)
389+
@Appender(_interval_shared_docs['set_closed'] % dict(
390+
klass='IntervalIndex',
391+
examples=textwrap.dedent("""\
392+
Examples
393+
--------
394+
>>> index = pd.interval_range(0, 3)
395+
>>> index
396+
IntervalIndex([(0, 1], (1, 2], (2, 3]],
397+
closed='right',
398+
dtype='interval[int64]')
399+
>>> index.set_closed('both')
400+
IntervalIndex([[0, 1], [1, 2], [2, 3]],
401+
closed='both',
402+
dtype='interval[int64]')
403+
"""),
404+
))
360405
def set_closed(self, closed):
361406
if closed not in _VALID_CLOSED:
362407
msg = "invalid option for 'closed': {closed}"
@@ -1134,52 +1179,17 @@ def equals(self, other):
11341179
self.right.equals(other.right) and
11351180
self.closed == other.closed)
11361181

1137-
def overlaps(self, other):
1138-
"""
1139-
Check elementwise if an Interval overlaps the values in the
1140-
IntervalIndex.
1141-
1142-
Two intervals overlap if they share a common point, including closed
1143-
endpoints. Intervals that only have an open endpoint in common do not
1144-
overlap.
1145-
1146-
.. versionadded:: 0.24.0
1147-
1148-
Parameters
1149-
----------
1150-
other : Interval
1151-
Interval to check against for an overlap.
1152-
1153-
Returns
1154-
-------
1155-
ndarray
1156-
Boolean array positionally indicating where an overlap occurs.
1157-
1158-
See Also
1159-
--------
1160-
Interval.overlaps : Check whether two Interval objects overlap.
1161-
1162-
Examples
1163-
--------
1182+
@Appender(_interval_shared_docs['overlaps'] % dict(
1183+
klass='IntervalIndex',
1184+
examples=textwrap.dedent("""\
11641185
>>> intervals = pd.IntervalIndex.from_tuples([(0, 1), (1, 3), (2, 4)])
11651186
>>> intervals
11661187
IntervalIndex([(0, 1], (1, 3], (2, 4]],
11671188
closed='right',
11681189
dtype='interval[int64]')
1169-
1170-
>>> intervals.overlaps(pd.Interval(0.5, 1.5))
1171-
array([ True, True, False])
1172-
1173-
Intervals that share closed endpoints overlap:
1174-
1175-
>>> intervals.overlaps(pd.Interval(1, 3, closed='left'))
1176-
array([ True, True, True])
1177-
1178-
Intervals that only have an open endpoint in common do not overlap:
1179-
1180-
>>> intervals.overlaps(pd.Interval(1, 2, closed='right'))
1181-
array([False, True, False])
1182-
"""
1190+
"""),
1191+
))
1192+
def overlaps(self, other):
11831193
return self._data.overlaps(other)
11841194

11851195
@Appender(_index_shared_docs['intersection'])

0 commit comments

Comments
 (0)