xArray: reindex_like(method='nearest') TypeError: unsupported operand type(s) for -: 'str' and 'str'

Viewed 276

I have two xArray dataarrays that indexed by datetime64 and contain a single feature: a list containing a single string. They are of different lengths, and I want to reindex one to match the indices of the other. However, calling reindex_like using the 'nearest' fill method throws an error (TypeError: unsupported operand type(s) for -: 'str' and 'str') in the underlying Pandas reindex_like function. Here's the full trace:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-100-a93735b2e55d> in <module>()
----> 1 b_dataarray.reindex_like(d_dataarray, method='nearest')

7 frames
/usr/local/lib/python3.7/dist-packages/xarray/core/dataarray.py in reindex_like(self, other, method, tolerance, copy, fill_value)
   1235             tolerance=tolerance,
   1236             copy=copy,
-> 1237             fill_value=fill_value,
   1238         )
   1239 

/usr/local/lib/python3.7/dist-packages/xarray/core/dataarray.py in reindex(self, indexers, method, tolerance, copy, fill_value, **indexers_kwargs)
   1298             tolerance=tolerance,
   1299             copy=copy,
-> 1300             fill_value=fill_value,
   1301         )
   1302         return self._from_temp_dataset(ds)

/usr/local/lib/python3.7/dist-packages/xarray/core/dataset.py in reindex(self, indexers, method, tolerance, copy, fill_value, **indexers_kwargs)
   2495             fill_value,
   2496             sparse=False,
-> 2497             **indexers_kwargs,
   2498         )
   2499 

/usr/local/lib/python3.7/dist-packages/xarray/core/dataset.py in _reindex(self, indexers, method, tolerance, copy, fill_value, sparse, **indexers_kwargs)
   2526             copy=copy,
   2527             fill_value=fill_value,
-> 2528             sparse=sparse,
   2529         )
   2530         coord_names = set(self._coord_names)

/usr/local/lib/python3.7/dist-packages/xarray/core/alignment.py in reindex_variables(variables, sizes, indexes, indexers, method, tolerance, copy, fill_value, sparse)
    550                 )
    551 
--> 552             int_indexer = get_indexer_nd(index, target, method, tolerance)
    553 
    554             # We uses negative values from get_indexer_nd to signify

/usr/local/lib/python3.7/dist-packages/xarray/core/indexing.py in get_indexer_nd(index, labels, method, tolerance)
    101     """
    102     flat_labels = np.ravel(labels)
--> 103     flat_indexer = index.get_indexer(flat_labels, method=method, tolerance=tolerance)
    104     indexer = flat_indexer.reshape(labels.shape)
    105     return indexer

/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in get_indexer(self, target, method, limit, tolerance)
   2994             indexer = self._get_fill_indexer(target, method, limit, tolerance)
   2995         elif method == "nearest":
-> 2996             indexer = self._get_nearest_indexer(target, limit, tolerance)
   2997         else:
   2998             if tolerance is not None:

/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/base.py in _get_nearest_indexer(self, target, limit, tolerance)
   3080 
   3081         target_values = target._values
-> 3082         left_distances = np.abs(self._values[left_indexer] - target_values)
   3083         right_distances = np.abs(self._values[right_indexer] - target_values)
   3084 

TypeError: unsupported operand type(s) for -: 'str' and 'str'

It looks like it's being thrown where Pandas is determining the closest index to fill from. However, I am confused as to why it's trying to subtract two strings when the indices of the dataarrays are datetime64. I did double check to make sure there isn't an errant string in the indices of either dataarray, and I can confirm that they are all datetime64. Perhaps Pandas is mistakenly comparing the data values of the dataarrays and not the datetime indices?

1 Answers

I had a very similar issue, and honestly wasn't sure on how to fix it. I avoided using the reindex_like by replacing

ppg_df.reindex_like(ecg_df, method='nearest')

with

ppg_df.reindex(df2.index, method='nearest')
Related