Skip to content

BUG: fix the value error when using read_csv with "," as decimal point and pd.Float64Dtype() #52696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ Conversion
^^^^^^^^^^
- Bug in :meth:`ArrowDtype.numpy_dtype` returning nanosecond units for non-nanosecond ``pyarrow.timestamp`` and ``pyarrow.duration`` types (:issue:`51800`)
- Bug in :meth:`DataFrame.info` raising ``ValueError`` when ``use_numba`` is set (:issue:`51922`)
- Bug in :meth:`TextReader._convert_with_dtype` raising ``ValueError`` when ``,`` is used as decimal pointer (:issue:`52086`)
-

Strings
Expand Down
3 changes: 3 additions & 0 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,9 @@ cdef class TextReader:
result, dtype=dtype, true_values=true_values,
false_values=false_values)
else:
if chr(self.parser.decimal) != ".":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This costs to much performance, I think that we have to fix this somewhere else

for i in range(len(result)):
result[i] = result[i].replace(chr(self.parser.decimal), ".")
result = array_type._from_sequence_of_strings(result, dtype=dtype)
except NotImplementedError:
raise NotImplementedError(
Expand Down
33 changes: 33 additions & 0 deletions pandas/tests/libs/test_lib.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import io

import numpy as np
import pytest

Expand All @@ -7,6 +9,7 @@
writers as libwriters,
)

import pandas as pd
from pandas import Index
import pandas._testing as tm

Expand Down Expand Up @@ -270,3 +273,33 @@ def test_no_default_pickle():
# GH#40397
obj = tm.round_trip_pickle(lib.no_default)
assert obj is lib.no_default


def test_pdfloat64_decimal_separator():
df1 = pd.read_csv(
io.StringIO('id\n"1,5"\n"1,6"\n'),
dtype={"id": pd.Float64Dtype()},
sep=";",
decimal=",",
)
df2 = pd.read_csv(
io.StringIO('id\n"1.5"\n"1.6"\n'),
dtype={"id": pd.Float64Dtype()},
sep=",",
decimal=".",
)
assert df1.equals(df2)

df1 = pd.read_csv(
io.StringIO('id\n"1,5"\n'),
dtype={"id": pd.Float64Dtype()},
sep=";",
decimal=",",
)
df2 = pd.read_csv(
io.StringIO('id\n"1.5"\n'),
dtype={"id": pd.Float64Dtype()},
sep=",",
decimal=".",
)
assert df1.equals(df2)