-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Make DataFrame arithmetic ops with 2D arrays behave like numpy analogues #23000
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
Changes from 5 commits
2206df4
1616e41
a0e27da
69fa4bb
f67be3b
78d4517
21ae42d
34edbb4
b621145
7611163
23a96e5
6fca5f5
855e49b
01ab2b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,44 @@ def test_df_flex_cmp_constant_return_types_empty(self, opname): | |
# Arithmetic | ||
|
||
class TestFrameFlexArithmetic(object): | ||
# TODO: tests for other arithmetic ops | ||
def test_df_add_2d_array_rowlike_broadcasts(self): | ||
# GH# | ||
arr = np.arange(6).reshape(3, 2) | ||
df = pd.DataFrame(arr, columns=[True, False], index=['A', 'B', 'C']) | ||
|
||
rowlike = arr[[1], :] # shape --> (1, ncols) | ||
expected = pd.DataFrame([[2, 4], | ||
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. can you assert on the shape of rowlike here (like your comment but more explict) 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. Good idea; I had messed up one of them. |
||
[4, 6], | ||
[6, 8]], | ||
columns=df.columns, index=df.index, | ||
# specify dtype explicitly to avoid failing | ||
# on 32bit builds | ||
dtype=arr.dtype) | ||
result = df + rowlike | ||
tm.assert_frame_equal(result, expected) | ||
result = rowlike + df | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# TODO: tests for other arithmetic ops | ||
def test_df_add_2d_array_collike_broadcasts(self): | ||
# GH# | ||
arr = np.arange(6).reshape(3, 2) | ||
df = pd.DataFrame(arr, columns=[True, False], index=['A', 'B', 'C']) | ||
|
||
collike = arr[[1], :] # shape --> (nrows, 1) | ||
expected = pd.DataFrame([[2, 4], | ||
[4, 6], | ||
[6, 8]], | ||
columns=df.columns, index=df.index, | ||
# specify dtype explicitly to avoid failing | ||
# on 32bit builds | ||
dtype=arr.dtype) | ||
result = df + collike | ||
tm.assert_frame_equal(result, expected) | ||
result = collike + df | ||
tm.assert_frame_equal(result, expected) | ||
|
||
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. do we have sufficient converage for a broadcast op with a non-homogenous frame? 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. its pretty scattered. specifically within this module its pretty bare |
||
def test_df_add_td64_columnwise(self): | ||
# GH#22534 Check that column-wise addition broadcasts correctly | ||
dti = pd.date_range('2016-01-01', periods=10) | ||
|
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.
is this relevant?
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.
For performance, if the answer is that it does make copies, then yes. At least in the sufficiently-new numpy case, we're passing a view in
left._constructor
.In this example its OK. I left the comment to do a more thorough check. Are you confident this is always OK?