Skip to content

Commit b45fe1f

Browse files
WillAyddavid-liu-brattle-1
authored andcommitted
BUG: Support for OO Optimization (pandas-dev#21093)
1 parent dec1e54 commit b45fe1f

File tree

4 files changed

+44
-27
lines changed

4 files changed

+44
-27
lines changed

doc/source/whatsnew/v0.23.1.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ Documentation Changes
4646
Bug Fixes
4747
~~~~~~~~~
4848

49-
- tab completion on :class:`Index` in IPython no longer outputs deprecation warnings (:issue:`21125`)
50-
5149
Groupby/Resample/Rolling
5250
^^^^^^^^^^^^^^^^^^^^^^^^
5351

@@ -101,3 +99,9 @@ Reshaping
10199

102100
- Bug in :func:`concat` where error was raised in concatenating :class:`Series` with numpy scalar and tuple names (:issue:`21015`)
103101
-
102+
103+
Other
104+
^^^^^
105+
106+
- Tab completion on :class:`Index` in IPython no longer outputs deprecation warnings (:issue:`21125`)
107+
- Bug preventing pandas from being importable with -OO optimization (:issue:`21071`)

pandas/tests/test_downstream.py

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"""
33
Testing that we work in the downstream packages
44
"""
5+
import subprocess
6+
57
import pytest
68
import numpy as np # noqa
79
from pandas import DataFrame
@@ -53,6 +55,11 @@ def test_xarray(df):
5355
assert df.to_xarray() is not None
5456

5557

58+
def test_oo_optimizable():
59+
# GH 21071
60+
subprocess.check_call(["python", "-OO", "-c", "import pandas"])
61+
62+
5663
@tm.network
5764
def test_statsmodels():
5865

pandas/tseries/offsets.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1090,12 +1090,17 @@ def apply(self, other):
10901090

10911091

10921092
class CustomBusinessMonthEnd(_CustomBusinessMonth):
1093-
__doc__ = _CustomBusinessMonth.__doc__.replace('[BEGIN/END]', 'end')
1093+
# TODO(py27): Replace condition with Subsitution after dropping Py27
1094+
if _CustomBusinessMonth.__doc__:
1095+
__doc__ = _CustomBusinessMonth.__doc__.replace('[BEGIN/END]', 'end')
10941096
_prefix = 'CBM'
10951097

10961098

10971099
class CustomBusinessMonthBegin(_CustomBusinessMonth):
1098-
__doc__ = _CustomBusinessMonth.__doc__.replace('[BEGIN/END]', 'beginning')
1100+
# TODO(py27): Replace condition with Subsitution after dropping Py27
1101+
if _CustomBusinessMonth.__doc__:
1102+
__doc__ = _CustomBusinessMonth.__doc__.replace('[BEGIN/END]',
1103+
'beginning')
10991104
_prefix = 'CBMS'
11001105

11011106

pandas/util/_decorators.py

+24-23
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import types
55
import warnings
66
from textwrap import dedent, wrap
7-
from functools import wraps, update_wrapper
7+
from functools import wraps, update_wrapper, WRAPPER_ASSIGNMENTS
88

99

1010
def deprecate(name, alternative, version, alt_name=None,
@@ -20,44 +20,45 @@ def deprecate(name, alternative, version, alt_name=None,
2020
Parameters
2121
----------
2222
name : str
23-
Name of function to deprecate
24-
alternative : str
25-
Name of function to use instead
23+
Name of function to deprecate.
24+
alternative : func
25+
Function to use instead.
2626
version : str
27-
Version of pandas in which the method has been deprecated
27+
Version of pandas in which the method has been deprecated.
2828
alt_name : str, optional
29-
Name to use in preference of alternative.__name__
29+
Name to use in preference of alternative.__name__.
3030
klass : Warning, default FutureWarning
3131
stacklevel : int, default 2
3232
msg : str
33-
The message to display in the warning.
34-
Default is '{name} is deprecated. Use {alt_name} instead.'
33+
The message to display in the warning.
34+
Default is '{name} is deprecated. Use {alt_name} instead.'
3535
"""
3636

3737
alt_name = alt_name or alternative.__name__
3838
klass = klass or FutureWarning
3939
warning_msg = msg or '{} is deprecated, use {} instead'.format(name,
4040
alt_name)
4141

42-
@wraps(alternative)
42+
# adding deprecated directive to the docstring
43+
msg = msg or 'Use `{alt_name}` instead.'.format(alt_name=alt_name)
44+
msg = '\n '.join(wrap(msg, 70))
45+
46+
@Substitution(version=version, msg=msg)
47+
@Appender(alternative.__doc__)
4348
def wrapper(*args, **kwargs):
49+
"""
50+
.. deprecated:: %(version)s
51+
52+
%(msg)s
53+
54+
"""
4455
warnings.warn(warning_msg, klass, stacklevel=stacklevel)
4556
return alternative(*args, **kwargs)
4657

47-
# adding deprecated directive to the docstring
48-
msg = msg or 'Use `{alt_name}` instead.'.format(alt_name=alt_name)
49-
tpl = dedent("""
50-
.. deprecated:: {version}
51-
52-
{msg}
53-
54-
{rest}
55-
""")
56-
rest = getattr(wrapper, '__doc__', '')
57-
docstring = tpl.format(version=version,
58-
msg='\n '.join(wrap(msg, 70)),
59-
rest=dedent(rest))
60-
wrapper.__doc__ = docstring
58+
# Since we are using Substitution to create the required docstring,
59+
# remove that from the attributes that should be assigned to the wrapper
60+
assignments = tuple(x for x in WRAPPER_ASSIGNMENTS if x != '__doc__')
61+
update_wrapper(wrapper, alternative, assigned=assignments)
6162

6263
return wrapper
6364

0 commit comments

Comments
 (0)