Skip to content

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

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ Backwards incompatible API changes

.. _whatsnew_0250.api.other:

Indexing and getitem
^^^^^^^^^^^^^^^^^^^^
Copy link
Contributor

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.

Copy link
Contributor Author

@tamuhey tamuhey Jan 28, 2019

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.


Indexing and getitem of pd.DataFrame now accept zerodim np.array.

.. ipython:: python
df = pd.DataFrame([[1, 2], [3, 4]])[np.array(0)]
df.iloc[np.array(0)]
df.loc[np.array(0)]
df[np.array(0)]

Indexing and getitem of pd.Series now accept zerodim np.array.

.. ipython:: python
sr = pd.Series([1, 2])
sr.iloc[np.array(0)]
sr.loc[np.array(0)]
sr[np.array(0)]

Other API Changes
^^^^^^^^^^^^^^^^^

Expand Down
1 change: 1 addition & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2881,6 +2881,7 @@ def _ixs(self, i, axis=0):
return result

def __getitem__(self, key):
key = lib.item_from_zerodim(key)
key = com.apply_if_callable(key, self)

# shortcut if the key is in columns
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np

from pandas._libs.indexing import _NDFrameIndexerBase
from pandas._libs.lib import item_from_zerodim
import pandas.compat as compat
from pandas.compat import range, zip
from pandas.errors import AbstractMethodError
Expand Down Expand Up @@ -1856,6 +1857,7 @@ def _getitem_axis(self, key, axis=None):
if axis is None:
axis = self.axis or 0

key = item_from_zerodim(key)
if is_iterator(key):
key = list(key)

Expand Down Expand Up @@ -2222,6 +2224,7 @@ def _getitem_axis(self, key, axis=None):

# a single integer
else:
key = item_from_zerodim(key)
if not is_integer(key):
raise TypeError("Cannot index by location index with a "
"non-integer key")
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

@tamuhey tamuhey Jan 28, 2019

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment is not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
20 changes: 20 additions & 0 deletions pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

these comments are not needed (and same below)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
20 changes: 20 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Copy link
Contributor

Choose a reason for hiding this comment

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

name as s

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
10 changes: 10 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])


Copy link
Contributor

Choose a reason for hiding this comment

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

see comment above

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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