Skip to content

Commit 99f0bb5

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 72cee4a commit 99f0bb5

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
@@ -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

+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
@@ -2239,6 +2240,26 @@ def test_should_cache(listlike, do_caching):
22392240
)
22402241

22412242

2243+
@pytest.mark.parametrize(
2244+
"listlike",
2245+
[
2246+
(deque([pd.Timestamp("2010-06-02 09:30:00")] * 51)),
2247+
([pd.Timestamp("2010-06-02 09:30:00")] * 51),
2248+
(tuple([pd.Timestamp("2010-06-02 09:30:00")] * 51)),
2249+
],
2250+
)
2251+
def test_no_slicing_errors_in_should_cache(listlike):
2252+
# GH 29403
2253+
assert tools.should_cache(listlike) is True
2254+
2255+
2256+
def test_to_datetime_from_deque():
2257+
# GH 29403
2258+
result = pd.to_datetime(deque([pd.Timestamp("2010-06-02 09:30:00")] * 51))
2259+
expected = pd.to_datetime([pd.Timestamp("2010-06-02 09:30:00")] * 51)
2260+
tm.assert_index_equal(result, expected)
2261+
2262+
22422263
@pytest.mark.parametrize(
22432264
"unique_share,check_count, err_message",
22442265
[

0 commit comments

Comments
 (0)