BUG: Raise ValueError for non numerical join columns in merge_asof by phofl · Pull Request #34488 · pandas-dev/pandas (original) (raw)

Sure, here is an example:

data1 = pd.DataFrame({
            'name': ['mary', 'randy'],
            'age': [33, 23],
            'city': ['nyc', 'chi'],
            'date': [pd.to_datetime('20210303'), pd.to_datetime('20210303')],
        })

data2 = pd.DataFrame({
            'name': ['pete', 'mary'],
            'age': [22, 44],
            'city': ['nyc', 'phi'],
            'date': [pd.to_datetime('20210303'), pd.to_datetime('20210505')],
        })

data2= data2.set_index('date')

together = pd.merge_asof(data1.sort_values('date'), data2.sort_values('date'), left_on='date', right_on='date', left_by='city', right_by='city', tolerance=pd.Timedelta('2D'))

`~/anaconda3/envs/dailytestenv/lib/python3.8/site-packages/pandas/core/reshape/merge.py in merge_asof(left, right, on, left_on, right_on, left_index, right_index, by, left_by, right_by, suffixes, tolerance, allow_exact_matches, direction)
579 4 2016-05-25 13:30:00.048 AAPL 98.00 100 NaN NaN
580 """
--> 581 op = _AsOfMerge(
582 left,
583 right,

~/anaconda3/envs/dailytestenv/lib/python3.8/site-packages/pandas/core/reshape/merge.py in init(self, left, right, on, left_on, right_on, left_index, right_index, by, left_by, right_by, axis, suffixes, copy, fill_method, how, tolerance, allow_exact_matches, direction)
1734 self.direction = direction
1735
-> 1736 _OrderedMerge.init(
1737 self,
1738 left,

~/anaconda3/envs/dailytestenv/lib/python3.8/site-packages/pandas/core/reshape/merge.py in init(self, left, right, on, left_on, right_on, left_index, right_index, axis, suffixes, copy, fill_method, how)
1617
1618 self.fill_method = fill_method
-> 1619 _MergeOperation.init(
1620 self,
1621 left,

~/anaconda3/envs/dailytestenv/lib/python3.8/site-packages/pandas/core/reshape/merge.py in init(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, copy, indicator, validate)
680 warnings.warn(msg, FutureWarning, stacklevel=3)
681
--> 682 self._validate_specification()
683
684 cross_col = None

~/anaconda3/envs/dailytestenv/lib/python3.8/site-packages/pandas/core/reshape/merge.py in _validate_specification(self)
1782 )
1783 ro_dtype = (
-> 1784 self.right[self.right_on[0]].dtype
1785 if not self.right_index
1786 else self.right.index.dtype

~/anaconda3/envs/dailytestenv/lib/python3.8/site-packages/pandas/core/frame.py in getitem(self, key)
3453 if self.columns.nlevels > 1:
3454 return self._getitem_multilevel(key)
-> 3455 indexer = self.columns.get_loc(key)
3456 if is_integer(indexer):
3457 indexer = [indexer]

~/anaconda3/envs/dailytestenv/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3361 return self._engine.get_loc(casted_key)
3362 except KeyError as err:
-> 3363 raise KeyError(key) from err
3364
3365 if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 'date'
`