5

I am trying to hide some python warnings when knitting an Rmd file. The usual chunk setup "warning=F, message=F" doesn't seem to work for python chunks.

Example of Rmd file with a python chunk that, purposefully, generates warnings:

---
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
```
Julien Massardier
  • 1,326
  • 1
  • 11
  • 29

2 Answers2

4

You can add warnings.filterwarnings('ignore') to the python chunk:

```{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
```
Matt
  • 2,947
  • 1
  • 9
  • 21
1

Seems like you need to suppress warnings in Python itself, take a look at official Python documentation on this.

slava-kohut
  • 4,203
  • 1
  • 7
  • 24