-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: indexing and __getitem__ of dataframe and series accept zerodim integer np.array as int #24924
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 6 commits
e38be24
16bbaa9
5fc0120
9bd6491
55d4cc3
721684f
b8acbde
8d4c9a1
83a1963
b979434
760a3a3
71de442
7e5c8d8
3242e29
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 |
---|---|---|
|
@@ -3682,3 +3682,14 @@ def test_functions_no_warnings(self): | |
with tm.assert_produces_warning(False): | ||
df['group'] = pd.cut(df.value, range(0, 105, 10), right=False, | ||
labels=labels) | ||
|
||
def test_getitem_zerodim_np_array(self): | ||
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. I would rather have these added test in pandas/tests/indexing, in a sub-file 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. move it to tests/indexing/test_scalar.py |
||
# GH24924 | ||
df = DataFrame([[1, 2], [3, 4]]) | ||
|
||
# should not raise an error | ||
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. this comment is not needed 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. Done! |
||
result = df[np.array(0)] | ||
|
||
# expected series | ||
sr = pd.Series([1, 3], name=0) | ||
tm.assert_series_equal(result, sr) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -675,3 +675,23 @@ def test_identity_slice_returns_new_object(self): | |
# should also be a shallow copy | ||
original_series[:3] = [7, 8, 9] | ||
assert all(sliced_series[:3] == [7, 8, 9]) | ||
|
||
def test_indexing_zero_dim_np_array(self): | ||
# GH24919 | ||
df = DataFrame([[1, 2], [3, 4]]) | ||
|
||
# should not raise an error | ||
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. these comments are not needed (and same below) 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. Done! |
||
result = df.iloc[np.array(0)] | ||
|
||
# expected series | ||
sr = pd.Series([1, 2], name=0) | ||
tm.assert_series_equal(result, sr) | ||
|
||
def test_series_indexing_zero_dim_np_array(self): | ||
# GH24919 | ||
sr = Series([1, 2]) | ||
|
||
# should not raise an error | ||
result = sr.iloc[np.array(0)] | ||
|
||
assert result == 1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -765,3 +765,23 @@ def test_loc_setitem_empty_append_raises(self): | |
msg = "cannot copy sequence with size 2 to array axis with dimension 0" | ||
with pytest.raises(ValueError, match=msg): | ||
df.loc[0:2, 'x'] = data | ||
|
||
def test_indexing_zero_dim_np_array(self): | ||
# GH24924 | ||
df = DataFrame([[1, 2], [3, 4]]) | ||
|
||
# should not raise an error | ||
result = df.loc[np.array(0)] | ||
|
||
# expected series | ||
sr = pd.Series([1, 2], name=0) | ||
tm.assert_series_equal(result, sr) | ||
|
||
def test_series_indexing_zero_dim_np_array(self): | ||
# GH24924 | ||
sr = Series([1, 2]) | ||
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. name as s 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. Done! |
||
|
||
# should not raise an error | ||
result = sr.loc[np.array(0)] | ||
|
||
assert result == 1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -838,3 +838,13 @@ def test_head_tail(test_data): | |
assert_series_equal(test_data.series.head(0), test_data.series[0:0]) | ||
assert_series_equal(test_data.series.tail(), test_data.series[-5:]) | ||
assert_series_equal(test_data.series.tail(0), test_data.series[0:0]) | ||
|
||
|
||
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. see comment above 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. move to tests/indexing/test_scalar.py |
||
def test_getitem_with_zerodim_np_array(): | ||
# GH24924 | ||
sr = Series([1, 2]) | ||
|
||
# should not raise an error | ||
result = sr[np.array(0)] | ||
|
||
assert result == 1 |
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.
This is not exactly what I was after. Let's just do a single line for now.
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.
@jreback Sorry, I cannot understand what you mean.
Please tell me more concretely.