





















































Hi ,
Welcome to a brand new issue of PythonPro!
In today’sExpert Insight we bring you an excerpt from the recently published, Polars Cookbook, which shows you how to convert DataFrames and Series between Polars and pandas.
News Highlights: Python Developer Survey: 55% use Linux, 6% still on Python 2; SuperTree enables interactive decision tree visuals in Jupyter; and OneBusAway launches Python and JavaScript SDKs for seamless data integration.
Here are my top 5 picks from our learning resources today:
And, today’s Featured Study, highlights how process mining, using tools like pm4py, can uncover insights into workflow efficiency, variability, and algorithmic performance.
Stay awesome!
Divya Anne Selvaraj
Editor-in-Chief
P.S.: This month’ssurvey is now live. Do take the opportunity to tell us what you think of PythonPro, request learning resources, and earn your one Packt Credit for this month.
supertree
- Interactive Decision Tree Visualization: This Python package is designed to create interactive visualizations of decision trees within Jupyter Notebooks, Jupyter Lab, Google Colab, and similar environments that support HTML rendering..zshrc
file, allowing users to switch between Anaconda and official Python installations without terminal commands.ssl_server.py
, that wraps http.server
to enable serving static sites over HTTPS using a self-signed SSL certificate. Read to learn how to serve static content securely.In "Navigating Process Mining: A Case Study using pm4py," Kovács et al., explore the application of the pm4py library in analysing road traffic fine management processes. The study aims to demonstrate how process mining can uncover key insights into process efficiency and optimisation.
Process mining is a technique that combines data mining and business process management to analyse event logs generated by information systems. It is particularly effective for uncovering hidden patterns, identifying bottlenecks, and optimising workflows. The study focuses on applying the pm4py library, an open-source Python tool, to a real-world road traffic fine management process. This approach offers a deeper understanding of process execution compared to traditional business intelligence tools.
The study's application of process mining to road traffic fine management revealed significant insights into process variability, algorithmic performance, and workflow complexity:
This study is relevant to data scientists, business analysts, and operations managers interested in optimising business processes. The pm4py library, as demonstrated in this case study, provides practical tools for analysing complex workflows, identifying inefficiencies, and improving operational efficiency. The insights gained can be applied to other business processes, making it a valuable resource for those aiming to enhance process performance.
The study used the pm4py library to analyse an event log related to the management of road traffic fines, covering activities such as creating fines, sending fines, adding penalties, managing appeals, and handling payments. The analysis involved three process mining algorithms—Alpha Miner, Inductive Miner, and Heuristic Miner—to discover process models from the event log data. The evaluation of simplicity and precision across these algorithms revealed that the Heuristic Miner achieved the highest precision score of 1.0, while the Alpha Miner provided a balance between simplicity and accuracy.
You can learn more by reading the entirepaper and accessing the pm4py library.
Here’s an excerpt from “Chapter 10: Interoperability with Other Python Libraries” in the Polars Cookbook,by Yuki Kakegawa, published in August 2024.
Converting to and from a pandas DataFrame
Many of you have used pandas before, especially in your day-to-day work. Although pandas and Polars are often compared as one-or-the-other tools, you can use these tools to supplement each other.
Polars allows you to convert between pandas and Polars DataFrames, which is exactly what we’ll cover in this recipe.
You needpandas
andpyarrow
installed for this recipe to work. Execute the following code to make sure that you havethem installed:
pip install pandas pyarrow
Here’s how to convert to and from pandas DataFrames. We’ll first create a Polars DataFrame and then go through ways to convert back and forth between Polarsand pandas:
df = pl.DataFrame({
'a': [1,2,3],
'b': [4,5,6]
})
type(df)
The preceding code will return thefollowing output:
>> polars.dataframe.frame.DataFrame
.
to_pandas()
method:pandas_df = df.to_pandas()
type(pandas_df)
The preceding code will return thefollowing output:
>> pandas.core.frame.DataFrame
.
from_pandas()
method:df = pl.from_pandas(pandas_df)
type(df)
The preceding code will return thefollowing output:
>> polars.dataframe.frame.DataFrame
use_pyarrow_extension_array
parameter:df.to_pandas(use_pyarrow_extension_array=True).dtypes
The preceding code will return thefollowing output:
>>
a int64[pyarrow]
b int64[pyarrow]
dtype: object
You can also create a Polars DataFrame by wrapping a pandas DataFrameusingpl.DataFrame()
:
type(pl.DataFrame(pandas_df))
The preceding code will return thefollowing output:
>> polars.dataframe.frame.DataFrame
Polars has built-in methods to interoperate with pandas such as.from_pandas()
and.to_pandas()
. Each method is descriptive enough that you can see that .from_pandas()
is used for reading data into Polars from pandas, whereas .to_pandas()
is used to convert Polars objectsinto pandas.
Theuse_pyarrow_extension_array
parameter of the.to_pandas()
method uses PyArrow-supported arrays instead of NumPy arrays for the columns within the pandas DataFrame. This enables zero-copy operations and maintains the integrity ofnull values.
You can convert to and from a pandas Series to aPolars Series:
s = pl.Series([1,2,3])
type(s.to_pandas())
The preceding code producesthe following:
>> pandas.core.series.Series
The.from_pandas()
method returns a Series object when a pandas Series waspassed in:
type(pl.from_pandas(s.to_pandas()))
The preceding code producesthe following:
>> polars.series.series.Series
Packt library subscribers cancontinue reading the entire book for free. You can buy the Polars Cookbook,by Yuki Kakegawa,here.
And that’s a wrap.
We have an entire range of newsletters with focused content for tech pros. Subscribe to the ones you find the most usefulhere. The complete PythonPro archives can be foundhere.
If you have any suggestions or feedback, or would like us to find you aPythonlearning resource on a particular subject, take thesurveyor just respond to this email!