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_object
  • Loading branch information
jbrockmendel committed Jul 12, 2018
commit cfce17dc95f9175680540e4e5c4907348a15f279
10 changes: 5 additions & 5 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
is_datetime64_any_dtype, is_datetime64tz_dtype,
is_timedelta64_dtype, is_datetimelike,
is_interval_dtype, is_scalar, is_list_like,
_ensure_platform_int, _ensure_object,
_ensure_platform_int, ensure_object,
_ensure_float64, _ensure_uint64,
_ensure_int64)
from pandas.compat.numpy import _np_version_under1p10
Expand Down Expand Up @@ -73,7 +73,7 @@ def _ensure_data(values, dtype=None):
# we check some simple dtypes first
try:
if is_object_dtype(dtype):
return _ensure_object(np.asarray(values)), 'object', 'object'
return ensure_object(np.asarray(values)), 'object', 'object'
if is_bool_dtype(values) or is_bool_dtype(dtype):
# we are actually coercing to uint64
# until our algos support uint8 directly (see TODO)
Expand All @@ -86,7 +86,7 @@ def _ensure_data(values, dtype=None):
elif is_float_dtype(values) or is_float_dtype(dtype):
return _ensure_float64(values), 'float64', 'float64'
elif is_object_dtype(values) and dtype is None:
return _ensure_object(np.asarray(values)), 'object', 'object'
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
Expand All @@ -98,7 +98,7 @@ def _ensure_data(values, dtype=None):
except (TypeError, ValueError):
# if we are trying to coerce to a dtype
# and it is incompat this will fall thru to here
return _ensure_object(values), 'object', 'object'
return ensure_object(values), 'object', 'object'

