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

Added implementation of nunique function #9

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions docs/user-guide/advanced/Pandas_API.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2507,6 +2507,46 @@
"tab.prod(numeric_only=True)"
]
},
{
"cell_type": "markdown",
"source": [
"### Table.nunique()\n",
"```\n",
"Table.nunique(axis=0, skipna=True, numeric_only=False, min_count=0)\n",
"```\n",
"\n",
"Returns the number of unique elements across the given axis.\n",
"\n",
"**Parameters:**\n",
"\n",
"| Name | Type | Description | Default |\n",
"| :----------: | :--: | :------------------------------------------------------------------------------- | :-----: |\n",
"| axis | int | The axis to calculate the sum across 0 is columns, 1 is rows. | 0 |\n",
chraberturas marked this conversation as resolved.
Show resolved Hide resolved
"| dropna | bool | Don’t include NaN in the counts. | True |\n",
chraberturas marked this conversation as resolved.
Show resolved Hide resolved
"\n",
"**Returns:**\n",
"\n",
" | Type | Description |\n",
" | :----------------: | :------------------------------------------------------------------- |\n",
" | Dictionary | A dictionary where the key represent the column name / row number and the values are the result of calling `nunique` on that column / row. |"
],
"metadata": {
"collapsed": false
},
"id": "7b39a07bd7cd0af7"
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"tab.nunique()"
],
"metadata": {
"collapsed": false
},
"id": "f5592b19b69ad46d"
},
{
"cell_type": "markdown",
"id": "655c3ad2",
Expand Down
9 changes: 9 additions & 0 deletions src/pykx/pandas_api/pandas_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,12 @@ def sum(self, axis=0, skipna=True, numeric_only=False, min_count=0):
res,
min_count
), cols)

@convert_result
def nunique(self, axis=0, dropna=True):
res, cols = preparse_computations(self, axis, skipna=False)
filterNan = q('{$[11h = type x;x;'
chraberturas marked this conversation as resolved.
Show resolved Hide resolved
'0h = type x;(x where not null x except w),(w:x where 10h=type each x);'
'x where not null x]}each')
res = filterNan(res) if dropna else res
return q('(\'[count;distinct]\')', res), cols

Choose a reason for hiding this comment

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

I leave this alternative syntax in case someone finds it useful to understand Christian's incredible composition:

q("(count distinct @) each", res)

37 changes: 37 additions & 0 deletions tests/test_pandas_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2029,3 +2029,40 @@ def test_keyed_loc_fixes(q):
mkt[['k1', 'y']]
with pytest.raises(KeyError):
mkt['k1']


def test_nunique(kx, q):
df = pd.DataFrame(
{
'a': [1, 2, 2, 4],
'b': [1, 2, 6, 7],
'c': [7, 8, 9, 10],
'd': ['foo', 'baz', 'baz', 'qux']
}
)
tab = kx.toq(df)
p_m = df.nunique()
q_m = tab.nunique()
for c in q.key(q_m).py():
assert p_m[c] == q_m[c].py()
p_m = df.nunique(axis=1)
q_m = tab.nunique(axis=1)
for c in range(len(tab)):
assert p_m[c] == q_m[c].py()

tab = kx.q('([]A:4 0n 7 6;B:4 0n 0n 7;C:``foo`foo`)')
df = tab.pd()
p_m = df.nunique()
q_m = tab.nunique()
for c in q.key(q_m).py():
assert p_m[c] == q_m[c].py()
p_m = df.nunique(axis=1, dropna=False)
q_m = tab.nunique(axis=1, dropna=False)
for c in range(len(tab)):
assert p_m[c] == q_m[c].py()
p_m = df.nunique(dropna=False)
q_m = tab.nunique(dropna=False)
for c in q.key(q_m).py():
assert p_m[c] == q_m[c].py()