From adde2bdd9209fdcb7c91e52144ac6e81ec969f08 Mon Sep 17 00:00:00 2001 From: Subhendu Ranjan Mishra Date: Sat, 28 Apr 2018 15:26:12 +0530 Subject: [PATCH 1/3] Fixes #20852 - Fix indexing.py to make a copy of the indices --- pandas/core/indexing.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index fe6d6775c4e0b..c2e830cd5bc2f 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -2413,7 +2413,10 @@ def maybe_convert_indices(indices, n): # If list is empty, np.array will return float and cause indexing # errors. return np.empty(0, dtype=np.int_) - + else: + # create a copy of indices + indices = np.array(indices, copy=True) + mask = indices < 0 if mask.any(): indices[mask] += n From 103d41d083dc33072132d1d2ef790e0e978be05d Mon Sep 17 00:00:00 2001 From: Subhendu Ranjan Mishra Date: Wed, 2 May 2018 19:55:08 +0530 Subject: [PATCH 2/3] Fix linting issue in 'indexing.py' --- pandas/core/indexing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index c2e830cd5bc2f..25907dd4d50f0 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -2416,7 +2416,7 @@ def maybe_convert_indices(indices, n): else: # create a copy of indices indices = np.array(indices, copy=True) - + mask = indices < 0 if mask.any(): indices[mask] += n From 6b0a8723e668bd99c64e2c4c2bbdfb26b7616d7c Mon Sep 17 00:00:00 2001 From: Subhendu Ranjan Mishra Date: Mon, 7 May 2018 22:23:53 +0530 Subject: [PATCH 3/3] Create a Copy the indices only when it's going to be changed --- pandas/core/indexing.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 25907dd4d50f0..035314e73f8e5 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -2413,12 +2413,10 @@ def maybe_convert_indices(indices, n): # If list is empty, np.array will return float and cause indexing # errors. return np.empty(0, dtype=np.int_) - else: - # create a copy of indices - indices = np.array(indices, copy=True) mask = indices < 0 if mask.any(): + indices = indices.copy() indices[mask] += n mask = (indices >= n) | (indices < 0)