From fd68c73083f86dda8ee7bebffca142160ee11b0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20V=C3=A1zquez?= Date: Thu, 14 Dec 2023 12:11:57 +0100 Subject: [PATCH] Lint and Fix Add_Pre_Suf --- src/pykx/pandas_api/pandas_indexing.py | 49 ++++++-------------------- src/pykx/pandas_api/pandas_meta.py | 11 +++--- tests/test_pandas_api.py | 25 +++++-------- 3 files changed, 25 insertions(+), 60 deletions(-) diff --git a/src/pykx/pandas_api/pandas_indexing.py b/src/pykx/pandas_api/pandas_indexing.py index a310efd..436875d 100644 --- a/src/pykx/pandas_api/pandas_indexing.py +++ b/src/pykx/pandas_api/pandas_indexing.py @@ -328,32 +328,6 @@ def _rename_columns(tab, labels): tab, labels) # noqa else: return q('{c:cols x; c:@[c;c?key y;y]; c xcol x}', tab, labels) - - -def _pre_suf_fix_columns(tab, fix, suf= True): - if "Keyed" in str(type(tab)): - f = ("c: `$ (string c) ,\: string y;" if suf - else "c: `$(string y) ,/: string c;") - return q("{c:cols value x;" - + f - + "key[x]!c xcol value x}", - tab, fix) # noqa - else: - f = ("c: `$(string c) ,\: string y;" if suf - else "c: `$ (string y) ,/: string c;") - return q('{c:cols x;' + f + 'c xcol x}', tab, fix) - - -def _pre_suf_fix_index(tab, fix, suf= True): - if "Keyed" in str(type(tab)): - f = ("idx: `$(string idx) ,\: string y;" if suf - else " idx: `$(string y) ,/: string idx;" ) - return q("{idx:first flip key x;" - + f - + "([] idx)!value x}", - tab, fix) # noqa - else: - return ValueError('nyi') class PandasIndexing: @@ -480,30 +454,27 @@ def rename(self, labels=None, index=None, columns=None, axis=0, return t - def add_suffix(self, suffix=None, axis=0): + def add_suffix(self, suffix, axis=0): t = self if suffix: if axis == 0: - t = _pre_suf_fix_columns(t, suffix, suf=True) + raise ValueError('nyi') elif axis == 1: - t = _pre_suf_fix_index(t, suffix, suf=True) + c_str = 'cols value' if "Keyed" in str(type(t)) else 'cols' + return q(f'{{(c!`$string[c:{c_str} y],\:string x)xcol y}}', suffix, t) else: raise ValueError(f'No axis named {axis}') - else: - raise ValueError("missing 1 required positional argument: 'suffix'") - return t - - def add_prefix(self, prefix=None, axis=0): + + def add_prefix(self, prefix, axis=0): t = self if prefix: if axis == 0: - t = _pre_suf_fix_columns(t, prefix, suf=False) + raise ValueError('nyi') elif axis == 1: - t = _pre_suf_fix_index(t, prefix, suf=False) + c_str = 'cols value' if "Keyed" in str(type(t)) else 'cols' + return q(f'{{(c!`$string[x],/:string c:{c_str} y)xcol y}}', prefix, t) else: raise ValueError(f'No axis named {axis}') - else: - raise ValueError("missing 1 required positional argument: 'prefix'") return t def sample(self, n=None, frac=None, replace=False, weights=None, @@ -618,7 +589,7 @@ def __init__(self, tab): def __getitem__(self, loc): if not isinstance(loc, tuple) or len(loc) != 2: raise ValueError('Expected 2 values for call to Table.at[]') - if q('{y in keys x(string y)}', self.tab, loc[1]): + if q('{y in keys x}', self.tab, loc[1]): raise QError('Can\'t get the value of a key in a KeyedTable using at.') return q('{x[y][z]}', self.tab, loc[0], loc[1]) diff --git a/src/pykx/pandas_api/pandas_meta.py b/src/pykx/pandas_api/pandas_meta.py index 819fc54..0aa2fad 100644 --- a/src/pykx/pandas_api/pandas_meta.py +++ b/src/pykx/pandas_api/pandas_meta.py @@ -295,9 +295,10 @@ def skew(self, axis=0, skipna=True, numeric_only=False): res, cols = preparse_computations(self, axis, skipna, numeric_only) return (q( '{[row]' - 'm:{(sum(y-avg y)xexp x)%count y};' - 'u:{sqrt[n*n-1]%neg[2]+n:count x};' - '{[u;m;x](u[x]*m[3][x]%(m[2][x]xexp 3%2))}[u;m]each 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) @@ -370,5 +371,5 @@ 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('{[row] count each row}',res), cols) \ No newline at end of file + res, cols = preparse_computations(self, axis, True, numeric_only) + return (q('{[row] count each row}', res), cols) diff --git a/tests/test_pandas_api.py b/tests/test_pandas_api.py index a5fffbe..2fb7421 100644 --- a/tests/test_pandas_api.py +++ b/tests/test_pandas_api.py @@ -1406,42 +1406,35 @@ def test_df_add_prefix(kx, q): q('sym:`aaa`bbb`ccc') t = q('([] 10?sym; til 10; 10?10; 10?1f)') - rez = t.add_prefix("col_") - + rez = t.add_prefix("col_", axis=1) + assert(q('{x~y}', rez, t.pd().add_prefix("col_"))) kt = kx.q('([idx:til 5] til 5; 5?5; 5?1f; (5;5)#100?" ")') - kt_res = kx.q('([idx: `col_0`col_1`col_2`col_3`col_4] til 5)') - - rez = kt.add_prefix("col_") - assert(q('{x~y}', rez, kt.pd().add_prefix("col_"))) rez = kt.add_prefix("col_", axis=1) - assert(q('{x~y}', kx.q("{(0!x) `idx}",rez), kx.q("{(0!x) `idx}",kt_res))) + assert(q('{x~y}', rez, kt.pd().add_prefix("col_"))) with pytest.raises(ValueError): - t.add_prefix() + t.add_prefix("col_", axis=0) + def test_df_add_suffix(kx, q): q('sym:`aaa`bbb`ccc') t = q('([] 10?sym; til 10; 10?10; 10?1f)') - rez = t.add_suffix("_col") - + rez = t.add_suffix("_col", axis=1) + assert(q('{x~y}', rez, t.pd().add_suffix("_col"))) kt = kx.q('([idx:til 5] til 5; 5?5; 5?1f; (5;5)#100?" ")') - kt_res = kx.q('([idx: `0_col`1_col`2_col`3_col`4_col] til 5)') rez = kt.add_suffix("_col", axis=1) - assert(q('{x~y}', kx.q("{(0!x) `idx}",rez), kx.q("{(0!x) `idx}",kt_res))) - - - rez = kt.add_suffix("_col") assert(q('{x~y}', rez, kt.pd().add_suffix("_col"))) with pytest.raises(ValueError): - t.add_suffix() + t.add_suffix("_col", axis=0) + @pytest.mark.pandas_api @pytest.mark.xfail(reason='Flaky randomization')