Use MATLAB Compiler SDK to Work with Pandas DataFrames
Supported platforms: Windows®, Linux®, Mac
This example shows how to create a Python® application that passes weather data stored in a Pandas DataFrame from Python to MATLAB®. The MATLAB function finds the maximum humidity in the given weather data. The Python application creates the DataFrame, calls the MATLAB function, and displays the result.
When you pass data between MATLAB and Python, MATLAB Compiler SDK™ converts the data into an equivalent data type. For a complete list of data types and their conversions, see Pass Data Between MATLAB and Python. For a list of data types and their conversions when they are converted within tables or DataFrames, see Convert Between MATLAB tables and Pandas DataFrames.
Prerequisites
Verify that you have a version of Python installed that is compatible with MATLAB Compiler SDK. For details, see MATLAB Compiler SDK Python Target Requirements.
The target machine requires MATLAB Runtime to run the deployed MATLAB code. You can include MATLAB Runtime in an installer for your Python package or install it separately. For details, see Download and Install MATLAB Runtime.
You also need a Python environment with Pandas installed.
Create Function in MATLAB
This MATLAB function takes weather data for the past 24 hours, specified as a
pandas.DataFrame
, and returns the maximum humidity within
that time period. You can use MATLAB
Compiler SDK to package the function into a Python module. To do so, first save this function as
getMaxHumidity.m
.
function max_humidity = getMaxHumidity(weatherData) % Given a table of data recorded by a weather station over 24 hours, get the max % humidity. max_humidity = max(weatherData(:,4)); end
Create Python Package
Build the Python package with the Python Package Compiler app or the
compiler.build.pythonPackage
function. Package the
getMaxHumidity.m
file and name the package
getMaxHumidityPkg
.
For example, if you are using compiler.build.pythonPackage
,
use this
command.
buildResults = compiler.build.pythonPackage( ... 'getMaxHumidity.m', ... 'PackageName','getMaxHumidityPkg');
For details, see the instructions in Generate Python Package and Build Python Application.
Write Python Application Code
Write code in Python for an application named getMaxHumiditySample.py
.
This sample application creates a pandas.DataFrame
with weather
data and passes it to the packaged function, which returns the maximum
humidity.
#!/usr/bin/env python
import getMaxHumidityPkg
# Import the matlab module only after you have imported
# MATLAB Compiler SDK generated Python modules.
import matlab
import datetime as dt
import pandas as pd
try:
# Initialize Python module created with MATLAB Compiler SDK.
my_getMaxHumidityPkg = getMaxHumidityPkg.initialize()
except Exception as e:
print("Error initializing getMaxHumidityPkg package\\n:{}".format(e))
exit(1)
try:
# Create a DataFrame from the data for the past 24 hours.
data = {
"Time": [
dt.datetime.strptime(i, "%Y-%m-%d %H:%M")
for i in [
"2024-10-30 18:00",
"2024-10-30 21:00",
"2024-10-31 00:00",
"2024-10-31 03:00",
"2024-10-31 06:00",
"2024-10-31 09:00",
"2024-10-31 12:00",
"2024-10-31 15:00",
"2024-10-31 18:00"
]
],
"Temperature (°F)": [67, 64, 62, 60, 58, 61, 70, 75, 79],
"Weather": [
"Clear",
"Overcast",
"Passing clouds",
"Passing clouds",
"Passing clouds",
"Partly sunny",
"Partly sunny",
"Passing clouds",
"Partly sunny"
],
"Wind Speed (mph)": [8, 10, 7, 6, 5, 7, 9, 10, 12],
"Humidity (%)": [51, 73, 78, 80, 82, 74, 65, 58, 50],
}
# Convert data into a DataFrame.
xIn = pd.DataFrame(data)
# Call the packaged function.
yOut = my_getMaxHumidityPkg.getMaxHumidity(xIn)
print(yOut)
except Exception as e:
print("Error occurred during program execution\\n:{}".format(e))
my_getMaxHumidityPkg.terminate()
The application performs these steps:
Imports the
getMaxHumidityPkg
,matlab
,datetime
, andpandas
packages.Instantiates the
getMaxHumidityPkg
instance asmy_getMaxHumidityPkg
.Creates a
dict
nameddata
containing 24 hours of weather data and converts it into a Pandas DataFrame.Calls the
getMaxHumidity
method in thegetMaxHumidityPkg
package to convert the weather data into a MATLAB table and get the maximum value in theHumidity (%)
column.Displays the result.
Uses a
try
block to catch and handle any exceptions.
Install and Run Python Package
On the target machine, install the generated Python package. For more details, see Install and Import MATLAB Compiler SDK Python Packages.
In the system command prompt, navigate to the folder containing the generated files and install the Python package.
python -m pip install .
Run the application.
python getMaxHumidityPkg.py
The application generates this output.
Humidity___ 0 82
See Also
Topics
- Convert Between MATLAB tables and Pandas DataFrames
- Use MATLAB Arrays in Python (MATLAB Production Server)
- Calculate Win Percentage Using Python Dictionary Data
- matlab Python Module (MATLAB Production Server)
- Invoke Packaged MATLAB Functions (MATLAB Production Server)