Skip to content

Commit b766c15

Browse files
authored
Create test_preserve_leading_zeros.py
1 parent aabbbc5 commit b766c15

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pytest
2+
from io import StringIO
3+
import pandas._testing as tm
4+
5+
6+
@pytest.mark.xfail(reason="Leading zeros preservation may not work consistently across all engines")
7+
def test_leading_zeros_preserved_with_dtype_str(all_parsers):
8+
"""
9+
Ensure that all parser engines preserve leading zeros when dtype=str is passed.
10+
11+
This test verifies that when dtype=str is specified, leading zeros in
12+
numeric-looking strings are preserved across all available parser engines.
13+
"""
14+
parser = all_parsers
15+
engine_name = getattr(parser, 'engine', 'unknown')
16+
17+
data = """col1|col2|col3|col4
18+
AB|000388907|abc|0150
19+
CD|101044572|def|0150
20+
EF|000023607|ghi|0205
21+
GH|100102040|jkl|0205"""
22+
23+
result = parser.read_csv(
24+
StringIO(data),
25+
sep="|",
26+
dtype=str,
27+
)
28+
29+
# Verify leading zeros are preserved in col2
30+
assert result.loc[0, "col2"] == "000388907", f"Engine {engine_name}: Leading zeros lost in col2, row 0. Got: {result.loc[0, 'col2']}"
31+
assert result.loc[2, "col2"] == "000023607", f"Engine {engine_name}: Leading zeros lost in col2, row 2. Got: {result.loc[2, 'col2']}"
32+
33+
# Verify leading zeros are preserved in col4
34+
assert result.loc[0, "col4"] == "0150", f"Engine {engine_name}: Leading zeros lost in col4, row 0. Got: {result.loc[0, 'col4']}"
35+
assert result.loc[2, "col4"] == "0205", f"Engine {engine_name}: Leading zeros lost in col4, row 2. Got: {result.loc[2, 'col4']}"
36+
37+
# Verify all columns are string type
38+
assert result.dtypes["col1"] == "object", f"Engine {engine_name}: col1 should be string type, got {result.dtypes['col1']}"
39+
assert result.dtypes["col2"] == "object", f"Engine {engine_name}: col2 should be string type, got {result.dtypes['col2']}"
40+
assert result.dtypes["col3"] == "object", f"Engine {engine_name}: col3 should be string type, got {result.dtypes['col3']}"
41+
assert result.dtypes["col4"] == "object", f"Engine {engine_name}: col4 should be string type, got {result.dtypes['col4']}"
42+
43+
# Verify shape
44+
assert result.shape == (4, 4), f"Engine {engine_name}: Expected shape (4, 4), got {result.shape}"
45+
46+
# Verify column names
47+
expected_columns = ["col1", "col2", "col3", "col4"]
48+
assert list(result.columns) == expected_columns, f"Engine {engine_name}: Expected columns {expected_columns}, got {list(result.columns)}"

0 commit comments

Comments
 (0)