Skip to content
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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ Other
- Bug in ``Series.list`` methods not preserving the original name. (:issue:`60522`)
- Bug in printing a :class:`DataFrame` with a :class:`DataFrame` stored in :attr:`DataFrame.attrs` raised a ``ValueError`` (:issue:`60455`)
- Bug in printing a :class:`Series` with a :class:`DataFrame` stored in :attr:`Series.attrs` raised a ``ValueError`` (:issue:`60568`)
- Fixed bug where the :class:`DataFrame` constructor misclassified array-like objects with a ``.name`` attribute as :class:`Series` or :class:`Index` (:issue:`61443`)
- Fixed regression in :meth:`DataFrame.from_records` not initializing subclasses properly (:issue:`57008`)

.. ***DO NOT USE THIS SECTION***
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@
BaseMaskedDtype,
ExtensionDtype,
)
from pandas.core.dtypes.generic import (
ABCIndex,
ABCSeries,
)
from pandas.core.dtypes.missing import (
isna,
notna,
Expand Down Expand Up @@ -795,12 +799,12 @@ def __init__(
dtype,
copy,
)
elif getattr(data, "name", None) is not None:
elif isinstance(data, (ABCSeries, ABCIndex)) and data.name is not None:
# i.e. Series/Index with non-None name
mgr = dict_to_mgr(
# error: Item "ndarray" of "Union[ndarray, Series, Index]" has no
# attribute "name"
{data.name: data}, # type: ignore[union-attr]
{data.name: data},
index,
columns,
dtype=dtype,
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2780,6 +2780,19 @@ def test_construction_nan_value_timedelta64_dtype(self):
)
tm.assert_frame_equal(result, expected)

def test_dataframe_from_array_like_with_name_attribute(self):
# GH#61443
class DummyArray(np.ndarray):
def __new__(cls, input_array):
obj = np.asarray(input_array).view(cls)
obj.name = "foo"
return obj

dummy = DummyArray(np.eye(3))
df = DataFrame(dummy)
expected = DataFrame(np.eye(3))
tm.assert_frame_equal(df, expected)


class TestDataFrameConstructorIndexInference:
def test_frame_from_dict_of_series_overlapping_monthly_period_indexes(self):
Expand Down
Loading