Find numpy array values bounding an input value
Question: I have a value, say 2016 and a sorted numpy array: `[2005, 2010, 2015, 2020,
2025, 2030]`. What is the pythonic way to find the 2 values in the array that
bound 2016. In this case, the answer will be an array [2015, 2020].

Not sure how to do it other than loop, but hoping for a more numpy based
solution

\--EDIT:

you can assume that you will never get a value that is in the array, I
prefilter for that


Answer: You could do something like this:

    
    
    In[1]: import numpy as np
    
    In[2]: x = np.array([2005, 2010, 2015, 2020, 2025, 2030])
    
    In[3]: x
    Out[3]: array([2005, 2010, 2015, 2020, 2025, 2030])
    
    
    In[4]: x[x > 2016].min()
    Out[4]: 2020
    
    
    In[5]: x[x < 2016].max()
    Out[5]: 2015
    
    In[6]: def bound(value, arr):
               return arr[arr < value].max(), arr[arr > value].min()
    
    In[7]: bound(2016, x)
    Out[7]: (2015, 2020)
    

