Skip to content

Commit 0be9ec6

Browse files
committed
non-internals changes
1 parent dacd98e commit 0be9ec6

File tree

7 files changed

+67
-43
lines changed

7 files changed

+67
-43
lines changed

pandas/compat/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,6 @@ def is_platform_mac():
451451

452452
def is_platform_32bit():
453453
return struct.calcsize("P") * 8 < 64
454+
455+
456+
_default_fill_value = object()

pandas/core/algorithms.py

+22
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
_ensure_platform_int, _ensure_object,
3131
_ensure_float64, _ensure_uint64,
3232
_ensure_int64)
33+
from pandas.compat import _default_fill_value
3334
from pandas.compat.numpy import _np_version_under1p10
3435
from pandas.core.dtypes.missing import isna, na_value_for_dtype
3536

@@ -1441,6 +1442,27 @@ def func(arr, indexer, out, fill_value=np.nan):
14411442
return func
14421443

14431444

1445+
def take(arr, indexer, fill_value=_default_fill_value):
1446+
indexer = np.asarray(indexer)
1447+
1448+
if fill_value is _default_fill_value:
1449+
# NumPy style
1450+
result = arr.take(indexer)
1451+
else:
1452+
# bounds checking
1453+
if (indexer < -1).any():
1454+
raise ValueError("Invalid value in 'indexer'. All values "
1455+
"must be non-negative or -1. When "
1456+
"'fill_value' is specified.")
1457+
1458+
# # take on empty array not handled as desired by numpy
1459+
# # in case of -1 (all missing take)
1460+
# if not len(arr) and mask.all():
1461+
# return arr._from_sequence([fill_value] * len(indexer))
1462+
result = take_1d(arr, indexer, fill_value=fill_value)
1463+
return result
1464+
1465+
14441466
def take_nd(arr, indexer, axis=0, out=None, fill_value=np.nan, mask_info=None,
14451467
allow_fill=True):
14461468
"""

pandas/core/arrays/base.py

+5-33
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
import numpy as np
1111

1212
from pandas.errors import AbstractMethodError
13+
from pandas.compat import _default_fill_value
1314
from pandas.compat.numpy import function as nv
1415
from pandas.util._decorators import Appender, Substitution
1516

1617
_not_implemented_message = "{} does not implement {}."
17-
_default_fill_value = object()
1818

1919

2020
_take_docstring = textwrap.dedent("""\
@@ -537,13 +537,12 @@ def _values_for_take(self):
537537
@Appender(_take_docstring)
538538
def take(self, indexer, fill_value=_default_fill_value):
539539
# type: (Sequence[int], Optional[Any]) -> ExtensionArray
540-
from pandas.core.missing import isna
541-
542-
if isna(fill_value):
543-
fill_value = self.dtype.na_value
540+
if fill_value is np.nan:
541+
import pdb; pdb.set_trace()
542+
from pandas.core.algorithms import take
544543

545544
data = self._values_for_take()
546-
result = take_ea(data, indexer, fill_value=fill_value)
545+
result = take(data, indexer, fill_value=fill_value)
547546
return self._from_sequence(result)
548547

