本文整理汇总了Python中pandas.merge_asof方法的典型用法代码示例。如果您正苦于以下问题:Python pandas.merge_asof方法的具体用法?Python pandas.merge_asof怎么用?Python pandas.merge_asof使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas
的用法示例。
在下文中一共展示了pandas.merge_asof方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_multi_index
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [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)
示例2: test_on_and_index
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [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)
示例3: test_tolerance_tz
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def test_tolerance_tz(self):
# GH 14844
left = pd.DataFrame(
{'date': pd.date_range(start=pd.to_datetime('2016-01-02'),
freq='D', periods=5,
tz=pytz.timezone('UTC')),
'value1': np.arange(5)})
right = pd.DataFrame(
{'date': pd.date_range(start=pd.to_datetime('2016-01-01'),
freq='D', periods=5,
tz=pytz.timezone('UTC')),
'value2': list("ABCDE")})
result = pd.merge_asof(left, right, on='date',
tolerance=pd.Timedelta('1 day'))
expected = pd.DataFrame(
{'date': pd.date_range(start=pd.to_datetime('2016-01-02'),
freq='D', periods=5,
tz=pytz.timezone('UTC')),
'value1': np.arange(5),
'value2': list("BCDEE")})
assert_frame_equal(result, expected)
示例4: test_allow_exact_matches_and_tolerance3
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def test_allow_exact_matches_and_tolerance3(self):
# GH 13709
df1 = pd.DataFrame({
'time': pd.to_datetime(['2016-07-15 13:30:00.030',
'2016-07-15 13:30:00.030']),
'username': ['bob', 'charlie']})
df2 = pd.DataFrame({
'time': pd.to_datetime(['2016-07-15 13:30:00.000',
'2016-07-15 13:30:00.030']),
'version': [1, 2]})
result = pd.merge_asof(df1, df2, on='time', allow_exact_matches=False,
tolerance=pd.Timedelta('10ms'))
expected = pd.DataFrame({
'time': pd.to_datetime(['2016-07-15 13:30:00.030',
'2016-07-15 13:30:00.030']),
'username': ['bob', 'charlie'],
'version': [np.nan, np.nan]})
assert_frame_equal(result, expected)
示例5: test_forward_by
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def test_forward_by(self):
# GH14887
left = pd.DataFrame({'a': [1, 5, 10, 12, 15],
'b': ['X', 'X', 'Y', 'Z', 'Y'],
'left_val': ['a', 'b', 'c', 'd', 'e']})
right = pd.DataFrame({'a': [1, 6, 11, 15, 16],
'b': ['X', 'Z', 'Y', 'Z', 'Y'],
'right_val': [1, 6, 11, 15, 16]})
expected = pd.DataFrame({'a': [1, 5, 10, 12, 15],
'b': ['X', 'X', 'Y', 'Z', 'Y'],
'left_val': ['a', 'b', 'c', 'd', 'e'],
'right_val': [1, np.nan, 11, 15, 16]})
result = pd.merge_asof(left, right, on='a', by='b',
direction='forward')
assert_frame_equal(result, expected)
示例6: test_nearest_by
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def test_nearest_by(self):
# GH14887
left = pd.DataFrame({'a': [1, 5, 10, 12, 15],
'b': ['X', 'X', 'Z', 'Z', 'Y'],
'left_val': ['a', 'b', 'c', 'd', 'e']})
right = pd.DataFrame({'a': [1, 6, 11, 15, 16],
'b': ['X', 'Z', 'Z', 'Z', 'Y'],
'right_val': [1, 6, 11, 15, 16]})
expected = pd.DataFrame({'a': [1, 5, 10, 12, 15],
'b': ['X', 'X', 'Z', 'Z', 'Y'],
'left_val': ['a', 'b', 'c', 'd', 'e'],
'right_val': [1, 1, 11, 11, 16]})
result = pd.merge_asof(left, right, on='a', by='b',
direction='nearest')
assert_frame_equal(result, expected)
示例7: test_on_float
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def test_on_float(self):
# mimics how to determine the minimum-price variation
df1 = pd.DataFrame({
'price': [5.01, 0.0023, 25.13, 340.05, 30.78, 1040.90, 0.0078],
'symbol': list("ABCDEFG")},
columns=['symbol', 'price'])
df2 = pd.DataFrame({
'price': [0.0, 1.0, 100.0],
'mpv': [0.0001, 0.01, 0.05]},
columns=['price', 'mpv'])
df1 = df1.sort_values('price').reset_index(drop=True)
result = pd.merge_asof(df1, df2, on='price')
expected = pd.DataFrame({
'symbol': list("BGACEDF"),
'price': [0.0023, 0.0078, 5.01, 25.13, 30.78, 340.05, 1040.90],
'mpv': [0.0001, 0.0001, 0.01, 0.01, 0.01, 0.05, 0.05]},
columns=['symbol', 'price', 'mpv'])
assert_frame_equal(result, expected)
示例8: test_tolerance_tz
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def test_tolerance_tz(self):
# GH 14844
left = pd.DataFrame(
{'date': pd.DatetimeIndex(start=pd.to_datetime('2016-01-02'),
freq='D', periods=5,
tz=pytz.timezone('UTC')),
'value1': np.arange(5)})
right = pd.DataFrame(
{'date': pd.DatetimeIndex(start=pd.to_datetime('2016-01-01'),
freq='D', periods=5,
tz=pytz.timezone('UTC')),
'value2': list("ABCDE")})
result = pd.merge_asof(left, right, on='date',
tolerance=pd.Timedelta('1 day'))
expected = pd.DataFrame(
{'date': pd.DatetimeIndex(start=pd.to_datetime('2016-01-02'),
freq='D', periods=5,
tz=pytz.timezone('UTC')),
'value1': np.arange(5),
'value2': list("BCDEE")})
assert_frame_equal(result, expected)
示例9: pct_chg
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def pct_chg(self, freq="Y", benchmark=True):
"""
年度,月,周涨幅统计
:param freq: str, default Y, could be M or W or anything pd.date_range accepts
:return: pd.DataFrame with columns date and pct_chg
"""
if getattr(self, "bmprice", None) is None:
benchmark = False
ydf = pd.merge_asof(
pd.DataFrame(
pd.date_range(
self.price["date"].iloc[0], self.price["date"].iloc[-1], freq=freq
),
columns=["date"],
),
self.price,
)
ydf["pct_chg"] = ydf["netvalue"].pct_change()
if benchmark:
ydf = pd.merge_asof(ydf, self.bmprice, on="date", suffixes=["", "_bc"])
ydf["pct_chg_benchmark"] = ydf["netvalue_bc"].pct_change()
ydf["pct_chg_difference"] = ydf["pct_chg"] - ydf["pct_chg_benchmark"]
return ydf[["date", "pct_chg", "pct_chg_benchmark", "pct_chg_difference"]]
return ydf[["date", "pct_chg"]]
示例10: test_examples1
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def test_examples1(self):
""" doc-string examples """
left = pd.DataFrame({'a': [1, 5, 10],
'left_val': ['a', 'b', 'c']})
right = pd.DataFrame({'a': [1, 2, 3, 6, 7],
'right_val': [1, 2, 3, 6, 7]})
expected = pd.DataFrame({'a': [1, 5, 10],
'left_val': ['a', 'b', 'c'],
'right_val': [1, 3, 7]})
result = pd.merge_asof(left, right, on='a')
assert_frame_equal(result, expected)
示例11: test_examples3
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def test_examples3(self):
""" doc-string examples """
# GH14887
left = pd.DataFrame({'a': [1, 5, 10],
'left_val': ['a', 'b', 'c']})
right = pd.DataFrame({'a': [1, 2, 3, 6, 7],
'right_val': [1, 2, 3, 6, 7]})
expected = pd.DataFrame({'a': [1, 5, 10],
'left_val': ['a', 'b', 'c'],
'right_val': [1, 6, np.nan]})
result = pd.merge_asof(left, right, on='a', direction='forward')
assert_frame_equal(result, expected)
示例12: test_examples4
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def test_examples4(self):
""" doc-string examples """
# GH14887
left = pd.DataFrame({'a': [1, 5, 10],
'left_val': ['a', 'b', 'c']})
right = pd.DataFrame({'a': [1, 2, 3, 6, 7],
'right_val': [1, 2, 3, 6, 7]})
expected = pd.DataFrame({'a': [1, 5, 10],
'left_val': ['a', 'b', 'c'],
'right_val': [1, 6, 7]})
result = pd.merge_asof(left, right, on='a', direction='nearest')
assert_frame_equal(result, expected)
示例13: test_basic
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def test_basic(self):
expected = self.asof
trades = self.trades
quotes = self.quotes
result = merge_asof(trades, quotes,
on='time',
by='ticker')
assert_frame_equal(result, expected)
示例14: test_basic_categorical
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def test_basic_categorical(self):
expected = self.asof
trades = self.trades.copy()
trades.ticker = trades.ticker.astype('category')
quotes = self.quotes.copy()
quotes.ticker = quotes.ticker.astype('category')
expected.ticker = expected.ticker.astype('category')
result = merge_asof(trades, quotes,
on='time',
by='ticker')
assert_frame_equal(result, expected)
示例15: test_basic_right_index
# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import merge_asof [as 别名]
def test_basic_right_index(self):
expected = self.asof
trades = self.trades
quotes = self.quotes.set_index('time')
result = merge_asof(trades, quotes,
left_on='time',
right_index=True,
by='ticker')
assert_frame_equal(result, expected)