Skip to content

Commit ae8ea3e

Browse files
authored
BUG: Fix DataFrame constructor misclassification of array-like with 'name' attribute (#61443) (#61451)
* BUG: Fix DataFrame constructor misclassification of array-like with 'name' attribute Previously, any object with a .name attribute (like some vtkArray-like objects) was assumed to be a Series or Index, causing the constructor to misinterpret the input. This fix ensures we only apply the named-Index/Series logic when the input is actually an instance of ABCSeries or ABCIndex *and* has a non-None name. Closes #61443. * Apply pre-commit fixes: isort and remove unused type ignore * TST/CLN: Add issue reference in test and update whatsnew for GH#61443
1 parent 6177e22 commit ae8ea3e

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ Other
904904
- Bug in ``Series.list`` methods not preserving the original name. (:issue:`60522`)
905905
- Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`)
906906
- Bug in printing a :class:`Series` with a :class:`DataFrame` stored in :attr:`Series.attrs` raised a ``ValueError`` (:issue:`60568`)
907+
- Fixed bug where the :class:`DataFrame` constructor misclassified array-like objects with a ``.name`` attribute as :class:`Series` or :class:`Index` (:issue:`61443`)
907908
- Fixed regression in :meth:`DataFrame.from_records` not initializing subclasses properly (:issue:`57008`)
908909

909910
.. ***DO NOT USE THIS SECTION***

pandas/core/frame.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@
112112
BaseMaskedDtype,
113113
ExtensionDtype,
114114
)
115+
from pandas.core.dtypes.generic import (
116+
ABCIndex,
117+
ABCSeries,
118+
)
115119
from pandas.core.dtypes.missing import (
116120
isna,
117121
notna,
@@ -795,12 +799,12 @@ def __init__(
795799
dtype,
796800
copy,
797801
)
798-
elif getattr(data, "name", None) is not None:
802+
elif isinstance(data, (ABCSeries, ABCIndex)) and data.name is not None:
799803
# i.e. Series/Index with non-None name
800804
mgr = dict_to_mgr(
801805
# error: Item "ndarray" of "Union[ndarray, Series, Index]" has no
802806
# attribute "name"
803-
{data.name: data}, # type: ignore[union-attr]
807+
{data.name: data},
804808
index,
805809
columns,
806810
dtype=dtype,

pandas/tests/frame/test_constructors.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,6 +2780,19 @@ def test_construction_nan_value_timedelta64_dtype(self):
27802780
)
27812781
tm.assert_frame_equal(result, expected)
27822782

2783+
def test_dataframe_from_array_like_with_name_attribute(self):
2784+
# GH#61443
2785+
class DummyArray(np.ndarray):
2786+
def __new__(cls, input_array):
2787+
obj = np.asarray(input_array).view(cls)
2788+
obj.name = "foo"
2789+
return obj
2790+
2791+
dummy = DummyArray(np.eye(3))
2792+
df = DataFrame(dummy)
2793+
expected = DataFrame(np.eye(3))
2794+
tm.assert_frame_equal(df, expected)
2795+
27832796

27842797
class TestDataFrameConstructorIndexInference:
27852798
def test_frame_from_dict_of_series_overlapping_monthly_period_indexes(self):

0 commit comments

Comments
 (0)