Skip to content

Commit 537b65c

Browse files
jbrockmendeljreback
authored andcommitted
[CLN] De-privatize commonly-used functions (#21870)
1 parent a860028 commit 537b65c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+385
-378
lines changed

pandas/_libs/lib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ cpdef ndarray[object] astype_str(ndarray arr):
485485

486486
def clean_index_list(list obj):
487487
"""
488-
Utility used in pandas.core.index._ensure_index
488+
Utility used in pandas.core.index.ensure_index
489489
"""
490490
cdef:
491491
Py_ssize_t i, n = len(obj)

pandas/core/algorithms.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
is_datetime64_any_dtype, is_datetime64tz_dtype,
2828
is_timedelta64_dtype, is_datetimelike,
2929
is_interval_dtype, is_scalar, is_list_like,
30-
_ensure_platform_int, _ensure_object,
31-
_ensure_float64, _ensure_uint64,
32-
_ensure_int64)
30+
ensure_platform_int, ensure_object,
31+
ensure_float64, ensure_uint64,
32+
ensure_int64)
3333
from pandas.compat.numpy import _np_version_under1p10
3434
from pandas.core.dtypes.missing import isna, na_value_for_dtype
3535

@@ -73,32 +73,32 @@ def _ensure_data(values, dtype=None):
7373
# we check some simple dtypes first
7474
try:
7575
if is_object_dtype(dtype):
76-
return _ensure_object(np.asarray(values)), 'object', 'object'
76+
return ensure_object(np.asarray(values)), 'object', 'object'
7777
if is_bool_dtype(values) or is_bool_dtype(dtype):
7878
# we are actually coercing to uint64
7979
# until our algos support uint8 directly (see TODO)
8080
return np.asarray(values).astype('uint64'), 'bool', 'uint64'
8181
elif is_signed_integer_dtype(values) or is_signed_integer_dtype(dtype):
82-
return _ensure_int64(values), 'int64', 'int64'
82+
return ensure_int64(values), 'int64', 'int64'
8383
elif (is_unsigned_integer_dtype(values) or
8484
is_unsigned_integer_dtype(dtype)):
85-
return _ensure_uint64(values), 'uint64', 'uint64'
85+
return ensure_uint64(values), 'uint64', 'uint64'
8686
elif is_float_dtype(values) or is_float_dtype(dtype):
87-
return _ensure_float64(values), 'float64', 'float64'
87+
return ensure_float64(values), 'float64', 'float64'
8888
elif is_object_dtype(values) and dtype is None:
89-
return _ensure_object(np.asarray(values)), 'object', 'object'
89+
return ensure_object(np.asarray(values)), 'object', 'object'
9090
elif is_complex_dtype(values) or is_complex_dtype(dtype):
9191

9292
# ignore the fact that we are casting to float
9393
# which discards complex parts
9494
with catch_warnings(record=True):
95-
values = _ensure_float64(values)
95+
values = ensure_float64(values)
9696
return values, 'float64', 'float64'
9797

9898
except (TypeError, ValueError):
9999
# if we are trying to coerce to a dtype
100100
# and it is incompat this will fall thru to here
101-
return _ensure_object(values), 'object', 'object'
101+
return ensure_object(values), 'object', 'object'
102102

