<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Xianqiu Li</title>
    <description>The latest articles on DEV Community by Xianqiu Li (@xianqiu_li_c875ff743fe6c0).</description>
    <link>https://dev.to/xianqiu_li_c875ff743fe6c0</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3800994%2F9b78e9a2-a887-42b9-964b-296548215a8b.png</url>
      <title>DEV Community: Xianqiu Li</title>
      <link>https://dev.to/xianqiu_li_c875ff743fe6c0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xianqiu_li_c875ff743fe6c0"/>
    <language>en</language>
    <item>
      <title>Why pandas cannot be used in TorchScript — and how xpandas fixes it</title>
      <dc:creator>Xianqiu Li</dc:creator>
      <pubDate>Mon, 02 Mar 2026 07:06:34 +0000</pubDate>
      <link>https://dev.to/xianqiu_li_c875ff743fe6c0/why-pandas-cannot-be-used-in-torchscript-and-how-xpandas-fixes-it-60o</link>
      <guid>https://dev.to/xianqiu_li_c875ff743fe6c0/why-pandas-cannot-be-used-in-torchscript-and-how-xpandas-fixes-it-60o</guid>
      <description>&lt;h2&gt;
  
  
  The problem I kept running into
&lt;/h2&gt;

&lt;p&gt;I often deploy PyTorch models using &lt;strong&gt;TorchScript&lt;/strong&gt; and &lt;strong&gt;LibTorch (C++)&lt;/strong&gt; for inference.&lt;/p&gt;

&lt;p&gt;The model itself is not the hard part.&lt;/p&gt;

&lt;p&gt;The problem is &lt;strong&gt;data processing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In many real pipelines — especially in quantitative finance, feature engineering, or low-latency systems — you still need to do things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;groupby + aggregation
&lt;/li&gt;
&lt;li&gt;rolling window operations
&lt;/li&gt;
&lt;li&gt;column-wise transformations
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Python, this is trivial with pandas.&lt;/p&gt;

&lt;p&gt;But the moment you try to move the whole pipeline into TorchScript or C++ inference, everything breaks.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why pandas (and friends) don’t work in TorchScript
&lt;/h2&gt;

&lt;p&gt;This is not a pandas problem. It’s a &lt;strong&gt;runtime boundary problem&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. pandas is Python-runtime dependent
&lt;/h3&gt;

&lt;p&gt;TorchScript explicitly removes the Python runtime:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;no Python objects
&lt;/li&gt;
&lt;li&gt;no dynamic typing
&lt;/li&gt;
&lt;li&gt;no CPython extensions
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;pandas relies heavily on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python objects
&lt;/li&gt;
&lt;li&gt;NumPy internals
&lt;/li&gt;
&lt;li&gt;CPython C-API
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So &lt;code&gt;torch.jit.script()&lt;/code&gt; simply cannot compile pandas code.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Polars / Arrow don’t solve this either
&lt;/h3&gt;

&lt;p&gt;You might think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“What about Polars? It’s fast and written in Rust.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Still no.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Polars is not TorchScript-aware
&lt;/li&gt;
&lt;li&gt;Arrow execution does not integrate with PyTorch JIT graphs
&lt;/li&gt;
&lt;li&gt;You cannot inline Polars logic inside a TorchScript model
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They are great libraries — just solving a &lt;strong&gt;different problem&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Python preprocessing + C++ inference is often unacceptable
&lt;/h3&gt;

&lt;p&gt;The usual workaround is:&lt;/p&gt;

&lt;p&gt;Python (pandas) → Tensor → TorchScript → C++&lt;/p&gt;

&lt;p&gt;This fails when you need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;single deployable artifact&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;low-latency&lt;/strong&gt; inference
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;no Python dependency&lt;/strong&gt; in production
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At that point, you either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;re-implement everything manually in C++
&lt;/li&gt;
&lt;li&gt;or give up on pandas-like logic altogether
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s the gap I kept hitting.&lt;/p&gt;




&lt;h2&gt;
  
  
  The core idea: pandas-like ops as Torch custom operators
&lt;/h2&gt;

&lt;p&gt;Instead of trying to make pandas work in TorchScript, I flipped the approach:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What if pandas-like operations were implemented &lt;strong&gt;directly as Torch custom ops&lt;/strong&gt;?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;inputs are &lt;code&gt;torch::Tensor&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;logic lives in C++ (LibTorch)
&lt;/li&gt;
&lt;li&gt;everything is &lt;strong&gt;TorchScript-compatible&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;the entire pipeline can be exported and run in C++
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is what &lt;strong&gt;xpandas&lt;/strong&gt; is.&lt;/p&gt;




&lt;h2&gt;
  
  
  What xpandas is (and what it is not)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What it &lt;strong&gt;is&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;small, opinionated subset&lt;/strong&gt; of pandas-like DataFrame operations
&lt;/li&gt;
&lt;li&gt;Implemented as &lt;strong&gt;Torch C++ custom operators&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Fully compatible with &lt;code&gt;torch.jit.script&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Designed for &lt;strong&gt;inference pipelines&lt;/strong&gt;, not exploratory analysis
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  What it &lt;strong&gt;is not&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A full pandas replacement
&lt;/li&gt;
&lt;li&gt;A dataframe library for interactive data science
&lt;/li&gt;
&lt;li&gt;A competitor to Polars or Arrow
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Repository:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/CVPaul/xpandas" rel="noopener noreferrer"&gt;https://github.com/CVPaul/xpandas&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>datascience</category>
      <category>dataprocessing</category>
    </item>
  </channel>
</rss>
