Skip to content

[CLN] De-privatize commonly-used functions #21870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
deprivatize ensure_float
  • Loading branch information
jbrockmendel committed Jul 12, 2018
commit f135f4ca0940fe85c89b45d98cd6f62ef1fa8d49
6 changes: 3 additions & 3 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
is_timedelta64_dtype, is_datetimelike,
is_interval_dtype, is_scalar, is_list_like,
_ensure_platform_int, ensure_object,
_ensure_float64, _ensure_uint64,
ensure_float64, _ensure_uint64,
_ensure_int64)
from pandas.compat.numpy import _np_version_under1p10
from pandas.core.dtypes.missing import isna, na_value_for_dtype
Expand Down Expand Up @@ -84,15 +84,15 @@ def _ensure_data(values, dtype=None):
is_unsigned_integer_dtype(dtype)):
return _ensure_uint64(values), 'uint64', 'uint64'
elif is_float_dtype(values) or is_float_dtype(dtype):
return _ensure_float64(values), 'float64', 'float64'
return ensure_float64(values), 'float64', 'float64'
elif is_object_dtype(values) and dtype is None:
return ensure_object(np.asarray(values)), 'object', 'object'
elif is_complex_dtype(values) or is_complex_dtype(dtype):

# ignore the fact that we are casting to float
# which discards complex parts
with catch_warnings(record=True):
values = _ensure_float64(values)
values = ensure_float64(values)
return values, 'float64', 'float64'

except (TypeError, ValueError):
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pandas import compat
from pandas.compat import long, zip, iteritems, PY36, OrderedDict
from pandas.core.config import get_option
from pandas.core.dtypes.generic import ABCSeries, ABCIndex
from pandas.core.dtypes.generic import ABCSeries, ABCIndex, ABCIndexClass
from pandas.core.dtypes.common import is_integer
from pandas.core.dtypes.inference import _iterable_not_string
from pandas.core.dtypes.missing import isna, isnull, notnull # noqa
Expand Down Expand Up @@ -299,11 +299,10 @@ def intersection(*seqs):


def _asarray_tuplesafe(values, dtype=None):
from pandas.core.index import Index

if not (isinstance(values, (list, tuple)) or hasattr(values, '__array__')):
values = list(values)
elif isinstance(values, Index):
elif isinstance(values, ABCIndexClass):
return values.values

if isinstance(values, list) and dtype in [np.object_, object]:
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
# oh the troubles to reduce import time
_is_scipy_sparse = None

_ensure_float64 = algos.ensure_float64
_ensure_float32 = algos.ensure_float32
ensure_float64 = algos.ensure_float64
ensure_float32 = algos.ensure_float32

_ensure_datetime64ns = conversion.ensure_datetime64ns
_ensure_timedelta64ns = conversion.ensure_timedelta64ns


