导致RuntimeWarning的Pandas查询:在log10中遇到零除

时间:2016-07-28 18:51:36

标签: python python-3.x pandas dataframe

我看到了类似的帖子(RuntimeWarning: divide by zero encountered in log10 in pandas align.py, prob from `query`-- cause/solution?),但没有示例代码,后续或回答。这是query的已知错误吗?

使用pd.query()时,我得到的RuntimeWarning对我来说没有意义。我尝试了几种不同的方法,并尝试简化数据,以确保它不是我,但无论我做什么,我都会不断收到警告。是否有人能够在不收到警告的情况下运行以下代码?这是一个错误吗?

import pandas as pd
import numpy as np
df = pd.DataFrame.from_items([('A', [1, 2, 3, 4, 5]), 
                              ('B', [14, 15, 16, 17, 18]), 
                              ('C', list(np.random.randn(5)))])
df.set_index(['A', 'B'], inplace = True)
df.query('15 <= B <= 17')

C:\Anaconda3\lib\site-packages\pandas\computation\align.py:98: 
RuntimeWarning: divide by zero encountered in log10
ordm = np.log10(abs(reindexer_size - term_axis_size))
Out[1]: 
             C
A B 
2 15 -0.852411
3 16 -0.665470
4 17  0.132162

我想也许是因为我正在使用15 <= B <= 17所以我试图将其分解成碎片,但得到了相同的RuntimeWarning。

In [2]: df.query('B <= 17')
C:\Anaconda3\lib\site-packages\pandas\computation\align.py:98: 
RuntimeWarning: divide by zero encountered in log10
ordm = np.log10(abs(reindexer_size - term_axis_size))
Out[2]: 
             C
A B
1 14  0.380167
2 15 -0.852411
3 16 -0.665470
4 17  0.132162

In [3]: df.query('B >= 15')
C:\Anaconda3\lib\site-packages\pandas\computation\align.py:98: 
RuntimeWarning: divide by zero encountered in log10
ordm = np.log10(abs(reindexer_size - term_axis_size))
Out[3]: 
             C
A B
2 15 -0.852411
3 16 -0.665470
4 17  0.132162
5 18  0.697867
  • Windows 7 64位
  • Spyder 3.0.0.dev0
  • Python 3.5.2
  • Pandas 0.18.1
  • Numpy 1.11.1

1 个答案:

答案 0 :(得分:0)

有一个bug,它仍处于打开状态。

它适合我:

In [2]: df
Out[2]:
             C
A B
1 14  0.159137
2 15  2.275652
3 16  0.798102
4 17  0.774097
5 18  2.361938

In [3]: df.query('15 <= B <= 17')
Out[3]:
             C
A B
2 15  2.275652
3 16  0.798102
4 17  0.774097

In [4]: df.query('B <= 17')
Out[4]:
             C
A B
1 14  0.159137
2 15  2.275652
3 16  0.798102
4 17  0.774097