Skip to content

Commit 94abece

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 35029d2 commit 94abece

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-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
@@ -118,7 +119,7 @@ def should_cache(
118119

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

121-
unique_elements = unique(arg[:check_count])
122+
unique_elements = set(islice(arg, check_count))
122123
if len(unique_elements) > check_count * unique_share:
123124
do_caching = False
124125
return do_caching

pandas/tests/indexes/datetimes/test_tools.py

+22-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

@@ -838,7 +839,7 @@ def test_datetime_invalid_index(self, values, format, infer):
838839

839840
@pytest.mark.parametrize("utc", [True, None])
840841
@pytest.mark.parametrize("format", ["%Y%m%d %H:%M:%S", None])
841-
@pytest.mark.parametrize("constructor", [list, tuple, np.array, pd.Index])
842+
@pytest.mark.parametrize("constructor", [list, tuple, np.array, pd.Index, deque])
842843
def test_to_datetime_cache(self, utc, format, constructor):
843844
date = "20130101 00:00:00"
844845
test_dates = [date] * 10 ** 5
@@ -2275,6 +2276,26 @@ def test_should_cache(listlike, do_caching):
22752276
)
22762277

22772278

2279+
@pytest.mark.parametrize(
2280+
"listlike",
2281+
[
2282+
(deque([pd.Timestamp("2010-06-02 09:30:00")] * 51)),
2283+
([pd.Timestamp("2010-06-02 09:30:00")] * 51),
2284+
(tuple([pd.Timestamp("2010-06-02 09:30:00")] * 51)),
2285+
],
2286+
)
2287+
def test_no_slicing_errors_in_should_cache(listlike):
2288+
# GH 29403
2289+
assert tools.should_cache(listlike) is True
2290+
2291+
2292+
def test_to_datetime_from_deque():
2293+
# GH 29403
2294+
result = pd.to_datetime(deque([pd.Timestamp("2010-06-02 09:30:00")] * 51))
2295+
expected = pd.to_datetime([pd.Timestamp("2010-06-02 09:30:00")] * 51)
2296+
tm.assert_index_equal(result, expected)
2297+
2298+
22782299
@pytest.mark.parametrize(
22792300
"unique_share,check_count, err_message",
22802301
[

0 commit comments

Comments
 (0)