在 Rmd 文件中使用 python 块时抑制警告
Suppress warnings when using a python chunk inside an Rmd file
我试图在编写 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
```
似乎您需要在 Python 本身中抑制警告,请查看官方 Python documentation。
您可以将 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
```
我试图在编写 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
```
似乎您需要在 Python 本身中抑制警告,请查看官方 Python documentation。
您可以将 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
```