From 1a018ff1df29c491f004345e8252d7aa70464c2e Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Fri, 16 Feb 2024 10:46:35 -0500 Subject: [PATCH 1/4] fix pandas v2.2 FutureWarning sys:1: FutureWarning: Index.format is deprecated and will be removed in a future version. Convert using index.astype(str) or index.map(formatter) instead. closes #1537 --- R/conversion.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/conversion.R b/R/conversion.R index 7e92ae3a7..132969080 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -226,7 +226,8 @@ py_to_r.pandas.core.series.Series <- function(x) { disable_conversion_scope(x) values <- py_to_r(x$values) index <- py_to_r(x$index) - names(values) <- index$format() + bt <- import("builtins") + names(values) <- as.character(bt$list(index$astype(bt$str))) values } @@ -326,13 +327,14 @@ py_to_r.pandas.core.frame.DataFrame <- function(x) { np <- import("numpy", convert = TRUE) pandas <- import("pandas", convert = TRUE) + bt <- import("builtins", convert = TRUE) # extract numpy arrays associated with each column columns <- x$columns$values # delegate to c++ converted <- py_convert_pandas_df(x) - names(converted) <- py_to_r(x$columns$format()) + names(converted) <- as.character(bt$list(x$columns$astype(bt$str))) # clean up converted objects for (i in seq_along(converted)) { From 5206ff910f48cb502b1048be7f9326e49fce260f Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Fri, 16 Feb 2024 12:43:09 -0500 Subject: [PATCH 2/4] update test --- tests/testthat/test-python-pandas.R | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-python-pandas.R b/tests/testthat/test-python-pandas.R index 3024c8e0f..a16d673a6 100644 --- a/tests/testthat/test-python-pandas.R +++ b/tests/testthat/test-python-pandas.R @@ -113,7 +113,11 @@ test_that("complex names are handled", { p <- pd$DataFrame(data = d) r <- py_to_r(p) - expect_equal(names(r), c("col1", "(col1, col2)")) + expect_equal(names(r)[1], "col1") + # pandas 2.2 removed index.format(), and users must pass custom formatters now, + # we default to using __str__ for formatting, which given a tuple, falls back + # to __repr__ (which prints strings with quotes). + expect_in(names(r)[2], c("(col1, col2)", "('col1', 'col2')")) }) From 736973c86c2f53c262f332091e63cb347e3c8180 Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Fri, 16 Feb 2024 12:46:15 -0500 Subject: [PATCH 3/4] Add news --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 7719c9929..0e40499c5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # reticulate (development version) +- Update for Pandas 2.2 deprecation of `Index.format()` (#1537, #1538). + +- Updates for CRAN R-devel (R 4.4). + # reticulate 1.35.0 - Subclassed Python list and dict objects are no longer automatically converted From 2768bc493257fd9405b99233656b4f385f20a3eb Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Mon, 19 Feb 2024 09:05:24 -0500 Subject: [PATCH 4/4] use `index.map` instead of `index.astype` for better backcompat Otherwise, tests fail on Python <= 3.7 --- R/conversion.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/conversion.R b/R/conversion.R index 132969080..956b8196b 100644 --- a/R/conversion.R +++ b/R/conversion.R @@ -227,7 +227,7 @@ py_to_r.pandas.core.series.Series <- function(x) { values <- py_to_r(x$values) index <- py_to_r(x$index) bt <- import("builtins") - names(values) <- as.character(bt$list(index$astype(bt$str))) + names(values) <- as.character(bt$list(index$map(bt$str))) values } @@ -334,7 +334,7 @@ py_to_r.pandas.core.frame.DataFrame <- function(x) { # delegate to c++ converted <- py_convert_pandas_df(x) - names(converted) <- as.character(bt$list(x$columns$astype(bt$str))) + names(converted) <- as.character(bt$list(x$columns$map(bt$str))) # clean up converted objects for (i in seq_along(converted)) {