Skip to content

Commit 035501e

Browse files
authored
BUG: read_csv not converting original col with keep_date_col true (#44633)
1 parent 94c5ce3 commit 035501e

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ I/O
659659
- Bug in :func:`read_csv` where reading multi-header input with unequal lengths incorrectly raising uncontrolled ``IndexError`` (:issue:`43102`)
660660
- Bug in :func:`read_csv`, changed exception class when expecting a file path name or file-like object from ``OSError`` to ``TypeError`` (:issue:`43366`)
661661
- Bug in :func:`read_csv` and :func:`read_fwf` ignoring all ``skiprows`` except first when ``nrows`` is specified for ``engine='python'`` (:issue:`44021`, :issue:`10261`)
662+
- Bug in :func:`read_csv` keeping the original column in object format when ``keep_date_col=True`` is set (:issue:`13378`)
662663
- Bug in :func:`read_json` not handling non-numpy dtypes correctly (especially ``category``) (:issue:`21892`, :issue:`33205`)
663664
- Bug in :func:`json_normalize` where multi-character ``sep`` parameter is incorrectly prefixed to every key (:issue:`43831`)
664665
- Bug in :func:`json_normalize` where reading data with missing multi-level metadata would not respect errors="ignore" (:issue:`44312`)

pandas/io/parsers/base_parser.py

+6
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,12 @@ def _isindex(colspec):
11741174
)
11751175

11761176
new_data[new_name] = col
1177+
1178+
# If original column can be converted to date we keep the converted values
1179+
# This can only happen if values are from single column
1180+
if len(colspec) == 1:
1181+
new_data[colspec[0]] = col
1182+
11771183
new_cols.append(new_name)
11781184
date_cols.update(old_names)
11791185

pandas/tests/io/parser/test_parse_dates.py

+16
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,22 @@ def test_date_parser_usecols_thousands(all_parsers):
18131813
tm.assert_frame_equal(result, expected)
18141814

18151815

1816+
@skip_pyarrow
1817+
def test_parse_dates_and_keep_orgin_column(all_parsers):
1818+
# GH#13378
1819+
parser = all_parsers
1820+
data = """A
1821+
20150908
1822+
20150909
1823+
"""
1824+
result = parser.read_csv(
1825+
StringIO(data), parse_dates={"date": ["A"]}, keep_date_col=True
1826+
)
1827+
expected_data = [Timestamp("2015-09-08"), Timestamp("2015-09-09")]
1828+
expected = DataFrame({"date": expected_data, "A": expected_data})
1829+
tm.assert_frame_equal(result, expected)
1830+
1831+
18161832
def test_dayfirst_warnings():
18171833
# GH 12585
18181834
warning_msg_day_first = (

0 commit comments

Comments
 (0)