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

Add count to pandas API #2

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 35 additions & 0 deletions docs/user-guide/advanced/Pandas_API.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2450,6 +2450,41 @@
"tab.sum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Table.count()\n",
"\n",
"```\n",
"Table.count(axis=0, numeric_only=False)\n",
"```\n",
"\n",
"Returns the ount non-NA values across the given axis.\n",
"\n",
"**Parameters:**\n",
"\n",
"| Name | Type | Description | Default |\n",
"| :----------: | :--: | :------------------------------------------------------------------------------- | :-----: |\n",
"| axis | int | The axis to calculate the product across 0 is columns, 1 is rows. | 0 |\n",
"| numeric_only | bool | Only use columns of the table that are of a numeric data type. | False |\n",
"\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 `count` on that column / row. |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tab.count()"
]
},
{
"cell_type": "markdown",
"id": "621766f6",
Expand Down
5 changes: 5 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,8 @@ def sum(self, axis=0, skipna=True, numeric_only=False, min_count=0):
res,
min_count
), cols)

@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)
26 changes: 26 additions & 0 deletions tests/test_pandas_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2029,3 +2029,29 @@ def test_keyed_loc_fixes(q):
mkt[['k1', 'y']]
with pytest.raises(KeyError):
mkt['k1']

def test_pandas_count(q):
tab = q('([] k1: 0n 2 0n 2 0n ; k2: (`a;`;`b;`;`c))')
df = tab.pd()

# Assert axis = 1
qcount = tab.count(axis=1).py()
pcount = df.count(axis=1)

print(pcount)
assert int(qcount[0]) == int(pcount[0])
assert int(qcount[1]) == 1

# Assert axis = 0
qcount = tab.count().py()
pcount = df.count()

assert int(qcount["k1"]) == int(pcount["k1"])
assert int(qcount["k2"]) == 3

# Assert only numeric
qcount = tab.count(numeric_only = True).py()
pcount = df.count(numeric_only = True)

assert int(qcount["k1"]) == int(pcount["k1"])