Table Of Contents

1.2.2. Using Pandas DataFrames

[1]:
import quasardb
import quasardb.pandas as qdbpd
import pandas as pd
import datetime as dt
import numpy as np
import matplotlib.pyplot as plt
sample_size=10
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
print(quasardb.version())
3.4.0
[2]:
c = quasardb.Cluster("qdb://qdb-server:2836")

Create dataframe with two columns

[3]:
#create timestamps to associate to timeseries
timestamps = np.array('2019-03-14', dtype=np.datetime64)+ np.arange(sample_size)

# Generate random doubles of timeserie
double_values = np.random.uniform(0.0, 100.0, sample_size)
# Generate random doubles of timeserie
int_values = np.random.randint(0, 100, sample_size)

pandas_frame = pd.DataFrame (index=timestamps,data={'close':double_values,'volume':int_values})
pandas_frame[:5]
[3]:
close volume
2019-03-14 90.922967 95
2019-03-15 6.311654 44
2019-03-16 0.214237 36
2019-03-17 2.176237 90
2019-03-18 46.669062 64

No need to create table in advance. The API will take care of everything

[4]:

qdbpd.write_dataframe(pandas_frame, c, "ts_pandas_1",create=True)

Reading back the data

[5]:
table = c.ts("ts_pandas_1")
read_dataframe = qdbpd.read_dataframe(table)
assert read_dataframe['close'].size == sample_size
[6]:
read_dataframe[:5]
[6]:
close volume
2019-03-14 90.922967 95
2019-03-15 6.311654 44
2019-03-16 0.214237 36
2019-03-17 2.176237 90
2019-03-18 46.669062 64
[7]:
query= c.query("select * from ts_pandas_1")
query_result = query.run()
[8]:
tmp = dict()
for col in query_result.tables['ts_pandas_1']:
    tmp[col.name] = col.data
result_as_pandas = pd.DataFrame.from_dict(tmp, orient='columns').set_index('$timestamp')
assert result_as_pandas['close'].size == sample_size
result_as_pandas[:5]
[8]:
close volume
$timestamp
2019-03-14 90.922967 95
2019-03-15 6.311654 44
2019-03-16 0.214237 36
2019-03-17 2.176237 90
2019-03-18 46.669062 64
[9]:
plt.style.use('seaborn-whitegrid')
plt.plot(result_as_pandas.index,result_as_pandas )
plt.xticks(rotation=90)
plt.show()
../../_images/primer_notebooks_Pandas_13_0.png
[10]:
#tearing down data for this sample
table = c.ts("ts_pandas_1")
table.remove()
[ ]:

arrow_backPrevious
1.2.1. Data in rows
Next arrow_forward
1.2.3. Data in columns