Skip to content

Commit

Permalink
Added _constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Feb 26, 2018
1 parent a5d6b67 commit 011d02e
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class ExtensionArray(object):
* copy
* _concat_same_type
Some additional methods are available to satisfy pandas' internal, private
block API.
Some additional methods are available to satisfy pandas' internals.
* _can_hold_na
* _formatting_values
* _constructor
This class does not inherit from 'abc.ABCMeta' for performance reasons.
Methods and properties required by the interface raise
Expand All @@ -49,13 +49,32 @@ class ExtensionArray(object):
assumptions on how the data are stored, just that it can be converted
to a NumPy array.
Extension arrays should be able to be constructed with instances of
the class, i.e. ``ExtensionArray(extension_array)`` should return
an instance, not error.
There are a few restrictions on how ExtensionArrays are created.
* An ExtensionArray should be valid, i.e.
``ExtensionArray(extension_array)`` should return an instance
* A sequence of the scalar type should be valid, i.e.
``ExtensionArray(Sequence[ExtensionDtype.type]])`` should return
an instance, not error.
"""
# '_typ' is for pandas.core.dtypes.generic.ABCExtensionArray.
# Don't override this.
_typ = 'extension'

@classmethod
def _constructor(cls, data):
"""Construct a new instance of of the extension array.
Parameters
----------
data : Sequence
Returns
-------
ExtensionArray
"""
return cls(data)

# ------------------------------------------------------------------------
# Must be a Sequence
# ------------------------------------------------------------------------
Expand Down Expand Up @@ -226,7 +245,7 @@ def unique(self):
from pandas import unique

uniques = unique(self.astype(object))
return type(self)(uniques)
return self._constructor(uniques)

# ------------------------------------------------------------------------
# Indexing methods
Expand Down

0 comments on commit 011d02e

Please sign in to comment.