Skip to content

BUG: DataFrame.getattribute raising if columns have dtype string #46301

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 1 commit into from
Mar 12, 2022
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down