From 004f7853b28ba1a6eef3f7224828cfeb136157d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rian=20=C3=93=20Cuinneag=C3=A1in?= Date: Tue, 16 Jan 2024 19:53:11 +0000 Subject: [PATCH] Refactor q code. pflake8 fixes. --- src/pykx/pandas_api/pandas_indexing.py | 14 +++++++---- src/pykx/pandas_api/pandas_meta.py | 34 ++++++++++++-------------- tests/test_pandas_api.py | 6 ++--- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/pykx/pandas_api/pandas_indexing.py b/src/pykx/pandas_api/pandas_indexing.py index 1e96f0c..e3dfeb0 100644 --- a/src/pykx/pandas_api/pandas_indexing.py +++ b/src/pykx/pandas_api/pandas_indexing.py @@ -453,12 +453,14 @@ def rename(self, labels=None, index=None, columns=None, axis=0, t = _rename_columns(t, columns) return t - + def add_suffix(self, suffix, axis=0): t = self if axis == 1: - c_str = 'cols value' if "Keyed" in str(type(t)) else 'cols' - t = q(f'{{(c!`$string[c:{c_str} y],\:string x)xcol y}}', suffix, t) + t = q('''{[s;t] + c:$[99h~type t;cols value@;cols] t; + (c!`$string[c],\\:string s) xcol t + }''', suffix, t) elif axis == 0: raise ValueError('nyi') else: @@ -468,8 +470,10 @@ def add_suffix(self, suffix, axis=0): def add_prefix(self, prefix, axis=0): t = self if axis == 1: - c_str = 'cols value' if "Keyed" in str(type(t)) else 'cols' - t = q(f'{{(c!`$string[x],/:string c:{c_str} y)xcol y}}', prefix, t) + t = q('''{[s;t] + c:$[99h~type t;cols value@;cols] t; + (c!`$string[s],/:string[c]) xcol t + }''', prefix, t) elif axis == 0: raise ValueError('nyi') else: diff --git a/src/pykx/pandas_api/pandas_meta.py b/src/pykx/pandas_api/pandas_meta.py index 95cec79..64a9af0 100644 --- a/src/pykx/pandas_api/pandas_meta.py +++ b/src/pykx/pandas_api/pandas_meta.py @@ -162,21 +162,19 @@ def std(self, axis: int = 0, ddof: int = 1, numeric_only: bool = False): if numeric_only: tab = _get_numeric_only_subtable(tab) - key_str = '' if axis == 0 else '`$string ' - val_str = '' if axis == 0 else '"f"$value ' - query_str = 'cols[tab]' if axis == 0 else 'til[count[tab]]' - where_str = ' where not (::)~/:r[;1]' - x_dev_str = f'{{avg sqrt (sum xexp[x-avg x;2]) % count[x]-{ddof}}}' - dev_str = 'dev' if ddof == 0 else 'sdev' if ddof == 1 else x_dev_str + axis_keys = q('{[axis;tab] $[0~axis;cols;`$string til count @] tab}', axis, tab) if ddof == len(tab): - return q(f'{{[tab]{query_str}!count[{query_str}]#0n}}', tab) + return q('{x!count[x]#0n}', axis_keys) return q( - '{[tab]' - f'r:{{[tab; x] ({key_str}x; {dev_str} {val_str}tab[x])}}[tab;] each {query_str};' - f'(,/) {{(enlist x 0)!(enlist x 1)}} each r{where_str}}}', - tab + '''{[tab;axis;ddof;axis_keys] + tab:$[0~axis;(::);flip] value flip tab; + d:$[0~ddof;dev; + 1~ddof;sdev; + {[ddof;x] avg sqrt (sum xexp[x-avg x;2]) % count[x]-ddof}ddof]; + axis_keys!d each tab + }''', tab, axis, ddof, axis_keys ) @api_return @@ -274,13 +272,11 @@ def prod(self, axis=0, skipna=True, numeric_only=False, min_count=0): def skew(self, axis=0, skipna=True, numeric_only=False): res, cols = preparse_computations(self, axis, skipna, numeric_only) return (q( - '{[row]' - # adjusted Fisher-Pearson standardized moment - 'm:{(sum (x - avg x) xexp y) % count x};' - 'g1:{[m;x]m:m[x]; m[3] % m[2] xexp 3%2}[m];' - '{[g1;x]g1[x] * sqrt[n * n-1] % neg[2] + n:count x}[g1] each row}', - res - ), cols) + '''{[row] + m:{(sum (x - avg x) xexp y) % count x}; + g1:{[m;x]m:m[x]; m[3] % m[2] xexp 3%2}[m]; + (g1 each row) * {sqrt[n * n-1] % neg[2] + n:count x} each row + }''', res), cols) @convert_result def sum(self, axis=0, skipna=True, numeric_only=False, min_count=0): @@ -352,4 +348,4 @@ def agg(self, func, axis=0, *args, **kwargs): # noqa: C901 @convert_result def count(self, axis=0, numeric_only=False): res, cols = preparse_computations(self, axis, True, numeric_only) - return (q('count each', res), cols) \ No newline at end of file + return (q('count each', res), cols) diff --git a/tests/test_pandas_api.py b/tests/test_pandas_api.py index ae65874..5bde38e 100644 --- a/tests/test_pandas_api.py +++ b/tests/test_pandas_api.py @@ -2037,7 +2037,7 @@ def test_pandas_count(q): qcount = tab.count(axis=1).py() pcount = df.count(axis=1) - + assert int(qcount[0]) == int(pcount[0]) assert int(qcount[1]) == 1 @@ -2068,7 +2068,7 @@ def test_df_add_prefix(kx, q): with pytest.raises(ValueError) as err: t.add_prefix("col_", axis=0) assert 'nyi' in str(err) - + with pytest.raises(ValueError) as err: t.add_prefix("col_", axis=3) assert 'No axis named 3' in str(err) @@ -2089,7 +2089,7 @@ def test_df_add_suffix(kx, q): with pytest.raises(ValueError) as err: t.add_suffix("_col", axis=0) assert 'nyi' in str(err) - + with pytest.raises(ValueError) as err: t.add_suffix("_col", axis=3) assert 'No axis named 3' in str(err)