Converts a pandas DataFrame into an R data.frame (or a
tibble), preserving the column types reticulate's default conversion gets
wrong: 64-bit integer ids, object-dtype columns, and datetimes.
Usage
pandas2df(
x,
use_arrow = FALSE,
keep_index = FALSE,
tibble = use_arrow,
bigint = c("auto", "integer64", "character")
)Arguments
- x
A pandas
DataFrame(a reticulatepandas.core.frame.DataFrame).- use_arrow
If
TRUE, convert via a temporary Feather file using thearrowpackage rather than in memory. Implies a tibble result.- keep_index
Whether to keep the pandas index as a column.
- tibble
Whether to return a tibble rather than a base data frame. Defaults to
use_arrow.- bigint
How to represent integer columns.
"auto"(the default) maps each column to the narrowest faithful base R type: values fitting a 32-bit Rintegerbecomeinteger, larger values below 2^53 becomedouble(exact), and values from 2^53 up to the signed 64-bit maximum becomebit64::integer64– reservinginteger64for what base R cannot hold exactly."integer64"returns every integer column asbit64::integer64for a stable schema regardless of magnitude."character"behaves like"auto"but returns the large (>= 2^53) tier as character, asdata.table::fread()offers.uint64values beyond the signed 64-bit range cannot be held byinteger64; they are always returned as character, with a warning unlessbigint = "character".
Details
The default in-memory path converts with reticulate and then patches individual columns:
Integer columns (
int64,uint64, andobjectcolumns whose cells are all Python ints) are classified by magnitude so each maps to the narrowest faithful R type. This mirrors reticulate's ownpy_to_r()where reticulate is faithful, and reservesbit64::integer64for values base R cannot hold exactly – seebigint.objectcolumns whose cells are all scalar (orNA) are flattened from a list of length-1 vectors to an atomic vector, with integer-valued cells read as strings so arbitrary-precision Python ints round-trip. Genuine list-valued columns (e.g. multi-select values) are left intact.datetime columns are normalised to
POSIXctin UTC.
The optional use_arrow path round-trips through a Feather file and needs
the Suggested arrow package; bigint does not apply to it.