KxSystems / pykx

PyKX is a Python first interface to the worlds fastest time-series database kdb+ and it's underlying vector programming language q.
https://code.kx.com/pykx
Other
49 stars 12 forks source link

Fixes `max`, `min`, `prod` & `sum` for keyed tables #19

Closed neutropolis closed 8 months ago

neutropolis commented 8 months ago

Bug Fix

What is the bug?

If possible please include a summary and reproducible use-case below: Several functions from the Pandas API raise a length error while working over the default axis.

>>> import pykx as kx
>>> t = kx.q('([a:1 2]b:3 4;c:5 6)')
>>> t
pykx.KeyedTable(pykx.q('
a| b c
-| ---
1| 3 5
2| 4 6
'))
>>> t.max()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/j/Github/hablapps/pykx/pykx-dev/lib/python3.9/site-packages/pykx/pandas_api/__init__.py", line 57, in return_va
l
    res = func(*args, **kwargs)
  File "/Users/j/Github/hablapps/pykx/pykx-dev/lib/python3.9/site-packages/pykx/pandas_api/pandas_meta.py", line 80, in inner
    return q('{[x; y] y!x}', res, cols)
  File "/Users/j/Github/hablapps/pykx/pykx-dev/lib/python3.9/site-packages/pykx/embedded_q.py", line 226, in __call__
    return factory(result, False)
  File "pykx/_wrappers.pyx", line 507, in pykx._wrappers._factory
  File "pykx/_wrappers.pyx", line 500, in pykx._wrappers.factory
pykx.exceptions.QError: length

How does the change fix it?

I think that the simplest fix is to reorder the calculation of the cols variable at the preparse_computations general method. By doing so, we get rid of the redundant keys and the result is consistent with @convert_result. This simple change fixes all max, min, prod and sum (and also any and all) when invoked with a keyed table and default axis.

Code

Testing