|
1 | 1 | from typing import Union |
2 | 2 | import collections |
3 | 3 |
|
4 | | -import pandas |
5 | | - |
6 | 4 | import data_algebra.util |
7 | 5 | import data_algebra.env |
8 | 6 |
|
@@ -43,6 +41,15 @@ def __uop_expr__(self, op, *, params=None): |
43 | 41 | raise TypeError("op is supposed to be a string") |
44 | 42 | return Expression(op, (self,), params=params) |
45 | 43 |
|
| 44 | + def __triop_expr__(self, op, x, y): |
| 45 | + if not isinstance(op, str): |
| 46 | + raise TypeError("op is supposed to be a string") |
| 47 | + if not isinstance(x, Term): |
| 48 | + x = Value(x) |
| 49 | + if not isinstance(y, Term): |
| 50 | + y = Value(y) |
| 51 | + return Expression(op, (self, x, y), inline=False) |
| 52 | + |
46 | 53 | # tree re-write |
47 | 54 |
|
48 | 55 | def replace_view(self, view): |
@@ -289,6 +296,9 @@ def is_null(self): |
289 | 296 | def is_bad(self): |
290 | 297 | return self.__uop_expr__("is_bad") |
291 | 298 |
|
| 299 | + def if_else(self, x, y): |
| 300 | + return self.__triop_expr__("if_else", x, y) |
| 301 | + |
292 | 302 |
|
293 | 303 | class Value(Term): |
294 | 304 | def __init__(self, value): |
@@ -337,15 +347,12 @@ def get_column_names(self, columns_seen): |
337 | 347 | } |
338 | 348 |
|
339 | 349 |
|
340 | | -pandas_eval_env = { |
341 | | - "is_null": lambda x: pandas.isnull(x), |
342 | | - "is_bad": data_algebra.util.is_bad, |
343 | | -} |
344 | | - |
345 | | - |
346 | 350 | pd_formatters = { |
347 | 351 | "is_bad": lambda expr: "@is_bad(" + expr.args[0].to_pandas() + ")", |
348 | 352 | "is_null": lambda expr: "@is_null(" + expr.args[0].to_pandas() + ")", |
| 353 | + "if_else": lambda expr: "@if_else(" + expr.args[0].to_pandas() +\ |
| 354 | + ", " + expr.args[1].to_pandas() +\ |
| 355 | + ", " + expr.args[2].to_pandas()+ ")", |
349 | 356 | "neg": lambda expr: "-" + expr.args[0].to_pandas(want_inline_parens=True), |
350 | 357 | } |
351 | 358 |
|
|
0 commit comments