當前位置: 首頁>>代碼示例>>Python>>正文


Python pandas.merge_asof方法代碼示例

本文整理匯總了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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:18,代碼來源:test_merge_asof.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:20,代碼來源:test_merge_asof.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:24,代碼來源:test_merge_asof.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:21,代碼來源:test_merge_asof.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:20,代碼來源:test_merge_asof.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:20,代碼來源:test_merge_asof.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:25,代碼來源:test_merge_asof.py

示例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) 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:24,代碼來源:test_merge_asof.py

示例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"]] 
開發者ID:refraction-ray,項目名稱:xalpha,代碼行數:29,代碼來源:indicator.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:16,代碼來源:test_merge_asof.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:17,代碼來源:test_merge_asof.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:17,代碼來源:test_merge_asof.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:12,代碼來源:test_merge_asof.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:15,代碼來源:test_merge_asof.py

示例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) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:13,代碼來源:test_merge_asof.py


注:本文中的pandas.merge_asof方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。