【问题标题】:Suppress warnings when using a python chunk inside an Rmd file在 Rmd 文件中使用 python 块时抑制警告
【发布时间】:2020-01-19 13:07:04
【问题描述】:

我试图在编织 Rmd 文件时隐藏一些 python 警告。通常的块设置“warning=F, message=F”似乎不适用于 python 块。

带有 python 块的 Rmd 文件示例,该文件故意生成警告:

---
title: "**warnings test**"
output: pdf_document
---


```{python, echo=F, warning=F, message=F}
import pandas as pd
d = {'col1': [1, 1, 2, 2], 'col2': [0, 0, 1, 1]}
df = pd.DataFrame(data=d)
df[df.col1==1]['col2']=2
```

【问题讨论】:

  • 添加import warnings warnings.filterwarnings('ignore')
  • 是的。这行得通,谢谢!想回答这个问题,以便我可以给你功劳吗?

标签: python r r-markdown reticulate


【解决方案1】:

你可以将warnings.filterwarnings('ignore')添加到python块中:

```{python, echo=F, warning=F, message=F}
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
d = {'col1': [1, 1, 2, 2], 'col2': [0, 0, 1, 1]}
df = pd.DataFrame(data=d)
df[df.col1==1]['col2']=2
```

【讨论】:

    【解决方案2】:

    似乎您需要在 Python 本身中抑制警告,请查看官方 Python documentation 对此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-03
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多