Resolving Reindexing only valid with uniquely valued Index objects

只谈情不闲聊 提交于 2021-02-09 15:14:20

问题


I have viewed many of the questions that come up with this error. I am running pandas '0.10.1'

df = DataFrame({'A' : np.random.randn(5),
 'B' : np.random.randn(5),'C' : np.random.randn(5), 
  'D':['a','b','c','d','e'] })

#gives error
df.take([2,0,1,2,3], axis=1).drop(['C'],axis=1)

#works fine
df.take([2,0,1,2,1], axis=1).drop(['C'],axis=1)

Only thing I can see is that in the former case I have the non-numeric column, which seems to be affecting the index somehow but the below command returns empty:

df.take([2,0,1,2,3], axis=1).index.get_duplicates()

Reindexing error makes no sense does not seem to apply as my old index is unique.

My index appears unique as far as I can tell using this command df.take([2,0,1,2,3], axis=1).index.get_duplicates() from this Q&A: problems with reindexing dataframes: Reindexing only valid with uniquely valued Index objects

"Reindexing only valid with uniquely valued Index objects" does not seem to apply

I think my pandas version# is ok so this should bug should not be the problem pandas Reindexing only valid with uniquely valued Index objects


回答1:


Firstly, I believe you meant to test for duplicates using the following command:

df.take([2,0,1,2,3],axis=1).columns.get_duplicates()

because if you used index instead of columns, then it would obviously returned an empty array because the random float values don't repeat. The above command returns, as expected:

['C']

Secondly, I think you're right, the non-numeric column is throwing it off, because even if you use the following, there is still an error:

df = DataFrame({'A' : np.random.randn(5), 'B' : np.random.randn(5),'C' :np.random.randn(5), 'D':[str(x) for x in np.random.randn(5) ]})

It could be a bug, because if you check out the core file called 'index.py', on line 86, and line 1228, the type it is expecting is either (respectively):

_engine_type = _index.ObjectEngine


_engine_type = _index.Int64Engine

and neither of those seem to be expecting a string, if you look deeper into the documentation. That's the best I got, good luck!! Let me know if you solve this as I'm interested too.



来源:https://stackoverflow.com/questions/21533706/resolving-reindexing-only-valid-with-uniquely-valued-index-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!