nodes: 0, module; 1, function_definition; 2, function_name:find_nearest; 3, parameters; 4, type; 5, block; 6, identifier:x; 7, identifier:x0; 8, generic_type; 9, expression_statement; 10, expression_statement; 11, comment:# for indexing upon return; 12, expression_statement; 13, comment:# %%; 14, if_statement; 15, if_statement; 16, comment:# %%; 17, expression_statement; 18, comment:# NOTE: not trapping IndexError (all-nan) becaues returning None can surprise with slice indexing; 19, for_statement; 20, return_statement; 21, identifier:Tuple; 22, type_parameter; 23, comment:"""
      This find_nearest function does NOT assume sorted input
  
      inputs:
      x: array (float, int, datetime, h5py.Dataset) within which to search for x0
      x0: singleton or array of values to search for in x
  
      outputs:
      idx: index of flattened x nearest to x0  (i.e. works with higher than 1-D arrays also)
      xidx: x[idx]
  
      Observe how bisect.bisect() gives the incorrect result!
  
      idea based on:
      http://stackoverflow.com/questions/2566412/find-nearest-value-in-numpy-array
  
      """; 24, assignment; 25, assignment; 26, boolean_operator; 27, block; 28, comparison_operator:x0.ndim not in (0, 1); 29, block; 30, assignment; 31, pattern_list; 32, call; 33, block; 34, expression_list; 35, type; 36, type; 37, identifier:x; 38, call; 39, identifier:x0; 40, call; 41, comparison_operator:x.size == 0; 42, comparison_operator:x0.size == 0; 43, raise_statement; 44, attribute; 45, tuple; 46, raise_statement; 47, identifier:ind; 48, call; 49, identifier:i; 50, identifier:xi; 51, identifier:enumerate; 52, argument_list; 53, if_statement; 54, subscript; 55, subscript; 56, identifier:int; 57, identifier:Any; 58, attribute; 59, argument_list; 60, attribute; 61, argument_list; 62, attribute; 63, integer:0; 64, attribute; 65, integer:0; 66, call; 67, identifier:x0; 68, identifier:ndim; 69, integer:0; 70, integer:1; 71, call; 72, attribute; 73, argument_list; 74, identifier:x0; 75, boolean_operator; 76, block; 77, else_clause; 78, call; 79, tuple; 80, call; 81, tuple; 82, identifier:np; 83, identifier:asanyarray; 84, identifier:x; 85, identifier:np; 86, identifier:atleast_1d; 87, identifier:x0; 88, identifier:x; 89, identifier:size; 90, identifier:x0; 91, identifier:size; 92, identifier:ValueError; 93, argument_list; 94, identifier:ValueError; 95, argument_list; 96, identifier:np; 97, identifier:empty_like; 98, identifier:x0; 99, keyword_argument; 100, comparison_operator:xi is not None; 101, parenthesized_expression; 102, expression_statement; 103, block; 104, attribute; 105, argument_list; 106, attribute; 107, argument_list; 108, string; 109, string; 110, identifier:dtype; 111, identifier:int; 112, identifier:xi; 113, None; 114, boolean_operator; 115, assignment; 116, raise_statement; 117, identifier:ind; 118, identifier:squeeze; 119, subscript; 120, identifier:squeeze; 121, string_content:empty input(s); 122, string_content:2-D x0 not handled yet; 123, call; 124, call; 125, subscript; 126, call; 127, call; 128, identifier:x; 129, identifier:ind; 130, identifier:isinstance; 131, argument_list; 132, attribute; 133, argument_list; 134, identifier:ind; 135, identifier:i; 136, attribute; 137, argument_list; 138, identifier:ValueError; 139, argument_list; 140, identifier:xi; 141, tuple; 142, identifier:np; 143, identifier:isfinite; 144, identifier:xi; 145, identifier:np; 146, identifier:nanargmin; 147, call; 148, string; 149, attribute; 150, attribute; 151, attribute; 152, identifier:abs; 153, argument_list; 154, string_content:x0 must NOT be None or NaN to avoid surprising None return value; 155, identifier:datetime; 156, identifier:datetime; 157, identifier:datetime; 158, identifier:date; 159, identifier:np; 160, identifier:datetime64; 161, binary_operator:x-xi; 162, identifier:x; 163, identifier:xi
code: def find_nearest(x, x0) -> Tuple[int, Any]:
      """
      This find_nearest function does NOT assume sorted input
  
      inputs:
      x: array (float, int, datetime, h5py.Dataset) within which to search for x0
      x0: singleton or array of values to search for in x
  
      outputs:
      idx: index of flattened x nearest to x0  (i.e. works with higher than 1-D arrays also)
      xidx: x[idx]
  
      Observe how bisect.bisect() gives the incorrect result!
  
      idea based on:
      http://stackoverflow.com/questions/2566412/find-nearest-value-in-numpy-array
  
      """
      x = np.asanyarray(x)  # for indexing upon return
      x0 = np.atleast_1d(x0)
  # %%
      if x.size == 0 or x0.size == 0:
          raise ValueError('empty input(s)')
  
      if x0.ndim not in (0, 1):
          raise ValueError('2-D x0 not handled yet')
  # %%
      ind = np.empty_like(x0, dtype=int)
  
      # NOTE: not trapping IndexError (all-nan) becaues returning None can surprise with slice indexing
      for i, xi in enumerate(x0):
          if xi is not None and (isinstance(xi, (datetime.datetime, datetime.date, np.datetime64)) or np.isfinite(xi)):
              ind[i] = np.nanargmin(abs(x-xi))
          else:
              raise ValueError('x0 must NOT be None or NaN to avoid surprising None return value')
  
      return ind.squeeze()[()], x[ind].squeeze()[()]