# datetimelike
if (needs_i8_conversion(values) or
Expand Down Expand Up @@ -135,7 +135,7 @@ def _ensure_data(values, dtype=None):

# we have failed, return object
values = np.asarray(values)
return _ensure_object(values), 'object', 'object'
return ensure_object(values), 'object', 'object'


def _reconstruct_data(values, dtype, original):
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pandas.core.dtypes.dtypes import CategoricalDtype
from pandas.core.dtypes.common import (
_ensure_int64,
_ensure_object,
ensure_object,
_ensure_platform_int,
is_dtype_equal,
is_datetimelike,
Expand Down Expand Up @@ -2425,8 +2425,8 @@ def _get_codes_for_values(values, categories):

from pandas.core.algorithms import _get_data_algo, _hashtables
if not is_dtype_equal(values.dtype, categories.dtype):
values = _ensure_object(values)
categories = _ensure_object(categories)
values = ensure_object(values)
categories = ensure_object(categories)

(hash_klass, vec_klass), vals = _get_data_algo(values, _hashtables)
(_, _), cats = _get_data_algo(categories, _hashtables)
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pandas._libs import tslib, lib, tslibs
from pandas._libs.tslibs import iNaT
from pandas.compat import string_types, text_type, PY3
from .common import (_ensure_object, is_bool, is_integer, is_float,
from .common import (ensure_object, is_bool, is_integer, is_float,
is_complex, is_datetimetz, is_categorical_dtype,
is_datetimelike,
is_extension_type,
Expand Down Expand Up @@ -85,7 +85,7 @@ def trans(x):

if isinstance(dtype, string_types):
if dtype == 'infer':
inferred_type = lib.infer_dtype(_ensure_object(result.ravel()))
inferred_type = lib.infer_dtype(ensure_object(result.ravel()))
if inferred_type == 'boolean':
dtype = 'bool'
elif inferred_type == 'integer':
Expand Down Expand Up @@ -948,7 +948,7 @@ def try_timedelta(v):
except Exception:
return v.reshape(shape)

inferred_type = lib.infer_datetimelike_array(_ensure_object(v))
inferred_type = lib.infer_datetimelike_array(ensure_object(v))

if inferred_type == 'date' and convert_dates:
value = try_datetime(v)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def _ensure_float(arr):
_ensure_int16 = algos.ensure_int16
_ensure_int8 = algos.ensure_int8
_ensure_platform_int = algos.ensure_platform_int
_ensure_object = algos.ensure_object
ensure_object = algos.ensure_object


def _ensure_categorical(arr):
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
is_string_like_dtype, is_bool_dtype,
is_integer_dtype, is_dtype_equal,
is_extension_array_dtype,
needs_i8_conversion, _ensure_object,
needs_i8_conversion, ensure_object,
pandas_dtype,
is_scalar,
is_object_dtype,
Expand Down Expand Up @@ -417,7 +417,7 @@ def array_equivalent(left, right, strict_nan=False):
if not strict_nan:
# isna considers NaN and None to be equivalent.
return lib.array_equivalent_object(
_ensure_object(left.ravel()), _ensure_object(right.ravel()))
ensure_object(left.ravel()), ensure_object(right.ravel()))

for left_value, right_value in zip(left, right):
if left_value is NaT and right_value is not NaT:
Expand Down Expand Up @@ -474,7 +474,7 @@ def _infer_fill_value(val):
if is_datetimelike(val):
return np.array('NaT', dtype=val.dtype)
elif is_object_dtype(val.dtype):
dtype = lib.infer_dtype(_ensure_object(val))
dtype = lib.infer_dtype(ensure_object(val))
if dtype in ['datetime', 'datetime64']:
return np.array('NaT', dtype=_NS_DTYPE)
elif dtype in ['timedelta', 'timedelta64']:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pandas._libs import tslib, properties
from pandas.core.dtypes.common import (
_ensure_int64,
_ensure_object,
ensure_object,
is_scalar,
is_number,
is_integer, is_bool,
Expand Down Expand Up @@ -3235,7 +3235,7 @@ def _drop_axis(self, labels, axis, level=None, errors='raise'):

# Case for non-unique axis
else:
labels = _ensure_object(com._index_labels_to_array(labels))
labels = ensure_object(com._index_labels_to_array(labels))
if level is not None:
if not isinstance(axis, MultiIndex):
raise AssertionError('axis must be a MultiIndex')
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
_ensure_float64,
_ensure_platform_int,
_ensure_int64,
_ensure_object,
ensure_object,
_ensure_categorical,
_ensure_float)
from pandas.core.dtypes.cast import maybe_downcast_to_dtype
Expand Down Expand Up @@ -2642,7 +2642,7 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1,
result, (counts > 0).view(np.uint8))
except ValueError:
result = lib.row_bool_subset_object(
_ensure_object(result),
ensure_object(result),
(counts > 0).view(np.uint8))
else:
result = result[counts > 0]
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from pandas.core.dtypes.cast import maybe_cast_to_integer_array
from pandas.core.dtypes.common import (
_ensure_int64,
_ensure_object,
ensure_object,
_ensure_categorical,
_ensure_platform_int,
is_integer,
Expand Down Expand Up @@ -1872,7 +1872,7 @@ def is_type_compatible(self, kind):
def is_all_dates(self):
if self._data is None:
return False
return is_datetime_array(_ensure_object(self.values))
return is_datetime_array(ensure_object(self.values))

def __reduce__(self):
d = dict(data=self._data)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
is_period_dtype,
is_bool_dtype,
pandas_dtype,
_ensure_object)
ensure_object)
from pandas.core.dtypes.generic import ABCSeries

import pandas.tseries.frequencies as frequencies
Expand Down Expand Up @@ -257,7 +257,7 @@ def __new__(cls, data=None, ordinal=None, freq=None, start=None, end=None,
"floating point in construction")

# anything else, likely an array of strings or periods
data = _ensure_object(data)
data = ensure_object(data)
freq = freq or period.extract_freq(data)
data = period.extract_ordinals(data, freq)
return cls._from_ordinals(data, name=name, freq=freq)
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
is_list_like,
is_scalar,
is_extension_array_dtype,
_ensure_object)
ensure_object)
from pandas.core.dtypes.cast import (
maybe_upcast_putmask, find_common_type,
construct_1d_object_array_from_listlike)
Expand Down Expand Up @@ -1387,8 +1387,8 @@ def na_op(x, y):
if (is_bool_dtype(x.dtype) and is_bool_dtype(y.dtype)):
result = op(x, y) # when would this be hit?
else:
x = _ensure_object(x)
y = _ensure_object(y)
x = ensure_object(x)
y = ensure_object(y)
result = libops.vec_binop(x, y, op)
else:
# let null fall thru
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
is_datetimelike,
_ensure_int64,
_ensure_float64,
_ensure_object,
ensure_object,
_get_dtype)
from pandas.core.dtypes.missing import na_value_for_dtype
from pandas.core.internals import (items_overlap_with_suffix,
Expand Down Expand Up @@ -1214,7 +1214,7 @@ def _asof_by_function(direction, on_type, by_type):
_type_casters = {
'int64_t': _ensure_int64,
'double': _ensure_float64,
'object': _ensure_object,
'object': ensure_object,
}

_cython_types = {
Expand Down Expand Up @@ -1561,8 +1561,8 @@ def _factorize_keys(lk, rk, sort=True):
rk = _ensure_int64(com._values_from_object(rk))
else:
klass = libhashtable.Factorizer
lk = _ensure_object(lk)
rk = _ensure_object(rk)
lk = ensure_object(lk)
rk = ensure_object(rk)

rizer = klass(max(len(lk), len(rk)))

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
_guess_datetime_format)

from pandas.core.dtypes.common import (
_ensure_object,
ensure_object,
is_datetime64_ns_dtype,
is_datetime64_dtype,
is_datetime64tz_dtype,
Expand Down Expand Up @@ -216,7 +216,7 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
raise TypeError('arg must be a string, datetime, list, tuple, '
'1-d array, or Series')

arg = _ensure_object(arg)
arg = ensure_object(arg)
require_iso8601 = False

if infer_datetime_format and format is None:
Expand Down Expand Up @@ -787,7 +787,7 @@ def _convert_listlike(arg, format):
raise TypeError('arg must be a string, datetime, list, tuple, '
'1-d array, or Series')

arg = _ensure_object(arg)
arg = ensure_object(arg)

if infer_time_format and format is None:
format = _guess_time_format_for_array(arg)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/tools/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
is_decimal,
is_datetime_or_timedelta_dtype,
is_number,
_ensure_object)
ensure_object)
from pandas.core.dtypes.generic import ABCSeries, ABCIndexClass
from pandas.core.dtypes.cast import maybe_downcast_to_dtype
from pandas._libs import lib
Expand Down Expand Up @@ -130,7 +130,7 @@ def to_numeric(arg, errors='raise', downcast=None):
elif is_datetime_or_timedelta_dtype(values):
values = values.astype(np.int64)
else:
values = _ensure_object(values)
values = ensure_object(values)
coerce_numeric = False if errors in ('ignore', 'raise') else True
values = lib.maybe_convert_numeric(values, set(),
coerce_numeric=coerce_numeric)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/tools/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
array_to_timedelta64)

