-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ERR: Raise ValueError when setting scalars in a dataframe with no index ( #16823) #16968
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -575,24 +575,16 @@ def f(): | |
def test_partial_set_empty_frame_row(self): | ||
# GH5720, GH5744 | ||
# don't create rows when empty | ||
expected = DataFrame(columns=['A', 'B', 'New'], | ||
index=pd.Index([], dtype='int64')) | ||
expected['A'] = expected['A'].astype('int64') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok this is fine here. |
||
expected['B'] = expected['B'].astype('float64') | ||
expected['New'] = expected['New'].astype('float64') | ||
|
||
df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]}) | ||
y = df[df.A > 5] | ||
y['New'] = np.nan | ||
tm.assert_frame_equal(y, expected) | ||
# tm.assert_frame_equal(y,expected) | ||
# GH16823 | ||
# Setting a column with a scalar and no index should raise | ||
with tm.assert_raises_regex(ValueError, 'If using all scalar values'): | ||
y['New'] = np.nan | ||
|
||
expected = DataFrame(columns=['a', 'b', 'c c', 'd']) | ||
expected['d'] = expected['d'].astype('int64') | ||
df = DataFrame(columns=['a', 'b', 'c c']) | ||
df['d'] = 3 | ||
tm.assert_frame_equal(df, expected) | ||
tm.assert_series_equal(df['c c'], Series(name='c c', dtype=object)) | ||
with tm.assert_raises_regex(ValueError, 'If using all scalar values'): | ||
df['d'] = 3 | ||
|
||
# reindex columns is ok | ||
df = DataFrame({"A": [1, 2, 3], "B": [1.2, 4.2, 5.2]}) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than do this (which is incorrect because the result doesn't have the correct index).
just change next statement to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I tried that and got an assertion error because the test was using a new empty dataframe as expected value, and the return value was an empty dataframe with a different index. I'll check again, and if it still breaks I'll change the expected value accordingly, assuming no other tests break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the statement to
if len(df) and values is None:
stills makes the crosstab raise the ValueError I'm implementing in this PR, because in this test len(df) is 0 and values IS None, so when it falls to:it's basically doing
df['__dummy__'] = None
which is what raises the error.What I think would fix it and preserve the index would be this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see my comment below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually just return did here i think is ok (and u had that before)
change and ping on green
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I'm not sure I understand what you mean. Should I change it back to return an empty DataFrame?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no change back to
you need to return the DataFrame with the correct index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, changed it back :)