# Pandas dataframe examples?

**URL:** <https://forum.tiledb.com/t/pandas-dataframe-examples/117>\
**Category:** Uncategorized\
**Created:** [October 25, 2019, 5:04pm UTC](https://forum.tiledb.com/t/pandas-dataframe-examples/117 "2019-10-25T17:04:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruce\_Martin](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/bruce_martin/32/36_2.png) [@Bruce\_Martin](https://forum.tiledb.com/u/Bruce_Martin)\
**Post date:** [October 25, 2019, 5:04pm UTC](https://forum.tiledb.com/t/pandas-dataframe-examples/117/1 "2019-10-25T17:04:01Z")

</div>

Do you have any examples/sample code that demonstrates best practices for moving data between tiledb arrays and pandas DataFrame objects? In particular, how to best accommodate categorical Series and numpy types that are not directly supported by tiledb (eg, np.bool).

---

<div class="post-metadata">

**Author:** ![Bruce\_Martin](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/bruce_martin/32/36_2.png) [@Bruce\_Martin](https://forum.tiledb.com/u/Bruce_Martin)\
**Post date:** [October 26, 2019, 12:23am UTC](https://forum.tiledb.com/t/pandas-dataframe-examples/117/2 "2019-10-26T00:23:38Z")

</div>

A few more questions related to this topic.

My goal is to move DataFrames in/out of tiledb and preserve the typing information that was in the original (source) DataFrame. Most unsupported types (eg, np.bool) are trivially converted to/from a tiledb type (np.bool to np.uint8) for writing, but reading loses this type information.

One possibility is to store this type information somewhere, and use it when constructing the DataFrame at read time (eg, using np.astype()). The recently landed array metadata looks ideal for this scenario, but is still WIP for Python API (PR #213). Suggested alternative?

---

<div class="post-metadata">

**Author:** ![qiuwei](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/qiuwei/32/80_2.png) [@qiuwei](https://forum.tiledb.com/u/qiuwei)\
**Post date:** [June 25, 2020, 9:17am UTC](https://forum.tiledb.com/t/pandas-dataframe-examples/117/3 "2020-06-25T09:17:00Z")

</div>

+1. Some example code would be very helpful for pandas users.

---

<div class="post-metadata">

**Author:** ![Jake](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@Jake](https://forum.tiledb.com/u/Jake)\
**Post date:** [September 27, 2020, 8:23am UTC](https://forum.tiledb.com/t/pandas-dataframe-examples/117/4 "2020-09-27T08:23:18Z")

</div>

For loading and parsing 1D multiattribute TileDB arrays into dataframes I’m currently using:

```auto
def tiledb_to_df(tiledb_arr, idx_col='datetime'):
    idx = tiledb_arr.pop(idx_col)
    df = pd.DataFrame(tiledb_arr, index=idx)
    
    return df

with tiledb.open(array_dir.replace('/', '\\'), 'r') as TileDB_array:
    tiledb_arr = TileDB_array[:]

df = tiledb_to_df(tiledb_arr)

```

---

<div class="post-metadata">

**Author:** ![qiuwei](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/qiuwei/32/80_2.png) [@qiuwei](https://forum.tiledb.com/u/qiuwei)\
**Post date:** [October 21, 2020, 2:35am UTC](https://forum.tiledb.com/t/pandas-dataframe-examples/117/5 "2020-10-21T02:35:47Z")

</div>

Just found some demo code from the developers:

> <https://gist.github.com/ihnorton/1544bb169b4ec8cd385e9c3e756c2bd5>
