Skip to content

zfill with leading + differs from Python stdlib and from pandas #22983

@MarcoGorelli

Description

@MarcoGorelli

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

In [1]: import polars as pl

In [2]: import pandas as pd

In [3]: '-1'.zfill(3)
Out[3]: '-01'

In [4]: pd.Series(['-1']).str.zfill(3)
Out[4]:
0    -01
dtype: object

In [5]: pl.Series(['-1']).str.zfill(3)
Out[5]:
shape: (1,)
Series: '' [str]
[
        "-01"
]

In [6]: '+1'.zfill(3)
Out[6]: '+01'

In [7]: pd.Series(['+1']).str.zfill(3)
Out[7]:
0    +01
dtype: object

In [8]: pl.Series(['+1']).str.zfill(3)
Out[8]:
shape: (1,)
Series: '' [str]
[
        "0+1"
]

Log output

Issue description

If there's a leading '-', then pandas, Polars, and Python agree

If there's a leading '+', then pandas and Python pad to its right, whereas Polars pads to its left

Expected behavior

I think

In [8]: pl.Series(['+1']).str.zfill(3)
Out[8]:
shape: (1,)
Series: '' [str]
[
        "0+1"
]

?

Installed versions

In [9]: pl.show_versions()
--------Version info---------
Polars:              1.29.0
Index type:          UInt32
Platform:            Linux-5.15.167.4-microsoft-standard-WSL2-x86_64-with-glibc2.35
Python:              3.12.5 (main, Aug 14 2024, 05:08:31) [Clang 18.1.8 ]
LTS CPU:             False

----Optional dependencies----
Azure CLI            <not installed>
adbc_driver_manager  <not installed>
altair               5.5.0
azure.identity       <not installed>
boto3                <not installed>
cloudpickle          3.1.1
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               2025.3.2
gevent               <not installed>
google.auth          2.38.0
great_tables         0.16.1
matplotlib           3.9.2
numpy                2.2.6
openpyxl             <not installed>
pandas               2.2.3
polars_cloud         <not installed>
pyarrow              19.0.0
pydantic             <not installed>
pyiceberg            <not installed>
sqlalchemy           <not installed>
torch                <not installed>
xlsx2csv             <not installed>
xlsxwriter           <not installed>

Metadata

Metadata

Assignees

No one assigned

    Labels

    P-lowPriority: lowacceptedReady for implementationbugSomething isn't workinggood first issueGood for newcomerspythonRelated to Python Polars

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions