Skip to content

BUG: pd.array failing to raise with DataFrame #51167

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 2 commits into from
Feb 5, 2023
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/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,7 @@ Metadata
Other
^^^^^
- Bug in :meth:`Series.searchsorted` inconsistent behavior when accepting :class:`DataFrame` as parameter ``value`` (:issue:`49620`)
- Bug in :func:`array` failing to raise on :class:`DataFrame` inputs (:issue:`51167`)
-

.. ***DO NOT USE THIS SECTION***
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
)
from pandas.core.dtypes.dtypes import PandasDtype
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCExtensionArray,
ABCIndex,
ABCPandasArray,
Expand Down Expand Up @@ -308,6 +309,8 @@ def array(
if lib.is_scalar(data):
msg = f"Cannot pass scalar '{data}' to 'pandas.array'."
raise ValueError(msg)
elif isinstance(data, ABCDataFrame):
raise TypeError("Cannot pass DataFrame to 'pandas.array'")

if dtype is None and isinstance(data, (ABCSeries, ABCIndex, ExtensionArray)):
# Note: we exclude np.ndarray here, will do type inference on it
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/arrays/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ def test_scalar_raises():
pd.array(1)


def test_dataframe_raises():
# GH#51167 don't accidentally cast to StringArray by doing inference on columns
df = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
msg = "Cannot pass DataFrame to 'pandas.array'"
with pytest.raises(TypeError, match=msg):
pd.array(df)


def test_bounds_check():
# GH21796
with pytest.raises(
Expand Down