Skip to content

Commit

Permalink
Merge branch 'feature/round_prefix_suffix_std_skew-REVIEW' of github.…
Browse files Browse the repository at this point in the history
…com:hablapps/pykx into feature/round_prefix_suffix_std_skew-REVIEW
  • Loading branch information
Jesús López-González committed Dec 18, 2023
2 parents 1944f5f + fd68c73 commit 3ffb68f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 55 deletions.
49 changes: 10 additions & 39 deletions src/pykx/pandas_api/pandas_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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])

Expand Down
25 changes: 9 additions & 16 deletions tests/test_pandas_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 3ffb68f

Please sign in to comment.