def _ensure_float(arr):
def ensure_float(arr):
"""
Ensure that an array object has a float dtype if possible.

Expand Down
10 changes: 5 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
is_dtype_equal,
needs_i8_conversion,
_get_dtype_from_object,
_ensure_float64,
ensure_float64,
_ensure_int64,
_ensure_platform_int,
is_list_like,
Expand Down Expand Up @@ -6749,14 +6749,14 @@ def corr(self, method='pearson', min_periods=1):
mat = numeric_df.values

if method == 'pearson':
correl = libalgos.nancorr(_ensure_float64(mat), minp=min_periods)
correl = libalgos.nancorr(ensure_float64(mat), minp=min_periods)
elif method == 'spearman':
correl = libalgos.nancorr_spearman(_ensure_float64(mat),
correl = libalgos.nancorr_spearman(ensure_float64(mat),
minp=min_periods)
else:
if min_periods is None:
min_periods = 1
mat = _ensure_float64(mat).T
mat = ensure_float64(mat).T
corrf = nanops.get_corr_func(method)
K = len(cols)
correl = np.empty((K, K), dtype=float)
Expand Down Expand Up @@ -6886,7 +6886,7 @@ def cov(self, min_periods=None):
baseCov = np.cov(mat.T)
baseCov = baseCov.reshape((len(cols), len(cols)))
else:
baseCov = libalgos.nancorr(_ensure_float64(mat), cov=True,
baseCov = libalgos.nancorr(ensure_float64(mat), cov=True,
minp=min_periods)

return self._constructor(baseCov, index=idx, columns=cols)
Expand Down
14 changes: 7 additions & 7 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
is_list_like,
is_hashable,
needs_i8_conversion,
_ensure_float64,
ensure_float64,
_ensure_platform_int,
_ensure_int64,
ensure_object,
_ensure_categorical,
_ensure_float)
ensure_float)
from pandas.core.dtypes.cast import maybe_downcast_to_dtype
from pandas.core.dtypes.generic import ABCSeries
from pandas.core.dtypes.missing import isna, isnull, notna, _maybe_fill
Expand Down Expand Up @@ -1081,7 +1081,7 @@ def _python_agg_general(self, func, *args, **kwargs):
# since we are masking, make sure that we have a float object
values = result
if is_numeric_dtype(values.dtype):
values = _ensure_float(values)
values = ensure_float(values)

output[name] = self._try_cast(values[mask], result)

Expand Down Expand Up @@ -2578,16 +2578,16 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1,
values = values.view('int64')
is_numeric = True
elif is_bool_dtype(values.dtype):
values = _ensure_float64(values)
values = ensure_float64(values)
elif is_integer_dtype(values):
# we use iNaT for the missing value on ints
# so pre-convert to guard this condition
if (values == iNaT).any():
values = _ensure_float64(values)
values = ensure_float64(values)
else:
values = values.astype('int64', copy=False)
elif is_numeric and not is_complex_dtype(values):
values = _ensure_float64(values)
values = ensure_float64(values)
else:
values = values.astype(object)

Expand All @@ -2596,7 +2596,7 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1,
kind, how, values, is_numeric)
except NotImplementedError:
if is_numeric:
values = _ensure_float64(values)
values = ensure_float64(values)
func = self._get_cython_function(
kind, how, values, is_numeric)
else:
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
is_scalar,
is_integer,
needs_i8_conversion,
_ensure_float64)
ensure_float64)

from pandas.core.dtypes.cast import infer_dtype_from_array
from pandas.core.dtypes.missing import isna
Expand Down Expand Up @@ -480,7 +480,7 @@ def pad_1d(values, limit=None, mask=None, dtype=None):
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
_method = _pad_1d_datetime
elif is_integer_dtype(values):
values = _ensure_float64(values)
values = ensure_float64(values)
_method = algos.pad_inplace_float64
elif values.dtype == np.object_:
_method = algos.pad_inplace_object
Expand All @@ -506,7 +506,7 @@ def backfill_1d(values, limit=None, mask=None, dtype=None):
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
_method = _backfill_1d_datetime
elif is_integer_dtype(values):
values = _ensure_float64(values)
values = ensure_float64(values)
_method = algos.backfill_inplace_float64
elif values.dtype == np.object_:
_method = algos.backfill_inplace_object
Expand All @@ -533,7 +533,7 @@ def pad_2d(values, limit=None, mask=None, dtype=None):
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
_method = _pad_2d_datetime
elif is_integer_dtype(values):
values = _ensure_float64(values)
values = ensure_float64(values)
_method = algos.pad_2d_inplace_float64
elif values.dtype == np.object_:
_method = algos.pad_2d_inplace_object
Expand Down Expand Up @@ -564,7 +564,7 @@ def backfill_2d(values, limit=None, mask=None, dtype=None):
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
_method = _backfill_2d_datetime
elif is_integer_dtype(values):
values = _ensure_float64(values)
values = ensure_float64(values)
_method = algos.backfill_2d_inplace_float64
elif values.dtype == np.object_:
_method = algos.backfill_2d_inplace_object
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
is_list_like,
is_datetimelike,
_ensure_int64,
_ensure_float64,
ensure_float64,
ensure_object,
_get_dtype)
from pandas.core.dtypes.missing import na_value_for_dtype
Expand Down Expand Up @@ -1213,7 +1213,7 @@ def _asof_by_function(direction, on_type, by_type):

_type_casters = {
'int64_t': _ensure_int64,
'double': _ensure_float64,
'double': ensure_float64,
'object': ensure_object,
}

Expand Down
10 changes: 5 additions & 5 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
needs_i8_conversion,
is_timedelta64_dtype,
is_list_like,
_ensure_float64,
ensure_float64,
is_scalar)

from pandas.core.base import (PandasObject, SelectionMixin,
Expand Down Expand Up @@ -208,9 +208,9 @@ def _prep_values(self, values=None, kill_inf=True):
# GH #12373 : rolling functions error on float32 data
# make sure the data is coerced to float64
if is_float_dtype(values.dtype):
values = _ensure_float64(values)
values = ensure_float64(values)
elif is_integer_dtype(values.dtype):
values = _ensure_float64(values)
values = ensure_float64(values)
elif needs_i8_conversion(values.dtype):
raise NotImplementedError("ops for {action} for this "
"dtype {dtype} are not "
Expand All @@ -219,7 +219,7 @@ def _prep_values(self, values=None, kill_inf=True):
dtype=values.dtype))
else:
try:
values = _ensure_float64(values)
values = ensure_float64(values)
except (ValueError, TypeError):
raise TypeError("cannot handle this type -> {0}"
"".format(values.dtype))
Expand Down Expand Up @@ -857,7 +857,7 @@ def _apply(self, func, name=None, window=None, center=None,
def func(arg, window, min_periods=None, closed=None):
minp = check_minp(min_periods, window)
# ensure we are only rolling on floats
arg = _ensure_float64(arg)
arg = ensure_float64(arg)
return cfunc(arg,
window, minp, indexi, closed, **kwargs)

Expand Down