ℕʘʘ*_*ḆḽḘ 13 python r msgpack pandas tibble
考虑这个简单的例子
import pandas as pd
mydata = pd.DataFrame({'mytime': [pd.to_datetime('2018-01-01 10:00:00.513'),
pd.to_datetime('2018-01-03 10:00:00.513')],
'myvariable': [1,2],
'mystring': ['hello', 'world']})
mydata
Out[7]:
mystring mytime myvariable
0 hello 2018-01-01 10:00:00.513 1
1 world 2018-01-03 10:00:00.513 2
Run Code Online (Sandbox Code Playgroud)
我知道我可以msgpack使用Pandas以下方式写入该数据框:
mydata.to_msgpack('C://Users/john/Documents/mypack')
Run Code Online (Sandbox Code Playgroud)
问题是:如何读取该msgpack文件R?
using RcppMsgPack返回一些令人困惑的输出,不是dataframe/tibble
library(tidyverse)
library(RcppMsgPack)
df <- msgpack_read('C://Users/john/Documents/mypack', simplify = TRUE)
> df
$axes
$axes[[1]]
$axes[[1]]$typ
[1] "index"
$axes[[1]]$name
NULL
$axes[[1]]$klass
[1] "Index"
$axes[[1]]$compress
NULL
$axes[[1]]$data
[1] "mystring" "mytime" "myvariable"
$axes[[1]]$dtype
[1] "object"
$axes[[2]]
$axes[[2]]$typ
[1] "range_index"
$axes[[2]]$name
NULL
$axes[[2]]$klass
[1] "RangeIndex"
$axes[[2]]$start
[1] 0
$axes[[2]]$step
[1] 1
$axes[[2]]$stop
[1] 2
$typ
[1] "block_manager"
$blocks
$blocks[[1]]
$blocks[[1]]$shape
[1] 1 2
$blocks[[1]]$klass
[1] "IntBlock"
$blocks[[1]]$compress
NULL
$blocks[[1]]$values
[1] 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00
attr(,"EXT")
[1] 0
$blocks[[1]]$locs
$blocks[[1]]$locs$typ
[1] "ndarray"
$blocks[[1]]$locs$dtype
[1] "int64"
$blocks[[1]]$locs$compress
NULL
$blocks[[1]]$locs$ndim
[1] 1
$blocks[[1]]$locs$data
[1] 02 00 00 00 00 00 00 00
attr(,"EXT")
[1] 0
$blocks[[1]]$locs$shape
[1] 1
$blocks[[1]]$dtype
[1] "int64"
$blocks[[2]]
$blocks[[2]]$shape
[1] 1 2
$blocks[[2]]$klass
[1] "DatetimeBlock"
$blocks[[2]]$compress
NULL
$blocks[[2]]$values
[1] 40 02 0e 64 4d a7 05 15 40 02 ac 86 76 44 06 15
attr(,"EXT")
[1] 0
$blocks[[2]]$locs
$blocks[[2]]$locs$typ
[1] "ndarray"
$blocks[[2]]$locs$dtype
[1] "int64"
$blocks[[2]]$locs$compress
NULL
$blocks[[2]]$locs$ndim
[1] 1
$blocks[[2]]$locs$data
[1] 01 00 00 00 00 00 00 00
attr(,"EXT")
[1] 0
$blocks[[2]]$locs$shape
[1] 1
$blocks[[2]]$dtype
[1] "datetime64[ns]"
$blocks[[3]]
$blocks[[3]]$shape
[1] 1 2
$blocks[[3]]$klass
[1] "ObjectBlock"
$blocks[[3]]$compress
NULL
$blocks[[3]]$values
[1] "hello" "world"
$blocks[[3]]$locs
$blocks[[3]]$locs$typ
[1] "ndarray"
$blocks[[3]]$locs$dtype
[1] "int64"
$blocks[[3]]$locs$compress
NULL
$blocks[[3]]$locs$ndim
[1] 1
$blocks[[3]]$locs$data
[1] 00 00 00 00 00 00 00 00
attr(,"EXT")
[1] 0
$blocks[[3]]$locs$shape
[1] 1
$blocks[[3]]$dtype
[1] "object"
$klass
[1] "DataFrame"
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
当然,从R回到Python也很好。谢谢!
你在 R 中使用怎么样library(reticulate):
library(reticulate)
pyData = py_run_string("import pandas as pd
mydata = pd.DataFrame({'mytime': [pd.to_datetime('2018-01-01 10:00:00.513'),
pd.to_datetime('2018-01-03 10:00:00.513')],
'myvariable': [1,2],
'mystring': ['hello', 'world']})")
Run Code Online (Sandbox Code Playgroud)
它将产生所需的输出:
pyData$mydata
mystring mytime myvariable
1 hello 2018-01-01 10:00:00 1
2 world 2018-01-03 10:00:00 2
Run Code Online (Sandbox Code Playgroud)
您可以将所有 python 代码保存在一个 python 文件中,例如mydata.py并使用该函数py_run_file("mydata.py")。
reticulate可以在这里找到概述:https: //github.com/rstudio/reticulate。
您最感兴趣的可能是类型转换的描述:
来源: https: //github.com/rstudio/reticulate#type-conversions。
附加问题 - 从 R 到 Python:
类型转换也适用于从 R“发送”数据到 Python,请参见此处:https: //rstudio.github.io/reticulate/articles/calling_python.html#commerce-scripts。
py = py_run_string("def add(x, y):
return x + y")
py$add(5, 10)
15
Run Code Online (Sandbox Code Playgroud)