Skip to content

Commit 7b18dc6

Browse files
committed
FIX: Parsing dates and dtype in read_csv (issue pandas-dev#34066)
1 parent ca95de3 commit 7b18dc6

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pandas/io/parsers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,9 @@ def _convert_to_ndarrays(
17081708
result = {}
17091709
for c, values in dct.items():
17101710
conv_f = None if converters is None else converters.get(c, None)
1711-
if isinstance(dtypes, dict):
1711+
if values.dtype != object:
1712+
cast_type = values.dtype
1713+
elif isinstance(dtypes, dict):
17121714
cast_type = dtypes.get(c, None)
17131715
else:
17141716
# single dtype or None
@@ -3264,6 +3266,9 @@ def _make_date_converter(
32643266
):
32653267
def converter(*date_cols):
32663268
if date_parser is None:
3269+
date_cols = tuple(
3270+
[x if isinstance(x, np.ndarray) else x.to_numpy() for x in date_cols]
3271+
)
32673272
strs = parsing.concat_date_cols(date_cols)
32683273

32693274
try:

pandas/tests/io/parser/test_common.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,3 +2135,17 @@ def test_no_header_two_extra_columns(all_parsers):
21352135
parser = all_parsers
21362136
df = parser.read_csv(stream, header=None, names=column_names, index_col=False)
21372137
tm.assert_frame_equal(df, ref)
2138+
2139+
2140+
def test_dtype_with_parse_dates(all_parsers):
2141+
# GH 34066
2142+
parser = all_parsers
2143+
data = """
2144+
a,b
2145+
1,2020-05-23 01:00:00"""
2146+
expected = DataFrame(
2147+
[["1", "2020-05-23 01:00:00"]], columns=["a", "b"], dtype="string"
2148+
)
2149+
expected = expected.astype({"b": np.datetime64})
2150+
df = parser.read_csv(StringIO(data), dtype="string", parse_dates=["b"])
2151+
tm.assert_frame_equal(df, expected)

0 commit comments

Comments
 (0)