Skip to content

Commit

Permalink
API: correctly provide __name__ for cum functions
Browse files Browse the repository at this point in the history
closes #12021

Author: Jeff Reback <jeff@reback.net>

Closes #12372 from jreback/closure and squashes the following commits:

2630345 [Jeff Reback] CLN: remove pandas.util.misc CLN: move _fill_zeros -> missing.py CLN: rename missing.* methods w/o leading _
0f754b0 [Jeff Reback] API: correctly provide __name__, __qualname__ for cum functions
  • Loading branch information
jreback committed Apr 5, 2016
1 parent fded942 commit a69d628
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 171 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.1.txt
Expand Up @@ -103,6 +103,7 @@ API changes
- ``CParserError`` is now a ``ValueError`` instead of just an ``Exception`` (:issue:`12551`)

- ``pd.show_versions()`` now includes ``pandas_datareader`` version (:issue:`12740`)
- Provide a proper ``__name__`` and ``__qualname__`` attributes for generic functions (:issue:`12021`)

.. _whatsnew_0181.apply_resample:

Expand Down Expand Up @@ -170,6 +171,7 @@ Performance Improvements



- Bug in ``__name__`` of ``.cum*`` functions (:issue:`12021`)



Expand Down
18 changes: 18 additions & 0 deletions pandas/compat/__init__.py
Expand Up @@ -242,6 +242,15 @@ def import_lzma():
import lzma
return lzma

def set_function_name(f, name, cls):
""" Bind the name/qualname attributes of the function """
f.__name__ = name
f.__qualname__ = '{klass}.{name}'.format(
klass=cls.__name__,
name=name)
f.__module__ = cls.__module__
return f

else:
string_types = basestring,
integer_types = (int, long)
Expand Down Expand Up @@ -284,6 +293,11 @@ def import_lzma():
from backports import lzma
return lzma

def set_function_name(f, name, cls):
""" Bind the name attributes of the function """
f.__name__ = name
return f

string_and_binary_types = string_types + (binary_type,)


Expand Down Expand Up @@ -369,6 +383,10 @@ def __reduce__(self): # optional, for pickle support


# https://github.com/pydata/pandas/pull/9123
def is_platform_little_endian():
""" am I little endian """
return sys.byteorder == 'little'

def is_platform_windows():
return sys.platform == 'win32' or sys.platform == 'cygwin'

Expand Down
80 changes: 0 additions & 80 deletions pandas/core/common.py
Expand Up @@ -5,7 +5,6 @@
import re
import collections
import numbers
import types
from datetime import datetime, timedelta
from functools import partial

Expand Down Expand Up @@ -130,31 +129,6 @@ def __instancecheck__(cls, inst):
ABCGeneric = _ABCGeneric("ABCGeneric", tuple(), {})


def bind_method(cls, name, func):
"""Bind a method to class, python 2 and python 3 compatible.
Parameters
----------
cls : type
class to receive bound method
name : basestring
name of method on class instance
func : function
function to be bound as method
Returns
-------
None
"""
# only python 2 has bound/unbound method issue
if not compat.PY3:
setattr(cls, name, types.MethodType(func, None, cls))
else:
setattr(cls, name, func)


def isnull(obj):
"""Detect missing values (NaN in numeric arrays, None/NaN in object arrays)
Expand Down Expand Up @@ -1466,60 +1440,6 @@ def _lcd_dtypes(a_dtype, b_dtype):
return np.object


def _fill_zeros(result, x, y, name, fill):
"""
if this is a reversed op, then flip x,y
if we have an integer value (or array in y)
and we have 0's, fill them with the fill,
return the result
mask the nan's from x
"""
if fill is None or is_float_dtype(result):
return result

if name.startswith(('r', '__r')):
x, y = y, x

is_typed_variable = (hasattr(y, 'dtype') or hasattr(y, 'type'))
is_scalar = lib.isscalar(y)

if not is_typed_variable and not is_scalar:
return result

if is_scalar:
y = np.array(y)

if is_integer_dtype(y):

if (y == 0).any():

# GH 7325, mask and nans must be broadcastable (also: PR 9308)
# Raveling and then reshaping makes np.putmask faster
mask = ((y == 0) & ~np.isnan(result)).ravel()

shape = result.shape
result = result.astype('float64', copy=False).ravel()

np.putmask(result, mask, fill)

# if we have a fill of inf, then sign it correctly
# (GH 6178 and PR 9308)
if np.isinf(fill):
signs = np.sign(y if name.startswith(('r', '__r')) else x)
negative_inf_mask = (signs.ravel() < 0) & mask
np.putmask(result, negative_inf_mask, -fill)

if "floordiv" in name: # (PR 9308)
nan_mask = ((y == 0) & (x == 0)).ravel()
np.putmask(result, nan_mask, np.nan)

result = result.reshape(shape)

return result


def _consensus_name_attr(objs):
name = objs[0].name
for obj in objs[1:]:
Expand Down

0 comments on commit a69d628

Please sign in to comment.