Skip to content

Commit 8e31f87

Browse files
committed
WIP - take refactoring
1 parent 78c94ab commit 8e31f87

File tree

8 files changed

+16
-27
lines changed

8 files changed

+16
-27
lines changed

pandas/core/algorithms.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,9 @@ def sort_mixed(values):
253253

254254
mask = (labels < -len(values)) | (labels >= len(values)) | \
255255
(labels == na_sentinel)
256+
np.putmask(labels, mask, -1)
256257

257-
# (Out of bound indices will be masked with `na_sentinel` next, so we may
258-
# deal with them here without performance loss using `mode='wrap'`.)
259-
new_labels = take_nd(reverse_indexer, labels, fill_value=na_sentinel,
260-
mask_info=(mask, mask.any()))
258+
new_labels = take_nd(reverse_indexer, labels, fill_value=na_sentinel)
261259

262260
return ordered, new_labels
263261

pandas/core/frame.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
_ensure_float,
4949
_ensure_float64,
5050
_ensure_int64,
51-
_ensure_platform_int,
5251
is_list_like,
5352
is_iterator,
5453
is_sequence,

pandas/core/indexing.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
is_categorical_dtype,
1010
is_list_like,
1111
is_sequence,
12-
is_scalar,
13-
_ensure_platform_int)
12+
is_scalar)
1413
from pandas.types.missing import isnull, _infer_fill_value
1514

1615
from pandas.core.index import Index, MultiIndex
@@ -864,7 +863,6 @@ def _convert_for_reindex(self, key, axis=0):
864863
keyarr = _asarray_tuplesafe(key)
865864

866865
if is_integer_dtype(keyarr) and not labels.is_integer():
867-
keyarr = _ensure_platform_int(keyarr)
868866
return labels.take(keyarr)
869867

870868
return keyarr
@@ -1860,10 +1858,11 @@ def maybe_convert_indices(indices, n):
18601858
# errors.
18611859
return np.empty(0, dtype=np.int_)
18621860

1861+
# cythonize this to one pass?
18631862
mask = indices < 0
18641863
if mask.any():
1865-
# don't want to mutate original array
1866-
indices = np.array(indices, copy=True)
1864+
# don't mutate original array
1865+
indices = indices.copy()
18671866
indices[mask] += n
18681867
mask = (indices >= n) | (indices < 0)
18691868
if mask.any():

pandas/core/reshape.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import numpy as np
88

9-
from pandas.types.common import _ensure_platform_int, is_list_like
9+
from pandas.types.common import is_list_like
1010
from pandas.types.cast import _maybe_promote
1111
from pandas.types.missing import notnull
1212
import pandas.types.concat as _concat

pandas/indexes/base.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,14 +1479,15 @@ def take(self, indices, axis=0, allow_fill=True,
14791479
def _assert_take_fillable(self, values, indices, allow_fill=True,
14801480
fill_value=None, na_value=np.nan):
14811481
""" internal method to handle NA filling of take """
1482+
indices = np.asarray(indices)
14821483
if allow_fill and fill_value is not None:
14831484
if (indices < -1).any():
14841485
msg = ('When allow_fill=True and fill_value is not None, '
14851486
'all indices must be >= -1')
14861487
raise ValueError(msg)
14871488
else:
14881489
taken = algos.take_nd(values, indices, allow_fill=allow_fill,
1489-
fill_value=fill_value)
1490+
fill_value=na_value)
14901491
else:
14911492
# provide wraparound semantics if fill_value not specified
14921493
from pandas.core.indexing import maybe_convert_indices
@@ -2529,7 +2530,7 @@ def _reindex_non_unique(self, target):
25292530
if len(missing):
25302531
l = np.arange(len(indexer))
25312532

2532-
missing = _ensure_platform_int(missing)
2533+
missing = missing
25332534
missing_labels = target.take(missing)
25342535
missing_indexer = _ensure_int64(l[~check])
25352536
cur_labels = self.take(indexer[check])._values
@@ -2723,12 +2724,9 @@ def _join_non_unique(self, other, how='left', return_indexers=False):
27232724
[other._values], how=how,
27242725
sort=True)
27252726

2726-
left_idx = _ensure_platform_int(left_idx)
2727-
right_idx = _ensure_platform_int(right_idx)
2728-
2729-
join_index = self.values.take(left_idx)
2730-
mask = left_idx == -1
2731-
np.putmask(join_index, mask, other._values.take(right_idx))
2727+
lvals = algos.take_nd(self.values, left_idx, fill_value=-1)
2728+
rvals = algos.take_nd(other._values, right_idx, fill_value=-1)
2729+
join_index = np.where(left_idx == -1, rvals, lvals)
27322730

27332731
join_index = self._wrap_joined_index(join_index, other)
27342732

pandas/indexes/multi.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,6 @@ def __getitem__(self, key):
10381038
def take(self, indices, axis=0, allow_fill=True,
10391039
fill_value=None, **kwargs):
10401040
nv.validate_take(tuple(), kwargs)
1041-
indices = _ensure_platform_int(indices)
10421041
taken = self._assert_take_fillable(self.labels, indices,
10431042
allow_fill=allow_fill,
10441043
fill_value=fill_value,
@@ -1340,7 +1339,6 @@ def sortlevel(self, level=0, ascending=True, sort_remaining=True):
13401339
if not ascending:
13411340
indexer = indexer[::-1]
13421341

1343-
indexer = _ensure_platform_int(indexer)
13441342
new_labels = [lab.take(indexer) for lab in self.labels]
13451343

13461344
new_index = MultiIndex(labels=new_labels, levels=self.levels,
@@ -1786,7 +1784,7 @@ def convert_indexer(start, stop, step, indexer=indexer, labels=labels):
17861784
# selected
17871785
from pandas import Series
17881786
mapper = Series(indexer)
1789-
indexer = labels.take(_ensure_platform_int(indexer))
1787+
indexer = labels.take(indexer)
17901788
result = Series(Index(indexer).isin(r).nonzero()[0])
17911789
m = result.map(mapper)._values
17921790

pandas/tools/merge.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
is_bool,
2424
is_list_like,
2525
_ensure_int64,
26-
_ensure_platform_int,
2726
_ensure_object)
2827
from pandas.types.missing import na_value_for_dtype
2928

@@ -434,12 +433,11 @@ def _merger(x, y):
434433
# if we DO have duplicates, then
435434
# we cannot guarantee order
436435

437-
sorter = _ensure_platform_int(
438-
np.concatenate([groupby.indices[g] for g, _ in groupby]))
436+
sorter = np.concatenate([groupby.indices[g] for g, _ in groupby])
439437
if len(result) != len(sorter):
440438
return result
441439

442-
rev = np.empty(len(sorter), dtype=np.int_)
440+
rev = np.empty(len(sorter), dtype=np.int64)
443441
rev.put(sorter, np.arange(len(sorter)))
444442
return result.take(rev).reset_index(drop=True)
445443

pandas/tseries/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,6 @@ def where(self, cond, other=None):
768768
values = _ensure_datetimelike_to_i8(self)
769769
result = np.where(cond, values, other).astype('i8')
770770

771-
import pdb; pdb.set_trace()
772771
result = self._ensure_localized(result)
773772
return self._shallow_copy(result,
774773
**self._get_attributes_dict())

0 commit comments

Comments
 (0)