[英]Python - inherited class method to operate on class attribute
I'm sure there's probably a simple solution but anyway the problem is this: I've got two classes, say A
and B
, both of which have attributes that are dataframe like -- those attributes are instances of a dataframe class, call it C
, which has its own methods. 我确定可能有一个简单的解决方案,但是无论如何问题是这样的:我有两个类,例如A
和B
,它们都具有类似于dataframe的属性-这些属性是dataframe类的实例,称之为C
,它有自己的方法。 I'd like to define an 'interface like' class D
which has methods that can operate on those attributes (ie operate on the dataframes which are attributes of A
and B
). 我想定义一个“接口”类D
,它具有可以对那些属性进行操作的方法(即,对作为A
和B
属性的数据框进行操作)。
edit for clarity : in what follows below, let a
and b
be dataframes (ie instances) from class C
. 为了清楚起见 ,请进行编辑 :在下面的内容中,让a
和b
为类C
数据帧(即实例)。 So that the methods of C
are available to a
and b
. 这样C
的方法可用于a
和b
。
To be more explicit: Suppose a
is the dataframe like attribute of A
, with attributes Series1
,..., Seriesn
. 更明确地说:假设a
是属性为A
的数据Series1
,其属性为Series1
,..., Seriesn
。 Since a
is dataframe like, I can call a.Series1, a.Series2, ...
etc to access the contents of Series1
, Series2
in a
. 因为a
是数据框就好了,我可以叫a.Series1, a.Series2, ...
等来访问的内容Series1
, Series2
中的a
。 Of course a
is an attribute of A
so I'm actually calling AaSeries1
, AaSeries2
.. etc, and a
has its own methods from class C
so I can call AaSeries1.methodfromclassC()
no problem. 当然, a
是A
的属性,所以我实际上是在调用AaSeries1
, AaSeries2
等。并且a
具有来自类C
自己的方法,因此我可以AaSeries1.methodfromclassC()
调用AaSeries1.methodfromclassC()
。 Anyhow. 无论如何。 Now suppose I want to make a transformation in a consistent fashion to the contents of a.Seriesj
, or b.Seriesj
, implemented as a method in class D
, that both A
and B
can access. 现在假设我想以一致的方式对作为类D
的方法实现的a.Seriesj
或b.Seriesj
的内容进行a.Seriesj
, A
和B
都可以访问。 The idea being that I'd like to be able to call a member of class A
like so: AaSeriesj.transformseries()
. 我的想法是希望能够像这样调用A
类的成员: AaSeriesj.transformseries()
。 The problem I run into is that Seriesj
has its own methods (inherited from class C
) and transformseries()
is not one of them. 我遇到的问题是Seriesj
有自己的方法(从类C
继承),而transformseries()
不是其中之一。
This probably seems a bit convoluted but the idea being that eventually I can chain multiple calls to the various methods of D
changing the state of the dataframe attributes: AaSeries2.transform1().transform2().transformj()
or BbSeriesj.transform6().transform3()
etc, so that the final representation of Aa
and Bb
is in the form that I'd like. 这似乎有些令人费解,但最终的想法是,我可以将多个调用链接到D
的各种方法,以更改数据帧属性的状态: AaSeries2.transform1().transform2().transformj()
或BbSeriesj.transform6().transform3()
等,这样Aa
和Bb
的最终表示形式就是我想要的形式。
Have you considered "injecting" new methods to your Seriesn
? 您是否考虑过向Seriesn
“注入”新方法? In Python you can add methods to classes dynamically like this: 在Python中,您可以像这样动态地向类添加方法:
setattr(MyClass, 'new_method', lambda self: 'return value')
It will work even in objects instanced before. 它甚至可以在之前实例化的对象中工作。 So you could add custom methods to Pandas' Series
and/or Dataframe
classes. 因此,您可以向Pandas的Series
和/或Dataframe
类添加自定义方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.