Skip to content

BUG: Fix to_datetime() cache behaviour to not omit duplicated output values #42261

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 8 commits into from
Jul 4, 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Categorical

Datetimelike
^^^^^^^^^^^^
-
- Bug in :func:`to_datetime` returning pd.NaT for inputs that produce duplicated values, when ``cache=True`` (:issue:`42259`)
-

Timedelta
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ def _maybe_cache(
if len(unique_dates) < len(arg):
cache_dates = convert_listlike(unique_dates, format)
cache_array = Series(cache_dates, index=unique_dates)
if not cache_array.is_unique:
# GH#39882 in case of None and NaT we get duplicates
cache_array = cache_array.drop_duplicates()
# GH#39882 and GH#35888 in case of None and NaT we get duplicates
if not cache_array.index.is_unique:
cache_array = cache_array[~cache_array.index.duplicated()]
return cache_array


Expand Down
34 changes: 28 additions & 6 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,18 +957,40 @@ def test_to_datetime_cache_scalar(self):
expected = Timestamp("20130101 00:00:00")
assert result == expected

def test_convert_object_to_datetime_with_cache(self):
@pytest.mark.parametrize(
"datetimelikes,expected_values",
(
(
(None, np.nan) + (NaT,) * start_caching_at,
(NaT,) * (start_caching_at + 2),
),
(
(None, Timestamp("2012-07-26")) + (NaT,) * start_caching_at,
(NaT, Timestamp("2012-07-26")) + (NaT,) * start_caching_at,
),
(
(None,)
+ (NaT,) * start_caching_at
+ ("2012 July 26", Timestamp("2012-07-26")),
(NaT,) * (start_caching_at + 1)
+ (Timestamp("2012-07-26"), Timestamp("2012-07-26")),
),
),
)
def test_convert_object_to_datetime_with_cache(
self, datetimelikes, expected_values
):
# GH#39882
ser = Series(
[None] + [NaT] * start_caching_at + [Timestamp("2012-07-26")],
datetimelikes,
dtype="object",
)
result = to_datetime(ser, errors="coerce")
expected = Series(
[NaT] * (start_caching_at + 1) + [Timestamp("2012-07-26")],
result_series = to_datetime(ser, errors="coerce")
expected_series = Series(
expected_values,
dtype="datetime64[ns]",
)
tm.assert_series_equal(result, expected)
tm.assert_series_equal(result_series, expected_series)

@pytest.mark.parametrize(
"date, format",
Expand Down