Skip to content

EA.values warning test #20793

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

Closed
Closed
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
13 changes: 13 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
This is an experimental API and subject to breaking changes
without warning.
"""
# Some notes, just for implementors.
#
# * Avoid a `.values` attribute
# There are corners in pandas that assume assume array.values is an ndarray,
# and does ndarray things like ge the dtype. If you have a non-ndarray
# `.values` attribute, things may break. We're working to fix these.
# * Reference the default implementations, but avoid using private methods.
# Some default implementations (e.g. `factorize`) use pandas' internal
# methods. If possible, avoid using those methods when overriding the default
# implementation. If that isn't possible, raise an issue to discuss adding
# those to the public API.


import numpy as np

from pandas.errors import AbstractMethodError
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/extension/base/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ 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
# Currently, pandas has places where we accepts Union[ndarray, EA]
# but in practice expect an ndarray, since we get `data.values.dtype`.
# This warns users to avoid using a `.values` attribute that is not
# an ndarray-like
if hasattr(data, 'values') and not hasattr(data.values, 'dtype'):
msg = ("ExtensionArray contains a 'values' attribute that does "
"not have a dtype attribute. This may cause issues in "
"pandas' internals.")
raise ValueError(msg)
23 changes: 23 additions & 0 deletions pandas/tests/extension/test_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from pandas.tests.extension.base import BaseInterfaceTests
from pandas.tests.extension.json.array import JSONArray


class JSONArray2(JSONArray):
@property
def values(self):
return self.data


@pytest.fixture
def data():
return JSONArray2([{"A": [1, 2], "B": [3, 4]}])


class TestBaseInterfaceTests(BaseInterfaceTests):
def test_no_values_attribute(self, data):
# GH-20735
with pytest.raises(ValueError) as m:
super(TestBaseInterfaceTests, self).test_no_values_attribute(data)
assert m.match("ExtensionArray contains")