语境:
我每天都使用 Pandas(处理测量数据),我想了解更多关于 Python 的信息。
为此,我正在开发一个(包装器)类——MyDataFrame——它将 Pandas DataFrame 功能与Pint的功能相结合——一个用于定义、操作和操纵物理量的 Python 包。
我已经设法通过__str__
,__get/setitem__
和__truediv__
MyDataFrame 的底层 MySeries (熊猫系列的包装器)获得了一些基本功能:
API 示例:
import pint
_u = pint.UnitRegistry()
_u("meter")
>>> 1 meter
type(_u("meter"))
>>> pint.unit.build_quantity_class.<locals>.Quantity
data = [[0,1,2,3],[4,5,6,7]]
df = pd.DataFrame(data,columns=["time","distance"])
units = {"distance":_u("meter"), "time":_u("second")}
mdf = MyDataFrame(df, units)
mdf["speed"] = mdf["distance"]/mdf["time"]
mdf["speed"].unit == _u("meter per second")
>>> True
到目前为止,我将实现保持在最低限度,例如:
class MyDataFrame:
"""df: pandas DataFrame, units: dict of str - Pint quantity key-value pairs."""
def __init__(self,df,units):
error_handling(df,units)
self.df = df
self.units = units
def __getitem__(self,key):
if key in units.keys():
return MySeries(self.df[key],self.units[key])
class MySeries:
"""series: pandas Series, units: a Pint quantity value."""
def __init__(self,series,unit):
self.series = series
self.unit = unit
def __truediv__(self,other):
return MySeries(self.series/other.series,self.unit/other.unit)
问题:
但是现在我想扩展这个基本概念,以便我们可以做例如
mdf["speed"] * 60*_u(second)
换句话说,使 MySeries 具有__mul__()
多态性 --- 不仅将 MySeries 与 MySeries 相乘,而且将 MySeries 与 Pint Quantities 相乘(反之亦然)。什么可能是一个好方法?
我的第一个想法是__mul__(self,other)
检查self
or的类型other
。然而,阅读更多关于 Python 中的多态性(这里)让我想知道其他人将如何实现这种多态二进制操作。
让我知道我是否应该澄清一下。
PS:顺便说一句。我注意到在尝试模仿 Pandas 语法时,我正在编写包装器,例如
def __getitem__(self,key):
return self.series[key]
def notnull(self):
return self.series.notnull()
关于将所有常用的 Pandas 方法调用重定向到 MyDataFrame / MySeries 类的 Pandas 部分的任何建议?
顺便说一句,我知道是时候深入研究 Python 的文档了……