Skip to content

Commit 5db6624

Browse files
committed
Doc and move tests
1 parent 74b2c09 commit 5db6624

File tree

3 files changed

+85
-46
lines changed

3 files changed

+85
-46
lines changed

pandas/core/algorithms.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,19 +1455,21 @@ def take(arr, indexer, allow_fill=False, fill_value=None):
14551455
14561456
Parameters
14571457
----------
1458-
arr : ndarray or ExtensionArray
1458+
arr : sequence
1459+
Non array-likes (sequences without a dtype) are coereced
1460+
to an ndarray.
14591461
indexer : sequence of integers
1460-
Indices to be taken. See Notes for how negative indicies
1461-
are handled.
1462+
Indices to be taken.
14621463
allow_fill : bool, default False
14631464
How to handle negative values in `indexer`.
14641465
1465-
For False values (the default), negative values in `indexer`
1466-
indiciate slices from the right.
1466+
* False: negative values in `indexer` indicate
1467+
slices from the right (the default)
1468+
1469+
* True: negative values in `indexer` indicate
1470+
missing values. These values are set to `fill_value`. Any other
1471+
other negative values raise a ``ValueError``.
14671472
1468-
For True values, indicies where `indexer` is ``-1`` indicate
1469-
missing values. These values are set to `fill_value`. Any other
1470-
other negative value should raise a ``ValueError``.
14711473
fill_value : any, optional
14721474
Fill value to use for NA-indicies when `allow_fill` is True.
14731475
This may be ``None``, in which case the default NA value for
@@ -1486,13 +1488,42 @@ def take(arr, indexer, allow_fill=False, fill_value=None):
14861488
When the indexer contains negative values other than ``-1``
14871489
and `allow_fill` is True.
14881490
1491+
Notes
1492+
-----
1493+
When `allow_fill` is False, `indexer` may be whatever dimensionality
1494+
is accepted by NumPy for `arr`.
1495+
1496+
When `allow_fill` is True, `indexer` should be 1-D.
1497+
14891498
See Also
14901499
--------
14911500
numpy.take
1501+
1502+
Examples
1503+
--------
1504+
>>> from pandas.api.extensions import take
1505+
1506+
With the default ``allow_fill=False``, negative numbers indicate
1507+
slices from the right.
1508+
1509+
>>> take(np.array([10, 20, 30]), [0, 0, -1])
1510+
array([10, 10, 30])
1511+
1512+
Setting ``allow_fill=True`` will place `fill_value` in those positions.
1513+
1514+
>>> take(np.array([10, 20, 30]), [0, 0, -1], allow_fill=True)
1515+
array([10., 10., nan])
1516+
1517+
>>> take(np.array([10, 20, 30]), [0, 0, -1], allow_fill=True,
1518+
... fill_value=-10)
1519+
array([ 10, 10, -10])
14921520
"""
14931521
from pandas.core.indexing import validate_indices
14941522

1495-
# Do we require int64 here?
1523+
if not is_array_like(arr):
1524+
arr = np.asarray(arr)
1525+
1526+
# Do we require int64 or intp here?
14961527
indexer = np.asarray(indexer, dtype='int')
14971528

14981529
if allow_fill:

pandas/tests/test_algos.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,40 +1564,3 @@ def test_index(self):
15641564
idx = Index(['1 day', '1 day', '-1 day', '-1 day 2 min',
15651565
'2 min', '2 min'], dtype='timedelta64[ns]')
15661566
tm.assert_series_equal(algos.mode(idx), exp)
1567-
1568-
1569-
class TestTake(object):
1570-
1571-
def test_bounds_check_large(self):
1572-
arr = np.array([1, 2])
1573-
with pytest.raises(IndexError):
1574-
algos.take(arr, [2, 3], allow_fill=True)
1575-
1576-
with pytest.raises(IndexError):
1577-
algos.take(arr, [2, 3], allow_fill=False)
1578-
1579-
def test_bounds_check_small(self):
1580-
arr = np.array([1, 2, 3], dtype=np.int64)
1581-
indexer = [0, -1, -2]
1582-
with pytest.raises(ValueError):
1583-
algos.take(arr, indexer, allow_fill=True)
1584-
1585-
result = algos.take(arr, indexer)
1586-
expected = np.array([1, 3, 2], dtype=np.int64)
1587-
tm.assert_numpy_array_equal(result, expected)
1588-
1589-
@pytest.mark.parametrize('allow_fill', [True, False])
1590-
def test_take_empty(self, allow_fill):
1591-
arr = np.array([], dtype=np.int64)
1592-
# empty take is ok
1593-
result = algos.take(arr, [], allow_fill=allow_fill)
1594-
tm.assert_numpy_array_equal(arr, result)
1595-
1596-
with pytest.raises(IndexError):
1597-
algos.take(arr, [0], allow_fill=allow_fill)
1598-
1599-
def test_take_na_empty(self):
1600-
result = algos.take(np.array([]), [-1, -1], allow_fill=True,
1601-
fill_value=0.0)
1602-
expected = np.array([0., 0.])
1603-
tm.assert_numpy_array_equal(result, expected)

pandas/tests/test_take.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import datetime
44

55
import numpy as np
6+
import pytest
67
from pandas.compat import long
78
import pandas.core.algorithms as algos
89
import pandas.util.testing as tm
@@ -445,3 +446,47 @@ def test_2d_datetime64(self):
445446
expected = arr.take(indexer, axis=1)
446447
expected[:, [2, 4]] = datetime(2007, 1, 1)
447448
tm.assert_almost_equal(result, expected)
449+
450+
451+
class TestExtensionTake(object):
452+
# The take method found in pd.api.extensions
453+
454+
def test_bounds_check_large(self):
455+
arr = np.array([1, 2])
456+
with pytest.raises(IndexError):
457+
algos.take(arr, [2, 3], allow_fill=True)
458+
459+
with pytest.raises(IndexError):
460+
algos.take(arr, [2, 3], allow_fill=False)
461+
462+
def test_bounds_check_small(self):
463+
arr = np.array([1, 2, 3], dtype=np.int64)
464+
indexer = [0, -1, -2]
465+
with pytest.raises(ValueError):
466+
algos.take(arr, indexer, allow_fill=True)
467+
468+
result = algos.take(arr, indexer)
469+
expected = np.array([1, 3, 2], dtype=np.int64)
470+
tm.assert_numpy_array_equal(result, expected)
471+
472+
@pytest.mark.parametrize('allow_fill', [True, False])
473+
def test_take_empty(self, allow_fill):
474+
arr = np.array([], dtype=np.int64)
475+
# empty take is ok
476+
result = algos.take(arr, [], allow_fill=allow_fill)
477+
tm.assert_numpy_array_equal(arr, result)
478+
479+
with pytest.raises(IndexError):
480+
algos.take(arr, [0], allow_fill=allow_fill)
481+
482+
def test_take_na_empty(self):
483+
result = algos.take(np.array([]), [-1, -1], allow_fill=True,
484+
fill_value=0.0)
485+
expected = np.array([0., 0.])
486+
tm.assert_numpy_array_equal(result, expected)
487+
488+
def test_take_coerces_list(self):
489+
arr = [1, 2, 3]
490+
result = algos.take(arr, [0, 0])
491+
expected = np.array([1, 1])
492+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)