@@ -640,8 +640,6 @@ def __init__(
640
640
self .axis = 1 - axis if self .left .ndim == 2 else 0
641
641
642
642
self .on = com .maybe_make_list (on )
643
- self .left_on = com .maybe_make_list (left_on )
644
- self .right_on = com .maybe_make_list (right_on )
645
643
646
644
self .copy = copy
647
645
self .suffixes = suffixes
@@ -674,7 +672,7 @@ def __init__(
674
672
msg , FutureWarning , stacklevel = find_stack_level (inspect .currentframe ())
675
673
)
676
674
677
- self ._validate_specification ( )
675
+ self .left_on , self . right_on = self . _validate_left_right_on ( left_on , right_on )
678
676
679
677
cross_col = None
680
678
if self .how == "cross" :
@@ -1077,7 +1075,7 @@ def _get_merge_keys(self):
1077
1075
# a pd.merge_asof(left_index=True, left_by=...) will result in a
1078
1076
# self.left_on array with a None in the middle of it. This requires
1079
1077
# a work-around as designated in the code below.
1080
- # See _validate_specification () for where this happens.
1078
+ # See _validate_left_right_on () for where this happens.
1081
1079
1082
1080
# ugh, spaghetti re #733
1083
1081
if _any (self .left_on ) and _any (self .right_on ):
@@ -1325,25 +1323,27 @@ def _create_cross_configuration(
1325
1323
cross_col ,
1326
1324
)
1327
1325
1328
- def _validate_specification (self ) -> None :
1326
+ def _validate_left_right_on (self , left_on , right_on ):
1327
+ left_on = com .maybe_make_list (left_on )
1328
+ right_on = com .maybe_make_list (right_on )
1329
+
1329
1330
if self .how == "cross" :
1330
1331
if (
1331
1332
self .left_index
1332
1333
or self .right_index
1333
- or self . right_on is not None
1334
- or self . left_on is not None
1334
+ or right_on is not None
1335
+ or left_on is not None
1335
1336
or self .on is not None
1336
1337
):
1337
1338
raise MergeError (
1338
1339
"Can not pass on, right_on, left_on or set right_index=True or "
1339
1340
"left_index=True"
1340
1341
)
1341
- return
1342
1342
# Hm, any way to make this logic less complicated??
1343
- elif self .on is None and self . left_on is None and self . right_on is None :
1343
+ elif self .on is None and left_on is None and right_on is None :
1344
1344
1345
1345
if self .left_index and self .right_index :
1346
- self . left_on , self . right_on = (), ()
1346
+ left_on , right_on = (), ()
1347
1347
elif self .left_index :
1348
1348
raise MergeError ("Must pass right_on or right_index=True" )
1349
1349
elif self .right_index :
@@ -1356,8 +1356,8 @@ def _validate_specification(self) -> None:
1356
1356
if len (common_cols ) == 0 :
1357
1357
raise MergeError (
1358
1358
"No common columns to perform merge on. "
1359
- f"Merge options: left_on={ self . left_on } , "
1360
- f"right_on={ self . right_on } , "
1359
+ f"Merge options: left_on={ left_on } , "
1360
+ f"right_on={ right_on } , "
1361
1361
f"left_index={ self .left_index } , "
1362
1362
f"right_index={ self .right_index } "
1363
1363
)
@@ -1366,9 +1366,9 @@ def _validate_specification(self) -> None:
1366
1366
or not right_cols .join (common_cols , how = "inner" ).is_unique
1367
1367
):
1368
1368
raise MergeError (f"Data columns not unique: { repr (common_cols )} " )
1369
- self . left_on = self . right_on = common_cols
1369
+ left_on = right_on = common_cols
1370
1370
elif self .on is not None :
1371
- if self . left_on is not None or self . right_on is not None :
1371
+ if left_on is not None or right_on is not None :
1372
1372
raise MergeError (
1373
1373
'Can only pass argument "on" OR "left_on" '
1374
1374
'and "right_on", not a combination of both.'
@@ -1378,40 +1378,42 @@ def _validate_specification(self) -> None:
1378
1378
'Can only pass argument "on" OR "left_index" '
1379
1379
'and "right_index", not a combination of both.'
1380
1380
)
1381
- self . left_on = self . right_on = self .on
1382
- elif self . left_on is not None :
1381
+ left_on = right_on = self .on
1382
+ elif left_on is not None :
1383
1383
if self .left_index :
1384
1384
raise MergeError (
1385
1385
'Can only pass argument "left_on" OR "left_index" not both.'
1386
1386
)
1387
- if not self .right_index and self . right_on is None :
1387
+ if not self .right_index and right_on is None :
1388
1388
raise MergeError ('Must pass "right_on" OR "right_index".' )
1389
- n = len (self . left_on )
1389
+ n = len (left_on )
1390
1390
if self .right_index :
1391
- if len (self . left_on ) != self .right .index .nlevels :
1391
+ if len (left_on ) != self .right .index .nlevels :
1392
1392
raise ValueError (
1393
1393
"len(left_on) must equal the number "
1394
1394
'of levels in the index of "right"'
1395
1395
)
1396
- self . right_on = [None ] * n
1397
- elif self . right_on is not None :
1396
+ right_on = [None ] * n
1397
+ elif right_on is not None :
1398
1398
if self .right_index :
1399
1399
raise MergeError (
1400
1400
'Can only pass argument "right_on" OR "right_index" not both.'
1401
1401
)
1402
- if not self .left_index and self . left_on is None :
1402
+ if not self .left_index and left_on is None :
1403
1403
raise MergeError ('Must pass "left_on" OR "left_index".' )
1404
- n = len (self . right_on )
1404
+ n = len (right_on )
1405
1405
if self .left_index :
1406
- if len (self . right_on ) != self .left .index .nlevels :
1406
+ if len (right_on ) != self .left .index .nlevels :
1407
1407
raise ValueError (
1408
1408
"len(right_on) must equal the number "
1409
1409
'of levels in the index of "left"'
1410
1410
)
1411
- self . left_on = [None ] * n
1412
- if self .how != "cross" and len (self . right_on ) != len (self . left_on ):
1411
+ left_on = [None ] * n
1412
+ if self .how != "cross" and len (right_on ) != len (left_on ):
1413
1413
raise ValueError ("len(right_on) must equal len(left_on)" )
1414
1414
1415
+ return left_on , right_on
1416
+
1415
1417
def _validate (self , validate : str ) -> None :
1416
1418
1417
1419
# Check uniqueness of each
@@ -1769,14 +1771,14 @@ def __init__(
1769
1771
fill_method = fill_method ,
1770
1772
)
1771
1773
1772
- def _validate_specification (self ) -> None :
1773
- super ()._validate_specification ( )
1774
+ def _validate_left_right_on (self , left_on , right_on ) :
1775
+ left_on , right_on = super ()._validate_left_right_on ( left_on , right_on )
1774
1776
1775
1777
# we only allow on to be a single item for on
1776
- if len (self . left_on ) != 1 and not self .left_index :
1778
+ if len (left_on ) != 1 and not self .left_index :
1777
1779
raise MergeError ("can only asof on a key for left" )
1778
1780
1779
- if len (self . right_on ) != 1 and not self .right_index :
1781
+ if len (right_on ) != 1 and not self .right_index :
1780
1782
raise MergeError ("can only asof on a key for right" )
1781
1783
1782
1784
if self .left_index and isinstance (self .left .index , MultiIndex ):
@@ -1797,27 +1799,27 @@ def _validate_specification(self) -> None:
1797
1799
1798
1800
# GH#29130 Check that merge keys do not have dtype object
1799
1801
if not self .left_index :
1800
- left_on = self . left_on [0 ]
1801
- if is_array_like (left_on ):
1802
- lo_dtype = left_on .dtype
1802
+ left_on_0 = left_on [0 ]
1803
+ if is_array_like (left_on_0 ):
1804
+ lo_dtype = left_on_0 .dtype
1803
1805
else :
1804
1806
lo_dtype = (
1805
- self .left [left_on ].dtype
1806
- if left_on in self .left .columns
1807
- else self .left .index .get_level_values (left_on )
1807
+ self .left [left_on_0 ].dtype
1808
+ if left_on_0 in self .left .columns
1809
+ else self .left .index .get_level_values (left_on_0 )
1808
1810
)
1809
1811
else :
1810
1812
lo_dtype = self .left .index .dtype
1811
1813
1812
1814
if not self .right_index :
1813
- right_on = self . right_on [0 ]
1814
- if is_array_like (right_on ):
1815
- ro_dtype = right_on .dtype
1815
+ right_on_0 = right_on [0 ]
1816
+ if is_array_like (right_on_0 ):
1817
+ ro_dtype = right_on_0 .dtype
1816
1818
else :
1817
1819
ro_dtype = (
1818
- self .right [right_on ].dtype
1819
- if right_on in self .right .columns
1820
- else self .right .index .get_level_values (right_on )
1820
+ self .right [right_on_0 ].dtype
1821
+ if right_on_0 in self .right .columns
1822
+ else self .right .index .get_level_values (right_on_0 )
1821
1823
)
1822
1824
else :
1823
1825
ro_dtype = self .right .index .dtype
@@ -1839,13 +1841,15 @@ def _validate_specification(self) -> None:
1839
1841
if len (self .left_by ) != len (self .right_by ):
1840
1842
raise MergeError ("left_by and right_by must be same length" )
1841
1843
1842
- self . left_on = self .left_by + list (self . left_on )
1843
- self . right_on = self .right_by + list (self . right_on )
1844
+ left_on = self .left_by + list (left_on )
1845
+ right_on = self .right_by + list (right_on )
1844
1846
1845
1847
# check 'direction' is valid
1846
1848
if self .direction not in ["backward" , "forward" , "nearest" ]:
1847
1849
raise MergeError (f"direction invalid: { self .direction } " )
1848
1850
1851
+ return left_on , right_on
1852
+
1849
1853
def _get_merge_keys (self ):
1850
1854
1851
1855
# note this function has side effects
0 commit comments