From 8f1464f40a95d8db0b94c0326c671eb5ac647799 Mon Sep 17 00:00:00 2001 From: cperezln Date: Fri, 5 Jan 2024 11:24:17 +0100 Subject: [PATCH 01/14] Isin first approach done. Still testing needed. --- src/pykx/pandas_api/pandas_meta.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/pykx/pandas_api/pandas_meta.py b/src/pykx/pandas_api/pandas_meta.py index 39668d5..5cde00d 100644 --- a/src/pykx/pandas_api/pandas_meta.py +++ b/src/pykx/pandas_api/pandas_meta.py @@ -1,7 +1,6 @@ from . import api_return from ..exceptions import QError - def _init(_q): global q q = _q @@ -209,7 +208,33 @@ def abs(self, numeric_only=False): if numeric_only: tab = _get_numeric_only_subtable(self) return q.abs(tab) - + + @api_return + def isin(self, values): + tab = self + # Function to check wether the elements of a list are in another list and what places + q('f: {[val; col] ' + 'fval: $[10h = type[val]; enlist[val]; val];' + '1h$(+/){' + '$[abs[type x] = abs[type y]; ' + 'x = y; ' + 'type[y] = 0h; ' + '{[z; p]$[type[z] = type[p]; z ~ p; 0b]}[x;] peach y;' + 'count[y]#0b]}[;col] each fval}') + q('g: {[tab; values] ' + 'table_dict: flip tab;' + 'fvalues: $[98h = type values; flip values; values];' + 'submatrix: (,/) {[k; v; d]enlist[k]!enlist f[raze enlist[v[k]]; d[k]]}[;fvalues; table_dict] peach key fvalues; ' + 'non_selected: cols[tab] except key fvalues;' + 'non_selected_fills:(,/) {[k; d]enlist[k]!enlist count[d[k]]#0b}[;table_dict] peach non_selected;' + 'cols[tab] xcols flip submatrix,non_selected_fills}') + if 'Table' in str(type(values)) or 'DataFrame' in str(type(values)): + return q('g', tab, values) + elif 'dict' in str(type(values)) or 'Dictionary' in str(type(values)): + return q('g', tab, values) + elif 'list' in str(type(values)) or 'List' in str(type(values)) or 'Vector' in str(type(values)): + return q("{[tab; values]flip (cols tab)!f[values; ] each value flip tab}", tab, values) + @convert_result def all(self, axis=0, bool_only=False, skipna=True): res, cols = preparse_computations(self, axis, skipna, bool_only=bool_only) From a95a3018faddf6323f0ec85341942bc78995ce33 Mon Sep 17 00:00:00 2001 From: marcosvm13 <61940472+marcosvm13@users.noreply.github.com> Date: Wed, 17 Jan 2024 12:30:40 +0100 Subject: [PATCH 02/14] Update pandas_meta.py --- src/pykx/pandas_api/pandas_meta.py | 33 ++++++++++-------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/pykx/pandas_api/pandas_meta.py b/src/pykx/pandas_api/pandas_meta.py index 5cde00d..7356c14 100644 --- a/src/pykx/pandas_api/pandas_meta.py +++ b/src/pykx/pandas_api/pandas_meta.py @@ -212,28 +212,17 @@ def abs(self, numeric_only=False): @api_return def isin(self, values): tab = self - # Function to check wether the elements of a list are in another list and what places - q('f: {[val; col] ' - 'fval: $[10h = type[val]; enlist[val]; val];' - '1h$(+/){' - '$[abs[type x] = abs[type y]; ' - 'x = y; ' - 'type[y] = 0h; ' - '{[z; p]$[type[z] = type[p]; z ~ p; 0b]}[x;] peach y;' - 'count[y]#0b]}[;col] each fval}') - q('g: {[tab; values] ' - 'table_dict: flip tab;' - 'fvalues: $[98h = type values; flip values; values];' - 'submatrix: (,/) {[k; v; d]enlist[k]!enlist f[raze enlist[v[k]]; d[k]]}[;fvalues; table_dict] peach key fvalues; ' - 'non_selected: cols[tab] except key fvalues;' - 'non_selected_fills:(,/) {[k; d]enlist[k]!enlist count[d[k]]#0b}[;table_dict] peach non_selected;' - 'cols[tab] xcols flip submatrix,non_selected_fills}') - if 'Table' in str(type(values)) or 'DataFrame' in str(type(values)): - return q('g', tab, values) - elif 'dict' in str(type(values)) or 'Dictionary' in str(type(values)): - return q('g', tab, values) - elif 'list' in str(type(values)) or 'List' in str(type(values)) or 'Vector' in str(type(values)): - return q("{[tab; values]flip (cols tab)!f[values; ] each value flip tab}", tab, values) + dic_value = kx.q("{$[98h = type x; flip x; x]}", values) + return q("{flip x! {" + "tipo: abs[type z[x][0]];" + "y: $[99h = type y; y[x]; y];" + "y: y[where tipo = abs type each y];" + "$[count[y] = 0; " + "count[z[x]]#0b;" + "(|/) $[tipo = 10; " + "y"+"~"+"/:\:z[x];" + "y"+"="+"\:z[x]]]}[;z;y]" + "each x}", tab.columns, tab, dic_value) @convert_result def all(self, axis=0, bool_only=False, skipna=True): From da1a17a8e707051fb57a4572db566b2056317f6b Mon Sep 17 00:00:00 2001 From: marcosvm13 <61940472+marcosvm13@users.noreply.github.com> Date: Thu, 18 Jan 2024 17:21:43 +0100 Subject: [PATCH 03/14] Update Christian suggestions --- src/pykx/pandas_api/pandas_meta.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/pykx/pandas_api/pandas_meta.py b/src/pykx/pandas_api/pandas_meta.py index 7356c14..fb1d1e8 100644 --- a/src/pykx/pandas_api/pandas_meta.py +++ b/src/pykx/pandas_api/pandas_meta.py @@ -212,17 +212,18 @@ def abs(self, numeric_only=False): @api_return def isin(self, values): tab = self - dic_value = kx.q("{$[98h = type x; flip x; x]}", values) - return q("{flip x! {" - "tipo: abs[type z[x][0]];" - "y: $[99h = type y; y[x]; y];" - "y: y[where tipo = abs type each y];" - "$[count[y] = 0; " - "count[z[x]]#0b;" - "(|/) $[tipo = 10; " - "y"+"~"+"/:\:z[x];" - "y"+"="+"\:z[x]]]}[;z;y]" - "each x}", tab.columns, tab, dic_value) + dic_values = kx.q("{$[98h = type x; flip x; x]}", values) + return kx.q("{flip x! {" + "col: y x;" + "ltype: abs[type col 0];" + "z: $[99h = type z; z x; z];" + "z@:where ltype = abs type each z;" + "$[0 = count z; " + "count[col]#0b;" + "any $[ltype = 10; " + "z~/:\:col;" + "z=\:col]]}[;y;z]" + "each x}", tab.columns, tab, dic_values) @convert_result def all(self, axis=0, bool_only=False, skipna=True): From 1e6370c99638701325c23a44877b2bf0090664d4 Mon Sep 17 00:00:00 2001 From: cperezln Date: Fri, 19 Jan 2024 08:59:34 +0100 Subject: [PATCH 04/14] Linting erros corrected --- src/pykx/pandas_api/pandas_meta.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/pykx/pandas_api/pandas_meta.py b/src/pykx/pandas_api/pandas_meta.py index fb1d1e8..c8a6240 100644 --- a/src/pykx/pandas_api/pandas_meta.py +++ b/src/pykx/pandas_api/pandas_meta.py @@ -1,6 +1,7 @@ from . import api_return from ..exceptions import QError + def _init(_q): global q q = _q @@ -208,23 +209,23 @@ def abs(self, numeric_only=False): if numeric_only: tab = _get_numeric_only_subtable(self) return q.abs(tab) - + @api_return def isin(self, values): tab = self - dic_values = kx.q("{$[98h = type x; flip x; x]}", values) - return kx.q("{flip x! {" - "col: y x;" - "ltype: abs[type col 0];" - "z: $[99h = type z; z x; z];" - "z@:where ltype = abs type each z;" - "$[0 = count z; " - "count[col]#0b;" - "any $[ltype = 10; " - "z~/:\:col;" - "z=\:col]]}[;y;z]" - "each x}", tab.columns, tab, dic_values) - + dic_values = q("{$[98h = type x; flip x; x]}", values) + return q("{flip x! {" + "col: y x;" + "ltype: abs[type col 0];" + "z: $[99h = type z; z x; z];" + "z@:where ltype = abs type each z;" + "$[0 = count z; " + "count[col]#0b;" + "any $[ltype = 10; " + r'z~/:\:col;' + r'z=\:col]]}[;y;z]' + "each x}", tab.columns, tab, dic_values) + @convert_result def all(self, axis=0, bool_only=False, skipna=True): res, cols = preparse_computations(self, axis, skipna, bool_only=bool_only) From 5f4aa5f54340b91a4eb770755ee3625c8fa5ebcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20V=C3=A1zquez?= Date: Mon, 22 Jan 2024 00:29:40 +0100 Subject: [PATCH 05/14] Add isin code for keyed table --- src/pykx/pandas_api/pandas_meta.py | 58 +++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/src/pykx/pandas_api/pandas_meta.py b/src/pykx/pandas_api/pandas_meta.py index c8a6240..c69a223 100644 --- a/src/pykx/pandas_api/pandas_meta.py +++ b/src/pykx/pandas_api/pandas_meta.py @@ -213,18 +213,52 @@ def abs(self, numeric_only=False): @api_return def isin(self, values): tab = self - dic_values = q("{$[98h = type x; flip x; x]}", values) - return q("{flip x! {" - "col: y x;" - "ltype: abs[type col 0];" - "z: $[99h = type z; z x; z];" - "z@:where ltype = abs type each z;" - "$[0 = count z; " - "count[col]#0b;" - "any $[ltype = 10; " - r'z~/:\:col;' - r'z=\:col]]}[;y;z]' - "each x}", tab.columns, tab, dic_values) + key_table = 'KeyedTable' in str(type(tab)) + key_value = 'KeyedTable' in str(type(values)) + n_rows = 0 + + if key_value and not key_table: + return q("""{u:(cols x); + v:(count[u],count[x])#0b; + flip u!v}""", tab) + + if key_value and key_table: + n_rows, tab, values, kcols = q("""{t:max 0, count[x] - + count u:(key y) inter key x; + (t;x value each u; value y; + key x)}""", tab, values) + + dic_value, is_tab = q("""{$[98h = type x; + (flip $[y;value x;x]; 1b); + (x; 0b)]}""", values, key_value) + + if key_table and not key_value and is_tab: + return q("""{u:(cols value x); + v:(count[u],count[x])#0b; + (cols kcol) xkey flip (kcol:flip key x),u!v}""", tab) + + ftable = q("""{ [table; values; is_tab; n_rows; key_table] + table: $[key_table;value table;table]; + flip (cols table)! + {[col_name;tab;values;v_is_tab; n_rows] + col: tab col_name; + ltype: .Q.ty col; + values: $[99h~type values; values col_name; values]; + $[v_is_tab or ltype=" ";; + values@:where (lower ltype) = .Q.t abs type each values]; + $[0 = count values; + (n_rows + count[col])#0b; + $[v_is_tab; + $[any ltype = (" ";"C");~';=] + [mlen#col;mlen#values], + (n_rows + max 0,count[col]- + mlen: min count[values], + count[col])#0b; + any $[any ltype = (" ";"C");~/:\:;=\:][values;col] + ] + ]}[;table;values;is_tab; n_rows] + each cols table}""", tab, dic_value, is_tab, n_rows, key_table) + return ftable.set_index(kcols if key_value else q.key(tab)) if key_table else ftable @convert_result def all(self, axis=0, bool_only=False, skipna=True): From 29a720783d47e6741d694d3cff0f050cea8e6828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20V=C3=A1zquez?= Date: Mon, 22 Jan 2024 03:49:33 +0100 Subject: [PATCH 06/14] Add isin code for keyed table --- src/pykx/pandas_api/pandas_meta.py | 79 +++++++++++++++--------------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/src/pykx/pandas_api/pandas_meta.py b/src/pykx/pandas_api/pandas_meta.py index c69a223..53d78a0 100644 --- a/src/pykx/pandas_api/pandas_meta.py +++ b/src/pykx/pandas_api/pandas_meta.py @@ -216,49 +216,48 @@ def isin(self, values): key_table = 'KeyedTable' in str(type(tab)) key_value = 'KeyedTable' in str(type(values)) n_rows = 0 - + false_dataframe_f = q("""{u:(cols x); + v:(count[u],count[x])#0b; + flip u!v}""") if key_value and not key_table: - return q("""{u:(cols x); - v:(count[u],count[x])#0b; - flip u!v}""", tab) - - if key_value and key_table: - n_rows, tab, values, kcols = q("""{t:max 0, count[x] - - count u:(key y) inter key x; - (t;x value each u; value y; - key x)}""", tab, values) - + return false_dataframe_f(tab) + if key_table: + kcols = q.key(tab) + if key_value: + n_rows, tab = q("""{n_rows:max 0, count[x]- + count rows:(key y) inter key x; + (n_rows; + x each rows)}""", tab, values) + values = q.value(values) + else: + tab = q.value(tab) dic_value, is_tab = q("""{$[98h = type x; - (flip $[y;value x;x]; 1b); - (x; 0b)]}""", values, key_value) - + (flip x; 1b); + (x; 0b)]}""", values) if key_table and not key_value and is_tab: - return q("""{u:(cols value x); - v:(count[u],count[x])#0b; - (cols kcol) xkey flip (kcol:flip key x),u!v}""", tab) - - ftable = q("""{ [table; values; is_tab; n_rows; key_table] - table: $[key_table;value table;table]; - flip (cols table)! - {[col_name;tab;values;v_is_tab; n_rows] - col: tab col_name; - ltype: .Q.ty col; - values: $[99h~type values; values col_name; values]; - $[v_is_tab or ltype=" ";; - values@:where (lower ltype) = .Q.t abs type each values]; - $[0 = count values; - (n_rows + count[col])#0b; - $[v_is_tab; - $[any ltype = (" ";"C");~';=] - [mlen#col;mlen#values], - (n_rows + max 0,count[col]- - mlen: min count[values], - count[col])#0b; - any $[any ltype = (" ";"C");~/:\:;=\:][values;col] - ] - ]}[;table;values;is_tab; n_rows] - each cols table}""", tab, dic_value, is_tab, n_rows, key_table) - return ftable.set_index(kcols if key_value else q.key(tab)) if key_table else ftable + ftable = false_dataframe_f(tab) + else: + ftable = q("""{ [table; values; is_tab; n_rows] + flip (cols table)! + {[col_name;tab;values;v_is_tab; n_rows] + col: tab col_name; + ltype: .Q.ty col; + values: $[99h~type values; values col_name; values]; + $[v_is_tab or ltype=" ";; + values@:where (lower ltype) = .Q.t abs type each values]; + $[0 = count values; + (n_rows + count[col])#0b; + $[v_is_tab; + $[any ltype = (" ";"C");~';=] + [mlen#col;mlen#values], + (n_rows + max 0,count[col]- + mlen: min count[values], + count[col])#0b; + any $[any ltype = (" ";"C");~/:\:;=\:][values;col] + ] + ]}[;table;values;is_tab; n_rows] + each cols table}""", tab, dic_value, is_tab, n_rows) + return ftable.set_index(kcols) if key_table else ftable @convert_result def all(self, axis=0, bool_only=False, skipna=True): From 0b86ee718f89fd28c72affd2e387e25c756a8979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20V=C3=A1zquez?= Date: Mon, 22 Jan 2024 15:18:10 +0100 Subject: [PATCH 07/14] Add test and pandas API --- tests/test_pandas_api.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_pandas_api.py b/tests/test_pandas_api.py index acfe55f..cf4a126 100644 --- a/tests/test_pandas_api.py +++ b/tests/test_pandas_api.py @@ -2029,3 +2029,27 @@ def test_keyed_loc_fixes(q): mkt[['k1', 'y']] with pytest.raises(KeyError): mkt['k1'] + +def test_pandas_isin(kx): + tab = kx.q("""([] k1: 0n 1. 0n 2. 0n; + k2: ("A";" ";"B";" ";"A"); + k3: (`a;1.;`c;5;`d))""") + keyed_tab = kx.q("""([`a`b`c`d`e] + k1: 0n 1. 0n 2. 0n; + k2: ("A";" ";"B";" ";"A"); + k3: (`a;1.;`c;5;`d))""") + + list_value = kx.q('(`a;1.;"A")') + dict_value = {"k1": [1., 2., 3.]} + tab_value = kx.q('([] k1: 1. 2. 3.; k2: ("A";"B";"C"))') + keyed_tab_value = kx.q('([`a`b] k1: 1. 2.; k2: ("A";"B"))') + + assert tab.isin(list_value).pd().equals(tab.pd().isin(list_value.py())) + assert tab.isin(dict_value).pd().equals(tab.pd().isin(dict_value)) + assert tab.isin(tab_value).pd().equals(tab.pd().isin(tab_value.pd())) + assert tab.isin(keyed_tab_value).pd().equals(tab.pd().isin(keyed_tab_value)) + assert keyed_tab.isin(list_value).pd().equals(keyed_tab.pd().isin(list_value.py())) + assert keyed_tab.isin(dict_value).pd().equals(keyed_tab.pd().isin(dict_value)) + assert keyed_tab.isin(keyed_tab_value).pd().equals(keyed_tab.pd().isin(keyed_tab_value.pd())) + assert keyed_tab.isin(tab_value).pd().equals(keyed_tab.pd().isin(tab_value)) + \ No newline at end of file From 6653b40369e9f6315014a0b21a151da26201cbf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20V=C3=A1zquez?= Date: Mon, 22 Jan 2024 15:19:18 +0100 Subject: [PATCH 08/14] Add pandas API --- docs/user-guide/advanced/Pandas_API.ipynb | 101 +++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/docs/user-guide/advanced/Pandas_API.ipynb b/docs/user-guide/advanced/Pandas_API.ipynb index 239c4c8..e99e87c 100644 --- a/docs/user-guide/advanced/Pandas_API.ipynb +++ b/docs/user-guide/advanced/Pandas_API.ipynb @@ -3014,6 +3014,105 @@ "\n", "Example Table." ] + }, + { + "cell_type": "markdown", + "id": "7f08eb84", + "metadata": {}, + "source": [ + "## Comparison\n", + "\n", + "### Table.isin()\n", + "\n", + "```\n", + "Table.apply(\n", + " values\n", + ")\n", + "```\n", + "\n", + "Whether each element in the DataFrame is contained in values.\n", + "\n", + "**Parameters:**\n", + "\n", + "| Name | Type | Description | Default |\n", + "| :--------------: | :---------------------------------: | :-------------------------------------------------------------------------- | :------: |\n", + "| values | Union[List, dict, Table, KeyedTable] | The result will only be true at a location if all the labels match. If values is a dict, the keys must be the column names, which must match. If values is a Table or KeyedTable, then both the index and column labels must match. | None|\n", + "\n", + "\n", + "**Returns:**\n", + "\n", + "| Type | Description |\n", + "| :-----------------------: | :---------------------------------------------- |\n", + "| Table | Boolean type Table/KeyedTable showing whether each element in the DataFrame is contained in values.|\n", + "\n", + "**Examples:**\n", + "\n", + "Example Table." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f6e453c8", + "metadata": {}, + "outputs": [], + "source": [ + "tab = kx.Table(data={'x': list(range(3)), 'y': [\"A\", \"B\", \"C\"]})" + ] + }, + { + "cell_type": "markdown", + "id": "aadd23c1", + "metadata": {}, + "source": [ + "Find if element \"A\" or \"1\" is in the table:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d41d40e0", + "metadata": {}, + "outputs": [], + "source": [ + "tab.isin([\"A\", 1])" + ] + }, + { + "cell_type": "markdown", + "id": "cff856fe", + "metadata": {}, + "source": [ + "Find if element \"A\" is in colum \"y\":" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bccf59d9", + "metadata": {}, + "outputs": [], + "source": [ + "tab.isin({\"y\": [\"A\"]})" + ] + }, + { + "cell_type": "markdown", + "id": "ed704cce", + "metadata": {}, + "source": [ + "Find if element \"A\" is in the first position of \"y\" column:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "41840cc0", + "metadata": {}, + "outputs": [], + "source": [ + "tab.isin(kx.Table(data={\"y\":[\"A\"]}))" + ] } ], "metadata": { @@ -3032,7 +3131,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.11.5" } }, "nbformat": 4, From adc1b4c5d9125be930ac7780e2b27b5631848b38 Mon Sep 17 00:00:00 2001 From: marcosvm13 <61940472+marcosvm13@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:20:14 +0100 Subject: [PATCH 09/14] Update Pandas_API notebook version --- docs/user-guide/advanced/Pandas_API.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user-guide/advanced/Pandas_API.ipynb b/docs/user-guide/advanced/Pandas_API.ipynb index e99e87c..6d84780 100644 --- a/docs/user-guide/advanced/Pandas_API.ipynb +++ b/docs/user-guide/advanced/Pandas_API.ipynb @@ -3131,7 +3131,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.8.3" } }, "nbformat": 4, From c39909c613418106fd3a633c8ee7d987fc1a44f9 Mon Sep 17 00:00:00 2001 From: marcosvm13 <61940472+marcosvm13@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:30:48 +0100 Subject: [PATCH 10/14] fix typo errors --- src/pykx/pandas_api/pandas_meta.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pykx/pandas_api/pandas_meta.py b/src/pykx/pandas_api/pandas_meta.py index 53d78a0..4e6594f 100644 --- a/src/pykx/pandas_api/pandas_meta.py +++ b/src/pykx/pandas_api/pandas_meta.py @@ -239,23 +239,23 @@ def isin(self, values): else: ftable = q("""{ [table; values; is_tab; n_rows] flip (cols table)! - {[col_name;tab;values;v_is_tab; n_rows] + {[col_name; tab; values; v_is_tab; n_rows] col: tab col_name; ltype: .Q.ty col; values: $[99h~type values; values col_name; values]; - $[v_is_tab or ltype=" ";; + $[v_is_tab or ltype=" "; ; values@:where (lower ltype) = .Q.t abs type each values]; $[0 = count values; (n_rows + count[col])#0b; $[v_is_tab; - $[any ltype = (" ";"C");~';=] + $[any ltype = (" ";"C"); ~'; =] [mlen#col;mlen#values], (n_rows + max 0,count[col]- mlen: min count[values], count[col])#0b; - any $[any ltype = (" ";"C");~/:\:;=\:][values;col] + any $[any ltype = (" ";"C"); ~/:\:; =\:][values;col] ] - ]}[;table;values;is_tab; n_rows] + ]}[; table; values; is_tab; n_rows] each cols table}""", tab, dic_value, is_tab, n_rows) return ftable.set_index(kcols) if key_table else ftable From 3d55cc4bd631a770927c2b8e68fbdff641ffe972 Mon Sep 17 00:00:00 2001 From: marcosvm13 <61940472+marcosvm13@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:44:28 +0100 Subject: [PATCH 11/14] fix linting errors in meta --- src/pykx/pandas_api/pandas_meta.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pykx/pandas_api/pandas_meta.py b/src/pykx/pandas_api/pandas_meta.py index 4e6594f..d116914 100644 --- a/src/pykx/pandas_api/pandas_meta.py +++ b/src/pykx/pandas_api/pandas_meta.py @@ -216,8 +216,8 @@ def isin(self, values): key_table = 'KeyedTable' in str(type(tab)) key_value = 'KeyedTable' in str(type(values)) n_rows = 0 - false_dataframe_f = q("""{u:(cols x); - v:(count[u],count[x])#0b; + false_dataframe_f = q("""{u:(cols x); + v:(count[u],count[x])#0b; flip u!v}""") if key_value and not key_table: return false_dataframe_f(tab) @@ -225,7 +225,7 @@ def isin(self, values): kcols = q.key(tab) if key_value: n_rows, tab = q("""{n_rows:max 0, count[x]- - count rows:(key y) inter key x; + count rows:(key y) inter key x; (n_rows; x each rows)}""", tab, values) values = q.value(values) From f07d89313680fba0e80ac15162b1a9d454a8deb4 Mon Sep 17 00:00:00 2001 From: marcosvm13 <61940472+marcosvm13@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:45:32 +0100 Subject: [PATCH 12/14] linting test fix --- tests/test_pandas_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pandas_api.py b/tests/test_pandas_api.py index cf4a126..faa065e 100644 --- a/tests/test_pandas_api.py +++ b/tests/test_pandas_api.py @@ -2030,6 +2030,7 @@ def test_keyed_loc_fixes(q): with pytest.raises(KeyError): mkt['k1'] + def test_pandas_isin(kx): tab = kx.q("""([] k1: 0n 1. 0n 2. 0n; k2: ("A";" ";"B";" ";"A"); @@ -2052,4 +2053,3 @@ def test_pandas_isin(kx): assert keyed_tab.isin(dict_value).pd().equals(keyed_tab.pd().isin(dict_value)) assert keyed_tab.isin(keyed_tab_value).pd().equals(keyed_tab.pd().isin(keyed_tab_value.pd())) assert keyed_tab.isin(tab_value).pd().equals(keyed_tab.pd().isin(tab_value)) - \ No newline at end of file From 5c769affbc93c1361f426ec53ec24aba1e2e5cb6 Mon Sep 17 00:00:00 2001 From: marcosvm13 <61940472+marcosvm13@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:51:28 +0100 Subject: [PATCH 13/14] fix typo error in documentation --- docs/user-guide/advanced/Pandas_API.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user-guide/advanced/Pandas_API.ipynb b/docs/user-guide/advanced/Pandas_API.ipynb index 6d84780..b660aae 100644 --- a/docs/user-guide/advanced/Pandas_API.ipynb +++ b/docs/user-guide/advanced/Pandas_API.ipynb @@ -3025,7 +3025,7 @@ "### Table.isin()\n", "\n", "```\n", - "Table.apply(\n", + "Table.isin(\n", " values\n", ")\n", "```\n", From 25fc39ba11f7a6232672c4c5fc22264d12f3fe0a Mon Sep 17 00:00:00 2001 From: marcosvm13 <61940472+marcosvm13@users.noreply.github.com> Date: Mon, 22 Jan 2024 16:00:23 +0100 Subject: [PATCH 14/14] fix identation --- src/pykx/pandas_api/pandas_meta.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/pykx/pandas_api/pandas_meta.py b/src/pykx/pandas_api/pandas_meta.py index d116914..ddfaa18 100644 --- a/src/pykx/pandas_api/pandas_meta.py +++ b/src/pykx/pandas_api/pandas_meta.py @@ -225,15 +225,14 @@ def isin(self, values): kcols = q.key(tab) if key_value: n_rows, tab = q("""{n_rows:max 0, count[x]- - count rows:(key y) inter key x; - (n_rows; - x each rows)}""", tab, values) + count rows:(key y) inter key x; + (n_rows; x each rows)}""", tab, values) values = q.value(values) else: tab = q.value(tab) dic_value, is_tab = q("""{$[98h = type x; - (flip x; 1b); - (x; 0b)]}""", values) + (flip x; 1b); + (x; 0b)]}""", values) if key_table and not key_value and is_tab: ftable = false_dataframe_f(tab) else: @@ -244,18 +243,17 @@ def isin(self, values): ltype: .Q.ty col; values: $[99h~type values; values col_name; values]; $[v_is_tab or ltype=" "; ; - values@:where (lower ltype) = .Q.t abs type each values]; + values@:where (lower ltype) = .Q.t abs type each values]; $[0 = count values; - (n_rows + count[col])#0b; - $[v_is_tab; - $[any ltype = (" ";"C"); ~'; =] - [mlen#col;mlen#values], - (n_rows + max 0,count[col]- - mlen: min count[values], - count[col])#0b; - any $[any ltype = (" ";"C"); ~/:\:; =\:][values;col] - ] - ]}[; table; values; is_tab; n_rows] + (n_rows + count[col])#0b; + $[v_is_tab; + $[any ltype = (" ";"C"); ~'; =] + [mlen#col;mlen#values], + (n_rows + max 0,count[col]- + mlen: min count[values], + count[col])#0b; + any $[any ltype = (" ";"C"); ~/:\:; =\:][values;col] + ]]}[; table; values; is_tab; n_rows] each cols table}""", tab, dic_value, is_tab, n_rows) return ftable.set_index(kcols) if key_table else ftable