Skip to content

BUG: Fix date format identification for dict #51479

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
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 9 additions & 3 deletions pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,11 @@ def _isindex(colspec):
raise ValueError(f"Date column {new_name} already in dict")

_, col, old_names = _try_convert_dates(
converter, colspec, data_dict, orig_names
converter,
colspec,
data_dict,
orig_names,
target_name=new_name,
)

new_data[new_name] = col
Expand All @@ -1268,7 +1272,9 @@ def _isindex(colspec):
return data_dict, new_cols


def _try_convert_dates(parser: Callable, colspec, data_dict, columns):
def _try_convert_dates(
parser: Callable, colspec, data_dict, columns, target_name: str | None = None
):
colset = set(columns)
colnames = []

Expand All @@ -1287,7 +1293,7 @@ def _try_convert_dates(parser: Callable, colspec, data_dict, columns):
new_name = "_".join([str(x) for x in colnames])
to_parse = [np.asarray(data_dict[c]) for c in colnames if c in data_dict]

new_col = parser(*to_parse, col=new_name)
new_col = parser(*to_parse, col=new_name if target_name is None else target_name)
return new_name, new_col, colnames


Expand Down
6 changes: 3 additions & 3 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@
more strings (corresponding to the columns defined by `parse_dates`) as
arguments.

.. deprecated:: 2.0.0
Use ``date_format`` instead, or read in as ``object`` and then apply
:func:`to_datetime` as-needed.
.. deprecated:: 2.0.0
Copy link
Member Author

Choose a reason for hiding this comment

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

Use ``date_format`` instead, or read in as ``object`` and then apply
:func:`to_datetime` as-needed.
date_format : str or dict of column -> format, default ``None``
If used in conjunction with ``parse_dates``, will parse dates according to this
format. For anything more complex,
Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2189,9 +2189,10 @@ def test_parse_dates_dict_format_two_columns(all_parsers, key, parse_dates):
31-,12-2019
31-,12-2020"""

result = parser.read_csv(
StringIO(data), date_format={key: "%d- %m-%Y"}, parse_dates=parse_dates
)
with tm.assert_produces_warning(None):
result = parser.read_csv(
StringIO(data), date_format={key: "%d- %m-%Y"}, parse_dates=parse_dates
)
expected = DataFrame(
{
key: [Timestamp("2019-12-31"), Timestamp("2020-12-31")],
Expand Down