How to stop the annoying warnings output by reticulate in R

221 views Asked by At

I am trying to read a pickle data in R, I have following R code:

library(reticulate)
pd <- suppressMessages(import("pandas"))
hubs.data <- suppressWarnings(pd$read_pickle("input.pickle"))

The code is worked well, but its will output a warnning:

sys:1: FutureWarning: pandas.Float64Index is deprecated and will be removed from pandas in a future version. Use pandas.Index with the appropriate dtype instead.
sys:1: FutureWarning: pandas.Int64Index is deprecated and will be removed from pandas in a future version. Use pandas.Index with the appropriate dtype instead.
sys:1: FutureWarning: pandas.UInt64Index is deprecated and will be removed from pandas in a future version. Use pandas.Index with the appropriate dtype instead.

This warning will show when exculate any other R code line after this. I want to stop it.

I have tried:

pd <- suppressWarnings(import("pandas"))
# or
pd <- suppressMessages(import("pandas"))

But the suppressWarnings/suppressMessages seem not work

2

There are 2 answers

3
margusl On BEST ANSWER

You can control Python warnings within Python / through reticulate; this is basically what reticulate::py_suppress_warnings() does.

For testings purposes we first set up a reprex to always show warnings, otherwise trigger_warning() would only warn once, during the first occurrence. Then we'll suppress it with warnings$simplefilter("ignore") when calling from R (or with warnings.simplefilter("ignore") from Python).

Python warning categories and filter actions along with examples can be found from https://docs.python.org/3/library/warnings.html

library(reticulate)
# use specific conda env
# use_condaenv("py311") 

# change Python(!) warning action from `once` to `always` for testing;
# create a dummy function to trigger FutureWarning
py_run_string('
import warnings

warnings.simplefilter("always")
def trigger_warning():
  warnings.warn("a future warning", FutureWarning)
  return 42
')

# triggering a warning:
py$trigger_warning()
#> <string>:6: FutureWarning: a future warning
#> [1] 42

# control Pyhon warnings through reticulate:
warnings <- import("warnings")

# ignore / suppress:
warnings$simplefilter("ignore")
py$trigger_warning()
#> [1] 42
6
phili_b On

The goal is not to suppress all warnings but only those I don't want to see: change the regexp inside testw_xxx_msg <-any(grepl("a warning message",w) )

My_suppressWarnings <- function (fct, ...) {

  tryCatch(
    withCallingHandlers (
      {
        fct(...)
      }, warning=function(w) {
        testw_1st_msg <-any(grepl("will be removed from pandas",w) )
        testw_2nd_msg <-any(grepl("other warning message",w) )
         
        if (testw_1st_msg || testw_2nd_msg ) {
          invokeRestart("muffleWarning")
        }    else {
          conditionMessage(w)
        }
      }),
    error = function(e) {
      print(e)
    }
  )
}

And try My_suppressWarnings(pd$read_pickle("input.pickle"))

In my R projects I have some functions like that one which works, for dplyr and ggplot2, but here without reproducible example it's not easy.

Look also that: https://r-critique.com/how_to_muffle_warnings_and_to_restart

You also can look for similar solution in the Python side.