当前位置: 首页>>代码示例>>Python>>正文


Python pandas.TimeGrouper方法代码示例

本文整理汇总了Python中pandas.TimeGrouper方法的典型用法代码示例。如果您正苦于以下问题:Python pandas.TimeGrouper方法的具体用法?Python pandas.TimeGrouper怎么用?Python pandas.TimeGrouper使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pandas的用法示例。


在下文中一共展示了pandas.TimeGrouper方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_apply_iteration

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_apply_iteration(self):
        # #2300
        N = 1000
        ind = pd.date_range(start="2000-01-01", freq="D", periods=N)
        df = DataFrame({'open': 1, 'close': 2}, index=ind)
        tg = TimeGrouper('M')

        _, grouper, _ = tg._get_grouper(df)

        # Errors
        grouped = df.groupby(grouper, group_keys=False)
        f = lambda df: df['close'] / df['open']

        # it works!
        result = grouped.apply(f)
        tm.assert_index_equal(result.index, df.index) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:18,代码来源:test_resample.py

示例2: test_timegrouper_apply_return_type_series

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_timegrouper_apply_return_type_series(self):
        # Using `apply` with the `TimeGrouper` should give the
        # same return type as an `apply` with a `Grouper`.
        # Issue #11742
        df = pd.DataFrame({'date': ['10/10/2000', '11/10/2000'],
                           'value': [10, 13]})
        df_dt = df.copy()
        df_dt['date'] = pd.to_datetime(df_dt['date'])

        def sumfunc_series(x):
            return pd.Series([x['value'].sum()], ('sum',))

        expected = df.groupby(pd.Grouper(key='date')).apply(sumfunc_series)
        result = (df_dt.groupby(pd.Grouper(freq='M', key='date'))
                  .apply(sumfunc_series))
        assert_frame_equal(result.reset_index(drop=True),
                           expected.reset_index(drop=True)) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:19,代码来源:test_timegrouper.py

示例3: test_timegrouper_apply_return_type_value

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_timegrouper_apply_return_type_value(self):
        # Using `apply` with the `TimeGrouper` should give the
        # same return type as an `apply` with a `Grouper`.
        # Issue #11742
        df = pd.DataFrame({'date': ['10/10/2000', '11/10/2000'],
                           'value': [10, 13]})
        df_dt = df.copy()
        df_dt['date'] = pd.to_datetime(df_dt['date'])

        def sumfunc_value(x):
            return x.value.sum()

        expected = df.groupby(pd.Grouper(key='date')).apply(sumfunc_value)
        with tm.assert_produces_warning(FutureWarning,
                                        check_stacklevel=False):
            result = (df_dt.groupby(pd.TimeGrouper(freq='M', key='date'))
                      .apply(sumfunc_value))
        assert_series_equal(result.reset_index(drop=True),
                            expected.reset_index(drop=True)) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:21,代码来源:test_timegrouper.py

示例4: test_apply

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_apply():
    with tm.assert_produces_warning(FutureWarning,
                                    check_stacklevel=False):
        grouper = pd.TimeGrouper(freq='A', label='right', closed='right')

    grouped = test_series.groupby(grouper)

    def f(x):
        return x.sort_values()[-3:]

    applied = grouped.apply(f)
    expected = test_series.groupby(lambda x: x.year).apply(f)

    applied.index = applied.index.droplevel(0)
    expected.index = expected.index.droplevel(0)
    assert_series_equal(applied, expected) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:18,代码来源:test_time_grouper.py

示例5: test_apply_iteration

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_apply_iteration():
    # #2300
    N = 1000
    ind = pd.date_range(start="2000-01-01", freq="D", periods=N)
    df = DataFrame({'open': 1, 'close': 2}, index=ind)
    tg = TimeGrouper('M')

    _, grouper, _ = tg._get_grouper(df)

    # Errors
    grouped = df.groupby(grouper, group_keys=False)

    def f(df):
        return df['close'] / df['open']

    # it works!
    result = grouped.apply(f)
    tm.assert_index_equal(result.index, df.index) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:20,代码来源:test_time_grouper.py

示例6: test_panel_aggregation

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_panel_aggregation():
    ind = pd.date_range('1/1/2000', periods=100)
    data = np.random.randn(2, len(ind), 4)

    wp = Panel(data, items=['Item1', 'Item2'], major_axis=ind,
               minor_axis=['A', 'B', 'C', 'D'])

    tg = TimeGrouper('M', axis=1)
    _, grouper, _ = tg._get_grouper(wp)
    bingrouped = wp.groupby(grouper)
    binagg = bingrouped.mean()

    def f(x):
        assert (isinstance(x, Panel))
        return x.mean(1)

    result = bingrouped.agg(f)
    tm.assert_panel_equal(result, binagg) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:20,代码来源:test_time_grouper.py

