diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 4704be34171ed6..6b3c8957ccf65b 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -8,9 +8,10 @@ v0.24.0 New features ~~~~~~~~~~~~ + - ``ExcelWriter`` now accepts ``mode`` as a keyword argument, enabling append to existing workbooks when using the ``openpyxl`` engine (:issue:`3441`) -.. _whatsnew_0240.enhancements.extension_array_operators +.. _whatsnew_0240.enhancements.extension_array_operators: ``ExtensionArray`` operator support ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -26,6 +27,56 @@ See the :ref:`ExtensionArray Operator Support ` documentation section for details on both ways of adding operator support. +.. _whatsnew_0240.enhancements.intna: + +Integer NA Support +^^^^^^^^^^^^^^^^^^ + +TODO(update docs) +Pandas has gained the ability to hold integer dtypes with missing values. This long requested feature is enable thru the use of ``ExtensionTypes`` . Here is an example of usage. + +We can construct a ``Series`` with the specified dtype. The dtype string ``Int64`` is a pandas ``ExtensionDtype``. Specifying an list or array using the traditional missing value +marker of ``np.nan`` will infer to integer dtype. The display of the ``Series`` will also use the ``NaN`` to indicate missing values in string outputs. + +.. ipython:: python + + s = pd.Series([1, 2, np.nan], dtype='Int64') + s + + +Operations on these dtypes will propagate ``NaN`` as other pandas operations. + +.. ipython:: python + + # arithmetic + s + 1 + + # comparison + s == 1 + + # indexing + s.iloc[1:3] + + # operate with other dtypes + s + s.iloc[1:3] + +These dtypes can operate as part of ``DataFrames`` as well. + +.. ipython:: python + + df = pd.DataFrame({'A': s, 'B': [1, 2, 3], 'C': list('aab')}) + df + df.dtypes + + +These dtypes can be merged & reshaped & casted as well. + +.. ipython:: python + + pd.concat([df[['A']], df[['B', 'C']]], axis=1).dtypes + df['A'].astype(float) + + .. _whatsnew_0240.enhancements.other: Other Enhancements @@ -134,6 +185,7 @@ Previous Behavior: ExtensionType Changes ^^^^^^^^^^^^^^^^^^^^^ +- ``ExtensionArray`` has gained the abstract methods ``.dropna()`` (:issue:`21185`) - ``ExtensionDtype`` has gained the ability to instantiate from string dtypes, e.g. ``decimal`` would instantiate a registered ``DecimalDtype``; furthermore the ``ExtensionDtype`` has gained the method ``construct_array_type`` (:issue:`21185`) - The ``ExtensionArray`` constructor, ``_from_sequence`` now take the keyword arg ``copy=False`` (:issue:`21185`) diff --git a/pandas/core/arrays/__init__.py b/pandas/core/arrays/__init__.py index 1b8a43d4293a58..6e8dafd125bfcc 100644 --- a/pandas/core/arrays/__init__.py +++ b/pandas/core/arrays/__init__.py @@ -1,6 +1,9 @@ from .base import (ExtensionArray, # noqa + ExtensionOpsMixin, ExtensionScalarOpsMixin) from .categorical import Categorical # noqa from .datetimes import DatetimeArrayMixin # noqa from .period import PeriodArrayMixin # noqa from .timedelta import TimedeltaArrayMixin # noqa +from .integer import ( # noqa + IntegerArray, to_integer_array) diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index fe4e461b0bd4f6..c0697dd29e4d06 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -12,8 +12,8 @@ from pandas.errors import AbstractMethodError from pandas.compat.numpy import function as nv from pandas.compat import set_function_name, PY3 -from pandas.core.dtypes.common import is_list_like from pandas.core import ops +from pandas.core.dtypes.common import is_list_like _not_implemented_message = "{} does not implement {}." @@ -88,7 +88,7 @@ class ExtensionArray(object): # Constructors # ------------------------------------------------------------------------ @classmethod - def _from_sequence(cls, scalars, copy=False): + def _from_sequence(cls, scalars, dtype=None, copy=False): """Construct a new ExtensionArray from a sequence of scalars. Parameters @@ -96,6 +96,8 @@ def _from_sequence(cls, scalars, copy=False): scalars : Sequence Each element will be an instance of the scalar type for this array, ``cls.dtype.type``. + dtype : Dtype, optional + consruct for this particular dtype copy : boolean, default False if True, copy the underlying data Returns @@ -378,7 +380,7 @@ def fillna(self, value=None, method=None, limit=None): func = pad_1d if method == 'pad' else backfill_1d new_values = func(self.astype(object), limit=limit, mask=mask) - new_values = self._from_sequence(new_values) + new_values = self._from_sequence(new_values, dtype=self.dtype) else: # fill with value new_values = self.copy() @@ -407,7 +409,7 @@ def unique(self): from pandas import unique uniques = unique(self.astype(object)) - return self._from_sequence(uniques) + return self._from_sequence(uniques, dtype=self.dtype) def _values_for_factorize(self): # type: () -> Tuple[ndarray, Any] @@ -559,7 +561,7 @@ def take(self, indices, allow_fill=False, fill_value=None): result = take(data, indices, fill_value=fill_value, allow_fill=allow_fill) - return self._from_sequence(result) + return self._from_sequence(result, dtype=self.dtype) """ # Implementer note: The `fill_value` parameter should be a user-facing # value, an instance of self.dtype.type. When passed `fill_value=None`, diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 0252b5b52ae945..d26e8ca1607059 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -487,8 +487,8 @@ def _constructor(self): return Categorical @classmethod - def _from_sequence(cls, scalars): - return Categorical(scalars) + def _from_sequence(cls, scalars, dtype=None, copy=False): + return Categorical(scalars, dtype=dtype) def copy(self): """ Copy constructor. """ diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py new file mode 100644 index 00000000000000..46e29b13d0a812 --- /dev/null +++ b/pandas/core/arrays/integer.py @@ -0,0 +1,529 @@ +import sys +import warnings +import numpy as np + +from pandas.compat import u +from pandas.core.dtypes.generic import ABCSeries, ABCIndexClass +from pandas.util._decorators import cache_readonly +from pandas.compat import set_function_name +from pandas.api.types import (is_integer, is_scalar, is_float, + is_float_dtype, is_integer_dtype, + is_object_dtype, + is_list_like, + infer_dtype) +from pandas.core.arrays import ExtensionArray, ExtensionOpsMixin +from pandas.core.dtypes.base import ExtensionDtype +from pandas.core.dtypes.dtypes import registry +from pandas.core.dtypes.missing import isna, notna +from pandas.io.formats.printing import ( + format_object_summary, format_object_attrs, default_pprint) + + +class IntegerDtype(ExtensionDtype): + type = None + na_value = np.nan + + @cache_readonly + def is_signed_integer(self): + return self.kind == 'i' + + @cache_readonly + def is_unsigned_integer(self): + return self.kind == 'u' + + @cache_readonly + def numpy_dtype(self): + """ Return an instance of our numpy dtype """ + return np.dtype(self.type) + + @cache_readonly + def kind(self): + return self.numpy_dtype.kind + + @classmethod + def construct_array_type(cls): + """Return the array type associated with this dtype + + Returns + ------- + type + """ + return IntegerArray + + @classmethod + def construct_from_string(cls, string): + """ + Construction from a string, raise a TypeError if not + possible + """ + if string == cls.name: + return cls() + raise TypeError("Cannot construct a '{}' from " + "'{}'".format(cls, string)) + + +def to_integer_array(values): + """ + Parameters + ---------- + values : 1D list-like + + Returns + ------- + infer and return an integer array + + Raises + ------ + TypeError if incompatible types + """ + values = np.array(values, copy=False) + try: + dtype = _dtypes[str(values.dtype)] + except KeyError: + if is_float_dtype(values): + return IntegerArray(values) + + raise TypeError("Incompatible dtype for {}".format(values.dtype)) + return IntegerArray(values, dtype=dtype, copy=False) + + +def coerce_to_array(values, dtype, mask=None, copy=False): + """ + Coerce the input values array to numpy arrays with a mask + + Parameters + ---------- + values : 1D list-like + dtype : integer dtype + mask : boolean 1D array, optional + copy : boolean, default False + if True, copy the input + + Returns + ------- + tuple of (values, mask) + """ + + if isinstance(values, IntegerArray): + values, mask = values.data, values.mask + if copy: + values = values.copy() + mask = mask.copy() + return values, mask + + values = np.array(values, copy=copy) + if is_object_dtype(values): + inferred_type = infer_dtype(values) + if inferred_type not in ['floating', 'integer', + 'mixed-integer', 'mixed-integer-float']: + raise TypeError("{} cannot be converted to an IntegerDtype".format( + values.dtype)) + + elif not (is_integer_dtype(values) or is_float_dtype(values)): + raise TypeError("{} cannot be converted to an IntegerDtype".format( + values.dtype)) + + if mask is None: + mask = isna(values) + else: + assert len(mask) == len(values) + + if not values.ndim == 1: + raise TypeError("values must be a 1D list-like") + if not mask.ndim == 1: + raise TypeError("mask must be a 1D list-like") + + # avoid float->int numpy conversion issues + if is_object_dtype(values): + mask |= isna(values) + + # infer dtype if needed + if dtype is None: + if is_integer_dtype(values): + dtype = values.dtype + else: + dtype = np.dtype('int64') + else: + dtype = dtype.type + + # we copy as need to coerce here + if mask.any(): + values = values.copy() + values[mask] = 1 + + values = values.astype(dtype) + else: + values = values.astype(dtype, copy=False) + + return values, mask + + +class IntegerArray(ExtensionArray, ExtensionOpsMixin): + """ + We represent an IntegerArray with 2 numpy arrays + - data: contains a numpy integer array of the appropriate dtype + - mask: a boolean array holding a mask on the data, False is missing + """ + + @cache_readonly + def dtype(self): + return _dtypes[str(self.data.dtype)] + + def __init__(self, values, mask=None, dtype=None, copy=False): + self.data, self.mask = coerce_to_array( + values, dtype=dtype, mask=mask, copy=copy) + + @classmethod + def _from_sequence(cls, scalars, mask=None, dtype=None, copy=False): + return cls(scalars, mask=mask, dtype=dtype, copy=copy) + + @classmethod + def _from_factorized(cls, values, original): + return cls(values, dtype=original.dtype) + + def __getitem__(self, item): + if is_integer(item): + if self.mask[item]: + return self.dtype.na_value + return self.data[item] + return type(self)(self.data[item], + mask=self.mask[item], + dtype=self.dtype) + + def _coerce_to_ndarray(self): + """ coerce to an ndarary, preserving my scalar types """ + + # TODO(jreback) make this better + data = self.data.astype(object) + data[self.mask] = self._na_value + return data + + def __array__(self, dtype=None): + """ + the array interface, return my values + We return an object array here to preserve our scalar values + """ + return self._coerce_to_ndarray() + + def __iter__(self): + """Iterate over elements of the array. + + """ + # This needs to be implemented so that pandas recognizes extension + # arrays as list-like. The default implementation makes successive + # calls to ``__getitem__``, which may be slower than necessary. + for i in range(len(self)): + if self.mask[i]: + yield self.dtype.na_value + else: + yield self.data[i] + + def _formatting_values(self): + # type: () -> np.ndarray + return self._coerce_to_ndarray() + + def take(self, indexer, allow_fill=False, fill_value=None): + from pandas.api.extensions import take + + # we always fill with 1 internally + # to avoid upcasting + data_fill_value = 1 if isna(fill_value) else fill_value + result = take(self.data, indexer, fill_value=data_fill_value, + allow_fill=allow_fill) + + mask = take(self.mask, indexer, fill_value=True, + allow_fill=allow_fill) + + # if we are filling + # we only fill where the indexer is null + # not existing missing values + # TODO(jreback) what if we have a non-na float as a fill value? + if allow_fill and notna(fill_value): + fill_mask = np.asarray(indexer) == -1 + result[fill_mask] = fill_value + mask = mask ^ fill_mask + + return type(self)(result, mask=mask, dtype=self.dtype) + + def copy(self, deep=False): + if deep: + return type(self)( + self.data.copy(), mask=self.mask.copy(), dtype=self.dtype) + return type(self)(self) + + def __setitem__(self, key, value): + _is_scalar = is_scalar(value) + if _is_scalar: + value = [value] + value, mask = coerce_to_array(value, dtype=self.dtype) + + if _is_scalar: + value = value[0] + mask = mask[0] + + self.data[key] = value + self.mask[key] = mask + + def __len__(self): + return len(self.data) + + def __repr__(self): + """ + Return a string representation for this object. + + Invoked by unicode(df) in py2 only. Yields a Unicode String in both + py2/py3. + """ + klass = self.__class__.__name__ + data = format_object_summary(self, default_pprint, False) + attrs = format_object_attrs(self) + space = " " + + prepr = (u(",%s") % + space).join(u("%s=%s") % (k, v) for k, v in attrs) + + res = u("%s(%s%s)") % (klass, data, prepr) + + return res + + @property + def nbytes(self): + return self.data.nbytes + self.mask.nbytes + + def isna(self): + return self.mask + + @property + def _na_value(self): + return np.nan + + @classmethod + def _concat_same_type(cls, to_concat): + data = np.concatenate([x.data for x in to_concat]) + mask = np.concatenate([x.mask for x in to_concat]) + return cls(data, mask=mask, dtype=to_concat[0].dtype) + + def astype(self, dtype, copy=True): + """Cast to a NumPy array with 'dtype'. + + Parameters + ---------- + dtype : str or dtype + Typecode or data-type to which the array is cast. + copy : bool, default True + Whether to copy the data, even if not necessary. If False, + a copy is made only if the old dtype does not match the + new dtype. + + Returns + ------- + array : ndarray + NumPy ndarray with 'dtype' for its dtype. + + Raises + ------ + TypeError + if incompatible type with an IntegerDtype, equivalent of same_kind + casting + """ + + # if we are astyping to an existing IntegerDtype we can fastpath + if isinstance(dtype, IntegerDtype): + result = self.data.astype(dtype.numpy_dtype, + casting='same_kind', copy=False) + return type(self)(result, mask=self.mask, + dtype=dtype, copy=False) + + # coerce + data = self._coerce_to_ndarray() + return data.astype(dtype=dtype, copy=False) + + @property + def _ndarray_values(self): + # type: () -> np.ndarray + """Internal pandas method for lossy conversion to a NumPy ndarray. + + This method is not part of the pandas interface. + + The expectation is that this is cheap to compute, and is primarily + used for interacting with our indexers. + """ + return self.data + + def value_counts(self, dropna=True): + """ + Returns a Series containing counts of each category. + + Every category will have an entry, even those with a count of 0. + + Parameters + ---------- + dropna : boolean, default True + Don't include counts of NaN. + + Returns + ------- + counts : Series + + See Also + -------- + Series.value_counts + + """ + + from pandas import Index, Series + + # compute counts on the data with no nans + data = self.data[~self.mask] + value_counts = Index(data).value_counts() + + array = value_counts.values + index = value_counts.index + + # if we want nans, count the mask + if not dropna: + array = np.append(array, [self.mask.sum()]) + + # TODO(extension) + # should this be an and Index backed by the + # Array type? + index = index.astype(object).append(Index([np.nan])) + + return Series(array, index=index) + + def _values_for_argsort(self): + # type: () -> ndarray + """Return values for sorting. + + Returns + ------- + ndarray + The transformed values should maintain the ordering between values + within the array. + + See Also + -------- + ExtensionArray.argsort + """ + data = self.data.copy() + data[self.mask] = data.min() - 1 + return data + + @classmethod + def _create_comparison_method(cls, op): + def cmp_method(self, other): + + op_str = op.__name__ + mask = None + if isinstance(other, IntegerArray): + other, mask = other.data, other.mask + elif is_list_like(other): + other = np.asarray(other) + if other.ndim > 0 and len(self) != len(other): + raise ValueError('Lengths must match to compare') + + # numpy will show a DeprecationWarning on invalid elementwise + # comparisons, this will raise in the future + with warnings.catch_warnings(record=True): + with np.errstate(all='ignore'): + result = op(self.data, other) + + # nans propagate + if mask is None: + mask = self.mask + else: + mask = self.mask | mask + + result[mask] = True if op_str == 'ne' else False + return result + + name = '__{name}__'.format(name=op.__name__) + return set_function_name(cmp_method, name, cls) + + def _maybe_mask_result(self, result, mask): + # may need to fill infs + # and mask wraparound + if is_float_dtype(result): + mask |= (result == np.inf) | (result == -np.inf) + + return type(self)(result, mask=mask, dtype=self.dtype, copy=False) + + @classmethod + def _create_arithmetic_method(cls, op): + def integer_arithmetic_method(self, other): + + op_str = op.__name__ + mask = None + if isinstance(other, (ABCSeries, ABCIndexClass)): + other = getattr(other, 'values', other) + + if isinstance(other, IntegerArray): + other, mask = other.data, other.mask + elif getattr(other, 'ndim', 0) > 1: + raise TypeError("can only perform ops with 1-d structures") + elif is_list_like(other): + other = np.asarray(other) + if not other.ndim: + other = other.item() + elif other.ndim == 1: + if not (is_float_dtype(other) or is_integer_dtype(other)): + raise TypeError( + "can only perform ops with numeric values") + else: + if not (is_float(other) or is_integer(other)): + raise TypeError("can only perform ops with numeric values") + + # nans propagate + if mask is None: + mask = self.mask + else: + mask = self.mask | mask + + with np.errstate(all='ignore'): + result = op(self.data, other) + + # divmod returns a tuple + if op_str == 'divmod': + div, mod = result + return (self._maybe_mask_result(div, mask), + self._maybe_mask_result(mod, mask)) + + return self._maybe_mask_result(result, mask) + + name = '__{name}__'.format(name=op.__name__) + return set_function_name(integer_arithmetic_method, name, cls) + + +IntegerArray._add_arithmetic_ops() +IntegerArray._add_comparison_ops() + + +module = sys.modules[__name__] + + +# create the Dtype +_dtypes = {} +for dtype in ['int8', 'int16', 'int32', 'int64', + 'uint8', 'uint16', 'uint32', 'uint64']: + + if dtype.startswith('u'): + name = "U{}".format(dtype[1:].capitalize()) + else: + name = dtype.capitalize() + classname = "{}Dtype".format(name) + attributes_dict = {'type': getattr(np, dtype), + 'name': name} + dtype_type = type(classname, (IntegerDtype, ), attributes_dict) + setattr(module, classname, dtype_type) + + # register + registry.register(dtype_type) + _dtypes[dtype] = dtype_type() + + +def make_data(): + return (list(range(8)) + + [np.nan] + + list(range(10, 98)) + + [np.nan] + + [99, 100]) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 2cd8144e43ceac..f3b1f2736c1c56 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -651,7 +651,8 @@ def astype_nansafe(arr, dtype, copy=True): # dispatch on extension dtype if needed if is_extension_array_dtype(dtype): - return dtype.array_type._from_sequence(arr, copy=copy) + return dtype.construct_array_type()._from_sequence( + arr, dtype=dtype, copy=copy) if not isinstance(dtype, np.dtype): dtype = pandas_dtype(dtype) diff --git a/pandas/core/missing.py b/pandas/core/missing.py index e9b9a734ec5f58..f5fb0070ffc4bf 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -638,7 +638,8 @@ def fill_zeros(result, x, y, name, fill): # if we have a fill of inf, then sign it correctly # (GH 6178 and PR 9308) if np.isinf(fill): - signs = np.sign(y if name.startswith(('r', '__r')) else x) + signs = y if name.startswith(('r', '__r')) else x + signs = np.sign(signs.astype('float', copy=False)) negative_inf_mask = (signs.ravel() < 0) & mask np.putmask(result, negative_inf_mask, -fill) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index fa6d88648cc636..1631f36d6a0631 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -135,7 +135,20 @@ def rfloordiv(left, right): def rmod(left, right): - return right % left + # cannot use % as an operand becaue this + # is valid on a string (as a formatting type) + # instead we must try the ops and catch + # the exceptions here which guards against an + # undefined __rmod__ op, we transform this to a + # TypeError as expected + try: + result = left.__rmod__(right) + if result is NotImplemented: + result = right.__mod__(left) + return result + except AttributeError: + raise TypeError("{typ} cannot perform the operation mod".format( + typ=type(left).__name__)) def rdivmod(left, right): @@ -1002,16 +1015,37 @@ def dispatch_to_extension_op(op, left, right): # The op calls will raise TypeError if the op is not defined # on the ExtensionArray + # TODO(jreback) + # we need to listify to avoid ndarray, or non-same-type extension array + # dispatching + if is_extension_array_dtype(left): - res_values = op(left.values, right) + + new_left = left.values + if (isinstance(right, np.ndarray) or + (is_extension_array_dtype(right) and + type(left) != type(right))): + new_right = list(right) + else: + new_right = right + else: - # We know that left is not ExtensionArray and is Series and right is - # ExtensionArray. Want to force ExtensionArray op to get called - res_values = op(list(left.values), right.values) + new_left = list(left.values) + new_right = right + + res_values = op(new_left, new_right) res_name = get_op_result_name(left, right) - return left._constructor(res_values, index=left.index, - name=res_name) + + # divmod returns a tuple + def compute_result(res_values): + return left._constructor(res_values, index=left.index, + name=res_name) + + if op.__name__ == 'divmod': + return map(compute_result, res_values) + + return compute_result(res_values) def _arith_method_SERIES(cls, op, special): @@ -1028,7 +1062,6 @@ def _arith_method_SERIES(cls, op, special): def na_op(x, y): import pandas.core.computation.expressions as expressions - try: result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs) except TypeError: @@ -1049,6 +1082,20 @@ def na_op(x, y): return result def safe_na_op(lvalues, rvalues): + """ + return the result of evaluating na_op on the passed in values + + try coercion to object type if the native types are not compatible + + Parameters + ---------- + lvalues : array-like + rvalues : array-like + + Raises + ------ + invalid operation raises TypeError + """ try: with np.errstate(all='ignore'): return na_op(lvalues, rvalues) @@ -1059,14 +1106,21 @@ def safe_na_op(lvalues, rvalues): raise def wrapper(left, right): - if isinstance(right, ABCDataFrame): return NotImplemented left, right = _align_method_SERIES(left, right) res_name = get_op_result_name(left, right) - if is_datetime64_dtype(left) or is_datetime64tz_dtype(left): + if is_categorical_dtype(left): + raise TypeError("{typ} cannot perform the operation " + "{op}".format(typ=type(left).__name__, op=str_rep)) + + elif (is_extension_array_dtype(left) or + is_extension_array_dtype(right)): + return dispatch_to_extension_op(op, left, right) + + elif is_datetime64_dtype(left) or is_datetime64tz_dtype(left): result = dispatch_to_index_op(op, left, right, pd.DatetimeIndex) return construct_result(left, result, index=left.index, name=res_name, @@ -1078,15 +1132,6 @@ def wrapper(left, right): index=left.index, name=res_name, dtype=result.dtype) - elif is_categorical_dtype(left): - raise TypeError("{typ} cannot perform the operation " - "{op}".format(typ=type(left).__name__, op=str_rep)) - - elif (is_extension_array_dtype(left) or - (is_extension_array_dtype(right) and - not is_categorical_dtype(right))): - return dispatch_to_extension_op(op, left, right) - lvalues = left.values rvalues = right if isinstance(rvalues, ABCSeries): @@ -1165,6 +1210,14 @@ def na_op(x, y): # The `not is_scalar(y)` check excludes the string "category" return op(y, x) + # handle extension array ops + # TODO(extension) + # the ops *between* non-same-type extension arrays are not + # very well defined + elif (is_extension_array_dtype(x) or + is_extension_array_dtype(y)): + return op(x, y) + elif is_object_dtype(x.dtype): result = _comp_method_OBJECT_ARRAY(op, x, y) diff --git a/pandas/core/series.py b/pandas/core/series.py index 93c1f866cad176..4c9adb3f6a72a2 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4096,7 +4096,7 @@ def _try_cast(arr, take_fast_path): elif is_extension_array_dtype(dtype): # create an extension array from its dtype array_type = dtype.construct_array_type() - subarr = array_type(subarr, copy=copy) + subarr = array_type(subarr, dtype=dtype, copy=copy) elif dtype is not None and raise_cast_failure: raise diff --git a/pandas/tests/extension/base/__init__.py b/pandas/tests/extension/base/__init__.py index 640b894e2245f9..b6b81bb941a59c 100644 --- a/pandas/tests/extension/base/__init__.py +++ b/pandas/tests/extension/base/__init__.py @@ -47,7 +47,7 @@ class TestMyDtype(BaseDtypeTests): from .groupby import BaseGroupbyTests # noqa from .interface import BaseInterfaceTests # noqa from .methods import BaseMethodsTests # noqa -from .ops import BaseArithmeticOpsTests, BaseComparisonOpsTests # noqa +from .ops import BaseArithmeticOpsTests, BaseComparisonOpsTests, BaseOpsUtil # noqa from .missing import BaseMissingTests # noqa from .reshaping import BaseReshapingTests # noqa from .setitem import BaseSetitemTests # noqa diff --git a/pandas/tests/extension/base/getitem.py b/pandas/tests/extension/base/getitem.py index e9df49780f1192..886a0f66b5f667 100644 --- a/pandas/tests/extension/base/getitem.py +++ b/pandas/tests/extension/base/getitem.py @@ -226,12 +226,14 @@ def test_reindex(self, data, na_value): n = len(data) result = s.reindex([-1, 0, n]) expected = pd.Series( - data._from_sequence([na_value, data[0], na_value]), + data._from_sequence([na_value, data[0], na_value], + dtype=s.dtype), index=[-1, 0, n]) self.assert_series_equal(result, expected) result = s.reindex([n, n + 1]) - expected = pd.Series(data._from_sequence([na_value, na_value]), + expected = pd.Series(data._from_sequence([na_value, na_value], + dtype=s.dtype), index=[n, n + 1]) self.assert_series_equal(result, expected) diff --git a/pandas/tests/extension/base/ops.py b/pandas/tests/extension/base/ops.py index 659b9757ac1e33..135abc7d4d4c53 100644 --- a/pandas/tests/extension/base/ops.py +++ b/pandas/tests/extension/base/ops.py @@ -7,6 +7,7 @@ class BaseOpsUtil(BaseExtensionTests): + def get_op_from_name(self, op_name): short_opname = op_name.strip('_') try: @@ -32,6 +33,17 @@ def _check_op(self, s, op, other, exc=NotImplementedError): with pytest.raises(exc): op(s, other) + def _check_divmod_op(self, s, other, exc=NotImplementedError): + # divmod has multiple return values, so check separatly + if exc is None: + result_div, result_mod = divmod(s, other) + expected_div, expected_mod = s.combine(other, divmod) + self.assert_series_equal(result_div, expected_div) + self.assert_series_equal(result_mod, expected_mod) + else: + with pytest.raises(exc): + divmod(s, other) + class BaseArithmeticOpsTests(BaseOpsUtil): """Various Series and DataFrame arithmetic ops methods.""" @@ -50,8 +62,8 @@ def test_arith_array(self, data, all_arithmetic_operators): def test_divmod(self, data): s = pd.Series(data) - self._check_op(s, divmod, 1, exc=TypeError) - self._check_op(1, divmod, s, exc=TypeError) + self._check_divmod_op(s, 1, exc=TypeError) + self._check_divmod_op(1, s, exc=TypeError) def test_error(self, data, all_arithmetic_operators): # invalid ops diff --git a/pandas/tests/extension/base/reshaping.py b/pandas/tests/extension/base/reshaping.py index c83726c5278a5e..0340289e0b6740 100644 --- a/pandas/tests/extension/base/reshaping.py +++ b/pandas/tests/extension/base/reshaping.py @@ -82,7 +82,8 @@ def test_concat_columns(self, data, na_value): # non-aligned df2 = pd.DataFrame({'B': [1, 2, 3]}, index=[1, 2, 3]) expected = pd.DataFrame({ - 'A': data._from_sequence(list(data[:3]) + [na_value]), + 'A': data._from_sequence(list(data[:3]) + [na_value], + dtype=data.dtype), 'B': [np.nan, 1, 2, 3]}) result = pd.concat([df1, df2], axis=1) @@ -96,8 +97,10 @@ def test_align(self, data, na_value): r1, r2 = pd.Series(a).align(pd.Series(b, index=[1, 2, 3])) # Assumes that the ctor can take a list of scalars of the type - e1 = pd.Series(data._from_sequence(list(a) + [na_value])) - e2 = pd.Series(data._from_sequence([na_value] + list(b))) + e1 = pd.Series(data._from_sequence(list(a) + [na_value], + dtype=data.dtype)) + e2 = pd.Series(data._from_sequence([na_value] + list(b), + dtype=data.dtype)) self.assert_series_equal(r1, e1) self.assert_series_equal(r2, e2) @@ -109,8 +112,10 @@ def test_align_frame(self, data, na_value): ) # Assumes that the ctor can take a list of scalars of the type - e1 = pd.DataFrame({'A': data._from_sequence(list(a) + [na_value])}) - e2 = pd.DataFrame({'A': data._from_sequence([na_value] + list(b))}) + e1 = pd.DataFrame({'A': data._from_sequence(list(a) + [na_value], + dtype=data.dtype)}) + e2 = pd.DataFrame({'A': data._from_sequence([na_value] + list(b), + dtype=data.dtype)}) self.assert_frame_equal(r1, e1) self.assert_frame_equal(r2, e2) @@ -120,7 +125,8 @@ def test_align_series_frame(self, data, na_value): df = pd.DataFrame({"col": np.arange(len(ser) + 1)}) r1, r2 = ser.align(df) - e1 = pd.Series(data._from_sequence(list(data) + [na_value]), + e1 = pd.Series(data._from_sequence(list(data) + [na_value], + dtype=data.dtype), name=ser.name) self.assert_series_equal(r1, e1) @@ -153,7 +159,8 @@ def test_merge(self, data, na_value): res = pd.merge(df1, df2) exp = pd.DataFrame( {'int1': [1, 1, 2], 'int2': [1, 2, 3], 'key': [0, 0, 1], - 'ext': data._from_sequence([data[0], data[0], data[1]])}) + 'ext': data._from_sequence([data[0], data[0], data[1]], + dtype=data.dtype)}) self.assert_frame_equal(res, exp[['ext', 'int1', 'key', 'int2']]) res = pd.merge(df1, df2, how='outer') @@ -161,5 +168,6 @@ def test_merge(self, data, na_value): {'int1': [1, 1, 2, 3, np.nan], 'int2': [1, 2, 3, np.nan, 4], 'key': [0, 0, 1, 2, 3], 'ext': data._from_sequence( - [data[0], data[0], data[1], data[2], na_value])}) + [data[0], data[0], data[1], data[2], na_value], + dtype=data.dtype)}) self.assert_frame_equal(res, exp[['ext', 'int1', 'key', 'int2']]) diff --git a/pandas/tests/extension/decimal/array.py b/pandas/tests/extension/decimal/array.py index 33adebbbe57800..3c852e3ac44985 100644 --- a/pandas/tests/extension/decimal/array.py +++ b/pandas/tests/extension/decimal/array.py @@ -38,7 +38,7 @@ def construct_from_string(cls, string): class DecimalArray(ExtensionArray, ExtensionScalarOpsMixin): dtype = DecimalDtype() - def __init__(self, values, copy=False): + def __init__(self, values, dtype=None, copy=False): for val in values: if not isinstance(val, self.dtype.type): raise TypeError("All values must be of type " + @@ -54,8 +54,8 @@ def __init__(self, values, copy=False): # self._values = self.values = self.data @classmethod - def _from_sequence(cls, scalars, copy=False): - return cls(scalars) + def _from_sequence(cls, scalars, dtype=None, copy=False): + return cls(scalars, copy=copy) @classmethod def _from_factorized(cls, values, original): diff --git a/pandas/tests/extension/integer/__init__.py b/pandas/tests/extension/integer/__init__.py new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/pandas/tests/extension/integer/test_integer.py b/pandas/tests/extension/integer/test_integer.py new file mode 100644 index 00000000000000..8e3e6efea147a4 --- /dev/null +++ b/pandas/tests/extension/integer/test_integer.py @@ -0,0 +1,494 @@ +import numpy as np +import pandas as pd +import pandas.util.testing as tm +import pytest + +from pandas.tests.extension import base +from pandas.api.types import is_integer, is_scalar + +from pandas.core.arrays import ( + to_integer_array, IntegerArray) +from pandas.core.arrays.integer import ( + Int8Dtype, Int16Dtype, Int32Dtype, Int64Dtype, + UInt8Dtype, UInt16Dtype, UInt32Dtype, UInt64Dtype, + make_data) + + +@pytest.fixture(params=[Int8Dtype, Int16Dtype, Int32Dtype, Int64Dtype, + UInt8Dtype, UInt16Dtype, UInt32Dtype, UInt64Dtype]) +def dtype(request): + return request.param() + + +@pytest.fixture +def data(dtype): + return IntegerArray(make_data(), dtype=dtype) + + +@pytest.fixture +def data_missing(dtype): + return IntegerArray([np.nan, 1], dtype=dtype) + + +@pytest.fixture +def data_repeated(data): + def gen(count): + for _ in range(count): + yield data + yield gen + + +@pytest.fixture +def data_for_sorting(dtype): + return IntegerArray([1, 2, 0], dtype=dtype) + + +@pytest.fixture +def data_missing_for_sorting(dtype): + return IntegerArray([1, np.nan, 0], dtype=dtype) + + +@pytest.fixture +def na_cmp(): + # we are np.nan + return lambda x, y: np.isnan(x) and np.isnan(y) + + +@pytest.fixture +def na_value(): + return np.nan + + +@pytest.fixture +def data_for_grouping(dtype): + b = 1 + a = 0 + c = 2 + na = np.nan + return IntegerArray([b, b, na, na, a, a, b, c], dtype=dtype) + + +def test_dtypes(dtype): + # smoke tests on auto dtype construction + + if dtype.is_signed_integer: + assert np.dtype(dtype.type).kind == 'i' + else: + assert np.dtype(dtype.type).kind == 'u' + assert dtype.name is not None + + +class BaseInteger(object): + + def assert_series_equal(self, left, right, *args, **kwargs): + + left_na = left.isna() + right_na = right.isna() + + tm.assert_series_equal(left_na, right_na) + return tm.assert_series_equal(left[~left_na], + right[~right_na], + *args, **kwargs) + + def assert_frame_equal(self, left, right, *args, **kwargs): + # TODO(EA): select_dtypes + tm.assert_index_equal( + left.columns, right.columns, + exact=kwargs.get('check_column_type', 'equiv'), + check_names=kwargs.get('check_names', True), + check_exact=kwargs.get('check_exact', False), + check_categorical=kwargs.get('check_categorical', True), + obj='{obj}.columns'.format(obj=kwargs.get('obj', 'DataFrame'))) + + integers = (left.dtypes == 'integer').index + + for col in integers: + self.assert_series_equal(left[col], right[col], + *args, **kwargs) + + left = left.drop(columns=integers) + right = right.drop(columns=integers) + tm.assert_frame_equal(left, right, *args, **kwargs) + + +class TestDtype(BaseInteger, base.BaseDtypeTests): + + @pytest.mark.skip(reason="using multiple dtypes") + def test_is_dtype_unboxes_dtype(self): + # we have multiple dtypes, so skip + pass + + def test_array_type_with_arg(self, data, dtype): + assert dtype.construct_array_type() is IntegerArray + + +class TestArithmeticOps(BaseInteger, base.BaseArithmeticOpsTests): + + def _check_divmod_op(self, s, other, exc=None): + # TODO(jreback) + pytest.xfail(reason="divmod testing is not happening") + + def _check_op(self, s, op_name, other, exc=None): + op = self.get_op_from_name(op_name) + + result = op(s, other) + + # compute expected + mask = s.isna() + + # other array is an Integer + if isinstance(other, IntegerArray): + omask = getattr(other, 'mask', None) + mask = getattr(other, 'data', other) + if omask is not None: + mask |= omask + + # to compare properly, we convert the expected + # to float, mask to nans and convert infs + # if we have uints then we process as uints + # then conert to float + # and we ultimately want to create a IntArray + # for comparisons + rs = pd.Series(s.values.data) + expected = op(rs, other) + + # truediv can make infs + if op_name in ['__trvuediv__', '__rtruediv__', '__rdiv__', '__div__']: + fill_value = np.nan + else: + fill_value = 0 + + # mod/rmod turn floating 0 into NaN while + # integer works as expected (no nan) + if op_name in ['__mod__', '__rmod__']: + if is_scalar(other): + if other == 0: + expected[s.values == 0] = 0 + else: + expected = expected.fillna(0) + else: + expected[(s.values == 0) & + ((expected == 0) | expected.isna())] = 0 + + try: + expected[(expected == np.inf) | (expected == -np.inf)] = fill_value + original = expected + expected = expected.astype(s.dtype) + + except ValueError: + + expected = expected.astype(float) + expected[(expected == np.inf) | (expected == -np.inf)] = fill_value + original = expected + expected = expected.astype(s.dtype) + + expected[mask] = np.nan + + # assert that the expected astype is ok + # (skip for unsigned as they have wrap around) + if not s.dtype.is_unsigned_integer: + original = original.astype('float') + original[mask] = np.nan + self.assert_series_equal(original, expected.astype('float')) + + # assert our expected result + self.assert_series_equal(result, expected) + + def test_arith_integer_array(self, data, all_arithmetic_operators): + # we operate with a rhs of an integer array + + op = all_arithmetic_operators + s = pd.Series(data) + rhs = pd.Series([1] * len(data), dtype=data.dtype) + rhs.iloc[-1] = np.nan + + self._check_op(s, op, rhs) + + def test_arith_scalar(self, data, all_arithmetic_operators): + # scalar + op = all_arithmetic_operators + + # TODO(jreback), failing on rtruediv (can use super method) + if op == '__rtruediv__': + pytest.xfail("test_arith_scalar with rtruediv failing - fix me") + + s = pd.Series(data) + self._check_op(s, op, 1, exc=TypeError) + + def test_arith_array(self, data, all_arithmetic_operators): + # ndarray & other series + op = all_arithmetic_operators + + # TODO(jreback), failing on rtruediv (can use super method) + if op == '__rtruediv__': + pytest.xfail("test_arith_array with rtruediv failing - fix me") + + s = pd.Series(data) + other = np.ones(len(s), dtype=s.dtype.type) + self._check_op(s, op, other, exc=TypeError) + + @pytest.mark.xfail(reason="NIY") + def test_arith_coerce_scalar(self, data, all_arithmetic_operators): + + op = all_arithmetic_operators + s = pd.Series(data) + + rhs = 0.01 + self._check_op(s, op, rhs) + + def test_error(self, data, all_arithmetic_operators): + # invalid ops + + op = all_arithmetic_operators + s = pd.Series(data) + ops = getattr(s, op) + opa = getattr(data, op) + + # invalid scalars + with pytest.raises(TypeError): + ops('foo') + with pytest.raises(TypeError): + ops(pd.Timestamp('20180101')) + + # invalid array-likes + with pytest.raises(TypeError): + ops(pd.Series('foo', index=s.index)) + + if op != '__rpow__': + # TODO(extension) + # rpow with a datetimelike coerces the integer array incorrectly + with pytest.raises(TypeError): + ops(pd.Series(pd.date_range('20180101', periods=len(s)))) + + # 2d + with pytest.raises(TypeError): + opa(pd.DataFrame({'A': s})) + with pytest.raises(TypeError): + opa(np.arange(len(s)).reshape(-1, len(s))) + + +class TestComparisonOps(BaseInteger, base.BaseComparisonOpsTests): + + def _compare_other(self, s, data, op_name, other): + op = self.get_op_from_name(op_name) + + # array + result = op(s, other) + expected = pd.Series(op(data.data, other)) + + # fill the nan locations + expected[data.mask] = True if op_name == '__ne__' else False + + tm.assert_series_equal(result, expected) + + # series + s = pd.Series(data) + result = op(s, other) + + expected = pd.Series(data.data) + expected = op(expected, other) + + # fill the nan locations + expected[data.mask] = True if op_name == '__ne__' else False + + tm.assert_series_equal(result, expected) + + +class TestInterface(BaseInteger, base.BaseInterfaceTests): + + def test_repr_array(self, data): + result = repr(data) + + # not long + assert '...' not in result + + assert 'dtype=' in result + assert 'IntegerArray' in result + + def test_repr_array_long(self, data): + # some arrays may be able to assert a ... in the repr + with pd.option_context('display.max_seq_items', 1): + result = repr(data) + + assert '...' in result + assert 'length' in result + + +class TestConstructors(BaseInteger, base.BaseConstructorsTests): + + def test_from_dtype_from_float(self, data): + # construct from our dtype & string dtype + dtype = data.dtype + + # from float + expected = pd.Series(data) + result = pd.Series(np.array(data).astype('float'), dtype=str(dtype)) + self.assert_series_equal(result, expected) + + # from int / list + expected = pd.Series(data) + result = pd.Series(np.array(data).tolist(), dtype=str(dtype)) + self.assert_series_equal(result, expected) + + # from int / array + expected = pd.Series(data).dropna().reset_index(drop=True) + dropped = np.array(data.dropna()).astype(np.dtype((dtype.type))) + result = pd.Series(dropped, dtype=str(dtype)) + self.assert_series_equal(result, expected) + + +class TestReshaping(BaseInteger, base.BaseReshapingTests): + + def test_concat_mixed_dtypes(self, data): + # https://github.com/pandas-dev/pandas/issues/20762 + df1 = pd.DataFrame({'A': data[:3]}) + df2 = pd.DataFrame({"A": [1, 2, 3]}) + df3 = pd.DataFrame({"A": ['a', 'b', 'c']}).astype('category') + df4 = pd.DataFrame({"A": pd.SparseArray([1, 2, 3])}) + dfs = [df1, df2, df3, df4] + + # dataframes + result = pd.concat(dfs) + expected = pd.concat([x.astype(object) for x in dfs]) + self.assert_frame_equal(result, expected) + + # series + result = pd.concat([x['A'] for x in dfs]) + expected = pd.concat([x['A'].astype(object) for x in dfs]) + self.assert_series_equal(result, expected) + + result = pd.concat([df1, df2]) + expected = pd.concat([df1.astype('object'), df2.astype('object')]) + self.assert_frame_equal(result, expected) + + # concat of an Integer and Int coerces to object dtype + # TODO(jreback) once integrated this would + # be a result of Integer + result = pd.concat([df1['A'], df2['A']]) + expected = pd.concat([df1['A'].astype('object'), + df2['A'].astype('object')]) + self.assert_series_equal(result, expected) + + +class TestGetitem(BaseInteger, base.BaseGetitemTests): + pass + + +class TestMissing(BaseInteger, base.BaseMissingTests): + pass + + +class TestMethods(BaseInteger, base.BaseMethodsTests): + + @pytest.mark.xfail(reason="need a Index type with ExtensionArrays") + @pytest.mark.parametrize('dropna', [True, False]) + def test_value_counts(self, all_data, dropna): + all_data = all_data[:10] + if dropna: + other = np.array(all_data[~all_data.isna()]) + else: + other = all_data + + result = pd.Series(all_data).value_counts(dropna=dropna).sort_index() + expected = pd.Series(other).value_counts( + dropna=dropna).sort_index() + + self.assert_series_equal(result, expected) + + +class TestCasting(BaseInteger, base.BaseCastingTests): + pass + + +class TestGroupby(BaseInteger, base.BaseGroupbyTests): + pass + + +def test_frame_repr(data_missing): + + df = pd.DataFrame({'A': data_missing}) + result = repr(df) + expected = ' A\n0 NaN\n1 1' + assert result == expected + + +def test_conversions(data_missing): + + # astype to object series + df = pd.DataFrame({'A': data_missing}) + result = df['A'].astype('object') + expected = pd.Series(np.array([np.nan, 1], dtype=object), name='A') + tm.assert_series_equal(result, expected) + + # convert to object ndarray + # we assert that we are exactly equal + # including type conversions of scalars + result = df['A'].astype('object').values + expected = np.array([np.nan, 1], dtype=object) + tm.assert_numpy_array_equal(result, expected) + + for r, e in zip(result, expected): + if pd.isnull(r): + assert pd.isnull(e) + elif is_integer(r): + # PY2 can be int or long + assert r == e + assert is_integer(e) + else: + assert r == e + assert type(r) == type(e) + + +@pytest.mark.parametrize( + 'values', + [ + ['foo', 'bar'], + 'foo', + 1, + 1.0, + pd.date_range('20130101', periods=2), + np.array(['foo'])]) +def test_to_integer_array_error(values): + # error in converting existing arrays to IntegerArrays + with pytest.raises(TypeError): + to_integer_array(values) + + +@pytest.mark.parametrize( + 'values, dtype', + [ + (np.array([1], dtype='int64'), Int64Dtype), + (np.array([1, np.nan]), Int64Dtype)]) +def test_to_integer_array(values, dtype): + # convert existing arrays to IntegerArrays + result = to_integer_array(values) + expected = IntegerArray(values, dtype=dtype) + tm.assert_extension_array_equal(result, expected) + + +def test_cross_type_arithmetic(): + + df = pd.DataFrame({'A': pd.Series([1, 2, np.nan], dtype='Int64'), + 'B': pd.Series([1, np.nan, 3], dtype='UInt8'), + 'C': [1, 2, 3]}) + + result = df.A + df.C + expected = pd.Series([2, 4, np.nan], dtype='Int64') + tm.assert_series_equal(result, expected) + + result = (df.A + df.C) * 3 == 12 + expected = pd.Series([False, True, False]) + tm.assert_series_equal(result, expected) + + result = df.A + df.B + expected = pd.Series([2, np.nan, np.nan], dtype='Int64') + tm.assert_series_equal(result, expected) + + +# TODO(jreback) - these need testing / are broken + +# groupby + +# shift + +# set_index (destroys type) diff --git a/pandas/tests/extension/json/array.py b/pandas/tests/extension/json/array.py index 160bf259e1e32f..d7bf14eb7d23f4 100644 --- a/pandas/tests/extension/json/array.py +++ b/pandas/tests/extension/json/array.py @@ -54,7 +54,7 @@ def construct_from_string(cls, string): class JSONArray(ExtensionArray): dtype = JSONDtype() - def __init__(self, values, copy=False): + def __init__(self, values, dtype=None, copy=False): for val in values: if not isinstance(val, self.dtype.type): raise TypeError("All values must be of type " + @@ -69,7 +69,7 @@ def __init__(self, values, copy=False): # self._values = self.values = self.data @classmethod - def _from_sequence(cls, scalars, copy=False): + def _from_sequence(cls, scalars, dtype=None, copy=False): return cls(scalars) @classmethod