Skip to content

BUG: Fix setitem with 1d matrix #44669

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 10 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ def sanitize_array(
if isinstance(data, ma.MaskedArray):
data = sanitize_masked_array(data)

if isinstance(data, np.matrix):
data = data.A

if isinstance(dtype, PandasDtype):
# Avoid ending up with a PandasArray
dtype = dtype.numpy_dtype
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,20 @@ def test_boolean_mask_nullable_int64(self):
)
tm.assert_frame_equal(result, expected)

def test_2d_array(self):
# for use-case df["x"] = sparse.random(10, 10).mean(axis=1)
expected = DataFrame(
{"np-array": np.ones(10), "np-matrix": np.ones(10)}, index=np.arange(10)
)

a = np.ones((10, 1))
df = DataFrame(index=np.arange(10))
df["np-array"] = a
with tm.assert_produces_warning(PendingDeprecationWarning):
df["np-matrix"] = np.matrix(a)

tm.assert_frame_equal(df, expected)


class TestSetitemTZAwareValues:
@pytest.fixture
Expand Down