Python ValueError:将新列指定给DataFrame时,无法从重复轴重新索引
我想知道我的datetime索引中有多少小时在两个不同的小时之间 这是我的代码:Python ValueError:将新列指定给DataFrame时,无法从重复轴重新索引,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我想知道我的datetime索引中有多少小时在两个不同的小时之间 这是我的代码: rbe60['result'] = rbe60.index.hour.to_series().between(3,23) 唯一的问题是我一直在犯这个错误 raise ValueError("cannot reindex from a duplicate axis") ValueError: cannot reindex from a duplicate axis 我看过其他一些帖子,意识到这意味着我的索
rbe60['result'] = rbe60.index.hour.to_series().between(3,23)
唯一的问题是我一直在犯这个错误
raise ValueError("cannot reindex from a duplicate axis")
ValueError: cannot reindex from a duplicate axis
我看过其他一些帖子,意识到这意味着我的索引或列中可能有重复的值。我试着跑去看看复制品会在哪里,但结果都是空的
dup = rbe60.index.get_duplicates() and
dup = rbe60.columns.get_duplicates()
还有什么我应该试试的吗
关于我正在尝试做的事情:
这是我的数据,我只是想给np.logical语句添加一个条件,检查我的数据帧索引的小时数是否在3到23之间
Open H L C O
DateTime
2013-12-30 14:30:00 -0.0756 -0.0729 -0.0756 -0.0737 2.8847
2013-12-30 15:30:00 -0.0735 -0.072 -0.0737 -0.0722 2.8870
2013-12-30 16:30:00 -0.0722 -0.0721 -0.0728 -0.0722 2.8930
2013-12-30 18:00:00 -0.0728 -0.0728 -0.0728 -0.0728 2.8826
2013-12-30 19:00:00 -0.0721 -0.0721 -0.0721 -0.0721 2.8872
错误的原因是索引对齐。数据帧由
datetime
s索引组成。现有代码返回的内容如下所示:
print(rbe60.index.hour.to_series().between(3,23))
DateTime
14 True
15 True
16 True
18 True
19 True
Name: DateTime, dtype: bool
请注意,索引值与原始索引值不匹配。这会让熊猫在作业中失去兴趣。解决方案是分配一个与索引完全不关联的数组
print(rbe60.index.hour.to_series().between(3,23).values)
array([ True, True, True, True, True])
瞧
请至少提供一些数据。或者,您所需要做的只是绕过索引allignment:
rbe60['result']=rbe60.index.hour.to_series()。介于(3,23)之间。值
@coldspeed进行了编辑并提供了数据。看起来使用.values就可以做到这一点。对于我自己的启发,有什么想法,为什么我在检查了两个轴上的重复项之后,首先会出现这种错误?希望答案能澄清它。确实如此。很好,谢谢。
rbe60['result'] = rbe60.index.hour.to_series().between(3,23).values