Skip to content
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

BUG: read_csv raising for arrow engine and parse_dates #53295

Merged
merged 2 commits into from
May 19, 2023
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v2.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Bug fixes
- Bug in :func:`api.interchange.from_dataframe` was returning :class:`DataFrame`'s of incorrect sizes when called on slices (:issue:`52824`)
- Bug in :func:`api.interchange.from_dataframe` was unnecessarily raising on bitmasks (:issue:`49888`)
- Bug in :func:`merge` when merging on datetime columns on different resolutions (:issue:`53200`)
- Bug in :func:`read_csv` raising ``OverflowError`` for ``engine="pyarrow"`` and ``parse_dates`` set (:issue:`53295`)
- Bug in :func:`to_datetime` was inferring format to contain ``"%H"`` instead of ``"%I"`` if date contained "AM" / "PM" tokens (:issue:`53147`)
- Bug in :func:`to_timedelta` was raising ``ValueError`` with ``pandas.NA`` (:issue:`52909`)
- Bug in :meth:`DataFrame.__getitem__` not preserving dtypes for :class:`MultiIndex` partial keys (:issue:`51895`)
Expand All @@ -38,7 +39,6 @@ Bug fixes
- Bug in :meth:`Series.rename` not making a lazy copy when Copy-on-Write is enabled when a scalar is passed to it (:issue:`52450`)
- Bug in :meth:`pd.array` raising for ``NumPy`` array and ``pa.large_string`` or ``pa.large_binary`` (:issue:`52590`)


.. ---------------------------------------------------------------------------
.. _whatsnew_202.other:

Expand Down
3 changes: 3 additions & 0 deletions pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,9 @@ def unpack_if_single_element(arg):
return arg

def converter(*date_cols, col: Hashable):
if len(date_cols) == 1 and date_cols[0].dtype.kind in "Mm":
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if I understood your comment in the OP correctly, but does this also fix parse_dates for numpy dtypes too?

Copy link
Member Author

Choose a reason for hiding this comment

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

Only NumPy dtypes as far as I know, we fixed this for Arrow dtypes a couple of weeks ago.

return date_cols[0]

if date_parser is lib.no_default:
strs = parsing.concat_date_cols(date_cols)
date_fmt = (
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2217,3 +2217,23 @@ def test_parse_dates_dict_format_index(all_parsers):
index=Index([Timestamp("2019-12-31"), Timestamp("2020-12-31")], name="a"),
)
tm.assert_frame_equal(result, expected)


def test_parse_dates_arrow_engine(all_parsers):
# GH#53295
parser = all_parsers
data = """a,b
2000-01-01 00:00:00,1
2000-01-01 00:00:01,1"""

result = parser.read_csv(StringIO(data), parse_dates=["a"])
expected = DataFrame(
{
"a": [
Timestamp("2000-01-01 00:00:00"),
Timestamp("2000-01-01 00:00:01"),
],
"b": 1,
}
)
tm.assert_frame_equal(result, expected)