Skip to content

Conversation

@akkik04
Copy link

@akkik04 akkik04 commented Nov 24, 2025

Fixed DataFrame.__setitem__ so that assigning a 2D NumPy array with dtype=object and shape (n, 1) to a single column works the same way as the non-object case, and raise clearer, high-level errors for unsupported shapes. More detail below:

Before this change:

  • Assigning a 2D NumPy dtype=object array with shape (n, 1) to a single DataFrame column (e.g., df["c1"] = t2) raised a low-level ValueError: Buffer has wrong number of dimensions (expected 1, got 2). This was coming from lib.maybe_convert_objects, instead of behaving like the non-object case.
  • 2D non-object arrays with shape (n, 1) already worked just fine, and assigning a 2D array with multiple columns to multiple columns (e.g., df[["c1", "c2"]] = t3) also worked, but ndim > 2 arrays could surface confusing internal errors.

After this change:

  • Assigning a 2D NumPy dtype=object array with shape (n, 1) to a single column now works by flattening (n, 1) to a 1D (n,) array, matching the behaviour of non-object arrays.
  • Assigning a 2D array with more than one column to a single column raises a clear, user-facing ValueError explaining that only (n, 1) is supported and suggesting multi-column assignment (e.g., df[["c1", "c2"]] = some_values) for wider arrays.
  • Assigning arrays with ndim >= 3 to a single column is now raises an explicit ValueError indicating that setting a column with that spec is not supported. The existing multi-column assignment with 2D arrays remains unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: setting column with 2D object array raises

1 participant