Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ S3method(as.matrix,numpy.ndarray)
S3method(as.raw,python.builtin.bytes)
S3method(as.vector,numpy.ndarray)
S3method(dim,numpy.ndarray)
S3method(dim,pandas.DataFrame)
S3method(dim,pandas.Series)
S3method(dim,pandas.core.frame.DataFrame)
S3method(dim,pandas.core.series.Series)
S3method(dim,scipy.sparse._base._spbase)
Expand All @@ -54,6 +56,8 @@ S3method(format,python.builtin.module)
S3method(format,python.builtin.object)
S3method(format,python.builtin.traceback)
S3method(length,numpy.ndarray)
S3method(length,pandas.DataFrame)
S3method(length,pandas.Series)
S3method(length,pandas.core.frame.DataFrame)
S3method(length,pandas.core.series.Series)
S3method(length,python.builtin.dict)
Expand Down Expand Up @@ -88,8 +92,11 @@ S3method(py_to_r,datatable.Frame)
S3method(py_to_r,datetime.date)
S3method(py_to_r,datetime.datetime)
S3method(py_to_r,default)
S3method(py_to_r,pandas.DataFrame)
S3method(py_to_r,pandas.Series)
S3method(py_to_r,pandas._libs.missing.C_NAType)
S3method(py_to_r,pandas._libs.missing.NAType)
S3method(py_to_r,pandas.arrays.NumpyExtensionArray)
S3method(py_to_r,pandas.core.arrays.categorical.Categorical)
S3method(py_to_r,pandas.core.categorical.Categorical)
S3method(py_to_r,pandas.core.frame.DataFrame)
Expand Down Expand Up @@ -117,6 +124,8 @@ S3method(r_to_py,sparseMatrix)
S3method(str,py_config)
S3method(str,python.builtin.module)
S3method(str,python.builtin.object)
S3method(summary,pandas.DataFrame)
S3method(summary,pandas.Series)
S3method(summary,pandas.core.frame.DataFrame)
S3method(summary,pandas.core.series.Series)
S3method(summary,python.builtin.object)
Expand Down
42 changes: 39 additions & 3 deletions R/conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ py_to_r.pandas.core.series.Series <- function(x) {
values
}

#' @export
py_to_r.pandas.Series <- py_to_r.pandas.core.series.Series

#' @export
py_to_r.pandas.core.categorical.Categorical <- function(x) {
local_conversion_scope(x, FALSE)
Expand All @@ -199,6 +202,11 @@ py_to_r.pandas.core.categorical.Categorical <- function(x) {
py_to_r.pandas.core.arrays.categorical.Categorical <-
py_to_r.pandas.core.categorical.Categorical

#' @export
py_to_r.pandas.arrays.NumpyExtensionArray <- function(x) {
py_to_r(x$to_numpy())
}

#' @export
py_to_r.pandas._libs.missing.NAType <- function(x) {
NA
Expand All @@ -221,6 +229,9 @@ summary.pandas.core.series.Series <- function(object, ...) {
object$describe()
}

#' @export
summary.pandas.Series <- summary.pandas.core.series.Series

#' @export
length.pandas.core.series.Series <- function(x) {
if (py_is_null_xptr(x) || !py_available())
Expand All @@ -230,11 +241,17 @@ length.pandas.core.series.Series <- function(x) {
}
}

#' @export
length.pandas.Series <- length.pandas.core.series.Series

#' @export
dim.pandas.core.series.Series <- function(x) {
NULL
}

#' @export
dim.pandas.Series <- dim.pandas.core.series.Series

#' @export
r_to_py.data.frame <- function(x, convert = FALSE) {

Expand Down Expand Up @@ -313,10 +330,14 @@ py_to_r.pandas.core.frame.DataFrame <- function(x) {
attr(df, "pandas.index") <- index

if (inherits(index, c("pandas.core.indexes.base.Index",
"pandas.indexes.base.Index"))) {
"pandas.indexes.base.Index",
"pandas.Index"
))) {

if (inherits(index, c("pandas.core.indexes.range.RangeIndex",
"pandas.indexes.range.RangeIndex")) &&
"pandas.indexes.range.RangeIndex",
"pandas.RangeIndex"
)) &&
np$issubdtype(index$dtype, np$number))
{
# check for a range index from 0 -> n. in such a case, we don't need
Expand Down Expand Up @@ -347,7 +368,9 @@ py_to_r.pandas.core.frame.DataFrame <- function(x) {
}

else if (inherits(index, c("pandas.core.indexes.datetimes.DatetimeIndex",
"pandas.tseries.index.DatetimeIndex"))) {
"pandas.tseries.index.DatetimeIndex",
"pandas.DatetimeIndex"
))) {

converted <- tryCatch(py_to_r(index$values), error = identity)

Expand Down Expand Up @@ -380,9 +403,15 @@ py_to_r.pandas.core.frame.DataFrame <- function(x) {

}

#' @export
py_to_r.pandas.DataFrame <- py_to_r.pandas.core.frame.DataFrame

#' @export
summary.pandas.core.frame.DataFrame <- summary.pandas.core.series.Series

#' @export
summary.pandas.DataFrame <- summary.pandas.core.frame.DataFrame

#' @export
length.pandas.core.frame.DataFrame <- function(x) {
if (py_is_null_xptr(x) || !py_available())
Expand All @@ -392,6 +421,9 @@ length.pandas.core.frame.DataFrame <- function(x) {
}
}

#' @export
length.pandas.DataFrame <- length.pandas.core.frame.DataFrame

#' @export
dim.pandas.core.frame.DataFrame <- function(x) {
if (py_is_null_xptr(x) || !py_available())
Expand All @@ -400,6 +432,10 @@ dim.pandas.core.frame.DataFrame <- function(x) {
py_object_shape(x)
}

#' @export
dim.pandas.DataFrame <- dim.pandas.core.frame.DataFrame


# Scipy sparse matrices
#' @importFrom Matrix Matrix

Expand Down
2 changes: 1 addition & 1 deletion R/knitr-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ eng_python_autoprint <- function(captured, options) {
.engine_context$pending_plots$push(included_path)
return("")

} else if (inherits(value, "pandas.core.frame.DataFrame")) {
} else if (inherits(value, c("pandas.core.frame.DataFrame", "pandas.DataFrame"))) {

return(captured)

Expand Down
3 changes: 1 addition & 2 deletions src/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4382,9 +4382,8 @@ PyObject* r_to_py_pandas_nullable_series (const RObject& column, const bool conv
// strings are not built using np array + mask. Instead they take a
// np array with OBJECT type, with None's in the place of NA's
if (TYPEOF(column) == STRSXP) {
PyObjectPtr args(PyTuple_New(2));
PyObjectPtr args(PyTuple_New(1));
PyTuple_SetItem(args, 0, (PyObject*)r_to_py_numpy(column, convert));
PyTuple_SetItem(args, 1, Py_False);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This used to reference the copy argument, which defaults to False and became a keyword argument in v3. We can simply not pass it


PyObject* pd_col(PyObject_Call(constructor, args, NULL));

Expand Down
9 changes: 7 additions & 2 deletions tests/testthat/test-python-pandas.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
context("pandas")

pandas_version <- function() {
pd <- import("pandas", convert = TRUE)
numeric_version(pd$`__version__`)
}

test_that("Simple Pandas data frames can be roundtripped", {
skip_if_no_pandas()

Expand Down Expand Up @@ -293,12 +298,12 @@ test_that("can cast from pandas nullable types", {

test_that("NA in string columns don't prevent simplification", {
skip_if_no_pandas()

pd <- import("pandas", convert = FALSE)
np <- import("numpy", convert = FALSE)

x <- pd$Series(list("a", pd$`NA`, NULL, np$nan))
expect_equal(py_to_r(x$dtype$name), "object")
expect_equal(py_to_r(x$dtype$name), if (pandas_version() < "3") "object" else "str")

r <- py_to_r(x)

Expand Down