小编Igo*_*eev的帖子

pandas:链接方法的组合,如.resample(),.rolling()等

我想构建一个扩展pandas.DataFrame- 让我们称之为SPDF- 它可以做到超出简单的DataFrame能力:

import pandas as pd
import numpy as np


def to_spdf(func):
    """Transform generic output of `func` to SPDF.

    Returns
    -------
    wrapper : callable
    """
    def wrapper(*args, **kwargs):
        res = func(*args, **kwargs)
        return SPDF(res)

    return wrapper


class SPDF:
    """Special-purpose dataframe.

    Parameters
    ----------
    df : pandas.DataFrame

    """

    def __init__(self, df):
        self.df = df

    def __repr__(self):
        return repr(self.df)

    def __getattr__(self, item):
        res = getattr(self.df, item)

        if callable(res):
            res = to_spdf(res)

        return res


if __name__ == …
Run Code Online (Sandbox Code Playgroud)

python chained object-composition pandas

6
推荐指数
1
解决办法
271
查看次数

属性获取器和上下文管理器

我想以这样的方式定义一个属性,无论何时调用它,它都是在上下文管理器中完成的。说吧,我从以下开始:

@property
def hangar(self):
    return self._hangar
Run Code Online (Sandbox Code Playgroud)

每当写下代码时,我都会写:

res = some_function(self.hangar)
Run Code Online (Sandbox Code Playgroud)

我希望它被评价为:

with pandas.HDFStore(...) as hangar:
    some_function(self.hangar)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

python properties decorator contextmanager pandas

5
推荐指数
1
解决办法
1862
查看次数