Skip to content

Commit b3e304e

Browse files
maroth96JulianWgs
authored andcommitted
BUG: Do not attempt to cache unhashable values in to_datetime (pandas-dev#39756) (pandas-dev#40414)
1 parent 04d2380 commit b3e304e

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ Reshaping
592592
- Bug in :meth:`DataFrame.pivot_table` returning a ``MultiIndex`` for a single value when operating on and empty ``DataFrame`` (:issue:`13483`)
593593
- Allow :class:`Index` to be passed to the :func:`numpy.all` function (:issue:`40180`)
594594
- Bug in :meth:`DataFrame.stack` not preserving ``CategoricalDtype`` in a ``MultiIndex`` (:issue:`36991`)
595+
- Bug in :func:`to_datetime` raising error when input sequence contains unhashable items (:issue:`39756`)
595596

596597
Sparse
597598
^^^^^^

pandas/core/tools/datetimes.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ def should_cache(
147147

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

150-
unique_elements = set(islice(arg, check_count))
150+
try:
151+
# We can't cache if the items are not hashable.
152+
unique_elements = set(islice(arg, check_count))
153+
except TypeError:
154+
return False
151155
if len(unique_elements) > check_count * unique_share:
152156
do_caching = False
153157
return do_caching

pandas/tests/tools/test_to_datetime.py

+6
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,12 @@ def test_to_datetime_unprocessable_input(self, cache):
16511651
with pytest.raises(TypeError, match=msg):
16521652
to_datetime([1, "1"], errors="raise", cache=cache)
16531653

1654+
@pytest.mark.parametrize("cache", [True, False])
1655+
def test_to_datetime_unhashable_input(self, cache):
1656+
series = Series([["a"]] * 100)
1657+
result = to_datetime(series, errors="ignore", cache=cache)
1658+
tm.assert_series_equal(series, result)
1659+
16541660
def test_to_datetime_other_datetime64_units(self):
16551661
# 5/25/2012
16561662
scalar = np.int64(1337904000000000).view("M8[us]")

0 commit comments

Comments
 (0)