Consider this simple example
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
I know I can write that dataframe to msgpack
using Pandas
:
mydata.to_msgpack('C://Users/john/Documents/mypack')
The problem is: how can I read that msgpack
file in R
?
Using RcppMsgPack
returns some puzzling output that is not a 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"
What should I do?
Of course, going back from R to Python would also be nice. Thanks!
msgpack
representation of apandas
DataFrame is very low level, so it cannot translate as is to an R suitable object. Either you write some code which converts theRcppMsgPack
output to an R data.frame or you change the process that produces themsgpack
file. This latter solution is of course way better: it's a very bad practice to produce output which can only be read with a specific language. – Swordbill