Skip to content

BUG: read_csv not converting original col with keep_date_col true #44633

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

Merged
merged 3 commits into from
Nov 28, 2021
Merged
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/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ I/O
- Bug in :func:`read_csv` where reading multi-header input with unequal lengths incorrectly raising uncontrolled ``IndexError`` (:issue:`43102`)
- Bug in :func:`read_csv`, changed exception class when expecting a file path name or file-like object from ``OSError`` to ``TypeError`` (:issue:`43366`)
- 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`)
- Bug in :func:`read_csv` keeping the original column in object format when ``keep_date_col=True`` is set (:issue:`13378`)
- Bug in :func:`read_json` not handling non-numpy dtypes correctly (especially ``category``) (:issue:`21892`, :issue:`33205`)
- Bug in :func:`json_normalize` where multi-character ``sep`` parameter is incorrectly prefixed to every key (:issue:`43831`)
- Bug in :func:`json_normalize` where reading data with missing multi-level metadata would not respect errors="ignore" (:issue:`44312`)
Expand Down
6 changes: 6 additions & 0 deletions pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,12 @@ def _isindex(colspec):
)

new_data[new_name] = col

# If original column can be converted to date we keep the converted values
# This can only happen if values are from single column
if len(colspec) == 1:
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems like very special casey here

Copy link
Member Author

Choose a reason for hiding this comment

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

It is, yes. We can only keep the original column formatted as date, if it represents the whole date. In cases where two columns combined represent a date we can only keep the original format

Copy link
Contributor

Choose a reason for hiding this comment

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

ok worth a comment about this then for future readers

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

new_data[colspec[0]] = col

new_cols.append(new_name)
date_cols.update(old_names)

Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,22 @@ def test_date_parser_usecols_thousands(all_parsers):
tm.assert_frame_equal(result, expected)


@skip_pyarrow
def test_parse_dates_and_keep_orgin_column(all_parsers):
# GH#13378
parser = all_parsers
data = """A
20150908
20150909
"""
result = parser.read_csv(
StringIO(data), parse_dates={"date": ["A"]}, keep_date_col=True
)
expected_data = [Timestamp("2015-09-08"), Timestamp("2015-09-09")]
expected = DataFrame({"date": expected_data, "A": expected_data})
tm.assert_frame_equal(result, expected)


def test_dayfirst_warnings():
# GH 12585
warning_msg_day_first = (
Expand Down