549548
def copy(self, deep=False):
@@ -609,30 +608,3 @@ def _ndarray_values(self):
609608
"""
610609
return np.array(self)
611610

612-
613-
@Substitution(arr=textwrap.dedent("""\
614-
arr : array-like
615-
Must satisfy NumPy's indexing sematnics, including `take`
616-
and boolean masking.
617-
"""))
618-
@Appender(_take_docstring)
619-
def take_ea(arr, indexer, fill_value=_default_fill_value):
620-
indexer = np.asarray(indexer)
621-
if fill_value is _default_fill_value:
622-
# NumPy style
623-
result = arr.take(indexer)
624-
else:
625-
mask = indexer == -1
626-
if (indexer < -1).any():
627-
raise ValueError("Invalid value in 'indexer'. All values "
628-
"must be non-negative or -1. When "
629-
"'fill_value' is specified.")
630-
631-
# take on empty array not handled as desired by numpy
632-
# in case of -1 (all missing take)
633-
if not len(arr) and mask.all():
634-
return arr._from_sequence([fill_value] * len(indexer))
635-
636-
result = arr.take(indexer)
637-
result[mask] = fill_value
638-
return result

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
from pandas.core.arrays import Categorical, ExtensionArray
7878
import pandas.core.algorithms as algorithms
7979
from pandas.compat import (range, map, zip, lrange, lmap, lzip, StringIO, u,
80-
OrderedDict, raise_with_traceback)
80+
OrderedDict, raise_with_traceback, _default_fill_value)
8181
from pandas import compat
8282
from pandas.compat import PY36
8383
from pandas.compat.numpy import function as nv
@@ -3504,7 +3504,7 @@ def _reindex_multi(self, axes, copy, fill_value):
35043504

35053505
@Appender(_shared_docs['align'] % _shared_doc_kwargs)
35063506
def align(self, other, join='outer', axis=None, level=None, copy=True,
3507-
fill_value=None, method=None, limit=None, fill_axis=0,
3507+
fill_value=_default_fill_value, method=None, limit=None, fill_axis=0,
35083508
broadcast_axis=None):
35093509
return super(DataFrame, self).align(other, join=join, axis=axis,
35103510
level=level, copy=copy,

pandas/core/generic.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
from pandas import compat
5151
from pandas.compat.numpy import function as nv
5252
from pandas.compat import (map, zip, lzip, lrange, string_types, to_str,
53-
isidentifier, set_function_name, cPickle as pkl)
53+
isidentifier, set_function_name, cPickle as pkl,
54+
_default_fill_value)
5455
from pandas.core.ops import _align_method_FRAME
5556
import pandas.core.nanops as nanops
5657
from pandas.util._decorators import (Appender, Substitution,
@@ -3660,7 +3661,7 @@ def reindex(self, *args, **kwargs):
36603661
copy = kwargs.pop('copy', True)
36613662
limit = kwargs.pop('limit', None)
36623663
tolerance = kwargs.pop('tolerance', None)
3663-
fill_value = kwargs.pop('fill_value', np.nan)
3664+
fill_value = kwargs.pop('fill_value', _default_fill_value)
36643665

36653666
# Series.reindex doesn't use / need the axis kwarg
36663667
# We pop and ignore it here, to make writing Series/Frame generic code
@@ -3790,7 +3791,9 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True,
37903791
return self._reindex_with_indexers({axis: [new_index, indexer]},
37913792
fill_value=fill_value, copy=copy)
37923793

3793-
def _reindex_with_indexers(self, reindexers, fill_value=np.nan, copy=False,
3794+
def _reindex_with_indexers(self, reindexers,
3795+
fill_value=_default_fill_value,
3796+
copy=False,
37943797
allow_dups=False):
37953798
"""allow_dups indicates an internal call here """
37963799

@@ -7209,7 +7212,7 @@ def ranker(data):
72097212

72107213
@Appender(_shared_docs['align'] % _shared_doc_kwargs)
72117214
def align(self, other, join='outer', axis=None, level=None, copy=True,
7212-
fill_value=None, method=None, limit=None, fill_axis=0,
7215+
fill_value=_default_fill_value, method=None, limit=None, fill_axis=0,
72137216
broadcast_axis=None):
72147217
from pandas import DataFrame, Series
72157218
method = missing.clean_fill_method(method)
@@ -7359,6 +7362,9 @@ def _align_series(self, other, join='outer', axis=None, level=None,
73597362
right = other.reindex(join_index, level=level)
73607363

73617364
# fill
7365+
if fill_value is _default_fill_value:
7366+
fill_value = None
7367+
73627368
fill_na = notna(fill_value) or (method is not None)
73637369
if fill_na:
73647370
left = left.fillna(fill_value, method=method, limit=limit,

pandas/core/series.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
from pandas import compat
5757
from pandas.io.formats.terminal import get_terminal_size
5858
from pandas.compat import (
59-
zip, u, OrderedDict, StringIO, range, get_range_parameters, PY36)
59+
zip, u, OrderedDict, StringIO, range, get_range_parameters, PY36, _default_fill_value)
6060
from pandas.compat.numpy import function as nv
6161

6262
import pandas.core.ops as ops
@@ -3216,7 +3216,10 @@ def _reindex_indexer(self, new_index, indexer, copy):
32163216
return self.copy()
32173217
return self
32183218

3219-
new_values = algorithms.take_1d(self._values, indexer)
3219+
from pandas.core.dtypes.missing import na_value_for_dtype
3220+
fill_value = na_value_for_dtype(self.dtype)
3221+
new_values = algorithms.take(self._values, indexer,
3222+
fill_value=fill_value)
32203223
return self._constructor(new_values, index=new_index)
32213224

32223225
def _needs_reindex_multi(self, axes, method, level):
@@ -3227,7 +3230,7 @@ def _needs_reindex_multi(self, axes, method, level):
32273230

32283231
@Appender(generic._shared_docs['align'] % _shared_doc_kwargs)
32293232
def align(self, other, join='outer', axis=None, level=None, copy=True,
3230-
fill_value=None, method=None, limit=None, fill_axis=0,
3233+
fill_value=_default_fill_value, method=None, limit=None, fill_axis=0,
32313234
broadcast_axis=None):
32323235
return super(Series, self).align(other, join=join, axis=axis,
32333236
level=level, copy=copy,

pandas/tests/extension/decimal/test_decimal.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,25 @@ class TestReshaping(BaseDecimal, base.BaseReshapingTests):
108108

109109

110110
class TestGetitem(BaseDecimal, base.BaseGetitemTests):
111-
pass
111+
112+
def test_take_basic(self):
113+
ea = DecimalArray([decimal.Decimal('1'),
114+
decimal.Decimal('2'),
115+
decimal.Decimal('3')])
116+
result = ea.take([1, 2, -1])
117+
expected = DecimalArray([decimal.Decimal('2'),
118+
decimal.Decimal('3'),
119+
decimal.Decimal('3')])
120+
self.assert_extension_array_equal(result, expected)
121+
122+
result = ea.take([1, 2, -1], fill_value=ea.dtype.na_value)
123+
expected = DecimalArray([decimal.Decimal('2'),
124+
decimal.Decimal('3'),
125+
decimal.Decimal('NaN')])
126+
self.assert_extension_array_equal(result, expected)
127+
128+
result = pd.Series(ea).reindex([1, 2, -1]).values
129+
self.assert_extension_array_equal(result, expected)
112130

113131

114132
class TestMissing(BaseDecimal, base.BaseMissingTests):

0 commit comments

Comments
 (0)