Skip to content

Commit c2ab939

Browse files
committed
Use new repr and fix docstring
1 parent 23b6115 commit c2ab939

File tree

2 files changed

+76
-15
lines changed

2 files changed

+76
-15
lines changed

pandas/core/arrays/interval.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@
116116
``Interval`` objects:
117117
118118
>>> pd.arrays.IntervalArray([pd.Interval(0, 1), pd.Interval(1, 5)])
119-
IntervalArray([(0, 1], (1, 5]],
120-
closed='right',
121-
dtype='interval[int64]')
119+
<IntervalArray>
120+
[(0, 1], (1, 5]]
121+
Length: 2, closed: right, dtype: interval[int64]
122122
123123
It may also be constructed using one of the constructor
124124
methods: :meth:`IntervalArray.from_arrays`,
@@ -842,15 +842,31 @@ def _format_data(self):
842842

843843
return summary
844844

845+
#def __repr__(self):
846+
# tpl = textwrap.dedent("""\
847+
# {cls}({data},
848+
# {lead}closed='{closed}',
849+
# {lead}dtype='{dtype}')""")
850+
# return tpl.format(cls=self.__class__.__name__,
851+
# data=self._format_data(),
852+
# lead=' ' * len(self.__class__.__name__) + ' ',
853+
# closed=self.closed, dtype=self.dtype)
854+
845855
def __repr__(self):
846-
tpl = textwrap.dedent("""\
847-
{cls}({data},
848-
{lead}closed='{closed}',
849-
{lead}dtype='{dtype}')""")
850-
return tpl.format(cls=self.__class__.__name__,
851-
data=self._format_data(),
852-
lead=' ' * len(self.__class__.__name__) + ' ',
853-
closed=self.closed, dtype=self.dtype)
856+
template = (
857+
'{class_name}'
858+
'{data}\n'
859+
'Length: {length}, closed: {closed}, dtype: {dtype}'
860+
)
861+
# the short repr has no trailing newline, while the truncated
862+
# repr does. So we include a newline in our template, and strip
863+
# any trailing newlines from format_object_summary
864+
data = self._format_data()
865+
class_name = '<{}>\n'.format(self.__class__.__name__)
866+
return template.format(class_name=class_name, data=data,
867+
length=len(self),
868+
closed=self.closed,
869+
dtype=self.dtype)
854870

855871
def _format_space(self):
856872
space = ' ' * (len(self.__class__.__name__) + 1)
@@ -1049,9 +1065,10 @@ def repeat(self, repeats, axis=None):
10491065
--------
10501066
>>> intervals = pd.%(qualname)s.from_tuples([(0, 1), (1, 3), (2, 4)])
10511067
>>> intervals
1052-
%(klass)s([(0, 1], (1, 3], (2, 4]],
1053-
closed='right',
1054-
dtype='interval[int64]')
1068+
<%(klass)s>
1069+
[(0, 1], (1, 3], (2, 4]]
1070+
Length: 3, closed: right, dtype: interval[int64]
1071+
10551072
>>> intervals.overlaps(pd.Interval(0.5, 1.5))
10561073
array([ True, True, False])
10571074

pandas/core/indexes/interval.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,52 @@ def equals(self, other):
10861086
self.right.equals(other.right) and
10871087
self.closed == other.closed)
10881088

1089-
@Appender(_interval_shared_docs['overlaps'] % _index_doc_kwargs)
10901089
def overlaps(self, other):
1090+
"""
1091+
Check elementwise if an Interval overlaps the values in the
1092+
IntervalIndex.
1093+
1094+
Two intervals overlap if they share a common point, including closed
1095+
endpoints. Intervals that only have an open endpoint in common do not
1096+
overlap.
1097+
1098+
.. versionadded:: 0.24.0
1099+
1100+
Parameters
1101+
----------
1102+
other : Interval
1103+
Interval to check against for an overlap.
1104+
1105+
Returns
1106+
-------
1107+
ndarray
1108+
Boolean array positionally indicating where an overlap occurs.
1109+
1110+
See Also
1111+
--------
1112+
Interval.overlaps : Check whether two Interval objects overlap.
1113+
1114+
Examples
1115+
--------
1116+
>>> intervals = pd.IntervalIndex.from_tuples([(0, 1), (1, 3), (2, 4)])
1117+
>>> intervals
1118+
IntervalIndex([(0, 1], (1, 3], (2, 4]],
1119+
closed='right',
1120+
dtype='interval[int64]')
1121+
1122+
>>> intervals.overlaps(pd.Interval(0.5, 1.5))
1123+
array([ True, True, False])
1124+
1125+
Intervals that share closed endpoints overlap:
1126+
1127+
>>> intervals.overlaps(pd.Interval(1, 3, closed='left'))
1128+
array([ True, True, True])
1129+
1130+
Intervals that only have an open endpoint in common do not overlap:
1131+
1132+
>>> intervals.overlaps(pd.Interval(1, 2, closed='right'))
1133+
array([False, True, False])
1134+
"""
10911135
return self._data.overlaps(other)
10921136

10931137
def _setop(op_name, sort=None):

0 commit comments

Comments
 (0)