Skip to content

Commit 6b45476

Browse files
authored
BUG: Fix date format identification for dict (#51479)
* BUG: Fix date format identification for dict * Cleanup docs * Cleanup docs
1 parent 450a1f0 commit 6b45476

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

pandas/io/excel/_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@
251251
more strings (corresponding to the columns defined by `parse_dates`) as
252252
arguments.
253253
254-
.. deprecated:: 2.0.0
255-
Use ``date_format`` instead, or read in as ``object`` and then apply
256-
:func:`to_datetime` as-needed.
254+
.. deprecated:: 2.0.0
255+
Use ``date_format`` instead, or read in as ``object`` and then apply
256+
:func:`to_datetime` as-needed.
257257
date_format : str or dict of column -> format, default ``None``
258258
If used in conjunction with ``parse_dates``, will parse dates according to this
259259
format. For anything more complex,

pandas/io/parsers/base_parser.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,11 @@ def _isindex(colspec):
12441244
raise ValueError(f"Date column {new_name} already in dict")
12451245

12461246
_, col, old_names = _try_convert_dates(
1247-
converter, colspec, data_dict, orig_names
1247+
converter,
1248+
colspec,
1249+
data_dict,
1250+
orig_names,
1251+
target_name=new_name,
12481252
)
12491253

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

12701274

1271-
def _try_convert_dates(parser: Callable, colspec, data_dict, columns):
1275+
def _try_convert_dates(
1276+
parser: Callable, colspec, data_dict, columns, target_name: str | None = None
1277+
):
12721278
colset = set(columns)
12731279
colnames = []
12741280

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

1290-
new_col = parser(*to_parse, col=new_name)
1296+
new_col = parser(*to_parse, col=new_name if target_name is None else target_name)
12911297
return new_name, new_col, colnames
12921298

12931299

pandas/io/parsers/readers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,9 @@
262262
more strings (corresponding to the columns defined by `parse_dates`) as
263263
arguments.
264264
265-
.. deprecated:: 2.0.0
266-
Use ``date_format`` instead, or read in as ``object`` and then apply
267-
:func:`to_datetime` as-needed.
265+
.. deprecated:: 2.0.0
266+
Use ``date_format`` instead, or read in as ``object`` and then apply
267+
:func:`to_datetime` as-needed.
268268
date_format : str or dict of column -> format, default ``None``
269269
If used in conjunction with ``parse_dates``, will parse dates according to this
270270
format. For anything more complex,

pandas/tests/io/parser/test_parse_dates.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,9 +2189,10 @@ def test_parse_dates_dict_format_two_columns(all_parsers, key, parse_dates):
21892189
31-,12-2019
21902190
31-,12-2020"""
21912191

2192-
result = parser.read_csv(
2193-
StringIO(data), date_format={key: "%d- %m-%Y"}, parse_dates=parse_dates
2194-
)
2192+
with tm.assert_produces_warning(None):
2193+
result = parser.read_csv(
2194+
StringIO(data), date_format={key: "%d- %m-%Y"}, parse_dates=parse_dates
2195+
)
21952196
expected = DataFrame(
21962197
{
21972198
key: [Timestamp("2019-12-31"), Timestamp("2020-12-31")],

0 commit comments

Comments
 (0)