Skip to content

Commit 48228cc

Browse files
committed
test accessor
1 parent a703982 commit 48228cc

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pandas/tests/test_col.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pandas as pd
66
import pandas._testing as tm
7+
from pandas.tests.test_register_accessor import ensure_removed
78

89

910
@pytest.mark.parametrize(
@@ -65,3 +66,20 @@ def test_invalid() -> None:
6566
df = pd.DataFrame({"a": [1, 2]})
6667
with pytest.raises(ValueError, match="did you mean"):
6768
df.assign(c=pd.col("b").mean())
69+
70+
71+
def test_custom_accessor() -> None:
72+
df = pd.DataFrame({"a": [1, 2, 3]})
73+
74+
class XYZAccessor:
75+
def __init__(self, pandas_obj):
76+
self._obj = pandas_obj
77+
78+
def mean(self):
79+
return self._obj.mean()
80+
81+
with ensure_removed(pd.Series, "xyz"):
82+
pd.api.extensions.register_series_accessor("xyz")(XYZAccessor)
83+
result = df.assign(b=pd.col("a").xyz.mean())
84+
expected = pd.DataFrame({"a": [1, 2, 3], "b": [2.0, 2.0, 2.0]})
85+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)