Skip to content

Commit 35edfbd

Browse files
committed
Fix various conversion issues.
The R package polars introduced a few changes in its class definition. The fixes highlighted an issue in rpy2 (conversion rules for R environment) that will be fixed in rpy2-robjects 3.6.3. That fix is required.
1 parent 82e3f4f commit 35edfbd

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ classifiers = [
2525
]
2626
dependencies = [
2727
"pyarrow",
28-
"rpy2 >= 3.5.15"
28+
"rpy2-robjects >= 3.6.3"
2929
]
3030
dynamic = ["version"]
3131

rpy2_arrow/polars.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,29 @@ def pypolars_to_rpolars_dataframe(
5858
rpack_polars = ensure_r_polars()
5959
# TODO: There appear to be an odd shortcircuiting that requires toggling
6060
# additional conversion off.
61-
with rpy2.robjects.default_converter.context():
61+
with rpy2arrow.converter.context():
6262
return rpack_polars.as_polars_df(r_arrow_table)
6363

64-
6564
# TODO: rpy2.rinterface.SexpExtPtr should have an robjects-level wrapper?
6665
def rpolar_to_pypolars_dataframe(
6766
dataf: rpy2.rinterface.SexpExtPtr
6867
) -> polars.DataFrame:
6968
# R polars to R arrow.
7069
rpack_arrow = ensure_r_arrow()
7170
ensure_r_polars()
72-
r_arrow_table = rpack_arrow.as_arrow_table(dataf)
71+
with rpy2.robjects.default_converter.context():
72+
r_arrow_table = rpack_arrow.as_arrow_table(dataf)
73+
return rarrow_to_pypolars_dataframe(r_arrow_table)
74+
75+
76+
def rpolars_env_to_pypolars_dataframe(
77+
dataf: rpy2.rinterface.sexp.SexpEnvironment
78+
) -> polars.DataFrame:
79+
# R polars to R arrow.
80+
rpack_arrow = ensure_r_arrow()
81+
ensure_r_polars()
82+
with rpy2.robjects.default_converter.context():
83+
r_arrow_table = rpack_arrow.as_arrow_table(dataf)
7384
return rarrow_to_pypolars_dataframe(r_arrow_table)
7485

7586

@@ -92,12 +103,14 @@ def rpolar_to_pypolars_dataframe(
92103

93104
converter._rpy2py_nc_map[rpy2.rinterface.SexpEnvironment].update(
94105
{
95-
'Table': rarrow_to_pypolars_dataframe,
106+
'polars_data_frame': rpolars_env_to_pypolars_dataframe,
107+
'Table': rarrow_to_pypolars_dataframe
96108
}
97109
)
98110

99111
converter._rpy2py_nc_map[rpy2.rinterface.SexpExtPtr].update(
100112
{
113+
# TODO: is this still needed?
101114
'polars_data_frame': rpolar_to_pypolars_dataframe,
102115
}
103116
)

rpy2_arrow/tests_polars.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_rpolar_to_pypolars_dataframe(self):
9797
([1, 2], polars.Int64, 'Int64', _cmp_simple), # Fails.
9898
([1.1, 2.1], polars.Float32, 'Float32', _cmp_float),
9999
([1.1, 2.1], polars.Float64, 'Float64', _cmp_float),
100-
(['wx', 'yz'], polars.Utf8, 'Utf8', _cmp_simple),
100+
(['wx', 'yz'], polars.Utf8, 'String', _cmp_simple),
101101
(['wx', 'yz', 'wx'], polars.Categorical,
102102
'Categorical', _cmp_simple)
103103
])
@@ -169,6 +169,6 @@ def test_rpl_to_pl(self, rstr, cls):
169169

170170
def test_pl_to_rpl(self):
171171
plobj = polars.DataFrame({'a': [1, 2, 3]})
172-
cls = rpy2.robjects.ExternalPointer
172+
cls = rpy2.robjects.environments.Environment
173173
rplobj = rpy2polars.pl_to_rpl(plobj)
174174
assert isinstance(rplobj, cls)

0 commit comments

Comments
 (0)