Skip to content

Add back getattr for ExtensionArrays #10278

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 14 additions & 2 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@ What's New

np.random.seed(123456)

.. _whats-new.2025.04.0:
.. _whats-new.2025.05.0:

v2025.04.0 (unreleased)
v2025.05.0 (unreleased)
-----------------------

Bug fixes
~~~~~~~~~

- Allow accessing arbitrary attributes on Pandas ExtensionArrays.
By `Deepak Cherian <https://github.com/dcherian`_.


.. _whats-new.2025.04.0:

v2025.04.0 (Apr 28, 2025)
-------------------------

This release brings bug fixes, better support for extension arrays including returning a
:py:class:`pandas.IntervalArray` from ``groupby_bins``, and performance improvements.
Thanks to the 24 contributors to this release:
Expand Down
5 changes: 4 additions & 1 deletion xarray/core/extension_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections.abc import Callable, Sequence
from dataclasses import dataclass
from typing import Generic, cast
from typing import Any, Generic, cast

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -142,3 +142,6 @@ def __array__(
return np.asarray(self.array, dtype=dtype, copy=copy)
else:
return np.asarray(self.array, dtype=dtype)

def __getattr__(self, attr: str) -> Any:
return getattr(self.array, attr)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return getattr(self.array, attr)
return getattr(super().__getattribute__("array"), attr)

5 changes: 5 additions & 0 deletions xarray/tests/test_duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,3 +1094,8 @@ def test_extension_array_singleton_equality(categorical1):
def test_extension_array_repr(int1):
int_duck_array = PandasExtensionArray(int1)
assert repr(int1) in repr(int_duck_array)


def test_extension_array_attr():
array = pd.Categorical(["cat2", "cat1", "cat2", "cat3", "cat1"])
assert (array.categories == PandasExtensionArray(array).categories).all()
Loading