103103
# datetimelike
104104
if (needs_i8_conversion(values) or
@@ -129,13 +129,13 @@ def _ensure_data(values, dtype=None):
129129

130130
# we are actually coercing to int64
131131
# until our algos support int* directly (not all do)
132-
values = _ensure_int64(values)
132+
values = ensure_int64(values)
133133

134134
return values, dtype, 'int64'
135135

136136
# we have failed, return object
137137
values = np.asarray(values)
138-
return _ensure_object(values), 'object', 'object'
138+
return ensure_object(values), 'object', 'object'
139139

140140

141141
def _reconstruct_data(values, dtype, original):
@@ -475,7 +475,7 @@ def _factorize_array(values, na_sentinel=-1, size_hint=None,
475475
labels = table.get_labels(values, uniques, 0, na_sentinel,
476476
na_value=na_value)
477477

478-
labels = _ensure_platform_int(labels)
478+
labels = ensure_platform_int(labels)
479479
uniques = uniques.to_array()
480480
return labels, uniques
481481

@@ -1309,7 +1309,7 @@ def _take_nd_object(arr, indexer, out, axis, fill_value, mask_info):
13091309
if arr.dtype != out.dtype:
13101310
arr = arr.astype(out.dtype)
13111311
if arr.shape[axis] > 0:
1312-
arr.take(_ensure_platform_int(indexer), axis=axis, out=out)
1312+
arr.take(ensure_platform_int(indexer), axis=axis, out=out)
13131313
if needs_masking:
13141314
outindexer = [slice(None)] * arr.ndim
13151315
outindexer[axis] = mask
@@ -1450,7 +1450,7 @@ def _get_take_nd_function(ndim, arr_dtype, out_dtype, axis=0, mask_info=None):
14501450
return func
14511451

14521452
def func(arr, indexer, out, fill_value=np.nan):
1453-
indexer = _ensure_int64(indexer)
1453+
indexer = ensure_int64(indexer)
14541454
_take_nd_object(arr, indexer, out, axis=axis, fill_value=fill_value,
14551455
mask_info=mask_info)
14561456

@@ -1609,7 +1609,7 @@ def take_nd(arr, indexer, axis=0, out=None, fill_value=np.nan, mask_info=None,
16091609
indexer = np.arange(arr.shape[axis], dtype=np.int64)
16101610
dtype, fill_value = arr.dtype, arr.dtype.type()
16111611
else:
1612-
indexer = _ensure_int64(indexer, copy=False)
1612+
indexer = ensure_int64(indexer, copy=False)
16131613
if not allow_fill:
16141614
dtype, fill_value = arr.dtype, arr.dtype.type()
16151615
mask_info = None, False
@@ -1687,11 +1687,11 @@ def take_2d_multi(arr, indexer, out=None, fill_value=np.nan, mask_info=None,
16871687
if row_idx is None:
16881688
row_idx = np.arange(arr.shape[0], dtype=np.int64)
16891689
else:
1690-
row_idx = _ensure_int64(row_idx)
1690+
row_idx = ensure_int64(row_idx)
16911691
if col_idx is None:
16921692
col_idx = np.arange(arr.shape[1], dtype=np.int64)
16931693
else:
1694-
col_idx = _ensure_int64(col_idx)
1694+
col_idx = ensure_int64(col_idx)
16951695
indexer = row_idx, col_idx
16961696
if not allow_fill:
16971697
dtype, fill_value = arr.dtype, arr.dtype.type()

pandas/core/arrays/categorical.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
coerce_indexer_dtype)
1818
from pandas.core.dtypes.dtypes import CategoricalDtype
1919
from pandas.core.dtypes.common import (
20-
_ensure_int64,
21-
_ensure_object,
22-
_ensure_platform_int,
20+
ensure_int64,
21+
ensure_object,
22+
ensure_platform_int,
2323
is_extension_array_dtype,
2424
is_dtype_equal,
2525
is_datetimelike,
@@ -1221,7 +1221,7 @@ def shift(self, periods):
12211221
if codes.ndim > 1:
12221222
raise NotImplementedError("Categorical with ndim > 1.")
12231223
if np.prod(codes.shape) and (periods != 0):
1224-
codes = np.roll(codes, _ensure_platform_int(periods), axis=0)
1224+
codes = np.roll(codes, ensure_platform_int(periods), axis=0)
12251225
if periods > 0:
12261226
codes[:periods] = -1
12271227
else:
@@ -2137,7 +2137,7 @@ def mode(self, dropna=True):
21372137
if dropna:
21382138
good = self._codes != -1
21392139
values = self._codes[good]
2140-
values = sorted(htable.mode_int64(_ensure_int64(values), dropna))
2140+
values = sorted(htable.mode_int64(ensure_int64(values), dropna))
21412141
result = self._constructor(values=values, categories=self.categories,
21422142
ordered=self.ordered, fastpath=True)
21432143
return result
@@ -2431,8 +2431,8 @@ def _get_codes_for_values(values, categories):
24312431

24322432
from pandas.core.algorithms import _get_data_algo, _hashtables
24332433
if not is_dtype_equal(values.dtype, categories.dtype):
2434-
values = _ensure_object(values)
2435-
categories = _ensure_object(categories)
2434+
values = ensure_object(values)
2435+
categories = ensure_object(categories)
24362436

24372437
(hash_klass, vec_klass), vals = _get_data_algo(values, _hashtables)
24382438
(_, _), cats = _get_data_algo(categories, _hashtables)

pandas/core/arrays/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
is_datetime64tz_dtype,
2222
is_datetime64_dtype,
2323
is_timedelta64_dtype,
24-
_ensure_int64)
24+
ensure_int64)
2525
from pandas.core.dtypes.dtypes import DatetimeTZDtype
2626
from pandas.core.dtypes.missing import isna
2727
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
@@ -167,7 +167,7 @@ def _simple_new(cls, values, freq=None, tz=None, **kwargs):
167167
values = np.array(values, copy=False)
168168

169169
if not is_datetime64_dtype(values):
170-
values = _ensure_int64(values).view(_NS_DTYPE)
170+
values = ensure_int64(values).view(_NS_DTYPE)
171171

172172
result = object.__new__(cls)
173173
result._data = values

pandas/core/arrays/interval.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
ABCSeries, ABCIntervalIndex,
2020
ABCInterval)
2121
from pandas.core.dtypes.missing import isna, notna
22-
from pandas.core.indexes.base import Index, _ensure_index
22+
from pandas.core.indexes.base import Index, ensure_index
2323
from pandas.util._decorators import Appender
2424
from pandas.util._doctools import _WritableDoc
2525

@@ -145,8 +145,8 @@ def _simple_new(cls, left, right, closed=None,
145145
result = IntervalMixin.__new__(cls)
146146

147147
closed = closed or 'right'
148-
left = _ensure_index(left, copy=copy)
149-
right = _ensure_index(right, copy=copy)
148+
left = ensure_index(left, copy=copy)
149+
right = ensure_index(right, copy=copy)
150150

151151
if dtype is not None:
152152
# GH 19262: dtype must be an IntervalDtype to override inferred

pandas/core/arrays/timedeltas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from pandas import compat
1212

1313
from pandas.core.dtypes.common import (
14-
_TD_DTYPE, _ensure_int64, is_timedelta64_dtype, is_list_like)
14+
_TD_DTYPE, ensure_int64, is_timedelta64_dtype, is_list_like)
1515
from pandas.core.dtypes.generic import ABCSeries
1616
from pandas.core.dtypes.missing import isna
1717

@@ -117,7 +117,7 @@ def _simple_new(cls, values, freq=None, **kwargs):
117117
# non-nano unit
118118
values = values.astype(_TD_DTYPE)
119119
else:
120-
values = _ensure_int64(values).view(_TD_DTYPE)
120+
values = ensure_int64(values).view(_TD_DTYPE)
121121

122122
result = object.__new__(cls)
123123
result._data = values

pandas/core/common.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pandas import compat
1414
from pandas.compat import long, zip, iteritems, PY36, OrderedDict
1515
from pandas.core.config import get_option
16-
from pandas.core.dtypes.generic import ABCSeries, ABCIndex
16+
from pandas.core.dtypes.generic import ABCSeries, ABCIndex, ABCIndexClass
1717
from pandas.core.dtypes.common import is_integer
1818
from pandas.core.dtypes.inference import _iterable_not_string
1919
from pandas.core.dtypes.missing import isna, isnull, notnull # noqa
@@ -120,11 +120,6 @@ def is_bool_indexer(key):
120120
return False
121121

122122

123-
def _default_index(n):
124-
from pandas.core.index import RangeIndex
125-
return RangeIndex(0, n, name=None)
126-
127-
128123
def _mut_exclusive(**kwargs):
129124
item1, item2 = kwargs.items()
130125
label1, val1 = item1
@@ -299,11 +294,10 @@ def intersection(*seqs):
299294

300295

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

304298
if not (isinstance(values, (list, tuple)) or hasattr(values, '__array__')):
305299
values = list(values)
306-
elif isinstance(values, Index):
300+
elif isinstance(values, ABCIndexClass):
307301
return values.values
308302

309303
if isinstance(values, list) and dtype in [np.object_, object]:

pandas/core/dtypes/cast.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pandas._libs import tslib, lib, tslibs
99
from pandas._libs.tslibs import iNaT
1010
from pandas.compat import string_types, text_type, PY3
11-
from .common import (_ensure_object, is_bool, is_integer, is_float,
11+
from .common import (ensure_object, is_bool, is_integer, is_float,
1212
is_complex, is_datetimetz, is_categorical_dtype,
1313
is_datetimelike,
1414
is_extension_type,
@@ -25,8 +25,8 @@
2525
is_bool_dtype, is_scalar,
2626
is_string_dtype, _string_dtypes,
2727
pandas_dtype,
28-
_ensure_int8, _ensure_int16,
29-
_ensure_int32, _ensure_int64,
28+
ensure_int8, ensure_int16,
29+
ensure_int32, ensure_int64,
3030
_NS_DTYPE, _TD_DTYPE, _INT64_DTYPE,
3131
_POSSIBLY_CAST_DTYPES)
3232
from .dtypes import (ExtensionDtype, PandasExtensionDtype, DatetimeTZDtype,
@@ -85,7 +85,7 @@ def trans(x):
8585

8686
if isinstance(dtype, string_types):
8787
if dtype == 'infer':
88-
inferred_type = lib.infer_dtype(_ensure_object(result.ravel()))
88+
inferred_type = lib.infer_dtype(ensure_object(result.ravel()))
8989
if inferred_type == 'boolean':
9090
dtype = 'bool'
9191
elif inferred_type == 'integer':
@@ -602,12 +602,12 @@ def coerce_indexer_dtype(indexer, categories):
602602
""" coerce the indexer input array to the smallest dtype possible """
603603
length = len(categories)
604604
if length < _int8_max:
605-
return _ensure_int8(indexer)
605+
return ensure_int8(indexer)
606606
elif length < _int16_max:
607-
return _ensure_int16(indexer)
607+
return ensure_int16(indexer)
608608
elif length < _int32_max:
609-
return _ensure_int32(indexer)
610-
return _ensure_int64(indexer)
609+
return ensure_int32(indexer)
610+
return ensure_int64(indexer)
611611

612612

613613
def coerce_to_dtypes(result, dtypes):
@@ -948,7 +948,7 @@ def try_timedelta(v):
948948
except Exception:
949949
return v.reshape(shape)
950950

951-
inferred_type = lib.infer_datetimelike_array(_ensure_object(v))
951+
inferred_type = lib.infer_datetimelike_array(ensure_object(v))
952952

953953
if inferred_type == 'date' and convert_dates:
954954
value = try_datetime(v)

pandas/core/dtypes/common.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
# oh the troubles to reduce import time
3333
_is_scipy_sparse = None
3434

35-
_ensure_float64 = algos.ensure_float64
36-
_ensure_float32 = algos.ensure_float32
35+
ensure_float64 = algos.ensure_float64
36+
ensure_float32 = algos.ensure_float32
3737

3838
_ensure_datetime64ns = conversion.ensure_datetime64ns
3939
_ensure_timedelta64ns = conversion.ensure_timedelta64ns
4040

4141

42-
def _ensure_float(arr):
42+
def ensure_float(arr):
4343
"""
4444
Ensure that an array object has a float dtype if possible.
4545
@@ -59,16 +59,16 @@ def _ensure_float(arr):
5959
return arr
6060

6161

62-
_ensure_uint64 = algos.ensure_uint64
63-
_ensure_int64 = algos.ensure_int64
64-
_ensure_int32 = algos.ensure_int32
65-
_ensure_int16 = algos.ensure_int16
66-
_ensure_int8 = algos.ensure_int8
67-
_ensure_platform_int = algos.ensure_platform_int
68-
_ensure_object = algos.ensure_object
62+
ensure_uint64 = algos.ensure_uint64
63+
ensure_int64 = algos.ensure_int64
64+
ensure_int32 = algos.ensure_int32
65+
ensure_int16 = algos.ensure_int16
66+
ensure_int8 = algos.ensure_int8
67+
ensure_platform_int = algos.ensure_platform_int
68+
ensure_object = algos.ensure_object
6969

7070

71-
def _ensure_categorical(arr):
71+
def ensure_categorical(arr):
7272
"""
7373
Ensure that an array-like object is a Categorical (if not already).
7474

pandas/core/dtypes/missing.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
is_string_like_dtype, is_bool_dtype,
1717
is_integer_dtype, is_dtype_equal,
1818
is_extension_array_dtype,
19-
needs_i8_conversion, _ensure_object,
19+
needs_i8_conversion, ensure_object,
2020
pandas_dtype,
2121
is_scalar,
2222
is_object_dtype,
@@ -413,7 +413,7 @@ def array_equivalent(left, right, strict_nan=False):
413413
if not strict_nan:
414414
# isna considers NaN and None to be equivalent.
415415
return lib.array_equivalent_object(
416-
_ensure_object(left.ravel()), _ensure_object(right.ravel()))
416+
ensure_object(left.ravel()), ensure_object(right.ravel()))
417417

418418
for left_value, right_value in zip(left, right):
419419
if left_value is NaT and right_value is not NaT:
@@ -470,7 +470,7 @@ def _infer_fill_value(val):
470470
if is_datetimelike(val):
471471
return np.array('NaT', dtype=val.dtype)
472472
elif is_object_dtype(val.dtype):
473-
dtype = lib.infer_dtype(_ensure_object(val))
473+
dtype = lib.infer_dtype(ensure_object(val))
474474
if dtype in ['datetime', 'datetime64']:
475475
return np.array('NaT', dtype=_NS_DTYPE)
476476
elif dtype in ['timedelta', 'timedelta64']:

0 commit comments

Comments
 (0)