diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index dfa87e3cd4574..dac4acb3fb964 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -393,6 +393,7 @@ Indexing - Bug in :meth:`Series.mask` with ``inplace=True`` or setting values with a boolean mask with small integer dtypes incorrectly raising (:issue:`45750`) - Bug in :meth:`DataFrame.mask` with ``inplace=True`` and ``ExtensionDtype`` columns incorrectly raising (:issue:`45577`) - Bug in getting a column from a DataFrame with an object-dtype row index with datetime-like values: the resulting Series now preserves the exact object-dtype Index from the parent DataFrame (:issue:`42950`) +- Bug in :meth:`DataFrame.__getattribute__` raising ``AttributeError`` if columns have ``"string"`` dtype (:issue:`46185`) - Bug in indexing on a :class:`DatetimeIndex` with a ``np.str_`` key incorrectly raising (:issue:`45580`) - Bug in :meth:`CategoricalIndex.get_indexer` when index contains ``NaN`` values, resulting in elements that are in target but not present in the index to be mapped to the index of the NaN element, instead of -1 (:issue:`45361`) - Bug in setting large integer values into :class:`Series` with ``float32`` or ``float16`` dtype incorrectly altering these values instead of coercing to ``float64`` dtype (:issue:`45844`) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index d717e5cfb1083..11ef1f9198605 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -97,6 +97,7 @@ is_object_dtype, is_scalar, is_signed_integer_dtype, + is_string_dtype, is_unsigned_integer_dtype, needs_i8_conversion, pandas_dtype, @@ -5274,7 +5275,7 @@ def _can_hold_identifiers_and_holds_name(self, name) -> bool: https://github.com/pandas-dev/pandas/issues/19764 """ - if self.is_object() or self.is_categorical(): + if self.is_object() or is_string_dtype(self.dtype) or self.is_categorical(): return name in self return False diff --git a/pandas/tests/frame/indexing/test_getitem.py b/pandas/tests/frame/indexing/test_getitem.py index 0d4ab84175aab..8cc8b487ff44f 100644 --- a/pandas/tests/frame/indexing/test_getitem.py +++ b/pandas/tests/frame/indexing/test_getitem.py @@ -72,6 +72,13 @@ def test_getitem_sparse_column_return_type_and_dtype(self): result = df.loc[:, "A"] tm.assert_series_equal(result, expected) + def test_getitem_string_columns(self): + # GH#46185 + df = DataFrame([[1, 2]], columns=Index(["A", "B"], dtype="string")) + result = df.A + expected = df["A"] + tm.assert_series_equal(result, expected) + class TestGetitemListLike: def test_getitem_list_missing_key(self):