本文整理汇总了Python中pandas.core.reshape.merge.MergeError方法的典型用法代码示例。如果您正苦于以下问题:Python merge.MergeError方法的具体用法?Python merge.MergeError怎么用?Python merge.MergeError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.core.reshape.merge
的用法示例。
在下文中一共展示了merge.MergeError方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_merge_misspecified
# 需要导入模块: from pandas.core.reshape import merge [as 别名]
# 或者: from pandas.core.reshape.merge import MergeError [as 别名]
def test_merge_misspecified(self):
msg = "Must pass right_on or right_index=True"
with pytest.raises(pd.errors.MergeError, match=msg):
merge(self.left, self.right, left_index=True)
msg = "Must pass left_on or left_index=True"
with pytest.raises(pd.errors.MergeError, match=msg):
merge(self.left, self.right, right_index=True)
msg = ('Can only pass argument "on" OR "left_on" and "right_on", not'
' a combination of both')
with pytest.raises(pd.errors.MergeError, match=msg):
merge(self.left, self.left, left_on='key', on='key')
msg = r"len\(right_on\) must equal len\(left_on\)"
with pytest.raises(ValueError, match=msg):
merge(self.df, self.df2, left_on=['key1'],
right_on=['key1', 'key2'])
示例2: test_overlapping_columns_error_message
# 需要导入模块: from pandas.core.reshape import merge [as 别名]
# 或者: from pandas.core.reshape.merge import MergeError [as 别名]
def test_overlapping_columns_error_message(self):
df = DataFrame({'key': [1, 2, 3],
'v1': [4, 5, 6],
'v2': [7, 8, 9]})
df2 = DataFrame({'key': [1, 2, 3],
'v1': [4, 5, 6],
'v2': [7, 8, 9]})
df.columns = ['key', 'foo', 'foo']
df2.columns = ['key', 'bar', 'bar']
expected = DataFrame({'key': [1, 2, 3],
'v1': [4, 5, 6],
'v2': [7, 8, 9],
'v3': [4, 5, 6],
'v4': [7, 8, 9]})
expected.columns = ['key', 'foo', 'foo', 'bar', 'bar']
assert_frame_equal(merge(df, df2), expected)
# #2649, #10639
df2.columns = ['key1', 'foo', 'foo']
msg = (r"Data columns not unique: Index\(\[u?'foo', u?'foo'\],"
r" dtype='object'\)")
with pytest.raises(MergeError, match=msg):
merge(df, df2)
示例3: test_multi_index
# 需要导入模块: from pandas.core.reshape import merge [as 别名]
# 或者: from pandas.core.reshape.merge import MergeError [as 别名]
def test_multi_index(self):
# MultiIndex is prohibited
trades = self.trades.set_index(['time', 'price'])
quotes = self.quotes.set_index('time')
with pytest.raises(MergeError):
merge_asof(trades, quotes,
left_index=True,
right_index=True)
trades = self.trades.set_index('time')
quotes = self.quotes.set_index(['time', 'bid'])
with pytest.raises(MergeError):
merge_asof(trades, quotes,
left_index=True,
right_index=True)
示例4: test_on_and_index
# 需要导入模块: from pandas.core.reshape import merge [as 别名]
# 或者: from pandas.core.reshape.merge import MergeError [as 别名]
def test_on_and_index(self):
# 'on' parameter and index together is prohibited
trades = self.trades.set_index('time')
quotes = self.quotes.set_index('time')
with pytest.raises(MergeError):
merge_asof(trades, quotes,
left_on='price',
left_index=True,
right_index=True)
trades = self.trades.set_index('time')
quotes = self.quotes.set_index('time')
with pytest.raises(MergeError):
merge_asof(trades, quotes,
right_on='bid',
left_index=True,
right_index=True)
示例5: test_valid_join_keys
# 需要导入模块: from pandas.core.reshape import merge [as 别名]
# 或者: from pandas.core.reshape.merge import MergeError [as 别名]
def test_valid_join_keys(self):
trades = self.trades
quotes = self.quotes
with pytest.raises(MergeError):
merge_asof(trades, quotes,
left_on='time',
right_on='bid',
by='ticker')
with pytest.raises(MergeError):
merge_asof(trades, quotes,
on=['time', 'ticker'],
by='ticker')
with pytest.raises(MergeError):
merge_asof(trades, quotes,
by='ticker')
示例6: test_no_overlap_more_informative_error
# 需要导入模块: from pandas.core.reshape import merge [as 别名]
# 或者: from pandas.core.reshape.merge import MergeError [as 别名]
def test_no_overlap_more_informative_error(self):
dt = datetime.now()
df1 = DataFrame({'x': ['a']}, index=[dt])
df2 = DataFrame({'y': ['b', 'c']}, index=[dt, dt])
msg = ('No common columns to perform merge on. '
'Merge options: left_on={lon}, right_on={ron}, '
'left_index={lidx}, right_index={ridx}'
.format(lon=None, ron=None, lidx=False, ridx=False))
with pytest.raises(MergeError, match=msg):
merge(df1, df2)
示例7: test_valid_allow_exact_matches
# 需要导入模块: from pandas.core.reshape import merge [as 别名]
# 或者: from pandas.core.reshape.merge import MergeError [as 别名]
def test_valid_allow_exact_matches(self):
trades = self.trades
quotes = self.quotes
with pytest.raises(MergeError):
merge_asof(trades, quotes,
on='time',
by='ticker',
allow_exact_matches='foo')
示例8: test_merge_datatype_error
# 需要导入模块: from pandas.core.reshape import merge [as 别名]
# 或者: from pandas.core.reshape.merge import MergeError [as 别名]
def test_merge_datatype_error(self):
""" Tests merge datatype mismatch error """
msg = r'merge keys \[0\] object and int64, must be the same type'
left = pd.DataFrame({'left_val': [1, 5, 10],
'a': ['a', 'b', 'c']})
right = pd.DataFrame({'right_val': [1, 2, 3, 6, 7],
'a': [1, 2, 3, 6, 7]})
with pytest.raises(MergeError, match=msg):
merge_asof(left, right, on='a')
示例9: test_no_overlap_more_informative_error
# 需要导入模块: from pandas.core.reshape import merge [as 别名]
# 或者: from pandas.core.reshape.merge import MergeError [as 别名]
def test_no_overlap_more_informative_error(self):
dt = datetime.now()
df1 = DataFrame({'x': ['a']}, index=[dt])
df2 = DataFrame({'y': ['b', 'c']}, index=[dt, dt])
pytest.raises(MergeError, merge, df1, df2)
msg = ('No common columns to perform merge on. '
'Merge options: left_on={lon}, right_on={ron}, '
'left_index={lidx}, right_index={ridx}'
.format(lon=None, ron=None, lidx=False, ridx=False))
with tm.assert_raises_regex(MergeError, msg):
merge(df1, df2)
示例10: test_multiby_indexed
# 需要导入模块: from pandas.core.reshape import merge [as 别名]
# 或者: from pandas.core.reshape.merge import MergeError [as 别名]
def test_multiby_indexed(self):
# GH15676
left = pd.DataFrame([
[pd.to_datetime('20160602'), 1, 'a'],
[pd.to_datetime('20160602'), 2, 'a'],
[pd.to_datetime('20160603'), 1, 'b'],
[pd.to_datetime('20160603'), 2, 'b']],
columns=['time', 'k1', 'k2']).set_index('time')
right = pd.DataFrame([
[pd.to_datetime('20160502'), 1, 'a', 1.0],
[pd.to_datetime('20160502'), 2, 'a', 2.0],
[pd.to_datetime('20160503'), 1, 'b', 3.0],
[pd.to_datetime('20160503'), 2, 'b', 4.0]],
columns=['time', 'k1', 'k2', 'value']).set_index('time')
expected = pd.DataFrame([
[pd.to_datetime('20160602'), 1, 'a', 1.0],
[pd.to_datetime('20160602'), 2, 'a', 2.0],
[pd.to_datetime('20160603'), 1, 'b', 3.0],
[pd.to_datetime('20160603'), 2, 'b', 4.0]],
columns=['time', 'k1', 'k2', 'value']).set_index('time')
result = pd.merge_asof(left,
right,
left_index=True,
right_index=True,
by=['k1', 'k2'])
assert_frame_equal(expected, result)
with pytest.raises(MergeError):
pd.merge_asof(left, right, left_index=True, right_index=True,
left_by=['k1', 'k2'], right_by=['k1'])
示例11: test_on_specialized_type
# 需要导入模块: from pandas.core.reshape import merge [as 别名]
# 或者: from pandas.core.reshape.merge import MergeError [as 别名]
def test_on_specialized_type(self):
# GH13936
for dtype in [np.uint8, np.uint16, np.uint32, np.uint64,
np.int8, np.int16, np.int32, np.int64,
np.float16, np.float32, np.float64]:
df1 = pd.DataFrame({
'value': [5, 2, 25, 100, 78, 120, 79],
'symbol': list("ABCDEFG")},
columns=['symbol', 'value'])
df1.value = dtype(df1.value)
df2 = pd.DataFrame({
'value': [0, 80, 120, 125],
'result': list('xyzw')},
columns=['value', 'result'])
df2.value = dtype(df2.value)
df1 = df1.sort_values('value').reset_index(drop=True)
if dtype == np.float16:
with pytest.raises(MergeError):
pd.merge_asof(df1, df2, on='value')
continue
result = pd.merge_asof(df1, df2, on='value')
expected = pd.DataFrame(
{'symbol': list("BACEGDF"),
'value': [2, 5, 25, 78, 79, 100, 120],
'result': list('xxxxxyz')
}, columns=['symbol', 'value', 'result'])
expected.value = dtype(expected.value)
assert_frame_equal(result, expected)