Skip to content

Commit

Permalink
CLN: remove pandas.util.misc
Browse files Browse the repository at this point in the history
CLN: move _fill_zeros -> missing.py
CLN: rename missing.* methods w/o leading _
  • Loading branch information
jreback committed Apr 5, 2016
1 parent 0f754b0 commit 2630345
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 153 deletions.
18 changes: 18 additions & 0 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
35 changes: 13 additions & 22 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
from pandas.core.internals import BlockManager
import pandas.core.algorithms as algos
import pandas.core.common as com
import pandas.core.missing as mis
import pandas.core.missing as missing
import pandas.core.datetools as datetools
from pandas import compat
from pandas.compat import map, zip, lrange, string_types, isidentifier, PY3
from pandas.compat import (map, zip, lrange, string_types,
isidentifier, set_function_name)
from pandas.core.common import (isnull, notnull, is_list_like,
_values_from_object, _maybe_promote,
_maybe_box_datetimelike, ABCSeries,
Expand Down Expand Up @@ -51,7 +52,7 @@ def _single_replace(self, to_replace, method, inplace, limit):

orig_dtype = self.dtype
result = self if inplace else self.copy()
fill_f = mis._get_fill_func(method)
fill_f = missing.get_fill_func(method)

mask = com.mask_missing(result.values, to_replace)
values = fill_f(result.values, limit=limit, mask=mask)
Expand Down Expand Up @@ -2189,7 +2190,7 @@ def reindex(self, *args, **kwargs):

# construct the args
axes, kwargs = self._construct_axes_from_arguments(args, kwargs)
method = mis._clean_reindex_fill_method(kwargs.pop('method', None))
method = missing.clean_reindex_fill_method(kwargs.pop('method', None))
level = kwargs.pop('level', None)
copy = kwargs.pop('copy', True)
limit = kwargs.pop('limit', None)
Expand Down Expand Up @@ -2304,7 +2305,7 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True,

axis_name = self._get_axis_name(axis)
axis_values = self._get_axis(axis_name)
method = mis._clean_reindex_fill_method(method)
method = missing.clean_reindex_fill_method(method)
new_index, indexer = axis_values.reindex(labels, method, level,
limit=limit)
return self._reindex_with_indexers({axis: [new_index, indexer]},
Expand Down Expand Up @@ -3099,7 +3100,7 @@ def fillna(self, value=None, method=None, axis=None, inplace=False,
if axis is None:
axis = 0
axis = self._get_axis_number(axis)
method = mis._clean_fill_method(method)
method = missing.clean_fill_method(method)

from pandas import DataFrame
if value is None:
Expand Down Expand Up @@ -3132,7 +3133,7 @@ def fillna(self, value=None, method=None, axis=None, inplace=False,

else:
# 2d or less
method = mis._clean_fill_method(method)
method = missing.clean_fill_method(method)
new_data = self._data.interpolate(method=method, axis=axis,
limit=limit, inplace=inplace,
coerce=True,
Expand Down Expand Up @@ -4121,7 +4122,7 @@ def align(self, other, join='outer', axis=None, level=None, copy=True,
fill_value=None, method=None, limit=None, fill_axis=0,
broadcast_axis=None):
from pandas import DataFrame, Series
method = mis._clean_fill_method(method)
method = missing.clean_fill_method(method)

if broadcast_axis == 1 and self.ndim != other.ndim:
if isinstance(self, Series):
Expand Down Expand Up @@ -5238,16 +5239,6 @@ def _doc_parms(cls):
%(outname)s : %(name1)s\n"""


def _set_function_name(f, name, cls):
f.__name__ = name
if PY3:
f.__qualname__ = '{klass}.{name}'.format(
klass=cls.__name__,
name=name)
f.__module__ = cls.__module__
return f


def _make_stat_function(cls, name, name1, name2, axis_descr, desc, f):
@Substitution(outname=name, desc=desc, name1=name1, name2=name2,
axis_descr=axis_descr)
Expand All @@ -5265,7 +5256,7 @@ def stat_func(self, axis=None, skipna=None, level=None, numeric_only=None,
return self._reduce(f, name, axis=axis, skipna=skipna,
numeric_only=numeric_only)

return _set_function_name(stat_func, name, cls)
return set_function_name(stat_func, name, cls)


def _make_stat_function_ddof(cls, name, name1, name2, axis_descr, desc, f):
Expand All @@ -5285,7 +5276,7 @@ def stat_func(self, axis=None, skipna=None, level=None, ddof=1,
return self._reduce(f, name, axis=axis, numeric_only=numeric_only,
skipna=skipna, ddof=ddof)

return _set_function_name(stat_func, name, cls)
return set_function_name(stat_func, name, cls)


def _make_cum_function(cls, name, name1, name2, axis_descr, desc, accum_func,
Expand Down Expand Up @@ -5320,7 +5311,7 @@ def cum_func(self, axis=None, dtype=None, out=None, skipna=True, **kwargs):
d['copy'] = False
return self._constructor(result, **d).__finalize__(self)

return _set_function_name(cum_func, name, cls)
return set_function_name(cum_func, name, cls)


def _make_logical_function(cls, name, name1, name2, axis_descr, desc, f):
Expand All @@ -5344,7 +5335,7 @@ def logical_func(self, axis=None, bool_only=None, skipna=None, level=None,
numeric_only=bool_only, filter_type='bool',
name=name)

return _set_function_name(logical_func, name, cls)
return set_function_name(logical_func, name, cls)


# install the indexes
Expand Down
32 changes: 17 additions & 15 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pandas.core.categorical import Categorical, maybe_to_categorical
from pandas.tseries.index import DatetimeIndex
import pandas.core.common as com
import pandas.core.missing as mis
import pandas.core.missing as missing
import pandas.core.convert as convert
from pandas.sparse.array import _maybe_to_sparse, SparseArray
import pandas.lib as lib
Expand Down Expand Up @@ -872,7 +872,7 @@ def check_int_bool(self, inplace):

# a fill na type method
try:
m = mis._clean_fill_method(method)
m = missing.clean_fill_method(method)
except:
m = None

Expand All @@ -887,7 +887,7 @@ def check_int_bool(self, inplace):
downcast=downcast, mgr=mgr)
# try an interp method
try:
m = mis._clean_interp_method(method, **kwargs)
m = missing.clean_interp_method(method, **kwargs)
except:
m = None

Expand Down Expand Up @@ -920,9 +920,9 @@ def _interpolate_with_fill(self, method='pad', axis=0, inplace=False,
values = self.values if inplace else self.values.copy()
values, _, fill_value, _ = self._try_coerce_args(values, fill_value)
values = self._try_operate(values)
values = mis.interpolate_2d(values, method=method, axis=axis,
limit=limit, fill_value=fill_value,
dtype=self.dtype)
values = missing.interpolate_2d(values, method=method, axis=axis,
limit=limit, fill_value=fill_value,
dtype=self.dtype)
values = self._try_coerce_result(values)

blocks = [self.make_block(values, klass=self.__class__, fastpath=True)]
Expand Down Expand Up @@ -955,11 +955,11 @@ def func(x):

# process a 1-d slice, returning it
# should the axis argument be handled below in apply_along_axis?
# i.e. not an arg to mis.interpolate_1d
return mis.interpolate_1d(index, x, method=method, limit=limit,
limit_direction=limit_direction,
fill_value=fill_value,
bounds_error=False, **kwargs)
# i.e. not an arg to missing.interpolate_1d
return missing.interpolate_1d(index, x, method=method, limit=limit,
limit_direction=limit_direction,
fill_value=fill_value,
bounds_error=False, **kwargs)

# interp each column independently
interp_values = np.apply_along_axis(func, axis, data)
Expand Down Expand Up @@ -2414,8 +2414,8 @@ def make_block_same_class(self, values, placement, sparse_index=None,
def interpolate(self, method='pad', axis=0, inplace=False, limit=None,
fill_value=None, **kwargs):

values = mis.interpolate_2d(self.values.to_dense(), method, axis,
limit, fill_value)
values = missing.interpolate_2d(self.values.to_dense(), method, axis,
limit, fill_value)
return self.make_block_same_class(values=values,
placement=self.mgr_locs)

Expand Down Expand Up @@ -3851,8 +3851,10 @@ def reindex(self, new_axis, indexer=None, method=None, fill_value=None,

# fill if needed
if method is not None or limit is not None:
new_values = mis.interpolate_2d(new_values, method=method,
limit=limit, fill_value=fill_value)
new_values = missing.interpolate_2d(new_values,
method=method,
limit=limit,
fill_value=fill_value)

if self._block.is_sparse:
make_block = self._block.make_block_same_class
Expand Down
Loading

0 comments on commit 2630345

Please sign in to comment.