Repository: scipy/scipy
Pull request: Bug: interpolate.NearestNDInterpolator (issue #10352)
fix  #10352
The problem is solved by casting the searched array.

Problem statement:
BUG: interpolate.NearestNDInterpolator with pandas
interpolate.NearestNDInterpolator does not work as expected when used  with selected pandas dataframe.
This is due to the index being maintained when making selections in pandas.

### Reproducing code example:
```
import numpy as np
import pandas as pd
from scipy import interpolate

df = pd.DataFrame(np.array([[0, 0, 0, 0, 1, 0, 1],
                            [0, 0, 0, 0, 0, 1, 1],
                            [0, 0, 0, 0, 1, 1, 2]]).T, columns=['x', 'y', 'z'])
df_select = df[3:]
NI = interpolate.NearestNDInterpolator((df_select.x, df_select.y), df_select.z)
print(NI([0.1, 0.9], [0.1, 0.9]))
```
I expect [0, 2] to be output.
But output is [Nan, 0] as pandas.Series.

This is due to the index being maintained when making selections in pandas.
Specifically, `df_select.z` has index[3, 4, 5, 6].
But, self.tree.query (xi) line 81, in scipy/interpolate/ndgriddata.py returns a index that assumes that the index starts from zero.
So, self.tree.query (xi) return [0, 3]
Therefore, self.values[i] line 82, in scipy/interpolate/ndgriddata.py using Invalid index.

### Note
if case of
```
df_select = df[3:].reset_index()
```
or
```
NI = interpolate.NearestNDInterpolator((df_select.x, df_select.y), np.array(df_select.z))
```
it works as expected.

Also, this bug does not occur in interpolate.LinearNDInterpolator.

### Scipy/Numpy/Python version information:
```
1.3.0 1.16.4 sys.version_info(major=3, minor=6, micro=8, releaselevel='final', serial=0)
```

Hints from issue discussion:

Commit message: fix #10352

Patch to review:
diff --git a/scipy/interpolate/ndgriddata.py b/scipy/interpolate/ndgriddata.py
index 023c439f8451..0aa4aedfb801 100644
--- a/scipy/interpolate/ndgriddata.py
+++ b/scipy/interpolate/ndgriddata.py
@@ -62,7 +62,7 @@ def __init__(self, x, y, rescale=False, tree_options=None):
         if tree_options is None:
             tree_options = dict()
         self.tree = cKDTree(self.points, **tree_options)
-        self.values = y
+        self.values = np.array(y)
 
     def __call__(self, *args):
         """


Code review comments:
diff_hunk: @@ -62,7 +62,7 @@ def __init__(self, x, y, rescale=False, tree_options=None):
         if tree_options is None:
             tree_options = dict()
         self.tree = cKDTree(self.points, **tree_options)
-        self.values = y
+        self.values = np.array(y)
path: scipy/interpolate/ndgriddata.py
text: @user1:
np.asarray(y), to not make unnecessary copies

@author:
Thank you @user1. 
I think that your suggestion is better performance.