# Save a DataFrame preserving a column of matrices

**URL:** https://discourse.julialang.org/t/save-a-dataframe-preserving-a-column-of-matrices/133895
**Category:** General Usage
**Tags:** dataframes, io
**Created:** [November 14, 2025, 11:49pm UTC](https://discourse.julialang.org/t/save-a-dataframe-preserving-a-column-of-matrices/133895 "2025-11-14T23:49:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [November 14, 2025, 11:49pm UTC](https://discourse.julialang.org/t/save-a-dataframe-preserving-a-column-of-matrices/133895/1 "2025-11-14T23:49:34Z")

</div>

I have a DataFrame with a column of 3x3 Float32 matrices. I would like to save this to a file and later read it back, preserving this column. I tried this round trip with several tools:

```
CSV => converts to String
JSONTables => converts to Vector{Any} containing Vector{Any}
Arrow => converts to Vector{Float32}, not bad!
Avro => "ArgumentError: type does not have a definite number of fields", this seems like I am probably using it wrong.
Parquet2 => "ArgumentError: type Matrix{Float32} does not have a corresponding parquet type"

```

Is there something else I could try that knows how to preserve matrices? If not, I will probably use CSV with a transform before to unpack the matrix into columns and a corresponding re-pack on load.

---

<div class="post-metadata">

### Author: ![technocrat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/technocrat/32/220947_2.png) [@technocrat](https://discourse.julialang.org/u/technocrat)
#### Post date: [November 15, 2025, 12:23am UTC](https://discourse.julialang.org/t/save-a-dataframe-preserving-a-column-of-matrices/133895/2 "2025-11-15T00:23:55Z")

</div>

Serialize

```julia-auto
using DataFrames, Random

num_rows = 5
data = [rand(Float32, 3, 3) for _ in 1:num_rows]
df = DataFrame(matrix = data)
# Save and reload the DataFrame, preserving the type of the "matrix" column (Vector{Matrix{Float32}})
using Serialization

fname = "df_serialized.bin"

# Serialize the DataFrame to preserve column types
open(fname, "w") do io
    serialize(io, df)
end

# To read back (as an example):
df2 = open(fname, "r") do io
    deserialize(io)
end

```

df  
julia\> df  
5×1 DataFrame  
Row │ matrix  
│ Array…  
─────┼───────────────────────────────────  
1 │ Float32[0.606444 0.947109 0.4793…  
2 │ Float32[0.349336 0.322748 0.6130…  
3 │ Float32[0.595545 0.502662 0.4875…  
4 │ Float32[0.442332 0.81498 0.42264…  
5 │ Float32[0.672831 0.567095 0.7454…

```julia-auto

```

df2  
julia\> df2  
5×1 DataFrame  
Row │ matrix  
│ Array…  
─────┼───────────────────────────────────  
1 │ Float32[0.606444 0.947109 0.4793…  
2 │ Float32[0.349336 0.322748 0.6130…  
3 │ Float32[0.595545 0.502662 0.4875…  
4 │ Float32[0.442332 0.81498 0.42264…  
5 │ Float32[0.672831 0.567095 0.7454…

```julia-auto

df2

```

---

<div class="post-metadata">

### Author: ![RomeoV](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romeov/32/37687_2.png) [@RomeoV](https://discourse.julialang.org/u/RomeoV)
#### Post date: [November 15, 2025, 7:18am UTC](https://discourse.julialang.org/t/save-a-dataframe-preserving-a-column-of-matrices/133895/3 "2025-11-15T07:18:04Z")

</div>

Perhaps check HDF5.jl

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [November 15, 2025, 10:16am UTC](https://discourse.julialang.org/t/save-a-dataframe-preserving-a-column-of-matrices/133895/4 "2025-11-15T10:16:53Z")

</div>

Did you try JLD2.jl ?

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [November 17, 2025, 4:59pm UTC](https://discourse.julialang.org/t/save-a-dataframe-preserving-a-column-of-matrices/133895/5 "2025-11-17T16:59:02Z")

</div>

JLD2 seems like great choice, I’m going to use that. It uses HDF5 underneath so there is hope of interoperability and is very easy to use.
