Skip to content

CLN: Ensure that setitem ops don't coerce values #51671

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 28, 2023

Conversation

phofl
Copy link
Member

@phofl phofl commented Feb 27, 2023

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

cc @jbrockmendel

Want to ensure that we don't coerce dtypes accidentally in the future. We shouldn't get here with DataFrames if we can avoid it. Changed the behavior of the single_block case a couple of weeks back.

@phofl phofl added the Refactor Internal refactoring of code label Feb 27, 2023
@@ -100,6 +100,6 @@ def test_insert_frame(self):
# GH#42403
df = DataFrame({"col1": [1, 2], "col2": [3, 4]})

msg = r"Expected a 1D array, got an array with shape \(2, 2\)"
msg = "Expected a one-dimensional object, got a DataFrame with 2 instead."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"2" here sounds like it is referring to 2 dimensions, which is accurate but i think not intended

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes yes, I forgot to add columns, thx

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, otherwise ok?

I think making this stricter helps in the long run

return _reindex_for_setitem(value, self.index)
elif is_dict_like(value):
# Using a DataFrame would mean coercing values to one dtype
assert not isinstance(value, DataFrame)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no other cases that can be affected by disallowing this here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None that I can think of or find. Isetitem iterates over the columns now. Don’t think that we can get here with a DataFrame now

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this PR, but some things I saw while double-checking:

in isetitem isinstance(value, DataFrame) case should we check that len(loc) == len(value.columns)?
_iset_item is only called from _replace_columnwise, where we know we have a Series with matching index, might be simpler to skip sanitize_column?
Might make sense to refactor _set_item back into __setitem__, as the latter isn't that complicated and the former has a name really similar to a bunch of other names

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yes we should probably raise if they don't match
  2. Should be faster at least
  3. Makes setitem pretty long, but shouldn't really hurt?

Copy link
Member

@jbrockmendel jbrockmendel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@phofl phofl added this to the 2.1 milestone Feb 28, 2023
@phofl phofl added the Indexing Related to indexing on series/frames, not to indexes themselves label Feb 28, 2023
@phofl phofl merged commit 7e410c1 into pandas-dev:main Feb 28, 2023
@phofl phofl deleted the indexing_coercion branch February 28, 2023 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Refactor Internal refactoring of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants