-
Notifications
You must be signed in to change notification settings - Fork 327
/
conversion.R
605 lines (501 loc) · 15.9 KB
/
conversion.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#' Convert between Python and R objects
#'
#' @inheritParams import
#' @param x A Python object.
#'
#' @return An \R object, as converted from the Python object.
#'
#' @name r-py-conversion
#' @export
r_to_py <- function(x, convert = FALSE) {
ensure_python_initialized()
UseMethod("r_to_py")
}
#' @rdname r-py-conversion
#' @export
py_to_r <- function(x) {
if(!is_py_object(x <- py_to_r_cpp(x))) # simple obj
return(x)
# Note, the new `x` here (with convert=TRUE) won't be visible to S3 methods
# UseMethod() doesn't allow modifying the original call args
UseMethod("py_to_r")
}
#' @export
r_to_py.default <- function(x, convert = FALSE) {
r_to_py_impl(x, convert = convert)
}
#' @export
py_to_r.default <- function(x) {
py_to_r_cpp(x)
}
#' R wrapper for Python objects
#'
#' S3 method to create a custom R wrapper for a Python object.
#' The default wrapper is either an R environment or an R function
#' (for callable python objects).
#'
#' @param x Python object
#'
#' @keywords internal
#'
#' @export
py_to_r_wrapper <- function(x) {
UseMethod("py_to_r_wrapper")
}
#' @export
py_to_r_wrapper.default <- function(x) {
x
}
#' @export
r_to_py.factor <- function(x, convert = FALSE) {
if (inherits(x, "ordered"))
warning("converting ordered factor to character; ordering will be lost")
r_to_py_impl(as.character(x), convert = convert)
}
#' @export
r_to_py.POSIXt <- function(x, convert = FALSE) {
tz <- attr(x, "tzone", TRUE)
## POSIXlt tzone is a length-3 vec, where "" means local/missing tzone
if(length(tz))
tz <- tz[[1L]] %""% NULL
# we prefer numpy datetime64 for efficiency
# numpy doesn't support tzone
# (internally, always stored from UTC epoch, like POSIXct in R)
if (is.null(tz) && py_module_available("numpy")) {
np_array <- r_to_py_impl(array(as.double(x) * 1E9), convert = FALSE)
return(np_array$astype(dtype = "datetime64[ns]"))
}
if(!is.null(tz)) {
if(py_version() >= "3.9")
tz <- import("zoneinfo", convert = FALSE)$ZoneInfo(tz)
else
tz <- import("pytz")$timezone(tz)
}
datetime <- import("datetime", convert = FALSE)
datetime$datetime$fromtimestamp(as.double(x), tz = tz)
}
#' @export
py_to_r.datetime.datetime <- function(x) {
if (py_version() >= "3") {
tz <- NULL
if (!is.null(x$tzinfo)) {
# in Python 3.9, there is a new zoneinfo.ZoneInfo class that
# accepts Olsonnames, similar to R's tz= semantics.
# Try to find the user supplied value in that case.
# Note that accessing `ZoneInfo.tzname()` is lossy. Eg.
# doing `ZoneInfo("America/New_York").tzname()` returns "EDT", which is
# not in R's OlsonNames() database, and also not stable wrt DST status.
# pytz.zone, round-trips better
if(is.null(tz))
tz <- as_r_value(py_get_attr(x$tzinfo, "zone", TRUE))
# zoneinfo.ZoneInfo(), roundtrip lossy (standardized), but in py stdlib
if (is.null(tz))
tz <- as_r_value(py_get_attr(x$tzinfo, "key", TRUE))
# common to pytz.timezone, ZoneInfo, other subclasses
if (is.null(tz))
tryCatch(tz <- as_r_value(x$tzname()), error = identity)
}
# TODO: if tzname() raised NotImplemented,
# - restore last user facing python exception,
# - have a fallback trying to construct a tz string w/ utcoffset().
return(.POSIXct(as_r_value(x$timestamp()), tz = tz))
}
# old python2 compat code.
# mangles tzinfo attribute: https://github.com/rstudio/reticulate/issues/1265
disable_conversion_scope(x)
# convert to POSIX time
time <- import("time", convert = TRUE)
posix <- time$mktime(x$timetuple())
# include microseconds as well
ms <- as_r_value(x$microsecond)
posix <- posix + as.numeric(ms) * 1E-6
# TODO: handle non-UTC timezones?
as.POSIXct(posix, origin = "1970-01-01", tz = "UTC")
}
#' @export
r_to_py.Date <- function(x, convert = FALSE) {
r_convert_date(x, convert)
}
#' @export
py_to_r.datetime.date <- function(x) {
iso <- as_r_value(x$isoformat())
as.Date.character(iso)
}
#' @export
py_to_r.collections.OrderedDict <- function(x) {
keys <- py_dict_get_keys(x)
py_set_convert(keys, FALSE)
local_conversion_scope(x, TRUE)
result <- lapply(seq_len(length(keys)) - 1L, function(i) {
py_dict_get_item(x, keys[i])
})
names(result) <- py_dict_get_keys_as_str(x)
result
}
#' @export
py_to_r.pandas.core.series.Series <- function(x) {
local_conversion_scope(x, FALSE)
values <- py_to_r(x$values)
index <- py_to_r(x$index)
bt <- import("builtins")
names(values) <- as.character(bt$list(index$map(bt$str)))
values
}
#' @export
py_to_r.pandas.core.categorical.Categorical <- function(x) {
local_conversion_scope(x, FALSE)
values <- py_to_r(x$get_values())
levels <- py_to_r(x$categories$values)
ordered <- py_to_r(x$dtype$ordered)
factor(values, levels = levels, ordered = ordered)
}
#' @export
py_to_r.pandas.core.arrays.categorical.Categorical <-
py_to_r.pandas.core.categorical.Categorical
#' @export
py_to_r.pandas._libs.missing.NAType <- function(x) {
NA
}
#' @export
py_to_r.pandas._libs.missing.C_NAType <- function(x) {
NA
}
py_object_shape <- function(object) {
unlist(as_r_value(object$shape))
}
#' @export
summary.pandas.core.series.Series <- function(object, ...) {
if (py_is_null_xptr(object) || !py_available())
str(object)
else
object$describe()
}
#' @export
length.pandas.core.series.Series <- function(x) {
if (py_is_null_xptr(x) || !py_available())
0L
else {
py_object_shape(x)[[1]]
}
}
#' @export
dim.pandas.core.series.Series <- function(x) {
NULL
}
#' @export
r_to_py.data.frame <- function(x, convert = FALSE) {
# if we don't have pandas, just use default implementation
if (!py_module_available("pandas"))
return(r_to_py_impl(x, convert = convert))
pd <- import("pandas", convert = FALSE)
# manually convert each column to associated Python vector type
columns <- r_convert_dataframe(x, convert = convert)
# generate DataFrame from dictionary
pdf <- pd$DataFrame$from_dict(columns)
# copy over row names if they exist
rni <- .row_names_info(x, type = 0L)
if (is.character(rni)) {
if (length(rni) == 1)
rni <- as.list(rni)
pdf$index <- rni
}
# re-order based on original columns
if (length(x) > 1)
pdf <- pdf$reindex(columns = names(x))
pdf
}
#' @export
py_to_r.datatable.Frame <- function(x) {
# TODO: it would be nice to avoid the extra conversion to pandas
py_to_r(x$to_pandas())
}
#' @export
py_to_r.pandas.core.frame.DataFrame <- function(x) {
local_conversion_scope(x, FALSE)
np <- import("numpy", convert = TRUE)
pandas <- import("pandas", convert = TRUE)
bt <- import("builtins", convert = TRUE)
# delegate to c++
converted <- py_convert_pandas_df(x)
converted <- lapply(converted, function(col) {
if (identical(length(dim(col)), 1L))
dim(col) <- NULL
col
})
# assign colnamnes
columns <- x$columns$values
columns <- as.character(bt$list(x$columns$map(bt$str)))
names(converted) <- columns
df <- converted
attr(df, "row.names") <- c(NA_integer_, py_to_r(-x$shape[[0L]]))
class(df) <- "data.frame"
# attempt to copy over index, and set as rownames when appropriate
#
# TODO: should we tag the R data.frame with the original Python index
# object in case users need it?
#
# TODO: Pandas allows for a large variety of index formats; we should
# try to explicitly whitelist a small family which we can represent
# effectively in R
index <- x$index
# tag the returned object with the Python index, in case
# the user needs to explicitly access / munge the index
# for some need
attr(df, "pandas.index") <- index
if (inherits(index, c("pandas.core.indexes.base.Index",
"pandas.indexes.base.Index"))) {
if (inherits(index, c("pandas.core.indexes.range.RangeIndex",
"pandas.indexes.range.RangeIndex")) &&
np$issubdtype(index$dtype, np$number))
{
# check for a range index from 0 -> n. in such a case, we don't need
# to copy or translate the index. note that we need to translate from
# Python's 0-based indexing to R's one-based indexing.
#
# NOTE: `_start` and friends were deprecated with Pandas 0.25.0,
# with non-private versions preferred for access instead
if (reticulate::py_has_attr(index, "start"))
{
start <- py_to_r(index[["start"]])
stop <- py_to_r(index[["stop"]])
step <- py_to_r(index[["step"]])
}
else
{
start <- py_to_r(index[["_start"]])
stop <- py_to_r(index[["_stop"]])
step <- py_to_r(index[["_step"]])
}
if (start != 0 || stop != nrow(df) || step != 1) {
values <- tryCatch(py_to_r(index$values), error = identity)
if (is.numeric(values)) {
rownames(df) <- values + 1
}
}
}
else if (inherits(index, c("pandas.core.indexes.datetimes.DatetimeIndex",
"pandas.tseries.index.DatetimeIndex"))) {
converted <- tryCatch(py_to_r(index$values), error = identity)
tz <- index[["tz"]]
if (inherits(tz, "pytz.tzinfo.BaseTzInfo") ||
inherits(tz, "pytz.UTC"))
{
zone <- tryCatch(py_to_r(tz$zone), error = function(e) NULL)
if (!is.null(zone) && zone %in% OlsonNames())
attr(converted, "tzone") <- zone
}
rownames(df) <- converted
}
else {
converted <- tryCatch(py_to_r(index$values), error = identity)
if (is.character(converted) || is.numeric(converted)) {
if (any(duplicated(converted))) {
warning("index contains duplicated values: row names not set")
} else {
rownames(df) <- converted
}
}
}
}
df
}
#' @export
summary.pandas.core.frame.DataFrame <- summary.pandas.core.series.Series
#' @export
length.pandas.core.frame.DataFrame <- function(x) {
if (py_is_null_xptr(x) || !py_available())
0L
else {
py_object_shape(x)[[2]]
}
}
#' @export
dim.pandas.core.frame.DataFrame <- function(x) {
if (py_is_null_xptr(x) || !py_available())
NULL
else
py_object_shape(x)
}
# Scipy sparse matrices
#' @importFrom Matrix Matrix
#' @export
dim.scipy.sparse.base.spmatrix <- function(x) {
if (py_is_null_xptr(x) || !py_available())
NULL
else
py_object_shape(x)
}
#' @export
length.scipy.sparse.base.spmatrix <- function(x) {
if (py_is_null_xptr(x) || !py_available())
0L
else
prod(py_object_shape(x))
}
#' @export
py_to_r.scipy.sparse.base.spmatrix <- function(x) {
py_to_r(x$tocsc())
}
#' @importFrom methods as
#' @export
r_to_py.sparseMatrix <- function(x, convert = FALSE) {
x <- if (package_version(as.vector(getNamespaceVersion("Matrix"))) >= "1.4-2")
as(as(as(x, "dMatrix"), "generalMatrix"), "CsparseMatrix")
else
as(x, "dgCMatrix")
r_to_py(x, convert = convert)
}
# Conversion between `Matrix::dgCMatrix` and `scipy.sparse.csc.csc_matrix`.
# Scipy CSC Matrix: https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csc_matrix.html
#' @export
r_to_py.dgCMatrix <- function(x, convert = FALSE) {
# use default implementation if scipy is not available
if (!py_module_available("scipy"))
return(r_to_py_impl(x, convert = convert))
sp <- import("scipy.sparse", convert = FALSE)
csc_x <- sp$csc_matrix(
tuple(
np_array(x@x), # Data array of the matrix
np_array(x@i), # CSC format index array
np_array(x@p), # CSC format index pointer array
convert = FALSE
),
shape = dim(x)
)
if (any(dim(x) != dim(csc_x)))
stop(
paste(
"Failed to convert: dimensions of the original Matrix::dgCMatrix ",
"object (", dim(x), ") and the converted Scipy CSC matrix (",
dim(csc_x), ") do not match", sep="", collapse=", "))
csc_x
}
#' @importFrom methods new
#' @export
py_to_r.scipy.sparse.csc.csc_matrix <- function(x) {
disable_conversion_scope(x)
x <- x$sorted_indices()
new(
"dgCMatrix",
i = as.integer(as_r_value(x$indices)),
p = as.integer(as_r_value(x$indptr)),
x = as.vector(as_r_value(x$data)),
Dim = as.integer(dim(x))
)
}
# Conversion between `Matrix::dgRMatrix` and `scipy.sparse.csr.csr_matrix`.
# Scipy CSR Matrix: https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html
#' @export
r_to_py.dgRMatrix <- function(x, convert = FALSE) {
# use default implementation if scipy is not available
if (!py_module_available("scipy"))
return(r_to_py_impl(x, convert = convert))
sp <- import("scipy.sparse", convert = FALSE)
csr_x <- sp$csr_matrix(
tuple(
np_array(x@x), # Data array of the matrix
np_array(x@j), # CSR format index array
np_array(x@p), # CSR format index pointer array
convert = FALSE
),
shape = dim(x)
)
if (any(dim(x) != dim(csr_x)))
stop(
paste(
"Failed to convert: dimensions of the original Matrix::dgRMatrix ",
"object (", dim(x), ") and the converted Scipy CSR matrix (",
dim(csr_x), ") do not match", sep="", collapse=", "))
csr_x
}
#' @export
py_to_r.scipy.sparse.csr.csr_matrix <- function(x) {
disable_conversion_scope(x)
x <- x$sorted_indices()
new(
"dgRMatrix",
j = as.integer(as_r_value(x$indices)),
p = as.integer(as_r_value(x$indptr)),
x = as.vector(as_r_value(x$data)),
Dim = as.integer(dim(x))
)
}
# Conversion between `Matrix::dgTMatrix` and `scipy.sparse.coo.coo_matrix`.
# Scipy COO Matrix: https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.coo_matrix.html
#' @export
r_to_py.dgTMatrix <- function(x, convert = FALSE) {
# use default implementation if scipy is not available
if (!py_module_available("scipy"))
return(r_to_py_impl(x, convert = convert))
sp <- import("scipy.sparse", convert = FALSE)
coo_x <- sp$coo_matrix(
tuple(
np_array(x@x), # Data array of the matrix
tuple(
np_array(x@i),
np_array(x@j),
convert = FALSE
),
convert = FALSE
),
shape = dim(x)
)
if (any(dim(x) != dim(coo_x)))
stop(
paste(
"Failed to convert: dimensions of the original Matrix::dgTMatrix ",
"object (", dim(x), ") and the converted Scipy COO matrix (",
dim(coo_x), ") do not match", sep="", collapse=", "))
coo_x
}
#' @importFrom methods new
#' @export
py_to_r.scipy.sparse.coo.coo_matrix <- function(x) {
disable_conversion_scope(x)
new(
"dgTMatrix",
i = as.integer(as_r_value(x$row)),
j = as.integer(as_r_value(x$col)),
x = as.vector(as_r_value(x$data)),
Dim = as.integer(dim(x))
)
}
r_convert_dataframe_column <- function(column, convert) {
pd <- import("pandas", convert = FALSE)
if (is.factor(column)) {
pd$Categorical(as.list(as.character(column)),
categories = as.list(levels(column)),
ordered = inherits(column, "ordered"))
} else if (is.numeric(column) || is.character(column)) {
np_array(column)
} else if (inherits(column, "POSIXt")) {
np_array(as.numeric(column) * 1E9, dtype = "datetime64[ns]")
} else {
r_to_py(column)
}
}
# workaround for deprecation of packages in scipy 1.8.0
#' @export
dim.scipy.sparse._base.spmatrix <- dim.scipy.sparse.base.spmatrix
#' @export
length.scipy.sparse._base.spmatrix <- length.scipy.sparse.base.spmatrix
#' @export
py_to_r.scipy.sparse._base.spmatrix <- py_to_r.scipy.sparse.base.spmatrix
#' @export
py_to_r.scipy.sparse._csc.csc_matrix <- py_to_r.scipy.sparse.csc.csc_matrix
#' @export
py_to_r.scipy.sparse._csr.csr_matrix <- py_to_r.scipy.sparse.csr.csr_matrix
#' @export
py_to_r.scipy.sparse._coo.coo_matrix <- py_to_r.scipy.sparse.coo.coo_matrix
# updated locations for scipy 1.11.0
#' @export
dim.scipy.sparse._base._spbase <- dim.scipy.sparse.base.spmatrix
#' @export
length.scipy.sparse._base._spbase <- length.scipy.sparse.base.spmatrix
#' @export
py_to_r.scipy.sparse._matrix.spmatrix <- py_to_r.scipy.sparse.base.spmatrix
#' @export
as.data.frame.polars.dataframe.frame.DataFrame <-
function(x, row.names = NULL, optional = FALSE, ...) {
py_set_convert(x$to_pandas, TRUE)()
}