Main Content

Convert Between MATLAB tables and Pandas DataFrames

MATLAB® Compiler SDK™ supports conversion between tables and Pandas DataFrames. This process also converts data inside the DataFrame or table implicitly. DataFrames can also be converted to timetables and vice versa when the index of the DataFrame is a datetime object. To convert between other types of data in MATLAB and Python®, see Pass Data Between MATLAB and Python. For an example of conversion between MATLAB tables and Pandas DataFrames, see Use MATLAB Compiler SDK to Work with Pandas DataFrames.

Note

MATLAB Compiler SDK supports only one level of conversion with DataFrames. It does not support recursive nesting. For example, you can create a DataFrame df1 containing another DataFrame df2 and convert it to a table. However, you cannot convert df1 if df2 contains another DataFrame df3.

Convert DataFrames to Tables

The list below includes all data types that, when contained within a Pandas DataFrame, are supported for conversion to a MATLAB table.

Python Data Type

Numpy Data Type

Pandas Data Type

Resulting MATLAB Data Type

 numpy.float64pandas.Float64double
 numpy.float32pandas.Float32single
 numpy.uint8 pandas.UInt8uint8
 numpy.uint16pandas.UInt16uint16
 numpy.uint32pandas.UInt32uint32
 numpy.uint64pandas.UInt64uint64
 numpy.int8pandas.Int8int8
 numpy.int16pandas.Int16int16
 numpy.int32pandas.Int32int32
 numpy.int64pandas.Int64int64
 numpy.complex64numpy.complex128complex (single, double)
bool  logical
  pandas.Categoricalcategorical
 numpy.datetime64pandas.Timestampdatetime
 numpy.timedelta64pandas.Timedeltaduration
 

object

objectstring (if the elements of the array are strings, otherwise not supported)
str pandas.StringDtypestring

Sequence (e.g. tuple or list)

  cell
dict  struct
  pandas.DataFrametable
  pandas.DataFrame with datetime indextimetable

Convert Tables to DataFrames

The list below includes all data types that, when contained within a MATLAB table, are supported for conversion to a Pandas DataFrame.

MATLAB Data typeResulting Numpy or Pandas Data Type
doublenumpy.float64
singlenumpy.float32
uint8numpy.uint8
uint16numpy.uint16
uint32numpy.uint32
uint64numpy.uint64
int8numpy.int8
int16numpy.int16
int32numpy.int32
int64numpy.int64
complex (single) numpy.complex64,
complex(double)numpy.complex128
logicalbool
categoricalpandas.Categorical
datetimenumpy.datetime64
durationnumpy.timedelta64
stringstring, but the type for the whole Series will be object
cellstrstring, but the type for the whole Series will be object
cell

Resulting type varies depending on the elements of the cell array, but the type for the whole Series will be object

struct

dict, but the type for the whole Series will be object

dictionary

matlab.dictionary, but the type for the whole Series will be object

tablepandas.DataFrame
timetablepandas.DataFrame with datetime index

See Also

Topics