Skip to content

Commit 0e11a00

Browse files
Backport PR #42030: Regression raising Error when having dup cols with single dtype for read csv (#42045)
Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 7488d8e commit 0e11a00

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

pandas/_libs/parsers.pyx

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ from pandas.core.dtypes.common import (
108108
is_object_dtype,
109109
)
110110
from pandas.core.dtypes.dtypes import CategoricalDtype
111+
from pandas.core.dtypes.inference import is_dict_like
111112

112113
cdef:
113114
float64_t INF = <float64_t>np.inf
@@ -689,6 +690,7 @@ cdef class TextReader:
689690
count = counts.get(name, 0)
690691
if (
691692
self.dtype is not None
693+
and is_dict_like(self.dtype)
692694
and self.dtype.get(old_name) is not None
693695
and self.dtype.get(name) is None
694696
):

pandas/io/parsers/python_parser.py

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626

2727
from pandas.core.dtypes.common import is_integer
28+
from pandas.core.dtypes.inference import is_dict_like
2829

2930
from pandas.io.parsers.base_parser import (
3031
ParserBase,
@@ -424,6 +425,7 @@ def _infer_columns(self):
424425
cur_count = counts[col]
425426
if (
426427
self.dtype is not None
428+
and is_dict_like(self.dtype)
427429
and self.dtype.get(old_col) is not None
428430
and self.dtype.get(col) is None
429431
):

pandas/tests/io/parser/dtypes/test_dtypes_basic.py

+9
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,12 @@ def test_dtype_mangle_dup_cols(all_parsers, dtypes, exp_value):
248248
result = parser.read_csv(StringIO(data), dtype={"a": str, **dtypes})
249249
expected = DataFrame({"a": ["1"], "a.1": [exp_value]})
250250
tm.assert_frame_equal(result, expected)
251+
252+
253+
def test_dtype_mangle_dup_cols_single_dtype(all_parsers):
254+
# GH#42022
255+
parser = all_parsers
256+
data = """a,a\n1,1"""
257+
result = parser.read_csv(StringIO(data), dtype=str)
258+
expected = DataFrame({"a": ["1"], "a.1": ["1"]})
259+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)