Skip to content

Commit 0006637

Browse files
committed
TST: Integration tests for keep_whitespace option of read_fwf. (pandas-dev#51569)
Signed-off-by: Ronald Barnes <[email protected]>
1 parent 40ad7db commit 0006637

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

pandas/tests/io/parser/test_read_fwf.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,15 @@ def test_whitespace_preservation():
633633
fwf_data = """
634634
a bbb
635635
ccdd """
636+
## This test is a mess:
637+
## It's trying to keep whitespace via passing in a non-space delimiter:
636638
result = read_fwf(
637-
StringIO(fwf_data), widths=[3, 3], header=header, skiprows=[0], delimiter="\n\t"
639+
StringIO(fwf_data),
640+
widths=[3, 3],
641+
header=header,
642+
skiprows=[0],
643+
# delimiter="\n\t",
644+
keep_whitespace=True,
638645
)
639646
expected = read_csv(StringIO(csv_data), header=header)
640647
tm.assert_frame_equal(result, expected)
@@ -1004,3 +1011,46 @@ def test_use_nullable_dtypes_option():
10041011

10051012
expected = DataFrame({"a": pd.Series([1, 3], dtype="Int64")})
10061013
tm.assert_frame_equal(result, expected)
1014+
1015+
1016+
@pytest.mark.parametrize(
1017+
"keep_whitespace, data, expected",
1018+
[
1019+
(
1020+
# Preserve all whitespace:
1021+
True,
1022+
# 10-byte wide fields:
1023+
["left ", " centre ", " right "],
1024+
DataFrame(["left ", " centre ", " right "]),
1025+
),
1026+
(
1027+
# Preserve no whitespace:
1028+
False,
1029+
# 10-byte wide fields:
1030+
["left ", " centre ", " right "],
1031+
DataFrame(["left", "centre", "right"]),
1032+
),
1033+
# Preserve leading whitespace only:
1034+
(
1035+
(True, False),
1036+
["left ", " centre ", " right"],
1037+
DataFrame(["left", " centre", " right"]),
1038+
),
1039+
# Preserve trailing whitespace only:
1040+
(
1041+
(False, True),
1042+
["left ", " centre ", " right"],
1043+
DataFrame(["left ", "centre ", "right"]),
1044+
),
1045+
],
1046+
)
1047+
def test_fwf_keep_whitespace_true(keep_whitespace, data, expected):
1048+
# see GH#####
1049+
1050+
result = read_fwf(
1051+
StringIO("\n".join(data)),
1052+
header=None,
1053+
widths=[10],
1054+
keep_whitespace=keep_whitespace,
1055+
)
1056+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)