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.float64 | pandas.Float64 | double | |
numpy.float32 | pandas.Float32 | single | |
numpy.uint8
| pandas.UInt8 | uint8 | |
numpy.uint16 | pandas.UInt16 | uint16 | |
numpy.uint32 | pandas.UInt32 | uint32 | |
numpy.uint64 | pandas.UInt64 | uint64 | |
numpy.int8 | pandas.Int8 | int8 | |
numpy.int16 | pandas.Int16 | int16 | |
numpy.int32 | pandas.Int32 | int32 | |
numpy.int64 | pandas.Int64 | int64 | |
numpy.complex64 | numpy.complex128 | complex (single ,
double ) | |
bool | logical | ||
pandas.Categorical | categorical | ||
numpy.datetime64 | pandas.Timestamp | datetime | |
numpy.timedelta64 | pandas.Timedelta | duration | |
| object | string (if the elements of the array are
strings, otherwise not supported) | |
str | pandas.StringDtype | string | |
Sequence (e.g. | cell | ||
dict | struct | ||
pandas.DataFrame | table | ||
pandas.DataFrame with
datetime index | timetable |
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 type | Resulting Numpy or Pandas Data Type |
---|---|
double | numpy.float64 |
single | numpy.float32 |
uint8 | numpy.uint8 |
uint16 | numpy.uint16 |
uint32 | numpy.uint32 |
uint64 | numpy.uint64 |
int8 | numpy.int8 |
int16 | numpy.int16 |
int32 | numpy.int32 |
int64 | numpy.int64 |
complex (single ) | numpy.complex64 , |
complex(double ) | numpy.complex128 |
logical | bool |
categorical | pandas.Categorical |
datetime | numpy.datetime64 |
duration | numpy.timedelta64 |
string | string , but the type for the whole Series will
be object |
cellstr | string , but the type for the whole Series will
be object |
cell | Resulting type varies depending on the elements of the
|
struct |
|
dictionary |
|
table | pandas.DataFrame |
timetable | pandas.DataFrame with datetime index |
See Also
Topics
- Pass Data Between MATLAB and Python
- Use MATLAB Arrays in Python (MATLAB Production Server)
- matlab Python Module (MATLAB Production Server)
- Invoke Packaged MATLAB Functions (MATLAB Production Server)