KxSystems / pyq

PyQ — Python for kdb+
http://code.kx.com/q/interfaces
Apache License 2.0
190 stars 49 forks source link

Conversion of nulls between K arrays of minute/time/second/date/month type to numpy array of corresponding type #90

Closed antipisa closed 5 years ago

antipisa commented 5 years ago

Numpy supports NaT for both datetime64 as well as timedelta64, and when converting K vectors of timestamp/timespan, nulls are converted to NaT as expected

np.asarray(q('0Np,1?0Wp'))

array(['NaT', '2212-04-14T11:35:56.648210432'],ndtype='datetime64[ns]')

np.asarray(q('0Nn,1?0Wn'))

array(['NaT', 8500661630873370624], dtype='timedelta64[ns]')

but when converting K arrays of type minute, time, second, date, or month, nulls are not handled properly:

np.asarray(q('0Nd,1?0Wd'))

array(['-5877611-06-22',  '2129873-03-10'], dtype='datetime64[D]')

np.asarray(q('0Nt,1?0Wt'))

array([-9223372036855,     1357006030], dtype='timedelta64[ms]')

If I upcast before the conversion and then downcast, everything works as expected, but this seems unnecessarily hacky.

np.array(q('`timestamp$0Nd,1?0Wd'), dtype='datetime64[D]')

array(['NaT', '1707-09-22'], dtype='datetime64[D]')

np.array(q('`timespan$0Nt,1?0Wt'), dtype='timedelta64[s]')

array([  'NaT', 1775124], dtype='timedelta64[s]')

Ideally, we would have this behavior without the up/down cast.

The conversion is also problematic in the reverse. NaT from numpy arrays are incorrectly converted into K. NaT from datetime64 gets converted to 1970.01.01, while NaT from timedelta64 gets converted to 0

K(np.array(['NaT','2001-01-01'], dtype='datetime64'))

k('1970.01.01 2001.01.01')

K(np.array(['NaT',1000], dtype='timedelta64[s]'))

k('0D00:00:00.000000000 0D00:16:40.000000000')
sashkab commented 5 years ago

Thanks for your report. We will add support for handling NaTs in the future release.

antipisa commented 5 years ago

Thanks a lot. Do you have a rough idea when 4.2.0 will be released?

sashkab commented 5 years ago

This issue was addressed in the upcoming release 4.2.0.