Skip to content

API: Added ExtensionArray constructors #19913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class ExtensionArray(object):
The interface includes the following abstract methods that must be
implemented by subclasses:

* _from_extension_array
* _from_scalars
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update this one as well

* __getitem__
* __len__
* dtype
Expand Down Expand Up @@ -56,6 +58,53 @@ class ExtensionArray(object):
# '_typ' is for pandas.core.dtypes.generic.ABCExtensionArray.
# Don't override this.
_typ = 'extension'

# ------------------------------------------------------------------------
# Constructors
# ------------------------------------------------------------------------
@classmethod
def _from_extension_array(cls, array, copy=True):
"""Construct a new ExtensionArray from an existing instance.

Parameters
----------
array : ExtensionArray
An extension array of the same type as cls.
copy : bool, default True
Whether a copy should be made using ``array.copy``. Note that
even if ``copy=False`` there's no guarantee that the underlying
data of the two arrays is the same.

Returns
-------
ExtensionArray

See Also
--------
_from_scalars
"""
raise AbstractMethodError(cls)

@classmethod
def _from_scalars(cls, scalars):
"""Construct a new ExtensionArray from a sequence of scalars.

Parameters
----------
scalars : Sequence
Each element will be an instance of the scalar type for this
array, ``cls.dtype.type``.

Returns
-------
ExtensionArray

See Also
--------
_from_extension_array
"""
raise AbstractMethodError(cls)

# ------------------------------------------------------------------------
# Must be a Sequence
# ------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def __init__(self, values):

self.values = values

@classmethod
def _from_extension_array(cls, array, copy=True):
return cls(array)

@classmethod
def _from_scalars(cls, scalars):
return cls(scalars)

def __getitem__(self, item):
if isinstance(item, numbers.Integral):
return self.values[item]
Expand Down
16 changes: 12 additions & 4 deletions pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ def __init__(self, values):
raise TypeError
self.data = values

@classmethod
def _from_extension_array(cls, array, copy=True):
return cls(array)

@classmethod
def _from_scalars(cls, scalars):
return cls(scalars)

def __getitem__(self, item):
if isinstance(item, numbers.Integral):
return self.data[item]
elif isinstance(item, np.ndarray) and item.dtype == 'bool':
return type(self)([x for x, m in zip(self, item) if m])
return self._from_scalars([x for x, m in zip(self, item) if m])
else:
return type(self)(self.data[item])
return self._from_extension_array(self.data[item])

def __setitem__(self, key, value):
if isinstance(key, numbers.Integral):
Expand Down Expand Up @@ -77,10 +85,10 @@ def isna(self):
def take(self, indexer, allow_fill=True, fill_value=None):
output = [self.data[loc] if loc != -1 else self._na_value
for loc in indexer]
return type(self)(output)
return self._from_scalars(output)

def copy(self, deep=False):
return type(self)(self.data[:])
return self._from_extension_array(self.data[:])

@property
def _na_value(self):
Expand Down