使用假设库创建多索引pd.DataFrame

问题描述

我需要创建一个具有多索引的pd.DataFrame。第一索引级别是1...n的简单范围。第二层是日期时间索引。所有列均包含floats。这是我的n=2示例。

from datetime import date

import pandas as pd
from hypothesis import given
from hypothesis import strategies as st
from hypothesis.extra.pandas import columns,data_frames,indexes


@given(
    df1=data_frames(
        columns=columns(
            ["asset1","asset2","asset3","cash_asset"],elements=st.floats(allow_nan=False,allow_infinity=False),),index=indexes(
            elements=st.dates(
                date.fromisoformat("2000-01-01"),date.fromisoformat("2020-12-31")
            ),min_size=10,unique=True,).map(sorted),df2=data_frames(
        columns=columns(
            ["asset1",)
def test_index_level(df1,df2):
    df = pd.concat([df1,df2],keys=["df1","df2"])

    assert df.index.nlevels == 2

我想知道如何使用hypothesis库直接创建多索引?很明显,我无法像玩具示例中那样手动定义df1df2等。
另一个限制是,对于所有level 2出现,level 1索引必须相等。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)