exxeleron / qPython

interprocess communication between Python and kdb+
http://www.devnet.de
Apache License 2.0
151 stars 90 forks source link

Dump QTable #27

Closed shashicstl closed 9 years ago

shashicstl commented 9 years ago

Hello,

I am trying to use qpython for the first time. One of my use cases is to run a query and dump data to csv file. Is there an easy way to do so? I can write individual rows, but how do I get column names.

Many thanks.

maciejlach commented 9 years ago

The QTable class extends numpy.recarray, so you can use numpy.savetxt to save it as a CSV:

    np = q('flip `name`iq!(`Dent`Beeblebrox`Prefect;98 42 126)')
    numpy.savetxt('np.csv', np, delimiter = ',', header = ','.join(np.dtype.names), fmt = '%s')

Or if you prefer to represent tables as pandas DataFrames, use pandas.DataFrame.to_csv:

    df = q('flip `name`iq!(`Dent`Beeblebrox`Prefect;98 42 126)', pandas = True)
    df.to_csv('df.csv', sep = ',')
shashicstl commented 9 years ago

Awesome thank you. New to qpython, numpy and pandas.

I was able to test dumping to csv using pandas df. Works, however I see that timestammps have '0 days' in them, eg: 0 days 05:01:12.965000000

I'd like to get rid of '0 days' part and print up to milliseconds.

Can you point me to doc that let's me how to format etc?

maciejlach commented 9 years ago

You can refer to this stackoverflow post: http://stackoverflow.com/questions/13999850/how-to-specify-date-format-when-using-pandas-to-csv

shashicstl commented 9 years ago

Thank you.

Appreciate help.