|
12 | 12 | from pandas.errors import AbstractMethodError
|
13 | 13 | from pandas.compat.numpy import function as nv
|
14 | 14 | from pandas.compat import set_function_name, PY3
|
15 |
| -from pandas.core.dtypes.common import is_list_like |
16 | 15 | from pandas.core import ops
|
| 16 | +from pandas.core.dtypes.common import is_list_like |
17 | 17 |
|
18 | 18 | _not_implemented_message = "{} does not implement {}."
|
19 | 19 |
|
@@ -88,16 +88,19 @@ class ExtensionArray(object):
|
88 | 88 | # Constructors
|
89 | 89 | # ------------------------------------------------------------------------
|
90 | 90 | @classmethod
|
91 |
| - def _from_sequence(cls, scalars, copy=False): |
| 91 | + def _from_sequence(cls, scalars, dtype=None, copy=False): |
92 | 92 | """Construct a new ExtensionArray from a sequence of scalars.
|
93 | 93 |
|
94 | 94 | Parameters
|
95 | 95 | ----------
|
96 | 96 | scalars : Sequence
|
97 | 97 | Each element will be an instance of the scalar type for this
|
98 | 98 | array, ``cls.dtype.type``.
|
| 99 | + dtype : dtype, optional |
| 100 | + Construct for this particular dtype. This should be a Dtype |
| 101 | + compatible with the ExtensionArray. |
99 | 102 | copy : boolean, default False
|
100 |
| - if True, copy the underlying data |
| 103 | + If True, copy the underlying data. |
101 | 104 | Returns
|
102 | 105 | -------
|
103 | 106 | ExtensionArray
|
@@ -378,7 +381,7 @@ def fillna(self, value=None, method=None, limit=None):
|
378 | 381 | func = pad_1d if method == 'pad' else backfill_1d
|
379 | 382 | new_values = func(self.astype(object), limit=limit,
|
380 | 383 | mask=mask)
|
381 |
| - new_values = self._from_sequence(new_values) |
| 384 | + new_values = self._from_sequence(new_values, dtype=self.dtype) |
382 | 385 | else:
|
383 | 386 | # fill with value
|
384 | 387 | new_values = self.copy()
|
@@ -407,7 +410,7 @@ def unique(self):
|
407 | 410 | from pandas import unique
|
408 | 411 |
|
409 | 412 | uniques = unique(self.astype(object))
|
410 |
| - return self._from_sequence(uniques) |
| 413 | + return self._from_sequence(uniques, dtype=self.dtype) |
411 | 414 |
|
412 | 415 | def _values_for_factorize(self):
|
413 | 416 | # type: () -> Tuple[ndarray, Any]
|
@@ -559,7 +562,7 @@ def take(self, indices, allow_fill=False, fill_value=None):
|
559 | 562 |
|
560 | 563 | result = take(data, indices, fill_value=fill_value,
|
561 | 564 | allow_fill=allow_fill)
|
562 |
| - return self._from_sequence(result) |
| 565 | + return self._from_sequence(result, dtype=self.dtype) |
563 | 566 | """
|
564 | 567 | # Implementer note: The `fill_value` parameter should be a user-facing
|
565 | 568 | # value, an instance of self.dtype.type. When passed `fill_value=None`,
|
@@ -634,6 +637,7 @@ class ExtensionOpsMixin(object):
|
634 | 637 | """
|
635 | 638 | A base class for linking the operators to their dunder names
|
636 | 639 | """
|
| 640 | + |
637 | 641 | @classmethod
|
638 | 642 | def _add_arithmetic_ops(cls):
|
639 | 643 | cls.__add__ = cls._create_arithmetic_method(operator.add)
|
|
0 commit comments