Use Python Pandas DataFrames in MATLAB
Since R2024a
Pandas is a core Python library that is useful for working with tabular data in Python. The pandas library defines a class called DataFrame.
You can convert a pandas DataFrame to a MATLAB table using the
tablefunction. You can also convert a pandas DataFrame to a MATLAB timetable using thetimetablefunction.You can convert a MATLAB table or timetable to a pandas DataFrame using the
py.pandas.DataFramefunction. You can also pass MATLAB tables or timetables directly to Python functions without converting them to pandas DataFrames first.
You must have the pandas library installed to run these examples. To ensure that the pandas library is properly installed, use the following code.
try py.importlib.import_module('pandas'); disp('Python library is installed and accessible.'); catch PE disp(['Error: ', PE.message]); disp('Python library might not be installed or accessible.'); end
Python library is installed and accessible.
Convert Pandas DataFrame to MATLAB Table
Convert a pandas DataFrame to a MATLAB table using the table function.
For example, create a pandas DataFrame and convert it to a MATLAB table. In this case, MATLAB converts the pandas Series to MATLAB strings and converts the Python integer values to MATLAB integer values.
last_names = py.pandas.Series({"Sanchez","Johnson","Zhang","Diaz","Brown"},dtype="object");
age = py.numpy.random.randint(18,high=85,size=py.len(last_names));
height = py.numpy.random.randint(60,high=78,size=py.len(last_names));
pd = py.pandas.DataFrame(struct("LastName",last_names,"Age",age,"Height",height));
md = table(pd)md=5×3 table
"Sanchez" 50 64
"Johnson" 19 77
"Zhang" 62 62
"Diaz" 43 73
"Brown" 34 65
Convert Pandas DataFrame to MATLAB Timetable
If you have a pandas DataFrame that contains timestamped values, you can convert it to a MATLAB timetable using the timetable function:
If the index of the pandas DataFrame has time values (of type
datetime64,timedelta64,Timestamp, orTimedelta), then those values are used to set the row times of the MATLAB timetable.If the index does not have time values, then the row times come from the first column of the pandas DataFrame that has time values (of type
datetime64,timedelta64,Timestamp, orTimedelta).
For example, create a pandas DataFrame and convert it to a MATLAB timetable. In this case, MATLAB converts the Python datetime values to MATLAB datetime values.
date_today = py.datetime.datetime.now(); mtimes = py.pandas.date_range(date_today,periods=int8(3),freq='s'); temp = py.list({double(37.3),double(39.1),double(42.3)}); pressure = py.list({int32(30.10),int32(30.56),int32(28.90)}); wspeed = py.list({single(13.4),single(6.5),single(7.3)}); ptt = py.pandas.DataFrame(struct("MeasurementTime",mtimes, ... "Temp",temp,"Pressure",pressure,"WindSpeed",wspeed)); mtt = timetable(ptt)
mtt=3×3 timetable
04-Nov-2025 11:03:07 37.3000 30 13.4000
04-Nov-2025 11:03:08 39.1000 31 6.5000
04-Nov-2025 11:03:09 42.3000 29 7.3000
Convert MATLAB Table to Pandas DataFrame
MATLAB implicitly converts any MATLAB table passed to a Python function into a pandas DataFrame. However, if you have a MATLAB table, you can explicitly convert it to a pandas DataFrame using py.pandas.DataFrame.
For example, create a MATLAB table and convert it to a pandas DataFrame.
dish_name = ["omelette";"soup";"salad";"fries";"cookie"]; price = [8.50;5.00;7.25;1.50;2.00]; sold_out = [true;false;true;false;true]; md = table(dish_name,price,sold_out); pd = py.pandas.DataFrame(md); py.print(pd)
dish_name price sold_out 0 omelette 8.50 True 1 soup 5.00 False 2 salad 7.25 True 3 fries 1.50 False 4 cookie 2.00 True
You can also pass MATLAB tables directly to Python functions without converting them to pandas DataFrames first. For example, find the length of md using py.len.
l = py.len(md)
l =
Python int with properties:
denominator: [1×1 py.int]
imag: [1×1 py.int]
numerator: [1×1 py.int]
real: [1×1 py.int]
5
Convert MATLAB Timetable to Pandas DataFrame
You can convert MATLAB timetables to pandas DataFrames using py.pandas.DataFrame.
For example, create a MATLAB timetable and convert it to a pandas DataFrame.
mtt = timetable(datetime(["2023-12-18";"2023-12-19";"2023-12-20"]), ... [37.3;39.1;42.3],[30.1;30.03;29.9],[13.4;6.5;7.3]); mtt.Properties.VariableNames = ["Temp","Pressure","WindSpeed"]; ptt = py.pandas.DataFrame(mtt); py.print(ptt)
Temp Pressure WindSpeed Time 2023-12-18 37.3 30.10 13.4 2023-12-19 39.1 30.03 6.5 2023-12-20 42.3 29.90 7.3
Data Type Conversion from Pandas DataFrames to MATLAB Tables or Timetables
When you convert a pandas DataFrame to a MATLAB table or timetable, MATLAB automatically converts these pandas data types to MATLAB types. In this table, py. refers to built-in Python data types, np. refers to NumPy, and pd. refers to pandas.
Pandas Data Type | Converted Data Type in MATLAB |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Other type | Python object |
|
|
|
|
| <missing> |
Data Type Conversion from MATLAB Tables or Timetables to Pandas DataFrame
When you call a Python function with a MATLAB table or you explicitly convert a MATLAB table or timetable to a pandas DataFrame, MATLAB automatically converts the table data into types that best represent the data in the pandas language. In this table, py. refers to built-in Python data types, np. refers to NumPy, and pd. refers to pandas.
MATLAB Data Type | Converted Data Type in Pandas |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<missing> |
|
<undefined> ( |
|