Pandas DataFrame子类,强制执行结构并可以自组织。
typeddfs的Python项目详细描述
类型化数据帧
Pandas DataFrame子类,强制执行结构并可以自组织。 因为您的函数不能完全接受any数据帧。在
子类数据帧可以具有必需的和/或可选的列和索引,
并支持定制需求。
列会自动转换为索引,
这意味着^{str1}$read_csv
和{MyDf.read_csv(mydf.to_csv())
只是mydf
。在
数据帧会很好地显示在Jupyter笔记本上,
并添加了一些方便的方法,如sort_natural
和{
CSV的简单示例如下:
key | value | note |
---|---|---|
abc | 123 | ? |
fromtypeddfsimportTypedDfs# Build me a Key-Value-Note class!KeyValue=(TypedDfs.typed('KeyValue')# typed means enforced requirements.require('key',index=True)# automagically make this an index.require('value')# required.reserve('note')# permitted but not required.strict()# don't allow other columns).build()# This will self-organize and use 'key' as the index:df=KeyValue.read_csv('example.csv')# For fun, let's write it and read it back:df.to_csv('remke.csv')df=KeyValue('remake.csv')print(df.index_names(),df.column_names())# ['key'], ['value', 'note']# And now, we can type a function to require a KeyValue,# and let it raise an `InvalidDfError` (here, a `MissingColumnError`):defmy_special_function(df:KeyValue)->float:returnKeyValue(df)['value'].sum()
所有正常的数据帧方法都可用。
使用.untyped()
或.vanilla()
生成不强制要求的脱类型副本。在
欢迎使用New issues和拉取请求。 请参考contributing guide。 用Tyrannosaurus生成。在
- 项目
标签: