Skip to content

Commit a0f9e74

Browse files
committed
added whatsnew and tests
1 parent a1736a9 commit a0f9e74

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ Categorical
198198
Datetimelike
199199
^^^^^^^^^^^^
200200
- Bug in :class:`DataFrame` constructor unnecessarily copying non-datetimelike 2D object arrays (:issue:`39272`)
201-
-
201+
- Bug in :func:`to_datetime` with ``format`` and ``pandas.NA`` was raising ``ValueError`` (:issue:`42957`)
202202

203203
Timedelta
204204
^^^^^^^^^

pandas/tests/tools/test_to_datetime.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,24 @@ def test_to_datetime_parse_timezone_keeps_name(self):
383383
expected = DatetimeIndex(["2010-01-01 12:00:00"], tz="UTC", name="foo")
384384
tm.assert_index_equal(result, expected)
385385

386+
@pytest.mark.parametrize(
387+
"data, format, expected",
388+
[
389+
([pd.NA], "%Y%m%d%H%M%S", DatetimeIndex(["NaT"])),
390+
(
391+
[pd.NA, "20210202202020"],
392+
"%Y%m%d%H%M%S",
393+
DatetimeIndex(["NaT", "2021-02-02 20:20:20"]),
394+
),
395+
(["201010", pd.NA], "%y%m%d", DatetimeIndex(["2020-10-10", "NaT"])),
396+
(["201010", pd.NA], "%d%m%y", DatetimeIndex(["2010-10-20", "NaT"])),
397+
],
398+
)
399+
def test_to_datetime_with_pdNA(self, data, format, expected):
400+
# GH#42957
401+
result = to_datetime(data, format=format)
402+
tm.assert_index_equal(result, expected)
403+
386404

387405
class TestToDatetime:
388406
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)