MCPcopy
hub / github.com/pandas-dev/pandas / test_custom_accessor

Function test_custom_accessor

pandas/tests/test_col.py:196–210  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

194
195
196def test_custom_accessor() -> None:
197 df = pd.DataFrame({"a": [1, 2, 3]})
198
199 class XYZAccessor:
200 def __init__(self, pandas_obj):
201 self._obj = pandas_obj
202
203 def mean(self):
204 return self._obj.mean()
205
206 with ensure_removed(pd.Series, "xyz"):
207 pd.api.extensions.register_series_accessor("xyz")(XYZAccessor)
208 result = df.assign(b=pd.col("a").xyz.mean())
209 expected = pd.DataFrame({"a": [1, 2, 3], "b": [2.0, 2.0, 2.0]})
210 tm.assert_frame_equal(result, expected)
211
212
213@pytest.mark.parametrize(

Callers

nothing calls this directly

Calls 4

assignMethod · 0.95
ensure_removedFunction · 0.90
colMethod · 0.80
meanMethod · 0.45

Tested by

no test coverage detected