You can convert an object from normal TicDat
dict-of-dict representations to a pandas.DataFrame
and back again.
This notebook does a quick demo of both, with a bit more detail on the latter.
We assume diet.py
and diet_sample_data.sql
are in this directory. These files are easy to find if you want to copy them over and reproduce this notebook.
In [1]:
from diet import input_schema
dat = input_schema.sql.create_tic_dat_from_sql("diet_sample_data.sql")
In [2]:
dat.nutrition_quantities
Out[2]:
In [3]:
pan_dat = input_schema.copy_to_pandas(dat)
In [4]:
pan_dat.nutrition_quantities
Out[4]:
In [5]:
{pan_dat.nutrition_quantities.__class__, pan_dat.categories.__class__, pan_dat.foods.__class__}
Out[5]:
Now going the other way, convering DataFrame
back into TicDat
objects. Since pan_dat
just has a collection of DataFrames
attached to it, this is easy to show.
In [6]:
dat2 = input_schema.TicDat(foods = pan_dat.foods, categories = pan_dat.categories,
nutrition_quantities = pan_dat.nutrition_quantities)
In [7]:
dat2.nutrition_quantities
Out[7]:
In [8]:
input_schema._same_data(dat, dat2)
Out[8]:
That said, if you drop the indicies then things don't work, so be sure to set DataFrame
indicies correctly.
In [9]:
df = pan_dat.nutrition_quantities.reset_index(drop=False)
df
Out[9]:
So, well this might appear to be ok, it does need the index for the current ticdat
implementation.
In [10]:
input_schema.TicDat(foods = pan_dat.foods, categories = pan_dat.categories,
nutrition_quantities = df)