Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pandas2ri - added named-vector-to-pandas conversion #887

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

tikuma-lsuhsc
Copy link
Contributor

@tikuma-lsuhsc tikuma-lsuhsc commented Jun 2, 2022

This PR adds conversions from R named vector to Pandas Series and R named matrix to Pandas DataFrame.

It resolves the issue I encountered while retrieving the fixed effect coefficients (named vector) from lme4.fixef() and CI matrices from confint() functions.

I have also added test_ri2pandas_named_vector() pytest function to test the changes.

@tikuma-lsuhsc tikuma-lsuhsc force-pushed the pandas2ri-support-named-vector branch from 201d5d7 to c5b18c5 Compare June 5, 2022 18:44
@codecov-commenter
Copy link

codecov-commenter commented Jun 25, 2022

Codecov Report

Merging #887 (ae76aad) into master (10d6614) will increase coverage by 0.05%.
The diff coverage is 90.90%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@            Coverage Diff             @@
##           master     #887      +/-   ##
==========================================
+ Coverage   88.97%   89.03%   +0.05%     
==========================================
  Files          39       39              
  Lines        6839     6848       +9     
==========================================
+ Hits         6085     6097      +12     
+ Misses        754      751       -3     
Files Coverage Δ
rpy2/robjects/pandas2ri.py 90.99% <90.90%> (+0.40%) ⬆️

... and 2 files with indirect coverage changes

📣 Codecov offers a browser extension for seamless coverage viewing on GitHub. Try it in Chrome or Firefox today!

@lgautier
Copy link
Member

Hi, thanks for the PR. A review is long due.

I am unsure about whether R named vectors should not be converted to pandas data frames by default, even within the context of the pandas2ri conversion rules. The reason is that while R data.frame objects inherit from R lists, not all R lists can be R data.frames.

Having that said, the current behavior in rpy2 does not seem predictable enough:

import rpy2.robjects as ro
import rpy2.robjects.pandas2ri as pd2r

# First list.

rlist = ro.r('c(a=1, b=2, c=3)')
with (ro.default_converter + pd2r.converter).context() as cv:
    pylist = cv.rpy2py(rlist)

pylist
# array([1., 2., 3.])

# Second list. The conversion is closer to what I would expect, with the exception of
# `b.x` and `b.y` instead of nesting 'x' and 'y' under 'b'. 

rlist = ro.r('c(a=1, b=list(x=list(z=4), y=5), c=3)')

with (ro.default_converter + pd2r.converter).context() as cv:
    pylist = cv.rpy2py(rlist 

pylist
# o{'a': [1.], 'b.x': o{'z': [4.], }, 'b.y': [5.], 'c': [3.], }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants