|
3 | 3 | from datetime import datetime
|
4 | 4 |
|
5 | 5 | import numpy as np
|
| 6 | +import pytest |
6 | 7 | from pandas.compat import long
|
7 | 8 | import pandas.core.algorithms as algos
|
8 | 9 | import pandas.util.testing as tm
|
@@ -445,3 +446,47 @@ def test_2d_datetime64(self):
|
445 | 446 | expected = arr.take(indexer, axis=1)
|
446 | 447 | expected[:, [2, 4]] = datetime(2007, 1, 1)
|
447 | 448 | 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