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

Hook to register sub-accessors #69

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Multi library support

Currently, we support the following dataframe libraries with
identical syntax:

- pandas
- dask.dataframe
- polars
Expand Down
4 changes: 4 additions & 0 deletions src/akimbo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

from awkward import behavior, mixin_class, mixin_class_method # re-export

import akimbo.datetimes as datetimes
import akimbo.mixin as mixin
import akimbo.strings as strings
from akimbo.io import read_json, read_parquet
from akimbo.version import version as __version__ # noqa

__all__ = (
"datetimes",
"mixin",
"read_parquet",
"read_json",
"behavior",
"mixin_class",
"mixin_class_method",
"strings",
)
13 changes: 10 additions & 3 deletions src/akimbo/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import awkward as ak
import pyarrow.compute as pc

from akimbo.mixin import Accessor


def _run_unary(layout, op, kind=None, **kw):
if layout.is_leaf and (kind is None or layout.dtype.kind == kind):
Expand Down Expand Up @@ -34,7 +36,7 @@ def func(arrays, **kwargs):
return ak.transform(func, arr, other)


def dec(func, mode="unary"):
def dec(func, mode="unary", kind=None):
# TODO: require kind= on functions that need timestamps

if mode == "unary":
Expand All @@ -46,7 +48,7 @@ def f(self, *args, **kwargs):
kwargs.update({k: arg for k, arg in zip(sig, args)})

return self.accessor.to_output(
run_unary(self.accessor.array, func, **kwargs)
run_unary(self.accessor.array, func, kind=kind, **kwargs)
)

elif mode == "binary":
Expand All @@ -58,7 +60,9 @@ def f(self, other, *args, **kwargs):
kwargs.update({k: arg for k, arg in zip(sig, args)})

return self.accessor.to_output(
run_binary(self.accessor.array, other.ak.array, func, **kwargs)
run_binary(
self.accessor.array, other.ak.array, func, kind=kind, **kwargs
)
)

else:
Expand Down Expand Up @@ -123,3 +127,6 @@ def _to_arrow(array):
def _make_unit_compatible(array):
# TODO, actually convert units if not compatible
return array


Accessor.register_accessor("dt", DatetimeAccessor)
19 changes: 6 additions & 13 deletions src/akimbo/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class Accessor(ArithmeticMixin):
aggregations = True # False means data is partitioned
series_type = ()
dataframe_type = ()
subaccessors = {}

def __init__(self, obj, behavior=None):
self._obj = obj
Expand Down Expand Up @@ -205,19 +206,9 @@ def array(self) -> ak.Array:
"""Data as an awkward array"""
return ak.with_name(ak.from_arrow(self.arrow), self._behavior)

@property
def str(self):
"""Nested string operations"""
from akimbo.strings import StringAccessor

return StringAccessor(self)

@property
def dt(self):
"""Nested datetime operations"""
from akimbo.datetimes import DatetimeAccessor

return DatetimeAccessor(self)
@classmethod
def register_accessor(cls, name, klass):
cls.subaccessors[name] = klass

def merge(self):
"""Make a single complex series out of the columns of a dataframe"""
Expand Down Expand Up @@ -255,6 +246,8 @@ def __getattr__(self, item):
if hasattr(arr, item) and callable(getattr(arr, item)):
func = getattr(arr, item)
args = ()
elif item in self.subaccessors:
return self.subaccessors[item](self)
elif hasattr(ak, item):
func = getattr(ak, item)
args = (arr,)
Expand Down
5 changes: 5 additions & 0 deletions src/akimbo/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import awkward as ak

from akimbo.mixin import Accessor


def _encode(layout):
if layout.is_record:
Expand Down Expand Up @@ -99,3 +101,6 @@ def f(*args, **kwargs):

def __dir__(self) -> list[str]:
return sorted(methods)


Accessor.register_accessor("str", StringAccessor)
Loading