Skip to content

Commit fc574eb

Browse files
committed
work on divide and tests
1 parent df622aa commit fc574eb

File tree

13 files changed

+79
-24
lines changed

13 files changed

+79
-24
lines changed

build/lib/data_algebra/BigQuery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ def _bigquery_ieee_divide_expr(dbmodel, expression):
7878
# https://cloud.google.com/bigquery/docs/reference/standard-sql/mathematical_functions#ieee_divide
7979
assert len(expression.args) == 2
8080
e0 = dbmodel.expr_to_sql(expression.args[0], want_inline_parens=False)
81-
e1 = dbmodel.expr_to_sql(expression.args[1], want_inline_parens=False)
82-
return f'IEEE_DIVIDE({e0}, 1.0 * ({e1}))'
81+
e1 = dbmodel.expr_to_sql(expression.args[1], want_inline_parens=True)
82+
return f'IEEE_DIVIDE({e0}, 1.0 * {e1})'
8383

8484

8585
BigQuery_formatters = {

build/lib/data_algebra/PostgreSQL.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def _postgresql_as_int64(dbmodel, expression):
2727
def _postgresql_null_divide_expr(dbmodel, expression):
2828
assert len(expression.args) == 2
2929
e0 = dbmodel.expr_to_sql(expression.args[0], want_inline_parens=True)
30-
e1 = dbmodel.expr_to_sql(expression.args[1], want_inline_parens=False)
31-
return f'{e0} / NULLIF({e1}, 0)'
30+
e1 = dbmodel.expr_to_sql(expression.args[1], want_inline_parens=True)
31+
return f'({e0} / NULLIF(1.0 * {e1}, 0))'
3232

3333

3434
# map from op-name to special SQL formatting code

build/lib/data_algebra/db_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,9 @@ def _db_float_divide_expr(dbmodel, expression):
439439
# don't issue an error
440440
# https://cloud.google.com/bigquery/docs/reference/standard-sql/mathematical_functions#ieee_divide
441441
assert len(expression.args) == 2
442-
e0 = dbmodel.expr_to_sql(expression.args[0], want_inline_parens=False)
443-
e1 = dbmodel.expr_to_sql(expression.args[1], want_inline_parens=False)
444-
return f'({e0}) / (1.0 * ({e1}))'
442+
e0 = dbmodel.expr_to_sql(expression.args[0], want_inline_parens=True)
443+
e1 = dbmodel.expr_to_sql(expression.args[1], want_inline_parens=True)
444+
return f'({e0} / (1.0 * {e1}))'
445445

446446

447447
def _db_nunique_expr(dbmodel, expression):

coverage.txt

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ tests/test_get_methods_used.py . [ 30%]
4747
tests/test_ghost_col_issue.py . [ 31%]
4848
tests/test_idioms.py ................. [ 37%]
4949
tests/test_if_else.py ..... [ 39%]
50-
tests/test_if_else_return_type.py . [ 39%]
50+
tests/test_if_else_return_type.py F [ 39%]
5151
tests/test_incomplete_agg.py . [ 39%]
5252
tests/test_join_check.py . [ 40%]
5353
tests/test_join_effects.py . [ 40%]
@@ -117,6 +117,58 @@ tests/test_window_fns.py ..... [ 98%]
117117
tests/test_with.py .. [ 99%]
118118
tests/test_xicor.py .. [100%]
119119

120+
=================================== FAILURES ===================================
121+
___________________________ test_if_else_return_type ___________________________
122+
123+
def test_if_else_return_type():
124+
pd = data_algebra.default_data_model.pd
125+
d = pd.DataFrame({
126+
'x': [True, False, None],
127+
})
128+
ops = (
129+
descr(d=d)
130+
.extend({
131+
'w': 'x.where(1.0, 2.0)',
132+
'i': 'x.if_else(1.0, 2.0)',
133+
})
134+
)
135+
res = ops.transform(d)
136+
expect = pd.DataFrame({
137+
'x': [True, False, None],
138+
'w': [1.0, 2.0, 2.0],
139+
'i': [1.0, 2.0, numpy.nan],
140+
})
141+
assert data_algebra.test_util.equivalent_frames(res, expect)
142+
assert str(res['w'].dtype) == 'float64'
143+
assert str(res['i'].dtype) == 'float64'
144+
> numpy.isnan(res) # when column types are wrong this threw in pyvteat test_KDD2009.py
145+
146+
tests/test_if_else_return_type.py:31:
147+
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
148+
../../../opt/anaconda3/envs/ai_academy_3_9/lib/python3.9/site-packages/pandas/core/generic.py:2032: in __array_ufunc__
149+
return arraylike.array_ufunc(self, ufunc, method, *inputs, **kwargs)
150+
../../../opt/anaconda3/envs/ai_academy_3_9/lib/python3.9/site-packages/pandas/core/arraylike.py:372: in array_ufunc
151+
result = mgr.apply(getattr(ufunc, method))
152+
../../../opt/anaconda3/envs/ai_academy_3_9/lib/python3.9/site-packages/pandas/core/internals/managers.py:325: in apply
153+
applied = b.apply(f, **kwargs)
154+
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
155+
156+
self = ObjectBlock: slice(0, 1, 1), 1 x 3, dtype: object
157+
func = <method-wrapper '__call__' of numpy.ufunc object at 0x7fb3f81ee7c0>
158+
kwargs = {}
159+
160+
@final
161+
def apply(self, func, **kwargs) -> list[Block]:
162+
"""
163+
apply the function to my values; return a block if we are not
164+
one
165+
"""
166+
with np.errstate(all="ignore"):
167+
> result = func(self.values, **kwargs)
168+
E TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
169+
170+
../../../opt/anaconda3/envs/ai_academy_3_9/lib/python3.9/site-packages/pandas/core/internals/blocks.py:382: TypeError
171+
120172
---------- coverage: platform darwin, python 3.9.7-final-0 -----------
121173
Name Stmts Miss Cover Missing
122174
--------------------------------------------------------------------
@@ -152,5 +204,6 @@ data_algebra/util.py 140 29 79% 26, 51, 56, 61, 84-
152204
--------------------------------------------------------------------
153205
TOTAL 5669 712 87%
154206

155-
156-
======================= 282 passed in 437.13s (0:07:17) ========================
207+
=========================== short test summary info ============================
208+
FAILED tests/test_if_else_return_type.py::test_if_else_return_type - TypeErro...
209+
================== 1 failed, 281 passed in 406.07s (0:06:46) ===================

data_algebra/BigQuery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ def _bigquery_ieee_divide_expr(dbmodel, expression):
7878
# https://cloud.google.com/bigquery/docs/reference/standard-sql/mathematical_functions#ieee_divide
7979
assert len(expression.args) == 2
8080
e0 = dbmodel.expr_to_sql(expression.args[0], want_inline_parens=False)
81-
e1 = dbmodel.expr_to_sql(expression.args[1], want_inline_parens=False)
82-
return f'IEEE_DIVIDE({e0}, 1.0 * ({e1}))'
81+
e1 = dbmodel.expr_to_sql(expression.args[1], want_inline_parens=True)
82+
return f'IEEE_DIVIDE({e0}, 1.0 * {e1})'
8383

8484

8585
BigQuery_formatters = {

data_algebra/PostgreSQL.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ def _postgresql_as_int64(dbmodel, expression):
2727
def _postgresql_null_divide_expr(dbmodel, expression):
2828
assert len(expression.args) == 2
2929
e0 = dbmodel.expr_to_sql(expression.args[0], want_inline_parens=True)
30-
e1 = dbmodel.expr_to_sql(expression.args[1], want_inline_parens=False)
31-
return f'{e0} / NULLIF({e1}, 0)'
30+
e1 = dbmodel.expr_to_sql(expression.args[1], want_inline_parens=True)
31+
return f'({e0} / NULLIF(1.0 * {e1}, 0))'
3232

3333

3434
# map from op-name to special SQL formatting code

data_algebra/db_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,9 @@ def _db_float_divide_expr(dbmodel, expression):
439439
# don't issue an error
440440
# https://cloud.google.com/bigquery/docs/reference/standard-sql/mathematical_functions#ieee_divide
441441
assert len(expression.args) == 2
442-
e0 = dbmodel.expr_to_sql(expression.args[0], want_inline_parens=False)
443-
e1 = dbmodel.expr_to_sql(expression.args[1], want_inline_parens=False)
444-
return f'({e0}) / (1.0 * ({e1}))'
442+
e0 = dbmodel.expr_to_sql(expression.args[0], want_inline_parens=True)
443+
e1 = dbmodel.expr_to_sql(expression.args[1], want_inline_parens=True)
444+
return f'({e0} / (1.0 * {e1}))'
445445

446446

447447
def _db_nunique_expr(dbmodel, expression):
-5 Bytes
Binary file not shown.

dist/data_algebra-1.3.1.tar.gz

-28 Bytes
Binary file not shown.

docs/data_algebra/BigQuery.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ <h1 class="modulename">
174174
<span class="c1"># https://cloud.google.com/bigquery/docs/reference/standard-sql/mathematical_functions#ieee_divide</span>
175175
<span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span>
176176
<span class="n">e0</span> <span class="o">=</span> <span class="n">dbmodel</span><span class="o">.</span><span class="n">expr_to_sql</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">want_inline_parens</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
177-
<span class="n">e1</span> <span class="o">=</span> <span class="n">dbmodel</span><span class="o">.</span><span class="n">expr_to_sql</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">want_inline_parens</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
178-
<span class="k">return</span> <span class="sa">f</span><span class="s1">&#39;IEEE_DIVIDE(</span><span class="si">{</span><span class="n">e0</span><span class="si">}</span><span class="s1">, 1.0 * (</span><span class="si">{</span><span class="n">e1</span><span class="si">}</span><span class="s1">))&#39;</span>
177+
<span class="n">e1</span> <span class="o">=</span> <span class="n">dbmodel</span><span class="o">.</span><span class="n">expr_to_sql</span><span class="p">(</span><span class="n">expression</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">want_inline_parens</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
178+
<span class="k">return</span> <span class="sa">f</span><span class="s1">&#39;IEEE_DIVIDE(</span><span class="si">{</span><span class="n">e0</span><span class="si">}</span><span class="s1">, 1.0 * </span><span class="si">{</span><span class="n">e1</span><span class="si">}</span><span class="s1">)&#39;</span>
179179

180180

181181
<span class="n">BigQuery_formatters</span> <span class="o">=</span> <span class="p">{</span>

0 commit comments

Comments
 (0)