Julien Massardier
Julien Massardier

Reputation: 1466

Suppress warnings when using a python chunk inside an Rmd file

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
```

Upvotes: 5

Views: 1346

Answers (2)

Matt
Matt

Reputation: 2987

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
```

Upvotes: 4

slava-kohut
slava-kohut

Reputation: 4233

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

Upvotes: 1

Related Questions