Skip to content

Commit 30cb546

Browse files
authored
BUG: pd.array failing to raise with DataFrame (#51167)
1 parent 1a48cd9 commit 30cb546

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,7 @@ Metadata
13341334
Other
13351335
^^^^^
13361336
- Bug in :meth:`Series.searchsorted` inconsistent behavior when accepting :class:`DataFrame` as parameter ``value`` (:issue:`49620`)
1337+
- Bug in :func:`array` failing to raise on :class:`DataFrame` inputs (:issue:`51167`)
13371338
-
13381339

13391340
.. ***DO NOT USE THIS SECTION***

pandas/core/construction.py

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
)
5353
from pandas.core.dtypes.dtypes import PandasDtype
5454
from pandas.core.dtypes.generic import (
55+
ABCDataFrame,
5556
ABCExtensionArray,
5657
ABCIndex,
5758
ABCPandasArray,
@@ -308,6 +309,8 @@ def array(
308309
if lib.is_scalar(data):
309310
msg = f"Cannot pass scalar '{data}' to 'pandas.array'."
310311
raise ValueError(msg)
312+
elif isinstance(data, ABCDataFrame):
313+
raise TypeError("Cannot pass DataFrame to 'pandas.array'")
311314

312315
if dtype is None and isinstance(data, (ABCSeries, ABCIndex, ExtensionArray)):
313316
# Note: we exclude np.ndarray here, will do type inference on it

pandas/tests/arrays/test_array.py

+8
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,14 @@ def test_scalar_raises():
343343
pd.array(1)
344344

345345

346+
def test_dataframe_raises():
347+
# GH#51167 don't accidentally cast to StringArray by doing inference on columns
348+
df = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"])
349+
msg = "Cannot pass DataFrame to 'pandas.array'"
350+
with pytest.raises(TypeError, match=msg):
351+
pd.array(df)
352+
353+
346354
def test_bounds_check():
347355
# GH21796
348356
with pytest.raises(

0 commit comments

Comments
 (0)