Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor q code. pflake8 fixes. #31

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/pykx/pandas_api/pandas_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

elif axis == 0:
raise ValueError('nyi')
else:
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not string c to keep it consistent with add_suffix? I ask it since we want to get used to your coding style.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree - I missed that

}''', prefix, t)
elif axis == 0:
raise ValueError('nyi')
else:
Expand Down
34 changes: 15 additions & 19 deletions src/pykx/pandas_api/pandas_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

)

@api_return
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


@convert_result
def sum(self, axis=0, skipna=True, numeric_only=False, min_count=0):
Expand Down Expand Up @@ -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)
return (q('count each', res), cols)
6 changes: 3 additions & 3 deletions tests/test_pandas_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down