Skip to content

Commit

Permalink
Merge pull request #1538 from rstudio/fix-pandas-2.2-future-warning
Browse files Browse the repository at this point in the history
fix pandas v2.2 FutureWarning
  • Loading branch information
t-kalinowski authored Feb 19, 2024
2 parents 1794b2e + 2768bc4 commit a2c5f5d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 4 additions & 2 deletions R/conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -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$map(bt$str)))
values
}

Expand Down Expand Up @@ -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$map(bt$str)))

# clean up converted objects
for (i in seq_along(converted)) {
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-python-pandas.R
Original file line number Diff line number Diff line change
Expand Up @@ -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')"))

})

Expand Down

0 comments on commit a2c5f5d

Please sign in to comment.