From e38be24da32faaaea79146647d055e3434efda88 Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Fri, 25 Jan 2019 14:43:27 +0900 Subject: [PATCH 01/11] ENH: df.iloc accepts zero dim integer np.array as int --- pandas/core/indexing.py | 2 ++ pandas/tests/indexing/test_iloc.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index bbcde8f3b3305..3c3fe49a1fe12 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -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 @@ -2222,6 +2223,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") diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index a867387db4b46..87900b9f9da58 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -675,3 +675,10 @@ 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 + df.iloc[np.array(0)] From 16bbaa9b5893a1070c5f7e1da33d545316e16054 Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Sun, 27 Jan 2019 13:28:33 +0900 Subject: [PATCH 02/11] test compares the result and expected value --- pandas/tests/indexing/test_iloc.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 87900b9f9da58..e955c951d8370 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -681,4 +681,8 @@ def test_indexing_zero_dim_np_array(self): df = DataFrame([[1, 2], [3, 4]]) # should not raise an error - df.iloc[np.array(0)] + result = df.iloc[np.array(0)] + + # expected series + sr = pd.Series([1, 2], name=0) + tm.assert_series_equal(result, sr) From 5fc0120c87366a21d987d6f62d1ecf5a4664061d Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Sun, 27 Jan 2019 14:12:21 +0900 Subject: [PATCH 03/11] ENH: df.loc accepts zerodim integer np.array, and add test --- pandas/core/indexing.py | 1 + pandas/tests/indexing/test_loc.py | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 3c3fe49a1fe12..48933f7a06a23 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1857,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) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 17e107c7a1130..a6021bcbfe5c1 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -765,3 +765,14 @@ 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) From 9bd6491759f4ea5733a0fb175df67720206dac4b Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Sun, 27 Jan 2019 14:28:04 +0900 Subject: [PATCH 04/11] ENH: df.__getitem__ accepts zerodim integer np.array, and add test --- pandas/core/frame.py | 1 + pandas/tests/frame/test_indexing.py | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b4f79bda25517..fe2ca72b46977 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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 diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py index 19b8ae4eb6e0f..d36af9b89fe58 100644 --- a/pandas/tests/frame/test_indexing.py +++ b/pandas/tests/frame/test_indexing.py @@ -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): + # GH24924 + df = DataFrame([[1, 2], [3, 4]]) + + # should not raise an error + result = df[np.array(0)] + + # expected series + sr = pd.Series([1, 3], name=0) + tm.assert_series_equal(result, sr) From 55d4cc3ca5f978619d802243e9ea4a97111d642c Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Sun, 27 Jan 2019 20:57:28 +0900 Subject: [PATCH 05/11] add whatsnew, v0.25.0.rst --- doc/source/whatsnew/v0.25.0.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index fac42dbd9c7c8..8f61dca4d6ae8 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -28,6 +28,17 @@ Backwards incompatible API changes .. _whatsnew_0250.api.other: +Indexing and getitem +^^^^^^^^^^^^^^^^^^^^ + +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)] + Other API Changes ^^^^^^^^^^^^^^^^^ From 721684fede8de078367c9fa37df38a3fa7811c13 Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Sun, 27 Jan 2019 23:06:40 +0900 Subject: [PATCH 06/11] add test and whatsnew about changes of pd.Series indexing and __getitem__ --- doc/source/whatsnew/v0.25.0.rst | 10 +++++++++- pandas/tests/indexing/test_iloc.py | 9 +++++++++ pandas/tests/indexing/test_loc.py | 9 +++++++++ pandas/tests/series/indexing/test_indexing.py | 10 ++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 8f61dca4d6ae8..db8924b0d4ad6 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -31,7 +31,7 @@ Backwards incompatible API changes Indexing and getitem ^^^^^^^^^^^^^^^^^^^^ -Indexing and getitem of pd.dataframe now accept zerodim np.array. +Indexing and getitem of pd.DataFrame now accept zerodim np.array. .. ipython:: python df = pd.DataFrame([[1, 2], [3, 4]])[np.array(0)] @@ -39,6 +39,14 @@ Indexing and getitem of pd.dataframe now accept zerodim np.array. 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 ^^^^^^^^^^^^^^^^^ diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index e955c951d8370..0e191e939f14a 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -686,3 +686,12 @@ def test_indexing_zero_dim_np_array(self): # 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 diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index a6021bcbfe5c1..7b173c4099fd5 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -776,3 +776,12 @@ def test_indexing_zero_dim_np_array(self): # 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]) + + # should not raise an error + result = sr.loc[np.array(0)] + + assert result == 1 diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index a5855f68127f4..8483c63b4c37e 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -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]) + + +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 From b8acbdeb26b0ae8912612c7a61963afe1cfda44d Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Mon, 28 Jan 2019 22:15:03 +0900 Subject: [PATCH 07/11] move tests of df.__getitem__ and s.__getitem__ to tests/indexing/test_scalar.py --- pandas/tests/frame/test_indexing.py | 11 ----------- pandas/tests/indexing/test_scalar.py | 13 +++++++++++++ pandas/tests/series/indexing/test_indexing.py | 10 ---------- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py index d36af9b89fe58..19b8ae4eb6e0f 100644 --- a/pandas/tests/frame/test_indexing.py +++ b/pandas/tests/frame/test_indexing.py @@ -3682,14 +3682,3 @@ 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): - # GH24924 - df = DataFrame([[1, 2], [3, 4]]) - - # should not raise an error - result = df[np.array(0)] - - # expected series - sr = pd.Series([1, 3], name=0) - tm.assert_series_equal(result, sr) diff --git a/pandas/tests/indexing/test_scalar.py b/pandas/tests/indexing/test_scalar.py index e4b8181a67514..5dac2a3a0625e 100644 --- a/pandas/tests/indexing/test_scalar.py +++ b/pandas/tests/indexing/test_scalar.py @@ -205,3 +205,16 @@ def test_iat_setter_incompatible_assignment(self): result.iat[0, 0] = None expected = DataFrame({"a": [None, 1], "b": [4, 5]}) tm.assert_frame_equal(result, expected) + + def test_getitem_zerodim_np_array(self): + # GH24924 + # dataframe __getitem__ + df = DataFrame([[1, 2], [3, 4]]) + result = df[np.array(0)] + expected = Series([1, 3], name=0) + tm.assert_series_equal(result, expected) + + # series __getitem__ + s = Series([1, 2]) + result = s[np.array(0)] + assert result == 1 diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index 8483c63b4c37e..a5855f68127f4 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -838,13 +838,3 @@ 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]) - - -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 From 8d4c9a13dab5d70bd33fde32b6ba1be41656ff74 Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Mon, 28 Jan 2019 22:18:35 +0900 Subject: [PATCH 08/11] CLN: remove redundant comment, and name series as s --- pandas/tests/indexing/test_iloc.py | 19 ++++++------------- pandas/tests/indexing/test_loc.py | 19 ++++++------------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 0e191e939f14a..5cd83d5fc3614 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -676,22 +676,15 @@ def test_identity_slice_returns_new_object(self): original_series[:3] = [7, 8, 9] assert all(sliced_series[:3] == [7, 8, 9]) - def test_indexing_zero_dim_np_array(self): + def test_indexing_zerodim_np_array(self): # GH24919 df = DataFrame([[1, 2], [3, 4]]) - - # should not raise an error result = df.iloc[np.array(0)] + s = pd.Series([1, 2], name=0) + tm.assert_series_equal(result, s) - # expected series - sr = pd.Series([1, 2], name=0) - tm.assert_series_equal(result, sr) - - def test_series_indexing_zero_dim_np_array(self): + def test_series_indexing_zerodim_np_array(self): # GH24919 - sr = Series([1, 2]) - - # should not raise an error - result = sr.iloc[np.array(0)] - + s = Series([1, 2]) + result = s.iloc[np.array(0)] assert result == 1 diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 7b173c4099fd5..ab63b85dd25c9 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -766,22 +766,15 @@ def test_loc_setitem_empty_append_raises(self): with pytest.raises(ValueError, match=msg): df.loc[0:2, 'x'] = data - def test_indexing_zero_dim_np_array(self): + def test_indexing_zerodim_np_array(self): # GH24924 df = DataFrame([[1, 2], [3, 4]]) - - # should not raise an error result = df.loc[np.array(0)] + s = pd.Series([1, 2], name=0) + tm.assert_series_equal(result, s) - # expected series - sr = pd.Series([1, 2], name=0) - tm.assert_series_equal(result, sr) - - def test_series_indexing_zero_dim_np_array(self): + def test_series_indexing_zerodim_np_array(self): # GH24924 - sr = Series([1, 2]) - - # should not raise an error - result = sr.loc[np.array(0)] - + s = Series([1, 2]) + result = s.loc[np.array(0)] assert result == 1 From 83a1963c942c65906afaf433d57bef4d4b7f6719 Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Tue, 29 Jan 2019 23:13:15 +0900 Subject: [PATCH 09/11] modified: doc/source/whatsnew/v0.25.0.rst --- doc/source/whatsnew/v0.25.0.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index db8924b0d4ad6..50090fc070970 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -28,8 +28,8 @@ Backwards incompatible API changes .. _whatsnew_0250.api.other: -Indexing and getitem -^^^^^^^^^^^^^^^^^^^^ +Indexing and getitem accept zerodim np.ndarray +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Indexing and getitem of pd.DataFrame now accept zerodim np.array. From b9794347d50917926f9723d9b926de5ffd561c96 Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Sat, 2 Feb 2019 23:55:33 +0900 Subject: [PATCH 10/11] fix whatsnew --- doc/source/whatsnew/v0.25.0.rst | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 50090fc070970..5d3e883ed21d1 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -16,7 +16,7 @@ including other versions of pandas. Other Enhancements ^^^^^^^^^^^^^^^^^^ -- +- Indexing and __getitem__ of DataFrame and Series now accept zerodim np.ndarray. (:issue:`24919`) - - @@ -28,25 +28,6 @@ Backwards incompatible API changes .. _whatsnew_0250.api.other: -Indexing and getitem accept zerodim np.ndarray -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -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 ^^^^^^^^^^^^^^^^^ From 3242e291a3e5c035146d3ea31c801a545cd8ab13 Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Wed, 20 Feb 2019 08:49:50 +0900 Subject: [PATCH 11/11] modified: doc/source/whatsnew/v0.25.0.rst --- doc/source/whatsnew/v0.25.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 31ac1747eaea2..658521803824b 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -19,7 +19,7 @@ including other versions of pandas. Other Enhancements ^^^^^^^^^^^^^^^^^^ -- Indexing and __getitem__ of DataFrame and Series now accept zerodim np.ndarray. (:issue:`24919`) +- Indexing of ``DataFrame`` and ``Series`` now accepts zerodim ``np.ndarray`` (:issue:`24919`) - :meth:`Timestamp.replace` now supports the ``fold`` argument to disambiguate DST transition times (:issue:`25017`) - :meth:`DataFrame.at_time` and :meth:`Series.at_time` now support :meth:`datetime.time` objects with timezones (:issue:`24043`) -