Skip to content

BUG: Do not attempt to cache unhashable values in to_datetime (#39756) #40414

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
Mar 14, 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
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 @@ -592,6 +592,7 @@ Reshaping
- Bug in :meth:`DataFrame.pivot_table` returning a ``MultiIndex`` for a single value when operating on and empty ``DataFrame`` (:issue:`13483`)
- Allow :class:`Index` to be passed to the :func:`numpy.all` function (:issue:`40180`)
- 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`)

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 @@ -147,7 +147,11 @@ def should_cache(

assert 0 < unique_share < 1, "unique_share must be in next bounds: (0; 1)"

unique_elements = set(islice(arg, check_count))
try:
# We can't cache if the items are not hashable.
unique_elements = set(islice(arg, check_count))
except TypeError:
return False
if len(unique_elements) > check_count * unique_share:
do_caching = False
return do_caching
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,12 @@ def test_to_datetime_unprocessable_input(self, cache):
with pytest.raises(TypeError, match=msg):
to_datetime([1, "1"], errors="raise", cache=cache)

@pytest.mark.parametrize("cache", [True, False])
def test_to_datetime_unhashable_input(self, cache):
series = Series([["a"]] * 100)
result = to_datetime(series, errors="ignore", cache=cache)
tm.assert_series_equal(series, result)

def test_to_datetime_other_datetime64_units(self):
# 5/25/2012
scalar = np.int64(1337904000000000).view("M8[us]")
Expand Down