覆盖模块方法以扩展功能,然后从另一个文件导入更新的模块 [英] Override module method to extend functionality then import the updated module from another file

查看:70
本文介绍了覆盖模块方法以扩展功能,然后从另一个文件导入更新的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不触摸pandas包的情况下向pandas添加自定义功能.我尝试遵循:

I want to add to pandas a custom function without touching the pandas package. I tried to following:

extra_pandas.py:

from pandas import *
class DataFrame2(pandas.core.frame.DataFrame):
    def new_function(self):
        print("I exist")
pandas.core.frame.DataFrame = DataFrame2

my_script.py:

import extra_pandas as pd
df = pd.read_csv('example.csv')
print(df.new_function())

似乎不起作用,我无法弄清楚出什么问题了.我收到以下错误:

It appears not to work and I can't figure out what is wrong. I get the following error:

AttributeError:"DataFrame"对象没有属性"new_function"

AttributeError: 'DataFrame' object has no attribute 'new_function'

我想念什么?

非常感谢

更新: 我尝试了Alternative解决方案,并希望使用以下代码片段在循环中修补所有pandas函数:

Update: I gave the Alternative solution a try and wanted to patch all pandas function in a loop using this snippet:

patch_function = [read_csv, read_json, read_html, read_clipboard, read_excel,
                  read_hdf, read_feather, read_parquet, read_msgpack,
                  read_stata, read_sas, read_pickle, read_sql, read_gbq]

for func in patch_function:
    orig_func = func

    def patch(*args, **kwargs):
        return DataFrame(orig_func(*args, **kwargs))

    func = patch

但是这不起作用.知道为什么吗?

But this is not working. Any idea why?

谢谢

推荐答案

解决方案1 ​​

您不能打补丁,但可以先更换:

Solution 1

You cannot patch but you cen replace:

extra_pandas.py:

from pandas import *

class DataFrame2(DataFrame):
    def new_function(self):
        print("I exist")

DataFrame = DataFrame2

my_script.py:

import extra_pandas as pd

df = pd.DataFrame(pd.read_csv('furniture.csv'))
print(df.new_function())

输出:

I exist

解决方案2

只需导入您自己的课程:

Solution 2

Just import you own class:

extra_pandas.py:

import pandas as pd

class DataFrame2(pd.DataFrame):
    def new_function(self):
        print("I exist")

my_script.py:

import pandas as pd
from extra_pandas import DataFrame2

df = DataFrame2(pd.read_csv('example.csv'))
print(df.new_function())

输出:

I exist

DataFrame将另一个数据框作为输入来制作新的数据框.

DataFrame takes another dataframe as input for making a new dataframe.

您尝试猴子修补DataFrame类.这是行不通的.这很可能是由于事实,该事实大部分是用Cython编写的,因此被编译为C扩展名.这会干扰您尝试修补猴子的行为.

You try to monkey patch the DataFrame class. This does not work. This is likely due to the fact that is largely written in Cython, hence compiled to a C extension. This interferes with your attempt to monkey patch.

或猴子补丁read_csv().

extra_pandas.py:

import pandas as pd

class DataFrame2(pd.DataFrame):
    def new_function(self):
        print("I exist")


orig_read_csv=pd.read_csv

def my_read_csv(*args, **kwargs):
    return DataFrame2(orig_read_csv(*args, **kwargs))

pd.read_csv = my_read_csv

my_script.py:

import pandas as pd

import extra_pandas

df = pd.read_csv('furniture.csv')
print(df.new_function())

输出:

I exist

这篇关于覆盖模块方法以扩展功能,然后从另一个文件导入更新的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