示例7: test_aaa_group_order

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_aaa_group_order():
    # GH 12840
    # check TimeGrouper perform stable sorts
    n = 20
    data = np.random.randn(n, 4)
    df = DataFrame(data, columns=['A', 'B', 'C', 'D'])
    df['key'] = [datetime(2013, 1, 1), datetime(2013, 1, 2),
                 datetime(2013, 1, 3), datetime(2013, 1, 4),
                 datetime(2013, 1, 5)] * 4
    grouped = df.groupby(TimeGrouper(key='key', freq='D'))

    tm.assert_frame_equal(grouped.get_group(datetime(2013, 1, 1)),
                          df[::5])
    tm.assert_frame_equal(grouped.get_group(datetime(2013, 1, 2)),
                          df[1::5])
    tm.assert_frame_equal(grouped.get_group(datetime(2013, 1, 3)),
                          df[2::5])
    tm.assert_frame_equal(grouped.get_group(datetime(2013, 1, 4)),
                          df[3::5])
    tm.assert_frame_equal(grouped.get_group(datetime(2013, 1, 5)),
                          df[4::5]) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:23,代码来源:test_time_grouper.py

示例8: test_aggregate_with_nat_size

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_aggregate_with_nat_size():
    # GH 9925
    n = 20
    data = np.random.randn(n, 4).astype('int64')
    normal_df = DataFrame(data, columns=['A', 'B', 'C', 'D'])
    normal_df['key'] = [1, 2, np.nan, 4, 5] * 4

    dt_df = DataFrame(data, columns=['A', 'B', 'C', 'D'])
    dt_df['key'] = [datetime(2013, 1, 1), datetime(2013, 1, 2), pd.NaT,
                    datetime(2013, 1, 4), datetime(2013, 1, 5)] * 4

    normal_grouped = normal_df.groupby('key')
    dt_grouped = dt_df.groupby(TimeGrouper(key='key', freq='D'))

    normal_result = normal_grouped.size()
    dt_result = dt_grouped.size()

    pad = Series([0], index=[3])
    expected = normal_result.append(pad)
    expected = expected.sort_index()
    expected.index = date_range(start='2013-01-01', freq='D',
                                periods=5, name='key')
    assert_series_equal(expected, dt_result)
    assert dt_result.index.name == 'key' 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:26,代码来源:test_time_grouper.py

示例9: test_resample_frame_basic

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_resample_frame_basic(self):
        df = tm.makeTimeDataFrame()

        b = TimeGrouper('M')
        g = df.groupby(b)

        # check all cython functions work
        funcs = ['add', 'mean', 'prod', 'min', 'max', 'var']
        for f in funcs:
            g._cython_agg_general(f)

        result = df.resample('A').mean()
        assert_series_equal(result['A'], df['A'].resample('A').mean())

        result = df.resample('M').mean()
        assert_series_equal(result['A'], df['A'].resample('M').mean())

        df.resample('M', kind='period').mean()
        df.resample('W-WED', kind='period').mean() 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:21,代码来源:test_resample.py

示例10: test_resample_ohlc

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_resample_ohlc(self):
        s = self.series

        grouper = TimeGrouper(Minute(5))
        expect = s.groupby(grouper).agg(lambda x: x[-1])
        result = s.resample('5Min').ohlc()

        assert len(result) == len(expect)
        assert len(result.columns) == 4

        xs = result.iloc[-2]
        assert xs['open'] == s[-6]
        assert xs['high'] == s[-6:-1].max()
        assert xs['low'] == s[-6:-1].min()
        assert xs['close'] == s[-2]

        xs = result.iloc[0]
        assert xs['open'] == s[0]
        assert xs['high'] == s[:5].max()
        assert xs['low'] == s[:5].min()
        assert xs['close'] == s[4] 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:23,代码来源:test_resample.py

示例11: test_panel_aggregation

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_panel_aggregation(self):
        ind = pd.date_range('1/1/2000', periods=100)
        data = np.random.randn(2, len(ind), 4)

        with catch_warnings(record=True):
            wp = Panel(data, items=['Item1', 'Item2'], major_axis=ind,
                       minor_axis=['A', 'B', 'C', 'D'])

            tg = TimeGrouper('M', axis=1)
            _, grouper, _ = tg._get_grouper(wp)
            bingrouped = wp.groupby(grouper)
            binagg = bingrouped.mean()

            def f(x):
                assert (isinstance(x, Panel))
                return x.mean(1)

            result = bingrouped.agg(f)
            tm.assert_panel_equal(result, binagg) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:21,代码来源:test_resample.py

