5

I am trying to save a pandas dataframe to a matlab .mat file using scipy.io.

I have the following:

array1 = np.array([1,2,3])
array2 = np.array(['a','b','c'])
array3 = np.array([1.01,2.02,3.03])
df = DataFrame({1:array1, 2:array2,3:array3}, index=('array1','array2','array3'))
recarray_ = df.to_records()
## Produces:
# rec.array([('array1', 1, 'a', 1.01), ('array2', 2, 'b', 2.02),
#   ('array3', 3, 'c', 3.03)], 
#  dtype=[('index', 'O'), ('1', '<i4'), ('2', 'O'), ('3', '<f8')])
scipy.io.savemat('test_recarray_struct.mat', {'struct':df.to_records()})

In Matlab, I would expect this to produce a struct containing three arrays (one int, one char, one float) but it actually produces is a struct containing 3 more structs, each containing four variables; 'index', 1, '2', 3. When trying to select 1, '2' or 3 I get the error 'The variable struct(1, 1).# does not exist.'

Can anyone explain the expected behaviour and how best to save DataFrames to .mat files?

2 Answers 2

4

I am using the following workaround in the meantime. Please let me know if you have a better solution:

a_dict = {col_name : df[col_name].values for col_name in df.columns.values}

## optional if you want to save the index as an array as well:
# a_dict[df.index.name] = df.index.values
scipy.io.savemat('test_struct_to_mat.mat', {'struct':a_dict})
Sign up to request clarification or add additional context in comments.

Comments

1

I think what you need is to create the dataframe like this:

df = DataFrame({'array1':array1, 'array2':array2,'array3':array3})

and save it like this:

scipy.io.savemat('test_recarray_struct.mat', {'struct':df.to_dict("list")})

So the code should be something like:

# ... import appropritely
array1 = np.array([1,2,3])
array2 = np.array(['a','b','c'])
array3 = np.array([1.01,2.02,3.03])
df = DataFrame({'array1':array1, 'array2':array2,'array3':array3})
scipy.io.savemat('test_recarray_struct.mat', {'struct':df.to_dict("list")})

1 Comment

Hello pashute, I checked what I wrote (on two computers) and it worked for me. Is there something about the solution that doesn't work for you? Cheers, Nenad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.