Skip to content

Commit 37dfcc1

Browse files
nathalierjreback
authored andcommitted
BUG: fix datetimes.should_cache() error for deque (GH 29403) (pandas-dev#29846)
1 parent cd04280 commit 37dfcc1

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ Datetimelike
701701
- Bug in :meth:`DatetimeIndex.strftime` and :meth:`Series.dt.strftime` where ``NaT`` was converted to the string ``'NaT'`` instead of ``np.nan`` (:issue:`29578`)
702702
- Bug in :attr:`Timestamp.resolution` being a property instead of a class attribute (:issue:`29910`)
703703
- Bug in :func:`pandas.to_datetime` when called with ``None`` raising ``TypeError`` instead of returning ``NaT`` (:issue:`30011`)
704+
- Bug in :func:`pandas.to_datetime` failing for `deques` when using ``cache=True`` (the default) (:issue:`29403`)
704705

705706
Timedelta
706707
^^^^^^^^^

pandas/core/tools/datetimes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import abc
22
from datetime import datetime, time
33
from functools import partial
4+
from itertools import islice
45
from typing import Optional, TypeVar, Union
56

67
import numpy as np
@@ -111,7 +112,7 @@ def should_cache(
111112

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

114-
unique_elements = unique(arg[:check_count])
115+
unique_elements = set(islice(arg, check_count))
115116
if len(unique_elements) > check_count * unique_share:
116117
do_caching = False
117118
return do_caching

pandas/tests/indexes/datetimes/test_tools.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""" test to_datetime """
22

33
import calendar
4+
from collections import deque
45
from datetime import datetime, time
56
import locale
67

@@ -861,7 +862,7 @@ def test_datetime_invalid_index(self, values, format, infer):
861862

862863
@pytest.mark.parametrize("utc", [True, None])
863864
@pytest.mark.parametrize("format", ["%Y%m%d %H:%M:%S", None])
864-
@pytest.mark.parametrize("constructor", [list, tuple, np.array, pd.Index])
865+
@pytest.mark.parametrize("constructor", [list, tuple, np.array, pd.Index, deque])
865866
def test_to_datetime_cache(self, utc, format, constructor):
866867
date = "20130101 00:00:00"
867868
test_dates = [date] * 10 ** 5
@@ -872,6 +873,24 @@ def test_to_datetime_cache(self, utc, format, constructor):
872873

873874
tm.assert_index_equal(result, expected)
874875

876+
@pytest.mark.parametrize(
877+
"listlike",
878+
[
879+
(deque([pd.Timestamp("2010-06-02 09:30:00")] * 51)),
880+
([pd.Timestamp("2010-06-02 09:30:00")] * 51),
881+
(tuple([pd.Timestamp("2010-06-02 09:30:00")] * 51)),
882+
],
883+
)
884+
def test_no_slicing_errors_in_should_cache(self, listlike):
885+
# GH 29403
886+
assert tools.should_cache(listlike) is True
887+
888+
def test_to_datetime_from_deque(self):
889+
# GH 29403
890+
result = pd.to_datetime(deque([pd.Timestamp("2010-06-02 09:30:00")] * 51))
891+
expected = pd.to_datetime([pd.Timestamp("2010-06-02 09:30:00")] * 51)
892+
tm.assert_index_equal(result, expected)
893+
875894
@pytest.mark.parametrize("utc", [True, None])
876895
@pytest.mark.parametrize("format", ["%Y%m%d %H:%M:%S", None])
877896
def test_to_datetime_cache_series(self, utc, format):

0 commit comments

Comments
 (0)