ValueError:无法从重复的轴 pandas 重新索引 [英] ValueError: cannot reindex from a duplicate axis Pandas

查看:77
本文介绍了ValueError:无法从重复的轴 pandas 重新索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个基于fund_id生成的时间序列数组:

So I have a an array of timeseries` that are generated based on a fund_id:

def get_adj_nav(self, fund_id):
    df_nav = read_frame(
        super(__class__, self).filter(fund__id=fund_id, nav__gt=0).exclude(fund__account_class=0).order_by(
            'valuation_period_end_date'), coerce_float=True,
        fieldnames=['income_payable', 'valuation_period_end_date', 'nav', 'outstanding_shares_par'],
        index_col='valuation_period_end_date')
    df_dvd, skip = self.get_dvd(fund_id=fund_id)
    df_nav_adj = calculate_adjusted_prices(
        df_nav.join(df_dvd).fillna(0).rename_axis({'payout_per_share': 'dividend'}, axis=1), column='nav')
return df_nav_adj

def json_total_return_table(request, fund_account_id):
ts_list = []
for fund_id in Fund.objects.get_fund_series(fund_account_id=fund_account_id):
    if NAV.objects.filter(fund__id=fund_id, income_payable__lt=0).exists():
        ts = NAV.objects.get_adj_nav(fund_id)['adj_nav']
        ts.name = Fund.objects.get(id=fund_id).account_class_description
        ts_list.append(ts.copy())
        print(ts)
    df_adj_nav = pd.concat(ts_list, axis=1) # ====> Throws error
    cols_to_datetime(df_adj_nav, 'index')
    df_adj_nav = ffn.core.calc_stats(df_adj_nav.dropna()).to_csv(sep=',')

因此,有关时间序列的示例如下:

So an example of how the time series look like is this:

valuation_period_end_date
2013-09-03    17.234000
2013-09-04    17.277000
2013-09-05    17.363000
2013-09-06    17.326900
2013-09-09    17.400800
2013-09-10    17.473000
2013-09-11    17.486800
2013-09-12    17.371600
....
Name: CLASS I, Length: 984, dtype: float64

另一个时间序列:

valuation_period_end_date
2013-09-03    17.564700
2013-09-04    17.608500
2013-09-05    17.696100
2013-09-06    17.659300
2013-09-09    17.734700
2013-09-10    17.808300
2013-09-11    17.823100
2013-09-12    17.704900
....
Name: CLASS F, Length: 984, dtype: float64

对于每个时间序列,长度是不同的,我想知道这是否是导致我得到错误的原因:cannot reindex from a duplicate axis.我是熊猫新手,所以我想知道你们是否有任何建议.

For each timeseries the Lengths are different and I am wondering if that is the reason for the error I am getting: cannot reindex from a duplicate axis. I am new to pandas so I was wondering if you guys have any advice.

谢谢

而且索引也不应该唯一.

Also the indexes aren't supposed to be unique.

推荐答案

也许这种方法可行.我已将fund_id添加到数据框中,并将其重新索引到valuation_period_end_datefund_id.

Perhaps something like this would work. I've added the fund_id to the dataframe and reindexed it to the valuation_period_end_date and fund_id.

# Only fourth line above error.
ts = (
    NAV.objects.get_adj_nav(fund_id['adj_nav']
    .to_frame()
    .assign(fund_id=fund)
    .reset_index()
    .set_index(['valuation_period_end_date', 'fund_id']))

然后与axis=0堆叠,将日期和fund_id分组(假设每个日期和fund_id只有一个唯一值,您可以采用第一个值),然后将unfund_id堆叠以将其作为列进行旋转:

And then stack with axis=0, group on the date and fund_id (assuming there is only one unique value per date and fund_id, you can take the first value), then unstack fund_id to pivot it as columns:

df_adj_nav = (
    pd.concat(ts_list, axis=0)
    .groupby(['valuation_period_end_date', 'fund_id'])
    .first()
    .to_frame()
    .unstack('fund_id'))

这篇关于ValueError:无法从重复的轴 pandas 重新索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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