Skip to content

Commit

Permalink
Add case_when & if_else expression helpers
Browse files Browse the repository at this point in the history
Created expressions.py and moved all the expressions
related functions and class from utils.py to there.
  • Loading branch information
has2k1 committed Nov 1, 2017
1 parent f939cd9 commit 5d8c029
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 103 deletions.
25 changes: 18 additions & 7 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ Two table verbs
right_join
semi_join


Expression helpers
==================
These classes can be used construct complicated conditional assignment
expressions.

.. currentmodule:: plydata.expressions

.. autosummary::
:toctree: generated/
:nosignatures:
:template: main.rst

case_when
if_else


Options
=======

Expand All @@ -126,10 +143,4 @@ Useful Functions
:template: main.rst

Q

n()
---
This is a special internal function that returns the number of
rows in the current groups. It can be used in verbs like
:class:`~plydata.verbs.summarize`, :class:`~plydata.verbs.define`.
and :class:`~plydata.verbs.create`.
n
1 change: 1 addition & 0 deletions plydata/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .one_table_verbs import * # noqa
from .two_table_verbs import * # noqa
from .helper_verbs import * # noqa
from .expressions import * # noqa
from ._version import get_versions

__version__ = get_versions()['version']
Expand Down
7 changes: 4 additions & 3 deletions plydata/dataframe/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import pandas.api.types as pdtypes
import numpy as np

from ..expressions import Expression
from ..types import GroupedDataFrame
from ..utils import get_empty_env, unique, Expression
from ..utils import get_empty_env, unique


def _get_groups(verb):
Expand Down Expand Up @@ -458,9 +459,9 @@ def get(cls, verb):
return cls._if(verb)


def make_expressions(verb):
def build_expressions(verb):
"""
Make expressions
Build expressions for helper verbs
Parameters
----------
Expand Down
6 changes: 3 additions & 3 deletions plydata/dataframe/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import numpy as np

from ..types import GroupedDataFrame
from ..utils import Expression
from .common import _get_groups, Selector, make_expressions
from ..expressions import Expression
from .common import _get_groups, Selector, build_expressions
from .one_table import arrange, create, define, group_by
from .one_table import mutate, rename, summarize

Expand Down Expand Up @@ -171,7 +171,7 @@ def _make_verb_helper(verb_func, add_groups=False):

@wraps(verb_func)
def _verb_func(verb):
verb.expressions, new_columns = make_expressions(verb)
verb.expressions, new_columns = build_expressions(verb)
if add_groups:
verb.groups = new_columns
return verb_func(verb)
Expand Down
Loading

0 comments on commit 5d8c029

Please sign in to comment.