示例12: test_aaa_group_order

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_aaa_group_order(self):
        # GH 12840
        # check TimeGrouper perform stable sorts
        n = 20
        data = np.random.randn(n, 4)
        df = DataFrame(data, columns=['A', 'B', 'C', 'D'])
        df['key'] = [datetime(2013, 1, 1), datetime(2013, 1, 2),
                     datetime(2013, 1, 3), datetime(2013, 1, 4),
                     datetime(2013, 1, 5)] * 4
        grouped = df.groupby(TimeGrouper(key='key', freq='D'))

        tm.assert_frame_equal(grouped.get_group(datetime(2013, 1, 1)),
                              df[::5])
        tm.assert_frame_equal(grouped.get_group(datetime(2013, 1, 2)),
                              df[1::5])
        tm.assert_frame_equal(grouped.get_group(datetime(2013, 1, 3)),
                              df[2::5])
        tm.assert_frame_equal(grouped.get_group(datetime(2013, 1, 4)),
                              df[3::5])
        tm.assert_frame_equal(grouped.get_group(datetime(2013, 1, 5)),
                              df[4::5]) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:23,代码来源:test_resample.py

示例13: test_aggregate_with_nat_size

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_aggregate_with_nat_size(self):
        # GH 9925
        n = 20
        data = np.random.randn(n, 4).astype('int64')
        normal_df = DataFrame(data, columns=['A', 'B', 'C', 'D'])
        normal_df['key'] = [1, 2, np.nan, 4, 5] * 4

        dt_df = DataFrame(data, columns=['A', 'B', 'C', 'D'])
        dt_df['key'] = [datetime(2013, 1, 1), datetime(2013, 1, 2), pd.NaT,
                        datetime(2013, 1, 4), datetime(2013, 1, 5)] * 4

        normal_grouped = normal_df.groupby('key')
        dt_grouped = dt_df.groupby(TimeGrouper(key='key', freq='D'))

        normal_result = normal_grouped.size()
        dt_result = dt_grouped.size()

        pad = Series([0], index=[3])
        expected = normal_result.append(pad)
        expected = expected.sort_index()
        expected.index = date_range(start='2013-01-01', freq='D',
                                    periods=5, name='key')
        assert_series_equal(expected, dt_result)
        assert dt_result.index.name == 'key' 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:26,代码来源:test_resample.py

示例14: compute

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def compute(self, txns):
        ltd = txns.pl.ltd_txn
        grouper = pd.TimeGrouper(self.reset_freq)
        period_rets = pd.Series(np.nan, index=ltd.index)
        aum = self.aum
        at = 0
        cf = OrderedDict()
        for key, grp in ltd.groupby(grouper):
            if grp.empty:
                continue
            eod = aum + grp
            sod = eod.shift(1)
            sod.iloc[0] = aum
            period_rets.iloc[at:at + len(grp.index)] = eod / sod - 1.
            at += len(grp.index)
            # get aum back to fixed amount
            cf[key] = eod.iloc[-1] - aum
        self.external_cash_flows = pd.Series(cf)
        crets = CumulativeRets(period_rets)
        return Performance(crets) 
开发者ID:bpsmith,项目名称:tia,代码行数:22,代码来源:ret.py

示例15: test_resample_basic

# 需要导入模块: import pandas [as 别名]
# 或者: from pandas import TimeGrouper [as 别名]
def test_resample_basic(self):
        rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min',
                         name='index')
        s = Series(np.random.randn(14), index=rng)
        result = s.resample('5min', closed='right', label='right').mean()

        exp_idx = date_range('1/1/2000', periods=4, freq='5min', name='index')
        expected = Series([s[0], s[1:6].mean(), s[6:11].mean(), s[11:].mean()],
                          index=exp_idx)
        assert_series_equal(result, expected)
        assert result.index.name == 'index'

        result = s.resample('5min', closed='left', label='right').mean()

        exp_idx = date_range('1/1/2000 00:05', periods=3, freq='5min',
                             name='index')
        expected = Series([s[:5].mean(), s[5:10].mean(),
                           s[10:].mean()], index=exp_idx)
        assert_series_equal(result, expected)

        s = self.series
        result = s.resample('5Min').last()
        grouper = TimeGrouper(Minute(5), closed='left', label='left')
        expect = s.groupby(grouper).agg(lambda x: x[-1])
        assert_series_equal(result, expect) 
开发者ID:securityclippy,项目名称:elasticintel,代码行数:27,代码来源:test_resample.py


注:本文中的pandas.TimeGrouper方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。