from pandas.core.dtypes.common import (
_ensure_object,
ensure_object,
is_integer_dtype,
is_timedelta64_dtype,
is_list_like)
Expand Down Expand Up @@ -171,7 +171,7 @@ def _convert_listlike(arg, unit='ns', box=True, errors='raise', name=None):
'timedelta64[ns]', copy=False)
else:
try:
value = array_to_timedelta64(_ensure_object(arg),
value = array_to_timedelta64(ensure_object(arg),
unit=unit, errors=errors)
value = value.astype('timedelta64[ns]', copy=False)
except ValueError:
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pandas.compat import (range, lrange, PY3, StringIO, lzip,
zip, string_types, map, u)
from pandas.core.dtypes.common import (
is_integer, _ensure_object,
is_integer, ensure_object,
is_list_like, is_integer_dtype,
is_float, is_dtype_equal,
is_object_dtype, is_string_dtype,
Expand Down Expand Up @@ -3005,7 +3005,7 @@ def converter(*date_cols):

try:
return tools.to_datetime(
_ensure_object(strs),
ensure_object(strs),
utc=None,
box=False,
dayfirst=dayfirst,
Expand Down
6 changes: 3 additions & 3 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
is_timedelta64_dtype,
is_datetime64tz_dtype,
is_datetime64_dtype,
_ensure_object,
ensure_object,
_ensure_int64,
_ensure_platform_int)
from pandas.core.dtypes.missing import array_equivalent
Expand Down Expand Up @@ -4656,7 +4656,7 @@ def _convert_string_array(data, encoding, errors, itemsize=None):

# create the sized dtype
if itemsize is None:
ensured = _ensure_object(data.ravel())
ensured = ensure_object(data.ravel())
itemsize = libwriters.max_len_string_array(ensured)

data = np.asarray(data, dtype="S%d" % itemsize)
Expand Down Expand Up @@ -4688,7 +4688,7 @@ def _unconvert_string_array(data, nan_rep=None, encoding=None,
encoding = _ensure_encoding(encoding)
if encoding is not None and len(data):

itemsize = libwriters.max_len_string_array(_ensure_object(data))
itemsize = libwriters.max_len_string_array(ensure_object(data))
if compat.PY3:
dtype = "U{0}".format(itemsize)
else:
Expand Down
8 changes: 4 additions & 4 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
zip, BytesIO)
from pandas.core.arrays import Categorical
from pandas.core.base import StringMixin
from pandas.core.dtypes.common import (is_categorical_dtype, _ensure_object,
from pandas.core.dtypes.common import (is_categorical_dtype, ensure_object,
is_datetime64_dtype)
from pandas.core.frame import DataFrame
from pandas.core.series import Series
Expand Down Expand Up @@ -1818,7 +1818,7 @@ def _dtype_to_stata_type(dtype, column):
if dtype.type == np.object_: # try to coerce it to the biggest string
# not memory efficient, what else could we
# do?
itemsize = max_len_string_array(_ensure_object(column.values))
itemsize = max_len_string_array(ensure_object(column.values))
return max(itemsize, 1)
elif dtype == np.float64:
return 255
Expand Down Expand Up @@ -1863,7 +1863,7 @@ def _dtype_to_default_stata_fmt(dtype, column, dta_version=114,
if not (inferred_dtype in ('string', 'unicode') or
len(column) == 0):
raise ValueError('Writing general object arrays is not supported')
itemsize = max_len_string_array(_ensure_object(column.values))
itemsize = max_len_string_array(ensure_object(column.values))
if itemsize > max_str_len:
if dta_version >= 117:
return '%9s'
Expand Down Expand Up @@ -2418,7 +2418,7 @@ def _dtype_to_stata_type_117(dtype, column, force_strl):
if dtype.type == np.object_: # try to coerce it to the biggest string
# not memory efficient, what else could we
# do?
itemsize = max_len_string_array(_ensure_object(column.values))
itemsize = max_len_string_array(ensure_object(column.values))
itemsize = max(itemsize, 1)
if itemsize <= 2045:
return itemsize
Expand Down