Skip to content

Commit

Permalink
add count implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Gómez Cofrades committed Dec 4, 2023
1 parent 7de2b7c commit db75da8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
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"])

0 comments on commit db75da8

Please sign in to comment.