如何将inf修改为索引 [英] How to modify inf into an index

查看:296
本文介绍了如何将inf修改为索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触Python行业,因此我要求社区提供一些额外的支持.我有一个以IntervalIndex的形式具有.index.column的熊猫DataFrame.从下面的输出中可以看到,tuple(?)的第一个.left元素和最后一个.right元素是inf:

I am very new to the Python business, therefore I ask to the community for some extra support. I have a pandas DataFrame that has .index and .column in the form of IntervalIndex. As you can see from the following output, the first .left element and the last .right element of the tuple(?) are inf:

IntervalIndex([[-inf, -1.0), [-1.0, -0.9), [-0.9, -0.8), [-0.8, -0.7), [-0.7, -0.6) ... [0.6000000000000001, 0.7000000000000002), [0.7000000000000002, 0.8), [0.8, 0.9000000000000001), [0.9000000000000001, 1.0), [1.0, inf)],
              closed='left',
              name='cos(theta)',
              dtype='interval[float64]')

我正在尝试使用plt.pcolormesh(x,y,C)绘制2D历史记录,这是因为:

I am trying to plot a 2D histo with plt.pcolormesh(x,y,C) , which complains because:

ValueError: x and y arguments to pcolormesh cannot have non-finite values or be of type numpy.ma.core.MaskedArray with masked values

我需要pcolormesh,因为我的数据由值(计数)的C矩阵,x索引(cosθ)和y列(能量)组成. C的尺寸应与x和y的长度匹配,因此我不能简单地用-c的pivot_pipanda.index.left[1:]切边.

I need pcolormesh because my data consists of a C matrix of values (counts), an x index (cos(theta)) and a y column (energy). The dimensions of C should match the length of x and y, therefore I can't simply slice the edges with something like pivot_pipanda.index.left[1:] for -inf.

我想用其元组的.right值的值(即[-inf, -1.0))替换-inf-之后的bin的中心值(所有bin具有相同的宽度),并且镜面反射.我曾考虑过像.apply().raplace()这样的幼稚方法,但它们不是.index.left.index.right的可用方法.我可以使用语法pivot_df.index.left.isin(["-inf"])

I want to replace -inf with the value of the .right value of its tuple (i.e. [-inf, -1.0) ) - the centre value of the bin after (all bins have the same width) and the specular for inf. I have thought about naive approaches like .apply() or .raplace(), but they aren't available methods for .index.left or .index.right . I can get a mask of booleans with the syntax pivot_df.index.left.isin(["-inf"])

 array([ True, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False])

但是我无法想象有一个函数可以完成工作.

but I can't imagine a function that does the job.

任何想法都非常受欢迎!

Any idea is very welcome!

推荐答案

解决方法如Jim my2Dhisto.numpy()所建议.

The workaround is as suggested by Jim my2Dhisto.numpy() .

有关更多详细信息,请在这里看看

这篇关于如何将inf修改为索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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