Skip to content

Commit 4e48e88

Browse files
committed
Py2 compat
Writable docstrings. Removed lambda as class attribute.
1 parent abb8a45 commit 4e48e88

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

pandas/compat/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,11 @@ def is_platform_mac():
451451

452452
def is_platform_32bit():
453453
return struct.calcsize("P") * 8 < 64
454+
455+
456+
class _WritableDoc(type):
457+
# Remove this when Python2 support is dropped
458+
# __doc__ is not mutable for new-style classes in Python2, which means
459+
# we can't use @Appender to share class docstrings. This can be used
460+
# with `add_metaclass` to make cls.__doc__ mutable.
461+
pass

pandas/core/arrays/interval.py

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from pandas._libs.interval import (Interval, IntervalMixin,
55
intervals_to_interval_bounds)
6+
from pandas.compat import add_metaclass, _WritableDoc
67
from pandas.compat.numpy import function as nv
78
from pandas.core.common import _all_not_none, _asarray_tuplesafe
89
from pandas.core.config import get_option
@@ -95,6 +96,7 @@
9596
versionadded="0.23.0",
9697
name='', extra_methods='', examples='',
9798
))
99+
@add_metaclass(_WritableDoc)
98100
class IntervalArray(IntervalMixin, ExtensionArray):
99101
dtype = IntervalDtype()
100102
ndim = 1

pandas/core/indexes/interval.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import numpy as np
66

7+
from pandas.compat import add_metaclass, _WritableDoc
78
from pandas.core.dtypes.missing import isna
89
from pandas.core.dtypes.cast import find_common_type
910
from pandas.core.dtypes.common import (
@@ -130,13 +131,12 @@ def _new_IntervalIndex(cls, d):
130131
"""),
131132
132133
))
134+
@add_metaclass(_WritableDoc)
133135
class IntervalIndex(IntervalMixin, Index):
134136
_typ = 'intervalindex'
135137
_comparables = ['name']
136138
_attributes = ['name', 'closed']
137139
_allow_index_ops = True
138-
_exception_rewrite = lambda: rewrite_exception('IntervalArray',
139-
'IntervalIndex')
140140

141141
# we would like our indexing holder to defer to us
142142
_defer_to_indexing = True
@@ -153,7 +153,7 @@ def __new__(cls, data, closed=None, dtype=None, copy=False,
153153
if name is None and hasattr(data, 'name'):
154154
name = data.name
155155

156-
with cls._exception_rewrite():
156+
with rewrite_exception("IntervalArray", cls.__name__):
157157
array = IntervalArray(data, closed=closed, copy=copy, dtype=dtype,
158158
fastpath=fastpath,
159159
verify_integrity=verify_integrity)
@@ -274,7 +274,7 @@ def from_intervals(cls, data, closed=None, name=None, copy=False,
274274
msg = ('IntervalIndex.from_intervals is deprecated and will be '
275275
'removed in a future version; Use IntervalIndex(...) instead')
276276
warnings.warn(msg, FutureWarning, stacklevel=2)
277-
with cls._exception_rewrite():
277+
with rewrite_exception("IntervalArray", cls.__name__):
278278
array = IntervalArray(data, closed=closed, copy=copy, dtype=dtype)
279279

280280
if name is None and isinstance(data, cls):
@@ -286,7 +286,7 @@ def from_intervals(cls, data, closed=None, name=None, copy=False,
286286
@Appender(_interval_shared_docs['from_tuples'] % _index_doc_kwargs)
287287
def from_tuples(cls, data, closed='right', name=None, copy=False,
288288
dtype=None):
289-
with cls._exception_rewrite():
289+
with rewrite_exception("IntervalArray", cls.__name__):
290290
arr = IntervalArray.from_tuples(data, closed=closed, copy=copy,
291291
dtype=dtype)
292292
return cls._simple_new(arr, name=name)

0 commit comments

Comments
 (0)