Skip to content

Commit e269670

Browse files
committed
BUG: fix datetimes.should_cache() error for deque (GH 29403)
itertools.islice() should be used to get slice of a deque. itertools.islice() also can be used (and is efficient) for other collections. So unique(arg[:check_count]) was replaced with set(islice(arg, check_count))
1 parent 2892b95 commit e269670

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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)