Skip to content

Commit 29c053c

Browse files
committed
owcsvimport: Fallback to float_precision="high" ...
... with non C-locale number format.
1 parent 62e5c69 commit 29c053c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Orange/widgets/data/owcsvimport.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,14 @@ def expand(ranges):
12351235
if opts.group_separator != "":
12361236
numbers_format_kwds["thousands"] = opts.group_separator
12371237

1238+
if numbers_format_kwds:
1239+
# float_precision = "round_trip" cannot handle non c-locale decimal and
1240+
# thousands sep (https://github.com/pandas-dev/pandas/issues/35365).
1241+
# Fallback to 'high'.
1242+
numbers_format_kwds["float_precision"] = "high"
1243+
else:
1244+
numbers_format_kwds["float_precision"] = "round_trip"
1245+
12381246
with ExitStack() as stack:
12391247
if isinstance(path, (str, bytes)):
12401248
f = stack.enter_context(_open(path, 'rb'))
@@ -1253,7 +1261,6 @@ def expand(ranges):
12531261
header=header, skiprows=skiprows,
12541262
dtype=dtypes, parse_dates=parse_dates, prefix=prefix,
12551263
na_values=na_values, keep_default_na=False,
1256-
float_precision="round_trip",
12571264
**numbers_format_kwds
12581265
)
12591266
df = guess_types(df, dtypes, columns_ignored)

Orange/widgets/data/tests/test_owcsvimport.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# pylint: disable=no-self-use
12
import unittest
23
from unittest import mock
34
from contextlib import ExitStack
@@ -287,6 +288,24 @@ class dialect(csv.excel):
287288
assert_array_equal(tb.X[:, 1], [0, np.nan, np.nan])
288289
assert_array_equal(tb.X[:, 2], [np.nan, 1, np.nan])
289290

291+
def test_decimal_format(self):
292+
class Dialect(csv.excel):
293+
delimiter = ";"
294+
295+
contents = b'3,21;3,37\n4,13;1.000,142'
296+
opts = owcsvimport.Options(
297+
encoding="ascii",
298+
dialect=Dialect(),
299+
decimal_separator=",",
300+
group_separator=".",
301+
columntypes=[
302+
(range(0, 2), ColumnType.Numeric),
303+
],
304+
rowspec=[],
305+
)
306+
df = owcsvimport.load_csv(io.BytesIO(contents), opts)
307+
assert_array_equal(df.values, np.array([[3.21, 3.37], [4.13, 1000.142]]))
308+
290309

291310
if __name__ == "__main__":
292311
unittest.main()

0 commit comments

Comments
 (0)