Skip to content

Bug in to_datetime raising ValueError with None and NaT and more than 50 elements #41006

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
Apr 20, 2021
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ Reshaping
- Bug in :meth:`DataFrame.stack` not preserving ``CategoricalDtype`` in a ``MultiIndex`` (:issue:`36991`)
- Bug in :func:`to_datetime` raising error when input sequence contains unhashable items (:issue:`39756`)
- Bug in :meth:`Series.explode` preserving index when ``ignore_index`` was ``True`` and values were scalars (:issue:`40487`)
- Bug in :func:`to_datetime` raising ``ValueError`` when :class:`Series` contains ``None`` and ``NaT`` and has more than 50 elements (:issue:`39882`)

Sparse
^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
Scalar = Union[int, float, str]
DatetimeScalar = TypeVar("DatetimeScalar", Scalar, datetime)
DatetimeScalarOrArrayConvertible = Union[DatetimeScalar, ArrayConvertible]
start_caching_at = 50


# ---------------------------------------------------------------------
Expand Down Expand Up @@ -130,7 +131,7 @@ def should_cache(
# default realization
if check_count is None:
# in this case, the gain from caching is negligible
if len(arg) <= 50:
if len(arg) <= start_caching_at:
return False

if len(arg) <= 5000:
Expand Down Expand Up @@ -193,6 +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()
return cache_array


Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/series/methods/test_convert_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# for most cases), and the specific cases where the result deviates from
# this default. Those overrides are defined as a dict with (keyword, val) as
# dictionary key. In case of multiple items, the last override takes precedence.
from pandas.core.tools.datetimes import start_caching_at

test_cases = [
(
# data
Expand Down Expand Up @@ -225,3 +227,16 @@ def test_convert_bool_dtype(self):
# GH32287
df = pd.DataFrame({"A": pd.array([True])})
tm.assert_frame_equal(df, df.convert_dtypes())

def test_convert_object_to_datetime_with_cache(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you put this with the other to_datetime tests: pandas/tests/tools/test_to_datetime.py; there a number of caching tests if you can locate nearby

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved, hope the location is ok, cache tests are everywhere there :)

Copy link
Contributor

Choose a reason for hiding this comment

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

yep lgtm. lots of tests :->

# GH#39882
ser = pd.Series(
[None] + [pd.NaT] * start_caching_at + [pd.Timestamp("2012-07-26")],
dtype="object",
)
result = pd.to_datetime(ser, errors="coerce")
expected = pd.Series(
[pd.NaT] * (start_caching_at + 1) + [pd.Timestamp("2012-07-26")],
dtype="datetime64[ns]",
)
tm.assert_series_equal(result, expected)