代码之家  ›  专栏  ›  技术社区  ›  Igor Pozdeev

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

  •  6
  • Igor Pozdeev  · 技术社区  · 7 年前

    我想构造一个 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__ == "__main__":
    
        # construct a generic SPDF
        df = pd.DataFrame(np.eye(4))
        an_spdf = SPDF(df)
    
        # call .diff() to obtain another SPDF
        print(an_spdf.diff())
    

    现在,方法 数据帧 那又是一个 数据帧 ,例如 .diff() 在上图中,再给我一个 自卫队 太好了。不过,我还想玩链式方法,例如 .resample('M').last() .rolling(2).mean() 生成一个 自卫队 最后。到目前为止我失败了,因为 .rolling() 类似的类型 callable 和我的包装纸 to_spdf 尝试构造 自卫队 从他们的输出没有“等待” .mean() 或表达式的任何其他最后部分。有什么办法解决这个问题吗?

    谢谢。

    2 回复  |  直到 7 年前
        1
  •  4
  •   modesitt    7 年前

    dataframe copy-constructor _constructor

    class SPDF(DataFrame):
    
        @property
        def _constructor(self):
            return SPDF
    

    attributes functions diff

    class SPDF(DataFrame):
        _metadata = ['prop']
        prop = 1
    
        @property
        def _constructor(self):
            return SPDF
    

    df = SPDF(np.eye(4))
    print(type(df))
    [<class '__main__.SPDF'>]
    new = df.diff()
    print(type(new))
    [<class '__main__.SPDF'>]
    
        2
  •  0
  •   mhsekhavat    7 年前

    DataFrame PendingSPDF

    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)
            if isinstance(res, pd.DataFrame):
                return SPDF(res)
            else:
                return PendingSPDF(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
    
    class PendingSPDF:
        def __init__(self, df):
            self.df = df
    
        def __getattr__(self, item):
            res = getattr(self.df, item)
    
            if callable(res):
                res = to_spdf(res)
    
            return res
    
    if __name__ == "__main__":
    
        # construct a generic SPDF
        df = pd.DataFrame(np.eye(4))
        an_spdf = SPDF(df)
    
        # call .diff() to obtain another SPDF
        print(an_spdf.diff())
    
    推荐文章