Skip to content

Commit c6aea1e

Browse files
committed
Add tests to check to_csv for dtypes - float16, float32, float64 and quoting option enabled
1 parent dc86902 commit c6aea1e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pandas/tests/frame/methods/test_to_csv.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,40 @@ def test_to_csv_quoting(self):
13161316
expected = tm.convert_rows_list_to_csv_str(expected_rows)
13171317
assert df.to_csv(quoting=csv.QUOTE_ALL) == expected
13181318

1319+
@pytest.mark.parametrize("data, dtype, expected_rows",
1320+
[
1321+
# Test Case 1: float16 precision
1322+
(
1323+
{"col": [8.57, 0.156, -0.312, 123.3, -54.5, np.nan]},
1324+
"float16",
1325+
['"","col"', '0,8.57', '1,0.156', '2,-0.312', '3,123.3', '4,-54.5', '5,""']
1326+
),
1327+
1328+
# Test Case 2: float32 precision
1329+
(
1330+
{"col": [8.57, 1.234567, -2.345678, 1e6, -1.5e6, np.nan]},
1331+
"float32",
1332+
['"","col"', '0,8.57', '1,1.234567', '2,-2.345678', '3,1000000.0', '4,-1500000.0', '5,""']
1333+
),
1334+
1335+
# Test Case 3: float64 precision
1336+
(
1337+
{"col": [8.57, 3.141592653589793, -2.718281828459045, 1.01e12, -5.67e11, np.nan]},
1338+
"float64",
1339+
['"","col"', '0,8.57', '1,3.141592653589793', '2,-2.718281828459045',
1340+
'3,1010000000000.0', '4,-567000000000.0', '5,""']
1341+
),
1342+
]
1343+
)
1344+
def test_to_csv_decimal_and_nonnumeric_quoting(self, data, dtype, expected_rows):
1345+
# https://github.com/pandas-dev/pandas/issues/60699
1346+
# combination of float dtype, no special formatting and
1347+
# quoting is specified (quoting=csv.QUOTE_NONNUMERIC)
1348+
df = pd.DataFrame(data, dtype=dtype)
1349+
result = df.to_csv(quoting=csv.QUOTE_NONNUMERIC)
1350+
expected = tm.convert_rows_list_to_csv_str(expected_rows)
1351+
assert result == expected
1352+
13191353
def test_period_index_date_overflow(self):
13201354
# see gh-15982
13211355

0 commit comments

Comments
 (0)