From b8a1b4dbacc8d0b00b86a146f655e84d569162b8 Mon Sep 17 00:00:00 2001 From: codingl2k1 <138426806+codingl2k1@users.noreply.github.com> Date: Mon, 10 Jul 2023 16:46:27 +0800 Subject: [PATCH] BUG: Fix StringAccessor __getitem__ error (#588) --- python/xorbits/core/adapter.py | 3 +++ python/xorbits/core/tests/test_execution.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/python/xorbits/core/adapter.py b/python/xorbits/core/adapter.py index 96079458d..bd3d93317 100644 --- a/python/xorbits/core/adapter.py +++ b/python/xorbits/core/adapter.py @@ -375,6 +375,9 @@ def __getattr__(self, item): # class variable return from_mars(attr) + def __getitem__(self, item): + return from_mars(self._mars_obj[to_mars(item)]) + def to_mars(inp: Union[DataRef, Tuple, List, Dict]): """ diff --git a/python/xorbits/core/tests/test_execution.py b/python/xorbits/core/tests/test_execution.py index cd5778287..6068d601f 100644 --- a/python/xorbits/core/tests/test_execution.py +++ b/python/xorbits/core/tests/test_execution.py @@ -324,3 +324,16 @@ def test_interactive_execution(setup, ip): ip.run_cell(raw_cell="df + 3") r = ip.run_cell(raw_cell="xorbits.core.execution.need_to_execute(_)") assert r.result + + +def test_getitem(setup): + import pandas as pd + + from ... import pandas as xpd + + data = {"a": ["abc", "def"]} + df = pd.DataFrame(data) + xdf = xpd.DataFrame(data) + result = xdf[xdf["a"].str[0] == "a"] + expected = df[df["a"].str[0] == "a"] + pd.testing.assert_frame_equal(result.to_pandas(), expected)