Skip to content

TST: ExtensionArrays disallow .values attribute #20794

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
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,19 @@ class ExtensionArray(object):

ExtensionArrays are limited to 1 dimension.

They may be backed by none, one, or many NumPy ararys. For example,
They may be backed by none, one, or many NumPy arrays. For example,
``pandas.Categorical`` is an extension array backed by two arrays,
one for codes and one for categories. An array of IPv6 address may
be backed by a NumPy structured array with two fields, one for the
lower 64 bits and one for the upper 64 bits. Or they may be backed
by some other storage type, like Python lists. Pandas makes no
assumptions on how the data are stored, just that it can be converted
to a NumPy array.
The ExtensionArray interface does not impose any rules on how this data
is stored. However, currently, the backing data cannot be stored in
attributes called ``.values`` or ``._values`` to ensure full compatibility
with pandas internals. But other names as ``.data``, ``._data``,
``._items``, ... can be freely used.
"""
# '_typ' is for pandas.core.dtypes.generic.ABCExtensionArray.
# Don't override this.
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/extension/base/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ def test_is_extension_array_dtype(self, data):
assert is_extension_array_dtype(data.dtype)
assert is_extension_array_dtype(pd.Series(data))
assert isinstance(data.dtype, ExtensionDtype)

def test_no_values_attribute(self, data):
# GH-20735: EA's with .values attribute give problems with internal
# code, disallowing this for now until solved
assert not hasattr(data, 'values')
assert not hasattr(data, '_values')
25 changes: 14 additions & 11 deletions pandas/tests/extension/decimal/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ class DecimalArray(ExtensionArray):
def __init__(self, values):
values = np.asarray(values, dtype=object)

self.values = values
self._data = values
# Some aliases for common attribute names to ensure pandas supports
# these
self._items = self._data = self.data = self.values
self._items = self.data = self._data
Copy link
Contributor

@jreback jreback Apr 23, 2018

Choose a reason for hiding this comment

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

you have .data here? (no a big deal, but this is private yes?) and I think you removed in liue of ._data)

Copy link
Contributor

Choose a reason for hiding this comment

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

These are just in the tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you exactly mean? It's not really about private/public, that's up to the EA author. The purpose of this it to ensure pandas has no problems if an EA author would use .data to store the underlying data (in geopandas we are actually using a data attribute)

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand you can have a .data items, it just doesn't seem a good pattern to advertise

The fact that these are in the tests is very odd itself.

And the comment makes these doubly confusing.

Copy link
Contributor

Choose a reason for hiding this comment

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

@jorisvandenbossche ok I re-read what you are testing, this is ok

# those aliases are currently not working due to assumptions
# in internal code (GH-20735)
# self._values = self.values = self.data

@classmethod
def _constructor_from_sequence(cls, scalars):
Expand All @@ -45,27 +48,27 @@ def _from_factorized(cls, values, original):

def __getitem__(self, item):
if isinstance(item, numbers.Integral):
return self.values[item]
return self._data[item]
else:
return type(self)(self.values[item])
return type(self)(self._data[item])

def copy(self, deep=False):
if deep:
return type(self)(self.values.copy())
return type(self)(self._data.copy())
return type(self)(self)

def __setitem__(self, key, value):
if pd.api.types.is_list_like(value):
value = [decimal.Decimal(v) for v in value]
else:
value = decimal.Decimal(value)
self.values[key] = value
self._data[key] = value

def __len__(self):
return len(self.values)
return len(self._data)

def __repr__(self):
return 'DecimalArray({!r})'.format(self.values)
return 'DecimalArray({!r})'.format(self._data)

@property
def nbytes(self):
Expand All @@ -75,7 +78,7 @@ def nbytes(self):
return 0

def isna(self):
return np.array([x.is_nan() for x in self.values])
return np.array([x.is_nan() for x in self._data])

def take(self, indexer, allow_fill=True, fill_value=None):
indexer = np.asarray(indexer)
Expand All @@ -86,7 +89,7 @@ def take(self, indexer, allow_fill=True, fill_value=None):
return type(self)([self._na_value] * len(indexer))

indexer = _ensure_platform_int(indexer)
out = self.values.take(indexer)
out = self._data.take(indexer)
out[mask] = self._na_value

return type(self)(out)
Expand All @@ -97,7 +100,7 @@ def _na_value(self):

@classmethod
def _concat_same_type(cls, to_concat):
return cls(np.concatenate([x.values for x in to_concat]))
return cls(np.concatenate([x._data for x in to_concat]))


def make_data():
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def __init__(self, values):
raise TypeError
self.data = values

# Some aliases for common attribute names to ensure pandas supports
# these
self._items = self._data = self.data
Copy link
Contributor

Choose a reason for hiding this comment

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

same?

# those aliases are currently not working due to assumptions
# in internal code (GH-20735)
# self._values = self.values = self.data

@classmethod
def _constructor_from_sequence(cls, scalars):
return cls(scalars)
Expand Down