Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_sort_values.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_value_counts.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_drop_duplicates.py +89 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_equals.py +181 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_indexing.py +45 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_is_monotonic.py +46 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_nat.py +53 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_sort_values.py +315 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_value_counts.py +103 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_arithmetic.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_constructors.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_date_range.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_datetime.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_formats.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_freq_attr.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_indexing.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_iter.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_join.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_npfuncs.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_ops.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_partial_slicing.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_pickle.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_reindex.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_scalar_compat.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_setops.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_timezones.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_asof.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_fillna.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_insert.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_map.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_normalize.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_round.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_period.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_series.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_factorize.py +125 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_fillna.py +62 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_isocalendar.py +28 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_map.py +47 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_repeat.py +83 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_resolution.py +31 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_round.py +221 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_shift.py +169 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_snap.py +47 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_frame.py +28 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_julian_date.py +45 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_pydatetime.py +51 -0
- env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_tz_convert.py +283 -0
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (199 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_sort_values.cpython-310.pyc
ADDED
Binary file (7.62 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/__pycache__/test_value_counts.cpython-310.pyc
ADDED
Binary file (3.29 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_drop_duplicates.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
PeriodIndex,
|
6 |
+
Series,
|
7 |
+
date_range,
|
8 |
+
period_range,
|
9 |
+
timedelta_range,
|
10 |
+
)
|
11 |
+
import pandas._testing as tm
|
12 |
+
|
13 |
+
|
14 |
+
class DropDuplicates:
|
15 |
+
def test_drop_duplicates_metadata(self, idx):
|
16 |
+
# GH#10115
|
17 |
+
result = idx.drop_duplicates()
|
18 |
+
tm.assert_index_equal(idx, result)
|
19 |
+
assert idx.freq == result.freq
|
20 |
+
|
21 |
+
idx_dup = idx.append(idx)
|
22 |
+
result = idx_dup.drop_duplicates()
|
23 |
+
|
24 |
+
expected = idx
|
25 |
+
if not isinstance(idx, PeriodIndex):
|
26 |
+
# freq is reset except for PeriodIndex
|
27 |
+
assert idx_dup.freq is None
|
28 |
+
assert result.freq is None
|
29 |
+
expected = idx._with_freq(None)
|
30 |
+
else:
|
31 |
+
assert result.freq == expected.freq
|
32 |
+
|
33 |
+
tm.assert_index_equal(result, expected)
|
34 |
+
|
35 |
+
@pytest.mark.parametrize(
|
36 |
+
"keep, expected, index",
|
37 |
+
[
|
38 |
+
(
|
39 |
+
"first",
|
40 |
+
np.concatenate(([False] * 10, [True] * 5)),
|
41 |
+
np.arange(0, 10, dtype=np.int64),
|
42 |
+
),
|
43 |
+
(
|
44 |
+
"last",
|
45 |
+
np.concatenate(([True] * 5, [False] * 10)),
|
46 |
+
np.arange(5, 15, dtype=np.int64),
|
47 |
+
),
|
48 |
+
(
|
49 |
+
False,
|
50 |
+
np.concatenate(([True] * 5, [False] * 5, [True] * 5)),
|
51 |
+
np.arange(5, 10, dtype=np.int64),
|
52 |
+
),
|
53 |
+
],
|
54 |
+
)
|
55 |
+
def test_drop_duplicates(self, keep, expected, index, idx):
|
56 |
+
# to check Index/Series compat
|
57 |
+
idx = idx.append(idx[:5])
|
58 |
+
|
59 |
+
tm.assert_numpy_array_equal(idx.duplicated(keep=keep), expected)
|
60 |
+
expected = idx[~expected]
|
61 |
+
|
62 |
+
result = idx.drop_duplicates(keep=keep)
|
63 |
+
tm.assert_index_equal(result, expected)
|
64 |
+
|
65 |
+
result = Series(idx).drop_duplicates(keep=keep)
|
66 |
+
expected = Series(expected, index=index)
|
67 |
+
tm.assert_series_equal(result, expected)
|
68 |
+
|
69 |
+
|
70 |
+
class TestDropDuplicatesPeriodIndex(DropDuplicates):
|
71 |
+
@pytest.fixture(params=["D", "3D", "h", "2h", "min", "2min", "s", "3s"])
|
72 |
+
def freq(self, request):
|
73 |
+
return request.param
|
74 |
+
|
75 |
+
@pytest.fixture
|
76 |
+
def idx(self, freq):
|
77 |
+
return period_range("2011-01-01", periods=10, freq=freq, name="idx")
|
78 |
+
|
79 |
+
|
80 |
+
class TestDropDuplicatesDatetimeIndex(DropDuplicates):
|
81 |
+
@pytest.fixture
|
82 |
+
def idx(self, freq_sample):
|
83 |
+
return date_range("2011-01-01", freq=freq_sample, periods=10, name="idx")
|
84 |
+
|
85 |
+
|
86 |
+
class TestDropDuplicatesTimedeltaIndex(DropDuplicates):
|
87 |
+
@pytest.fixture
|
88 |
+
def idx(self, freq_sample):
|
89 |
+
return timedelta_range("1 day", periods=10, freq=freq_sample, name="idx")
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_equals.py
ADDED
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Tests shared for DatetimeIndex/TimedeltaIndex/PeriodIndex
|
3 |
+
"""
|
4 |
+
from datetime import (
|
5 |
+
datetime,
|
6 |
+
timedelta,
|
7 |
+
)
|
8 |
+
|
9 |
+
import numpy as np
|
10 |
+
import pytest
|
11 |
+
|
12 |
+
import pandas as pd
|
13 |
+
from pandas import (
|
14 |
+
CategoricalIndex,
|
15 |
+
DatetimeIndex,
|
16 |
+
Index,
|
17 |
+
PeriodIndex,
|
18 |
+
TimedeltaIndex,
|
19 |
+
date_range,
|
20 |
+
period_range,
|
21 |
+
timedelta_range,
|
22 |
+
)
|
23 |
+
import pandas._testing as tm
|
24 |
+
|
25 |
+
|
26 |
+
class EqualsTests:
|
27 |
+
def test_not_equals_numeric(self, index):
|
28 |
+
assert not index.equals(Index(index.asi8))
|
29 |
+
assert not index.equals(Index(index.asi8.astype("u8")))
|
30 |
+
assert not index.equals(Index(index.asi8).astype("f8"))
|
31 |
+
|
32 |
+
def test_equals(self, index):
|
33 |
+
assert index.equals(index)
|
34 |
+
assert index.equals(index.astype(object))
|
35 |
+
assert index.equals(CategoricalIndex(index))
|
36 |
+
assert index.equals(CategoricalIndex(index.astype(object)))
|
37 |
+
|
38 |
+
def test_not_equals_non_arraylike(self, index):
|
39 |
+
assert not index.equals(list(index))
|
40 |
+
|
41 |
+
def test_not_equals_strings(self, index):
|
42 |
+
other = Index([str(x) for x in index], dtype=object)
|
43 |
+
assert not index.equals(other)
|
44 |
+
assert not index.equals(CategoricalIndex(other))
|
45 |
+
|
46 |
+
def test_not_equals_misc_strs(self, index):
|
47 |
+
other = Index(list("abc"))
|
48 |
+
assert not index.equals(other)
|
49 |
+
|
50 |
+
|
51 |
+
class TestPeriodIndexEquals(EqualsTests):
|
52 |
+
@pytest.fixture
|
53 |
+
def index(self):
|
54 |
+
return period_range("2013-01-01", periods=5, freq="D")
|
55 |
+
|
56 |
+
# TODO: de-duplicate with other test_equals2 methods
|
57 |
+
@pytest.mark.parametrize("freq", ["D", "M"])
|
58 |
+
def test_equals2(self, freq):
|
59 |
+
# GH#13107
|
60 |
+
idx = PeriodIndex(["2011-01-01", "2011-01-02", "NaT"], freq=freq)
|
61 |
+
assert idx.equals(idx)
|
62 |
+
assert idx.equals(idx.copy())
|
63 |
+
assert idx.equals(idx.astype(object))
|
64 |
+
assert idx.astype(object).equals(idx)
|
65 |
+
assert idx.astype(object).equals(idx.astype(object))
|
66 |
+
assert not idx.equals(list(idx))
|
67 |
+
assert not idx.equals(pd.Series(idx))
|
68 |
+
|
69 |
+
idx2 = PeriodIndex(["2011-01-01", "2011-01-02", "NaT"], freq="h")
|
70 |
+
assert not idx.equals(idx2)
|
71 |
+
assert not idx.equals(idx2.copy())
|
72 |
+
assert not idx.equals(idx2.astype(object))
|
73 |
+
assert not idx.astype(object).equals(idx2)
|
74 |
+
assert not idx.equals(list(idx2))
|
75 |
+
assert not idx.equals(pd.Series(idx2))
|
76 |
+
|
77 |
+
# same internal, different tz
|
78 |
+
idx3 = PeriodIndex._simple_new(
|
79 |
+
idx._values._simple_new(idx._values.asi8, dtype=pd.PeriodDtype("h"))
|
80 |
+
)
|
81 |
+
tm.assert_numpy_array_equal(idx.asi8, idx3.asi8)
|
82 |
+
assert not idx.equals(idx3)
|
83 |
+
assert not idx.equals(idx3.copy())
|
84 |
+
assert not idx.equals(idx3.astype(object))
|
85 |
+
assert not idx.astype(object).equals(idx3)
|
86 |
+
assert not idx.equals(list(idx3))
|
87 |
+
assert not idx.equals(pd.Series(idx3))
|
88 |
+
|
89 |
+
|
90 |
+
class TestDatetimeIndexEquals(EqualsTests):
|
91 |
+
@pytest.fixture
|
92 |
+
def index(self):
|
93 |
+
return date_range("2013-01-01", periods=5)
|
94 |
+
|
95 |
+
def test_equals2(self):
|
96 |
+
# GH#13107
|
97 |
+
idx = DatetimeIndex(["2011-01-01", "2011-01-02", "NaT"])
|
98 |
+
assert idx.equals(idx)
|
99 |
+
assert idx.equals(idx.copy())
|
100 |
+
assert idx.equals(idx.astype(object))
|
101 |
+
assert idx.astype(object).equals(idx)
|
102 |
+
assert idx.astype(object).equals(idx.astype(object))
|
103 |
+
assert not idx.equals(list(idx))
|
104 |
+
assert not idx.equals(pd.Series(idx))
|
105 |
+
|
106 |
+
idx2 = DatetimeIndex(["2011-01-01", "2011-01-02", "NaT"], tz="US/Pacific")
|
107 |
+
assert not idx.equals(idx2)
|
108 |
+
assert not idx.equals(idx2.copy())
|
109 |
+
assert not idx.equals(idx2.astype(object))
|
110 |
+
assert not idx.astype(object).equals(idx2)
|
111 |
+
assert not idx.equals(list(idx2))
|
112 |
+
assert not idx.equals(pd.Series(idx2))
|
113 |
+
|
114 |
+
# same internal, different tz
|
115 |
+
idx3 = DatetimeIndex(idx.asi8, tz="US/Pacific")
|
116 |
+
tm.assert_numpy_array_equal(idx.asi8, idx3.asi8)
|
117 |
+
assert not idx.equals(idx3)
|
118 |
+
assert not idx.equals(idx3.copy())
|
119 |
+
assert not idx.equals(idx3.astype(object))
|
120 |
+
assert not idx.astype(object).equals(idx3)
|
121 |
+
assert not idx.equals(list(idx3))
|
122 |
+
assert not idx.equals(pd.Series(idx3))
|
123 |
+
|
124 |
+
# check that we do not raise when comparing with OutOfBounds objects
|
125 |
+
oob = Index([datetime(2500, 1, 1)] * 3, dtype=object)
|
126 |
+
assert not idx.equals(oob)
|
127 |
+
assert not idx2.equals(oob)
|
128 |
+
assert not idx3.equals(oob)
|
129 |
+
|
130 |
+
# check that we do not raise when comparing with OutOfBounds dt64
|
131 |
+
oob2 = oob.map(np.datetime64)
|
132 |
+
assert not idx.equals(oob2)
|
133 |
+
assert not idx2.equals(oob2)
|
134 |
+
assert not idx3.equals(oob2)
|
135 |
+
|
136 |
+
@pytest.mark.parametrize("freq", ["B", "C"])
|
137 |
+
def test_not_equals_bday(self, freq):
|
138 |
+
rng = date_range("2009-01-01", "2010-01-01", freq=freq)
|
139 |
+
assert not rng.equals(list(rng))
|
140 |
+
|
141 |
+
|
142 |
+
class TestTimedeltaIndexEquals(EqualsTests):
|
143 |
+
@pytest.fixture
|
144 |
+
def index(self):
|
145 |
+
return timedelta_range("1 day", periods=10)
|
146 |
+
|
147 |
+
def test_equals2(self):
|
148 |
+
# GH#13107
|
149 |
+
idx = TimedeltaIndex(["1 days", "2 days", "NaT"])
|
150 |
+
assert idx.equals(idx)
|
151 |
+
assert idx.equals(idx.copy())
|
152 |
+
assert idx.equals(idx.astype(object))
|
153 |
+
assert idx.astype(object).equals(idx)
|
154 |
+
assert idx.astype(object).equals(idx.astype(object))
|
155 |
+
assert not idx.equals(list(idx))
|
156 |
+
assert not idx.equals(pd.Series(idx))
|
157 |
+
|
158 |
+
idx2 = TimedeltaIndex(["2 days", "1 days", "NaT"])
|
159 |
+
assert not idx.equals(idx2)
|
160 |
+
assert not idx.equals(idx2.copy())
|
161 |
+
assert not idx.equals(idx2.astype(object))
|
162 |
+
assert not idx.astype(object).equals(idx2)
|
163 |
+
assert not idx.astype(object).equals(idx2.astype(object))
|
164 |
+
assert not idx.equals(list(idx2))
|
165 |
+
assert not idx.equals(pd.Series(idx2))
|
166 |
+
|
167 |
+
# Check that we dont raise OverflowError on comparisons outside the
|
168 |
+
# implementation range GH#28532
|
169 |
+
oob = Index([timedelta(days=10**6)] * 3, dtype=object)
|
170 |
+
assert not idx.equals(oob)
|
171 |
+
assert not idx2.equals(oob)
|
172 |
+
|
173 |
+
oob2 = Index([np.timedelta64(x) for x in oob], dtype=object)
|
174 |
+
assert (oob == oob2).all()
|
175 |
+
assert not idx.equals(oob2)
|
176 |
+
assert not idx2.equals(oob2)
|
177 |
+
|
178 |
+
oob3 = oob.map(np.timedelta64)
|
179 |
+
assert (oob3 == oob).all()
|
180 |
+
assert not idx.equals(oob3)
|
181 |
+
assert not idx2.equals(oob3)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_indexing.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
from pandas import (
|
6 |
+
DatetimeIndex,
|
7 |
+
Index,
|
8 |
+
)
|
9 |
+
import pandas._testing as tm
|
10 |
+
|
11 |
+
dtlike_dtypes = [
|
12 |
+
np.dtype("timedelta64[ns]"),
|
13 |
+
np.dtype("datetime64[ns]"),
|
14 |
+
pd.DatetimeTZDtype("ns", "Asia/Tokyo"),
|
15 |
+
pd.PeriodDtype("ns"),
|
16 |
+
]
|
17 |
+
|
18 |
+
|
19 |
+
@pytest.mark.parametrize("ldtype", dtlike_dtypes)
|
20 |
+
@pytest.mark.parametrize("rdtype", dtlike_dtypes)
|
21 |
+
def test_get_indexer_non_unique_wrong_dtype(ldtype, rdtype):
|
22 |
+
vals = np.tile(3600 * 10**9 * np.arange(3), 2)
|
23 |
+
|
24 |
+
def construct(dtype):
|
25 |
+
if dtype is dtlike_dtypes[-1]:
|
26 |
+
# PeriodArray will try to cast ints to strings
|
27 |
+
return DatetimeIndex(vals).astype(dtype)
|
28 |
+
return Index(vals, dtype=dtype)
|
29 |
+
|
30 |
+
left = construct(ldtype)
|
31 |
+
right = construct(rdtype)
|
32 |
+
|
33 |
+
result = left.get_indexer_non_unique(right)
|
34 |
+
|
35 |
+
if ldtype is rdtype:
|
36 |
+
ex1 = np.array([0, 3, 1, 4, 2, 5] * 2, dtype=np.intp)
|
37 |
+
ex2 = np.array([], dtype=np.intp)
|
38 |
+
tm.assert_numpy_array_equal(result[0], ex1)
|
39 |
+
tm.assert_numpy_array_equal(result[1], ex2)
|
40 |
+
|
41 |
+
else:
|
42 |
+
no_matches = np.array([-1] * 6, dtype=np.intp)
|
43 |
+
missing = np.arange(6, dtype=np.intp)
|
44 |
+
tm.assert_numpy_array_equal(result[0], no_matches)
|
45 |
+
tm.assert_numpy_array_equal(result[1], missing)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_is_monotonic.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pandas import (
|
2 |
+
Index,
|
3 |
+
NaT,
|
4 |
+
date_range,
|
5 |
+
)
|
6 |
+
|
7 |
+
|
8 |
+
def test_is_monotonic_with_nat():
|
9 |
+
# GH#31437
|
10 |
+
# PeriodIndex.is_monotonic_increasing should behave analogously to DatetimeIndex,
|
11 |
+
# in particular never be monotonic when we have NaT
|
12 |
+
dti = date_range("2016-01-01", periods=3)
|
13 |
+
pi = dti.to_period("D")
|
14 |
+
tdi = Index(dti.view("timedelta64[ns]"))
|
15 |
+
|
16 |
+
for obj in [pi, pi._engine, dti, dti._engine, tdi, tdi._engine]:
|
17 |
+
if isinstance(obj, Index):
|
18 |
+
# i.e. not Engines
|
19 |
+
assert obj.is_monotonic_increasing
|
20 |
+
assert obj.is_monotonic_increasing
|
21 |
+
assert not obj.is_monotonic_decreasing
|
22 |
+
assert obj.is_unique
|
23 |
+
|
24 |
+
dti1 = dti.insert(0, NaT)
|
25 |
+
pi1 = dti1.to_period("D")
|
26 |
+
tdi1 = Index(dti1.view("timedelta64[ns]"))
|
27 |
+
|
28 |
+
for obj in [pi1, pi1._engine, dti1, dti1._engine, tdi1, tdi1._engine]:
|
29 |
+
if isinstance(obj, Index):
|
30 |
+
# i.e. not Engines
|
31 |
+
assert not obj.is_monotonic_increasing
|
32 |
+
assert not obj.is_monotonic_increasing
|
33 |
+
assert not obj.is_monotonic_decreasing
|
34 |
+
assert obj.is_unique
|
35 |
+
|
36 |
+
dti2 = dti.insert(3, NaT)
|
37 |
+
pi2 = dti2.to_period("h")
|
38 |
+
tdi2 = Index(dti2.view("timedelta64[ns]"))
|
39 |
+
|
40 |
+
for obj in [pi2, pi2._engine, dti2, dti2._engine, tdi2, tdi2._engine]:
|
41 |
+
if isinstance(obj, Index):
|
42 |
+
# i.e. not Engines
|
43 |
+
assert not obj.is_monotonic_increasing
|
44 |
+
assert not obj.is_monotonic_increasing
|
45 |
+
assert not obj.is_monotonic_decreasing
|
46 |
+
assert obj.is_unique
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_nat.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
DatetimeIndex,
|
6 |
+
NaT,
|
7 |
+
PeriodIndex,
|
8 |
+
TimedeltaIndex,
|
9 |
+
)
|
10 |
+
import pandas._testing as tm
|
11 |
+
|
12 |
+
|
13 |
+
class NATests:
|
14 |
+
def test_nat(self, index_without_na):
|
15 |
+
empty_index = index_without_na[:0]
|
16 |
+
|
17 |
+
index_with_na = index_without_na.copy(deep=True)
|
18 |
+
index_with_na._data[1] = NaT
|
19 |
+
|
20 |
+
assert empty_index._na_value is NaT
|
21 |
+
assert index_with_na._na_value is NaT
|
22 |
+
assert index_without_na._na_value is NaT
|
23 |
+
|
24 |
+
idx = index_without_na
|
25 |
+
assert idx._can_hold_na
|
26 |
+
|
27 |
+
tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
|
28 |
+
assert idx.hasnans is False
|
29 |
+
|
30 |
+
idx = index_with_na
|
31 |
+
assert idx._can_hold_na
|
32 |
+
|
33 |
+
tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
|
34 |
+
assert idx.hasnans is True
|
35 |
+
|
36 |
+
|
37 |
+
class TestDatetimeIndexNA(NATests):
|
38 |
+
@pytest.fixture
|
39 |
+
def index_without_na(self, tz_naive_fixture):
|
40 |
+
tz = tz_naive_fixture
|
41 |
+
return DatetimeIndex(["2011-01-01", "2011-01-02"], tz=tz)
|
42 |
+
|
43 |
+
|
44 |
+
class TestTimedeltaIndexNA(NATests):
|
45 |
+
@pytest.fixture
|
46 |
+
def index_without_na(self):
|
47 |
+
return TimedeltaIndex(["1 days", "2 days"])
|
48 |
+
|
49 |
+
|
50 |
+
class TestPeriodIndexNA(NATests):
|
51 |
+
@pytest.fixture
|
52 |
+
def index_without_na(self):
|
53 |
+
return PeriodIndex(["2011-01-01", "2011-01-02"], freq="D")
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_sort_values.py
ADDED
@@ -0,0 +1,315 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
DatetimeIndex,
|
6 |
+
Index,
|
7 |
+
NaT,
|
8 |
+
PeriodIndex,
|
9 |
+
TimedeltaIndex,
|
10 |
+
timedelta_range,
|
11 |
+
)
|
12 |
+
import pandas._testing as tm
|
13 |
+
|
14 |
+
|
15 |
+
def check_freq_ascending(ordered, orig, ascending):
|
16 |
+
"""
|
17 |
+
Check the expected freq on a PeriodIndex/DatetimeIndex/TimedeltaIndex
|
18 |
+
when the original index is generated (or generate-able) with
|
19 |
+
period_range/date_range/timedelta_range.
|
20 |
+
"""
|
21 |
+
if isinstance(ordered, PeriodIndex):
|
22 |
+
assert ordered.freq == orig.freq
|
23 |
+
elif isinstance(ordered, (DatetimeIndex, TimedeltaIndex)):
|
24 |
+
if ascending:
|
25 |
+
assert ordered.freq.n == orig.freq.n
|
26 |
+
else:
|
27 |
+
assert ordered.freq.n == -1 * orig.freq.n
|
28 |
+
|
29 |
+
|
30 |
+
def check_freq_nonmonotonic(ordered, orig):
|
31 |
+
"""
|
32 |
+
Check the expected freq on a PeriodIndex/DatetimeIndex/TimedeltaIndex
|
33 |
+
when the original index is _not_ generated (or generate-able) with
|
34 |
+
period_range/date_range//timedelta_range.
|
35 |
+
"""
|
36 |
+
if isinstance(ordered, PeriodIndex):
|
37 |
+
assert ordered.freq == orig.freq
|
38 |
+
elif isinstance(ordered, (DatetimeIndex, TimedeltaIndex)):
|
39 |
+
assert ordered.freq is None
|
40 |
+
|
41 |
+
|
42 |
+
class TestSortValues:
|
43 |
+
@pytest.fixture(params=[DatetimeIndex, TimedeltaIndex, PeriodIndex])
|
44 |
+
def non_monotonic_idx(self, request):
|
45 |
+
if request.param is DatetimeIndex:
|
46 |
+
return DatetimeIndex(["2000-01-04", "2000-01-01", "2000-01-02"])
|
47 |
+
elif request.param is PeriodIndex:
|
48 |
+
dti = DatetimeIndex(["2000-01-04", "2000-01-01", "2000-01-02"])
|
49 |
+
return dti.to_period("D")
|
50 |
+
else:
|
51 |
+
return TimedeltaIndex(
|
52 |
+
["1 day 00:00:05", "1 day 00:00:01", "1 day 00:00:02"]
|
53 |
+
)
|
54 |
+
|
55 |
+
def test_argmin_argmax(self, non_monotonic_idx):
|
56 |
+
assert non_monotonic_idx.argmin() == 1
|
57 |
+
assert non_monotonic_idx.argmax() == 0
|
58 |
+
|
59 |
+
def test_sort_values(self, non_monotonic_idx):
|
60 |
+
idx = non_monotonic_idx
|
61 |
+
ordered = idx.sort_values()
|
62 |
+
assert ordered.is_monotonic_increasing
|
63 |
+
ordered = idx.sort_values(ascending=False)
|
64 |
+
assert ordered[::-1].is_monotonic_increasing
|
65 |
+
|
66 |
+
ordered, dexer = idx.sort_values(return_indexer=True)
|
67 |
+
assert ordered.is_monotonic_increasing
|
68 |
+
tm.assert_numpy_array_equal(dexer, np.array([1, 2, 0], dtype=np.intp))
|
69 |
+
|
70 |
+
ordered, dexer = idx.sort_values(return_indexer=True, ascending=False)
|
71 |
+
assert ordered[::-1].is_monotonic_increasing
|
72 |
+
tm.assert_numpy_array_equal(dexer, np.array([0, 2, 1], dtype=np.intp))
|
73 |
+
|
74 |
+
def check_sort_values_with_freq(self, idx):
|
75 |
+
ordered = idx.sort_values()
|
76 |
+
tm.assert_index_equal(ordered, idx)
|
77 |
+
check_freq_ascending(ordered, idx, True)
|
78 |
+
|
79 |
+
ordered = idx.sort_values(ascending=False)
|
80 |
+
expected = idx[::-1]
|
81 |
+
tm.assert_index_equal(ordered, expected)
|
82 |
+
check_freq_ascending(ordered, idx, False)
|
83 |
+
|
84 |
+
ordered, indexer = idx.sort_values(return_indexer=True)
|
85 |
+
tm.assert_index_equal(ordered, idx)
|
86 |
+
tm.assert_numpy_array_equal(indexer, np.array([0, 1, 2], dtype=np.intp))
|
87 |
+
check_freq_ascending(ordered, idx, True)
|
88 |
+
|
89 |
+
ordered, indexer = idx.sort_values(return_indexer=True, ascending=False)
|
90 |
+
expected = idx[::-1]
|
91 |
+
tm.assert_index_equal(ordered, expected)
|
92 |
+
tm.assert_numpy_array_equal(indexer, np.array([2, 1, 0], dtype=np.intp))
|
93 |
+
check_freq_ascending(ordered, idx, False)
|
94 |
+
|
95 |
+
@pytest.mark.parametrize("freq", ["D", "h"])
|
96 |
+
def test_sort_values_with_freq_timedeltaindex(self, freq):
|
97 |
+
# GH#10295
|
98 |
+
idx = timedelta_range(start=f"1{freq}", periods=3, freq=freq).rename("idx")
|
99 |
+
|
100 |
+
self.check_sort_values_with_freq(idx)
|
101 |
+
|
102 |
+
@pytest.mark.parametrize(
|
103 |
+
"idx",
|
104 |
+
[
|
105 |
+
DatetimeIndex(
|
106 |
+
["2011-01-01", "2011-01-02", "2011-01-03"], freq="D", name="idx"
|
107 |
+
),
|
108 |
+
DatetimeIndex(
|
109 |
+
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"],
|
110 |
+
freq="h",
|
111 |
+
name="tzidx",
|
112 |
+
tz="Asia/Tokyo",
|
113 |
+
),
|
114 |
+
],
|
115 |
+
)
|
116 |
+
def test_sort_values_with_freq_datetimeindex(self, idx):
|
117 |
+
self.check_sort_values_with_freq(idx)
|
118 |
+
|
119 |
+
@pytest.mark.parametrize("freq", ["D", "2D", "4D"])
|
120 |
+
def test_sort_values_with_freq_periodindex(self, freq):
|
121 |
+
# here with_freq refers to being period_range-like
|
122 |
+
idx = PeriodIndex(
|
123 |
+
["2011-01-01", "2011-01-02", "2011-01-03"], freq=freq, name="idx"
|
124 |
+
)
|
125 |
+
self.check_sort_values_with_freq(idx)
|
126 |
+
|
127 |
+
@pytest.mark.parametrize(
|
128 |
+
"idx",
|
129 |
+
[
|
130 |
+
PeriodIndex(["2011", "2012", "2013"], name="pidx", freq="Y"),
|
131 |
+
Index([2011, 2012, 2013], name="idx"), # for compatibility check
|
132 |
+
],
|
133 |
+
)
|
134 |
+
def test_sort_values_with_freq_periodindex2(self, idx):
|
135 |
+
# here with_freq indicates this is period_range-like
|
136 |
+
self.check_sort_values_with_freq(idx)
|
137 |
+
|
138 |
+
def check_sort_values_without_freq(self, idx, expected):
|
139 |
+
ordered = idx.sort_values(na_position="first")
|
140 |
+
tm.assert_index_equal(ordered, expected)
|
141 |
+
check_freq_nonmonotonic(ordered, idx)
|
142 |
+
|
143 |
+
if not idx.isna().any():
|
144 |
+
ordered = idx.sort_values()
|
145 |
+
tm.assert_index_equal(ordered, expected)
|
146 |
+
check_freq_nonmonotonic(ordered, idx)
|
147 |
+
|
148 |
+
ordered = idx.sort_values(ascending=False)
|
149 |
+
tm.assert_index_equal(ordered, expected[::-1])
|
150 |
+
check_freq_nonmonotonic(ordered, idx)
|
151 |
+
|
152 |
+
ordered, indexer = idx.sort_values(return_indexer=True, na_position="first")
|
153 |
+
tm.assert_index_equal(ordered, expected)
|
154 |
+
|
155 |
+
exp = np.array([0, 4, 3, 1, 2], dtype=np.intp)
|
156 |
+
tm.assert_numpy_array_equal(indexer, exp)
|
157 |
+
check_freq_nonmonotonic(ordered, idx)
|
158 |
+
|
159 |
+
if not idx.isna().any():
|
160 |
+
ordered, indexer = idx.sort_values(return_indexer=True)
|
161 |
+
tm.assert_index_equal(ordered, expected)
|
162 |
+
|
163 |
+
exp = np.array([0, 4, 3, 1, 2], dtype=np.intp)
|
164 |
+
tm.assert_numpy_array_equal(indexer, exp)
|
165 |
+
check_freq_nonmonotonic(ordered, idx)
|
166 |
+
|
167 |
+
ordered, indexer = idx.sort_values(return_indexer=True, ascending=False)
|
168 |
+
tm.assert_index_equal(ordered, expected[::-1])
|
169 |
+
|
170 |
+
exp = np.array([2, 1, 3, 0, 4], dtype=np.intp)
|
171 |
+
tm.assert_numpy_array_equal(indexer, exp)
|
172 |
+
check_freq_nonmonotonic(ordered, idx)
|
173 |
+
|
174 |
+
def test_sort_values_without_freq_timedeltaindex(self):
|
175 |
+
# GH#10295
|
176 |
+
|
177 |
+
idx = TimedeltaIndex(
|
178 |
+
["1 hour", "3 hour", "5 hour", "2 hour ", "1 hour"], name="idx1"
|
179 |
+
)
|
180 |
+
expected = TimedeltaIndex(
|
181 |
+
["1 hour", "1 hour", "2 hour", "3 hour", "5 hour"], name="idx1"
|
182 |
+
)
|
183 |
+
self.check_sort_values_without_freq(idx, expected)
|
184 |
+
|
185 |
+
@pytest.mark.parametrize(
|
186 |
+
"index_dates,expected_dates",
|
187 |
+
[
|
188 |
+
(
|
189 |
+
["2011-01-01", "2011-01-03", "2011-01-05", "2011-01-02", "2011-01-01"],
|
190 |
+
["2011-01-01", "2011-01-01", "2011-01-02", "2011-01-03", "2011-01-05"],
|
191 |
+
),
|
192 |
+
(
|
193 |
+
["2011-01-01", "2011-01-03", "2011-01-05", "2011-01-02", "2011-01-01"],
|
194 |
+
["2011-01-01", "2011-01-01", "2011-01-02", "2011-01-03", "2011-01-05"],
|
195 |
+
),
|
196 |
+
(
|
197 |
+
[NaT, "2011-01-03", "2011-01-05", "2011-01-02", NaT],
|
198 |
+
[NaT, NaT, "2011-01-02", "2011-01-03", "2011-01-05"],
|
199 |
+
),
|
200 |
+
],
|
201 |
+
)
|
202 |
+
def test_sort_values_without_freq_datetimeindex(
|
203 |
+
self, index_dates, expected_dates, tz_naive_fixture
|
204 |
+
):
|
205 |
+
tz = tz_naive_fixture
|
206 |
+
|
207 |
+
# without freq
|
208 |
+
idx = DatetimeIndex(index_dates, tz=tz, name="idx")
|
209 |
+
expected = DatetimeIndex(expected_dates, tz=tz, name="idx")
|
210 |
+
|
211 |
+
self.check_sort_values_without_freq(idx, expected)
|
212 |
+
|
213 |
+
@pytest.mark.parametrize(
|
214 |
+
"idx,expected",
|
215 |
+
[
|
216 |
+
(
|
217 |
+
PeriodIndex(
|
218 |
+
[
|
219 |
+
"2011-01-01",
|
220 |
+
"2011-01-03",
|
221 |
+
"2011-01-05",
|
222 |
+
"2011-01-02",
|
223 |
+
"2011-01-01",
|
224 |
+
],
|
225 |
+
freq="D",
|
226 |
+
name="idx1",
|
227 |
+
),
|
228 |
+
PeriodIndex(
|
229 |
+
[
|
230 |
+
"2011-01-01",
|
231 |
+
"2011-01-01",
|
232 |
+
"2011-01-02",
|
233 |
+
"2011-01-03",
|
234 |
+
"2011-01-05",
|
235 |
+
],
|
236 |
+
freq="D",
|
237 |
+
name="idx1",
|
238 |
+
),
|
239 |
+
),
|
240 |
+
(
|
241 |
+
PeriodIndex(
|
242 |
+
[
|
243 |
+
"2011-01-01",
|
244 |
+
"2011-01-03",
|
245 |
+
"2011-01-05",
|
246 |
+
"2011-01-02",
|
247 |
+
"2011-01-01",
|
248 |
+
],
|
249 |
+
freq="D",
|
250 |
+
name="idx2",
|
251 |
+
),
|
252 |
+
PeriodIndex(
|
253 |
+
[
|
254 |
+
"2011-01-01",
|
255 |
+
"2011-01-01",
|
256 |
+
"2011-01-02",
|
257 |
+
"2011-01-03",
|
258 |
+
"2011-01-05",
|
259 |
+
],
|
260 |
+
freq="D",
|
261 |
+
name="idx2",
|
262 |
+
),
|
263 |
+
),
|
264 |
+
(
|
265 |
+
PeriodIndex(
|
266 |
+
[NaT, "2011-01-03", "2011-01-05", "2011-01-02", NaT],
|
267 |
+
freq="D",
|
268 |
+
name="idx3",
|
269 |
+
),
|
270 |
+
PeriodIndex(
|
271 |
+
[NaT, NaT, "2011-01-02", "2011-01-03", "2011-01-05"],
|
272 |
+
freq="D",
|
273 |
+
name="idx3",
|
274 |
+
),
|
275 |
+
),
|
276 |
+
(
|
277 |
+
PeriodIndex(
|
278 |
+
["2011", "2013", "2015", "2012", "2011"], name="pidx", freq="Y"
|
279 |
+
),
|
280 |
+
PeriodIndex(
|
281 |
+
["2011", "2011", "2012", "2013", "2015"], name="pidx", freq="Y"
|
282 |
+
),
|
283 |
+
),
|
284 |
+
(
|
285 |
+
# For compatibility check
|
286 |
+
Index([2011, 2013, 2015, 2012, 2011], name="idx"),
|
287 |
+
Index([2011, 2011, 2012, 2013, 2015], name="idx"),
|
288 |
+
),
|
289 |
+
],
|
290 |
+
)
|
291 |
+
def test_sort_values_without_freq_periodindex(self, idx, expected):
|
292 |
+
# here without_freq means not generateable by period_range
|
293 |
+
self.check_sort_values_without_freq(idx, expected)
|
294 |
+
|
295 |
+
def test_sort_values_without_freq_periodindex_nat(self):
|
296 |
+
# doesn't quite fit into check_sort_values_without_freq
|
297 |
+
idx = PeriodIndex(["2011", "2013", "NaT", "2011"], name="pidx", freq="D")
|
298 |
+
expected = PeriodIndex(["NaT", "2011", "2011", "2013"], name="pidx", freq="D")
|
299 |
+
|
300 |
+
ordered = idx.sort_values(na_position="first")
|
301 |
+
tm.assert_index_equal(ordered, expected)
|
302 |
+
check_freq_nonmonotonic(ordered, idx)
|
303 |
+
|
304 |
+
ordered = idx.sort_values(ascending=False)
|
305 |
+
tm.assert_index_equal(ordered, expected[::-1])
|
306 |
+
check_freq_nonmonotonic(ordered, idx)
|
307 |
+
|
308 |
+
|
309 |
+
def test_order_stability_compat():
|
310 |
+
# GH#35922. sort_values is stable both for normal and datetime-like Index
|
311 |
+
pidx = PeriodIndex(["2011", "2013", "2015", "2012", "2011"], name="pidx", freq="Y")
|
312 |
+
iidx = Index([2011, 2013, 2015, 2012, 2011], name="idx")
|
313 |
+
ordered1, indexer1 = pidx.sort_values(return_indexer=True, ascending=False)
|
314 |
+
ordered2, indexer2 = iidx.sort_values(return_indexer=True, ascending=False)
|
315 |
+
tm.assert_numpy_array_equal(indexer1, indexer2)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimelike_/test_value_counts.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
from pandas import (
|
4 |
+
DatetimeIndex,
|
5 |
+
NaT,
|
6 |
+
PeriodIndex,
|
7 |
+
Series,
|
8 |
+
TimedeltaIndex,
|
9 |
+
date_range,
|
10 |
+
period_range,
|
11 |
+
timedelta_range,
|
12 |
+
)
|
13 |
+
import pandas._testing as tm
|
14 |
+
|
15 |
+
|
16 |
+
class TestValueCounts:
|
17 |
+
# GH#7735
|
18 |
+
|
19 |
+
def test_value_counts_unique_datetimeindex(self, tz_naive_fixture):
|
20 |
+
tz = tz_naive_fixture
|
21 |
+
orig = date_range("2011-01-01 09:00", freq="h", periods=10, tz=tz)
|
22 |
+
self._check_value_counts_with_repeats(orig)
|
23 |
+
|
24 |
+
def test_value_counts_unique_timedeltaindex(self):
|
25 |
+
orig = timedelta_range("1 days 09:00:00", freq="h", periods=10)
|
26 |
+
self._check_value_counts_with_repeats(orig)
|
27 |
+
|
28 |
+
def test_value_counts_unique_periodindex(self):
|
29 |
+
orig = period_range("2011-01-01 09:00", freq="h", periods=10)
|
30 |
+
self._check_value_counts_with_repeats(orig)
|
31 |
+
|
32 |
+
def _check_value_counts_with_repeats(self, orig):
|
33 |
+
# create repeated values, 'n'th element is repeated by n+1 times
|
34 |
+
idx = type(orig)(
|
35 |
+
np.repeat(orig._values, range(1, len(orig) + 1)), dtype=orig.dtype
|
36 |
+
)
|
37 |
+
|
38 |
+
exp_idx = orig[::-1]
|
39 |
+
if not isinstance(exp_idx, PeriodIndex):
|
40 |
+
exp_idx = exp_idx._with_freq(None)
|
41 |
+
expected = Series(range(10, 0, -1), index=exp_idx, dtype="int64", name="count")
|
42 |
+
|
43 |
+
for obj in [idx, Series(idx)]:
|
44 |
+
tm.assert_series_equal(obj.value_counts(), expected)
|
45 |
+
|
46 |
+
tm.assert_index_equal(idx.unique(), orig)
|
47 |
+
|
48 |
+
def test_value_counts_unique_datetimeindex2(self, tz_naive_fixture):
|
49 |
+
tz = tz_naive_fixture
|
50 |
+
idx = DatetimeIndex(
|
51 |
+
[
|
52 |
+
"2013-01-01 09:00",
|
53 |
+
"2013-01-01 09:00",
|
54 |
+
"2013-01-01 09:00",
|
55 |
+
"2013-01-01 08:00",
|
56 |
+
"2013-01-01 08:00",
|
57 |
+
NaT,
|
58 |
+
],
|
59 |
+
tz=tz,
|
60 |
+
)
|
61 |
+
self._check_value_counts_dropna(idx)
|
62 |
+
|
63 |
+
def test_value_counts_unique_timedeltaindex2(self):
|
64 |
+
idx = TimedeltaIndex(
|
65 |
+
[
|
66 |
+
"1 days 09:00:00",
|
67 |
+
"1 days 09:00:00",
|
68 |
+
"1 days 09:00:00",
|
69 |
+
"1 days 08:00:00",
|
70 |
+
"1 days 08:00:00",
|
71 |
+
NaT,
|
72 |
+
]
|
73 |
+
)
|
74 |
+
self._check_value_counts_dropna(idx)
|
75 |
+
|
76 |
+
def test_value_counts_unique_periodindex2(self):
|
77 |
+
idx = PeriodIndex(
|
78 |
+
[
|
79 |
+
"2013-01-01 09:00",
|
80 |
+
"2013-01-01 09:00",
|
81 |
+
"2013-01-01 09:00",
|
82 |
+
"2013-01-01 08:00",
|
83 |
+
"2013-01-01 08:00",
|
84 |
+
NaT,
|
85 |
+
],
|
86 |
+
freq="h",
|
87 |
+
)
|
88 |
+
self._check_value_counts_dropna(idx)
|
89 |
+
|
90 |
+
def _check_value_counts_dropna(self, idx):
|
91 |
+
exp_idx = idx[[2, 3]]
|
92 |
+
expected = Series([3, 2], index=exp_idx, name="count")
|
93 |
+
|
94 |
+
for obj in [idx, Series(idx)]:
|
95 |
+
tm.assert_series_equal(obj.value_counts(), expected)
|
96 |
+
|
97 |
+
exp_idx = idx[[2, 3, -1]]
|
98 |
+
expected = Series([3, 2, 1], index=exp_idx, name="count")
|
99 |
+
|
100 |
+
for obj in [idx, Series(idx)]:
|
101 |
+
tm.assert_series_equal(obj.value_counts(dropna=False), expected)
|
102 |
+
|
103 |
+
tm.assert_index_equal(idx.unique(), exp_idx)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (195 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_arithmetic.cpython-310.pyc
ADDED
Binary file (2.01 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_constructors.cpython-310.pyc
ADDED
Binary file (32.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_date_range.cpython-310.pyc
ADDED
Binary file (50.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_datetime.cpython-310.pyc
ADDED
Binary file (6.93 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_formats.cpython-310.pyc
ADDED
Binary file (11 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_freq_attr.cpython-310.pyc
ADDED
Binary file (1.79 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_indexing.cpython-310.pyc
ADDED
Binary file (22.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_iter.cpython-310.pyc
ADDED
Binary file (2.89 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_join.cpython-310.pyc
ADDED
Binary file (4.88 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_npfuncs.cpython-310.pyc
ADDED
Binary file (820 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_ops.cpython-310.pyc
ADDED
Binary file (2.09 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_partial_slicing.cpython-310.pyc
ADDED
Binary file (12.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_pickle.cpython-310.pyc
ADDED
Binary file (1.89 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_reindex.cpython-310.pyc
ADDED
Binary file (2.29 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_scalar_compat.cpython-310.pyc
ADDED
Binary file (9.38 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_setops.cpython-310.pyc
ADDED
Binary file (18 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_timezones.cpython-310.pyc
ADDED
Binary file (7.65 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_asof.cpython-310.pyc
ADDED
Binary file (1.17 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_fillna.cpython-310.pyc
ADDED
Binary file (1.33 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_insert.cpython-310.pyc
ADDED
Binary file (6.93 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_map.cpython-310.pyc
ADDED
Binary file (2.37 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_normalize.cpython-310.pyc
ADDED
Binary file (2.58 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_round.cpython-310.pyc
ADDED
Binary file (6.02 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_period.cpython-310.pyc
ADDED
Binary file (7.39 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_series.cpython-310.pyc
ADDED
Binary file (940 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_factorize.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
DatetimeIndex,
|
6 |
+
Index,
|
7 |
+
date_range,
|
8 |
+
factorize,
|
9 |
+
)
|
10 |
+
import pandas._testing as tm
|
11 |
+
|
12 |
+
|
13 |
+
class TestDatetimeIndexFactorize:
|
14 |
+
def test_factorize(self):
|
15 |
+
idx1 = DatetimeIndex(
|
16 |
+
["2014-01", "2014-01", "2014-02", "2014-02", "2014-03", "2014-03"]
|
17 |
+
)
|
18 |
+
|
19 |
+
exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp)
|
20 |
+
exp_idx = DatetimeIndex(["2014-01", "2014-02", "2014-03"])
|
21 |
+
|
22 |
+
arr, idx = idx1.factorize()
|
23 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
24 |
+
tm.assert_index_equal(idx, exp_idx)
|
25 |
+
assert idx.freq == exp_idx.freq
|
26 |
+
|
27 |
+
arr, idx = idx1.factorize(sort=True)
|
28 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
29 |
+
tm.assert_index_equal(idx, exp_idx)
|
30 |
+
assert idx.freq == exp_idx.freq
|
31 |
+
|
32 |
+
# tz must be preserved
|
33 |
+
idx1 = idx1.tz_localize("Asia/Tokyo")
|
34 |
+
exp_idx = exp_idx.tz_localize("Asia/Tokyo")
|
35 |
+
|
36 |
+
arr, idx = idx1.factorize()
|
37 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
38 |
+
tm.assert_index_equal(idx, exp_idx)
|
39 |
+
assert idx.freq == exp_idx.freq
|
40 |
+
|
41 |
+
idx2 = DatetimeIndex(
|
42 |
+
["2014-03", "2014-03", "2014-02", "2014-01", "2014-03", "2014-01"]
|
43 |
+
)
|
44 |
+
|
45 |
+
exp_arr = np.array([2, 2, 1, 0, 2, 0], dtype=np.intp)
|
46 |
+
exp_idx = DatetimeIndex(["2014-01", "2014-02", "2014-03"])
|
47 |
+
arr, idx = idx2.factorize(sort=True)
|
48 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
49 |
+
tm.assert_index_equal(idx, exp_idx)
|
50 |
+
assert idx.freq == exp_idx.freq
|
51 |
+
|
52 |
+
exp_arr = np.array([0, 0, 1, 2, 0, 2], dtype=np.intp)
|
53 |
+
exp_idx = DatetimeIndex(["2014-03", "2014-02", "2014-01"])
|
54 |
+
arr, idx = idx2.factorize()
|
55 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
56 |
+
tm.assert_index_equal(idx, exp_idx)
|
57 |
+
assert idx.freq == exp_idx.freq
|
58 |
+
|
59 |
+
def test_factorize_preserves_freq(self):
|
60 |
+
# GH#38120 freq should be preserved
|
61 |
+
idx3 = date_range("2000-01", periods=4, freq="ME", tz="Asia/Tokyo")
|
62 |
+
exp_arr = np.array([0, 1, 2, 3], dtype=np.intp)
|
63 |
+
|
64 |
+
arr, idx = idx3.factorize()
|
65 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
66 |
+
tm.assert_index_equal(idx, idx3)
|
67 |
+
assert idx.freq == idx3.freq
|
68 |
+
|
69 |
+
arr, idx = factorize(idx3)
|
70 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
71 |
+
tm.assert_index_equal(idx, idx3)
|
72 |
+
assert idx.freq == idx3.freq
|
73 |
+
|
74 |
+
def test_factorize_tz(self, tz_naive_fixture, index_or_series):
|
75 |
+
tz = tz_naive_fixture
|
76 |
+
# GH#13750
|
77 |
+
base = date_range("2016-11-05", freq="h", periods=100, tz=tz)
|
78 |
+
idx = base.repeat(5)
|
79 |
+
|
80 |
+
exp_arr = np.arange(100, dtype=np.intp).repeat(5)
|
81 |
+
|
82 |
+
obj = index_or_series(idx)
|
83 |
+
|
84 |
+
arr, res = obj.factorize()
|
85 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
86 |
+
expected = base._with_freq(None)
|
87 |
+
tm.assert_index_equal(res, expected)
|
88 |
+
assert res.freq == expected.freq
|
89 |
+
|
90 |
+
def test_factorize_dst(self, index_or_series):
|
91 |
+
# GH#13750
|
92 |
+
idx = date_range("2016-11-06", freq="h", periods=12, tz="US/Eastern")
|
93 |
+
obj = index_or_series(idx)
|
94 |
+
|
95 |
+
arr, res = obj.factorize()
|
96 |
+
tm.assert_numpy_array_equal(arr, np.arange(12, dtype=np.intp))
|
97 |
+
tm.assert_index_equal(res, idx)
|
98 |
+
if index_or_series is Index:
|
99 |
+
assert res.freq == idx.freq
|
100 |
+
|
101 |
+
idx = date_range("2016-06-13", freq="h", periods=12, tz="US/Eastern")
|
102 |
+
obj = index_or_series(idx)
|
103 |
+
|
104 |
+
arr, res = obj.factorize()
|
105 |
+
tm.assert_numpy_array_equal(arr, np.arange(12, dtype=np.intp))
|
106 |
+
tm.assert_index_equal(res, idx)
|
107 |
+
if index_or_series is Index:
|
108 |
+
assert res.freq == idx.freq
|
109 |
+
|
110 |
+
@pytest.mark.parametrize("sort", [True, False])
|
111 |
+
def test_factorize_no_freq_non_nano(self, tz_naive_fixture, sort):
|
112 |
+
# GH#51978 case that does not go through the fastpath based on
|
113 |
+
# non-None freq
|
114 |
+
tz = tz_naive_fixture
|
115 |
+
idx = date_range("2016-11-06", freq="h", periods=5, tz=tz)[[0, 4, 1, 3, 2]]
|
116 |
+
exp_codes, exp_uniques = idx.factorize(sort=sort)
|
117 |
+
|
118 |
+
res_codes, res_uniques = idx.as_unit("s").factorize(sort=sort)
|
119 |
+
|
120 |
+
tm.assert_numpy_array_equal(res_codes, exp_codes)
|
121 |
+
tm.assert_index_equal(res_uniques, exp_uniques.as_unit("s"))
|
122 |
+
|
123 |
+
res_codes, res_uniques = idx.as_unit("s").to_series().factorize(sort=sort)
|
124 |
+
tm.assert_numpy_array_equal(res_codes, exp_codes)
|
125 |
+
tm.assert_index_equal(res_uniques, exp_uniques.as_unit("s"))
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_fillna.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
import pandas as pd
|
4 |
+
import pandas._testing as tm
|
5 |
+
|
6 |
+
|
7 |
+
class TestDatetimeIndexFillNA:
|
8 |
+
@pytest.mark.parametrize("tz", ["US/Eastern", "Asia/Tokyo"])
|
9 |
+
def test_fillna_datetime64(self, tz):
|
10 |
+
# GH 11343
|
11 |
+
idx = pd.DatetimeIndex(["2011-01-01 09:00", pd.NaT, "2011-01-01 11:00"])
|
12 |
+
|
13 |
+
exp = pd.DatetimeIndex(
|
14 |
+
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"]
|
15 |
+
)
|
16 |
+
tm.assert_index_equal(idx.fillna(pd.Timestamp("2011-01-01 10:00")), exp)
|
17 |
+
|
18 |
+
# tz mismatch
|
19 |
+
exp = pd.Index(
|
20 |
+
[
|
21 |
+
pd.Timestamp("2011-01-01 09:00"),
|
22 |
+
pd.Timestamp("2011-01-01 10:00", tz=tz),
|
23 |
+
pd.Timestamp("2011-01-01 11:00"),
|
24 |
+
],
|
25 |
+
dtype=object,
|
26 |
+
)
|
27 |
+
tm.assert_index_equal(idx.fillna(pd.Timestamp("2011-01-01 10:00", tz=tz)), exp)
|
28 |
+
|
29 |
+
# object
|
30 |
+
exp = pd.Index(
|
31 |
+
[pd.Timestamp("2011-01-01 09:00"), "x", pd.Timestamp("2011-01-01 11:00")],
|
32 |
+
dtype=object,
|
33 |
+
)
|
34 |
+
tm.assert_index_equal(idx.fillna("x"), exp)
|
35 |
+
|
36 |
+
idx = pd.DatetimeIndex(["2011-01-01 09:00", pd.NaT, "2011-01-01 11:00"], tz=tz)
|
37 |
+
|
38 |
+
exp = pd.DatetimeIndex(
|
39 |
+
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"], tz=tz
|
40 |
+
)
|
41 |
+
tm.assert_index_equal(idx.fillna(pd.Timestamp("2011-01-01 10:00", tz=tz)), exp)
|
42 |
+
|
43 |
+
exp = pd.Index(
|
44 |
+
[
|
45 |
+
pd.Timestamp("2011-01-01 09:00", tz=tz),
|
46 |
+
pd.Timestamp("2011-01-01 10:00"),
|
47 |
+
pd.Timestamp("2011-01-01 11:00", tz=tz),
|
48 |
+
],
|
49 |
+
dtype=object,
|
50 |
+
)
|
51 |
+
tm.assert_index_equal(idx.fillna(pd.Timestamp("2011-01-01 10:00")), exp)
|
52 |
+
|
53 |
+
# object
|
54 |
+
exp = pd.Index(
|
55 |
+
[
|
56 |
+
pd.Timestamp("2011-01-01 09:00", tz=tz),
|
57 |
+
"x",
|
58 |
+
pd.Timestamp("2011-01-01 11:00", tz=tz),
|
59 |
+
],
|
60 |
+
dtype=object,
|
61 |
+
)
|
62 |
+
tm.assert_index_equal(idx.fillna("x"), exp)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_isocalendar.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pandas import (
|
2 |
+
DataFrame,
|
3 |
+
DatetimeIndex,
|
4 |
+
date_range,
|
5 |
+
)
|
6 |
+
import pandas._testing as tm
|
7 |
+
|
8 |
+
|
9 |
+
def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
|
10 |
+
# GH#6538: Check that DatetimeIndex and its TimeStamp elements
|
11 |
+
# return the same weekofyear accessor close to new year w/ tz
|
12 |
+
dates = ["2013/12/29", "2013/12/30", "2013/12/31"]
|
13 |
+
dates = DatetimeIndex(dates, tz="Europe/Brussels")
|
14 |
+
result = dates.isocalendar()
|
15 |
+
expected_data_frame = DataFrame(
|
16 |
+
[[2013, 52, 7], [2014, 1, 1], [2014, 1, 2]],
|
17 |
+
columns=["year", "week", "day"],
|
18 |
+
index=dates,
|
19 |
+
dtype="UInt32",
|
20 |
+
)
|
21 |
+
tm.assert_frame_equal(result, expected_data_frame)
|
22 |
+
|
23 |
+
|
24 |
+
def test_dti_timestamp_isocalendar_fields():
|
25 |
+
idx = date_range("2020-01-01", periods=10)
|
26 |
+
expected = tuple(idx.isocalendar().iloc[-1].to_list())
|
27 |
+
result = idx[-1].isocalendar()
|
28 |
+
assert result == expected
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_map.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas import (
|
4 |
+
DatetimeIndex,
|
5 |
+
Index,
|
6 |
+
MultiIndex,
|
7 |
+
Period,
|
8 |
+
date_range,
|
9 |
+
)
|
10 |
+
import pandas._testing as tm
|
11 |
+
|
12 |
+
|
13 |
+
class TestMap:
|
14 |
+
def test_map(self):
|
15 |
+
rng = date_range("1/1/2000", periods=10)
|
16 |
+
|
17 |
+
f = lambda x: x.strftime("%Y%m%d")
|
18 |
+
result = rng.map(f)
|
19 |
+
exp = Index([f(x) for x in rng])
|
20 |
+
tm.assert_index_equal(result, exp)
|
21 |
+
|
22 |
+
def test_map_fallthrough(self, capsys):
|
23 |
+
# GH#22067, check we don't get warnings about silently ignored errors
|
24 |
+
dti = date_range("2017-01-01", "2018-01-01", freq="B")
|
25 |
+
|
26 |
+
dti.map(lambda x: Period(year=x.year, month=x.month, freq="M"))
|
27 |
+
|
28 |
+
captured = capsys.readouterr()
|
29 |
+
assert captured.err == ""
|
30 |
+
|
31 |
+
def test_map_bug_1677(self):
|
32 |
+
index = DatetimeIndex(["2012-04-25 09:30:00.393000"])
|
33 |
+
f = index.asof
|
34 |
+
|
35 |
+
result = index.map(f)
|
36 |
+
expected = Index([f(index[0])])
|
37 |
+
tm.assert_index_equal(result, expected)
|
38 |
+
|
39 |
+
@pytest.mark.parametrize("name", [None, "name"])
|
40 |
+
def test_index_map(self, name):
|
41 |
+
# see GH#20990
|
42 |
+
count = 6
|
43 |
+
index = date_range("2018-01-01", periods=count, freq="ME", name=name).map(
|
44 |
+
lambda x: (x.year, x.month)
|
45 |
+
)
|
46 |
+
exp_index = MultiIndex.from_product(((2018,), range(1, 7)), names=[name, name])
|
47 |
+
tm.assert_index_equal(index, exp_index)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_repeat.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
DatetimeIndex,
|
6 |
+
Timestamp,
|
7 |
+
date_range,
|
8 |
+
)
|
9 |
+
import pandas._testing as tm
|
10 |
+
|
11 |
+
|
12 |
+
class TestRepeat:
|
13 |
+
def test_repeat_range(self, tz_naive_fixture):
|
14 |
+
rng = date_range("1/1/2000", "1/1/2001")
|
15 |
+
|
16 |
+
result = rng.repeat(5)
|
17 |
+
assert result.freq is None
|
18 |
+
assert len(result) == 5 * len(rng)
|
19 |
+
|
20 |
+
def test_repeat_range2(self, tz_naive_fixture, unit):
|
21 |
+
tz = tz_naive_fixture
|
22 |
+
index = date_range("2001-01-01", periods=2, freq="D", tz=tz, unit=unit)
|
23 |
+
exp = DatetimeIndex(
|
24 |
+
["2001-01-01", "2001-01-01", "2001-01-02", "2001-01-02"], tz=tz
|
25 |
+
).as_unit(unit)
|
26 |
+
for res in [index.repeat(2), np.repeat(index, 2)]:
|
27 |
+
tm.assert_index_equal(res, exp)
|
28 |
+
assert res.freq is None
|
29 |
+
|
30 |
+
def test_repeat_range3(self, tz_naive_fixture, unit):
|
31 |
+
tz = tz_naive_fixture
|
32 |
+
index = date_range("2001-01-01", periods=2, freq="2D", tz=tz, unit=unit)
|
33 |
+
exp = DatetimeIndex(
|
34 |
+
["2001-01-01", "2001-01-01", "2001-01-03", "2001-01-03"], tz=tz
|
35 |
+
).as_unit(unit)
|
36 |
+
for res in [index.repeat(2), np.repeat(index, 2)]:
|
37 |
+
tm.assert_index_equal(res, exp)
|
38 |
+
assert res.freq is None
|
39 |
+
|
40 |
+
def test_repeat_range4(self, tz_naive_fixture, unit):
|
41 |
+
tz = tz_naive_fixture
|
42 |
+
index = DatetimeIndex(["2001-01-01", "NaT", "2003-01-01"], tz=tz).as_unit(unit)
|
43 |
+
exp = DatetimeIndex(
|
44 |
+
[
|
45 |
+
"2001-01-01",
|
46 |
+
"2001-01-01",
|
47 |
+
"2001-01-01",
|
48 |
+
"NaT",
|
49 |
+
"NaT",
|
50 |
+
"NaT",
|
51 |
+
"2003-01-01",
|
52 |
+
"2003-01-01",
|
53 |
+
"2003-01-01",
|
54 |
+
],
|
55 |
+
tz=tz,
|
56 |
+
).as_unit(unit)
|
57 |
+
for res in [index.repeat(3), np.repeat(index, 3)]:
|
58 |
+
tm.assert_index_equal(res, exp)
|
59 |
+
assert res.freq is None
|
60 |
+
|
61 |
+
def test_repeat(self, tz_naive_fixture, unit):
|
62 |
+
tz = tz_naive_fixture
|
63 |
+
reps = 2
|
64 |
+
msg = "the 'axis' parameter is not supported"
|
65 |
+
|
66 |
+
rng = date_range(start="2016-01-01", periods=2, freq="30Min", tz=tz, unit=unit)
|
67 |
+
|
68 |
+
expected_rng = DatetimeIndex(
|
69 |
+
[
|
70 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
71 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
72 |
+
Timestamp("2016-01-01 00:30:00", tz=tz),
|
73 |
+
Timestamp("2016-01-01 00:30:00", tz=tz),
|
74 |
+
]
|
75 |
+
).as_unit(unit)
|
76 |
+
|
77 |
+
res = rng.repeat(reps)
|
78 |
+
tm.assert_index_equal(res, expected_rng)
|
79 |
+
assert res.freq is None
|
80 |
+
|
81 |
+
tm.assert_index_equal(np.repeat(rng, reps), expected_rng)
|
82 |
+
with pytest.raises(ValueError, match=msg):
|
83 |
+
np.repeat(rng, reps, axis=1)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_resolution.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dateutil.tz import tzlocal
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas.compat import IS64
|
5 |
+
|
6 |
+
from pandas import date_range
|
7 |
+
|
8 |
+
|
9 |
+
@pytest.mark.parametrize(
|
10 |
+
"freq,expected",
|
11 |
+
[
|
12 |
+
("YE", "day"),
|
13 |
+
("QE", "day"),
|
14 |
+
("ME", "day"),
|
15 |
+
("D", "day"),
|
16 |
+
("h", "hour"),
|
17 |
+
("min", "minute"),
|
18 |
+
("s", "second"),
|
19 |
+
("ms", "millisecond"),
|
20 |
+
("us", "microsecond"),
|
21 |
+
],
|
22 |
+
)
|
23 |
+
def test_dti_resolution(request, tz_naive_fixture, freq, expected):
|
24 |
+
tz = tz_naive_fixture
|
25 |
+
if freq == "YE" and not IS64 and isinstance(tz, tzlocal):
|
26 |
+
request.applymarker(
|
27 |
+
pytest.mark.xfail(reason="OverflowError inside tzlocal past 2038")
|
28 |
+
)
|
29 |
+
|
30 |
+
idx = date_range(start="2013-04-01", periods=30, freq=freq, tz=tz)
|
31 |
+
assert idx.resolution == expected
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_round.py
ADDED
@@ -0,0 +1,221 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas._libs.tslibs import to_offset
|
4 |
+
from pandas._libs.tslibs.offsets import INVALID_FREQ_ERR_MSG
|
5 |
+
|
6 |
+
from pandas import (
|
7 |
+
DatetimeIndex,
|
8 |
+
Timestamp,
|
9 |
+
date_range,
|
10 |
+
)
|
11 |
+
import pandas._testing as tm
|
12 |
+
|
13 |
+
|
14 |
+
class TestDatetimeIndexRound:
|
15 |
+
def test_round_daily(self):
|
16 |
+
dti = date_range("20130101 09:10:11", periods=5)
|
17 |
+
result = dti.round("D")
|
18 |
+
expected = date_range("20130101", periods=5)
|
19 |
+
tm.assert_index_equal(result, expected)
|
20 |
+
|
21 |
+
dti = dti.tz_localize("UTC").tz_convert("US/Eastern")
|
22 |
+
result = dti.round("D")
|
23 |
+
expected = date_range("20130101", periods=5).tz_localize("US/Eastern")
|
24 |
+
tm.assert_index_equal(result, expected)
|
25 |
+
|
26 |
+
result = dti.round("s")
|
27 |
+
tm.assert_index_equal(result, dti)
|
28 |
+
|
29 |
+
@pytest.mark.parametrize(
|
30 |
+
"freq, error_msg",
|
31 |
+
[
|
32 |
+
("YE", "<YearEnd: month=12> is a non-fixed frequency"),
|
33 |
+
("ME", "<MonthEnd> is a non-fixed frequency"),
|
34 |
+
("foobar", "Invalid frequency: foobar"),
|
35 |
+
],
|
36 |
+
)
|
37 |
+
def test_round_invalid(self, freq, error_msg):
|
38 |
+
dti = date_range("20130101 09:10:11", periods=5)
|
39 |
+
dti = dti.tz_localize("UTC").tz_convert("US/Eastern")
|
40 |
+
with pytest.raises(ValueError, match=error_msg):
|
41 |
+
dti.round(freq)
|
42 |
+
|
43 |
+
def test_round(self, tz_naive_fixture, unit):
|
44 |
+
tz = tz_naive_fixture
|
45 |
+
rng = date_range(start="2016-01-01", periods=5, freq="30Min", tz=tz, unit=unit)
|
46 |
+
elt = rng[1]
|
47 |
+
|
48 |
+
expected_rng = DatetimeIndex(
|
49 |
+
[
|
50 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
51 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
52 |
+
Timestamp("2016-01-01 01:00:00", tz=tz),
|
53 |
+
Timestamp("2016-01-01 02:00:00", tz=tz),
|
54 |
+
Timestamp("2016-01-01 02:00:00", tz=tz),
|
55 |
+
]
|
56 |
+
).as_unit(unit)
|
57 |
+
expected_elt = expected_rng[1]
|
58 |
+
|
59 |
+
result = rng.round(freq="h")
|
60 |
+
tm.assert_index_equal(result, expected_rng)
|
61 |
+
assert elt.round(freq="h") == expected_elt
|
62 |
+
|
63 |
+
msg = INVALID_FREQ_ERR_MSG
|
64 |
+
with pytest.raises(ValueError, match=msg):
|
65 |
+
rng.round(freq="foo")
|
66 |
+
with pytest.raises(ValueError, match=msg):
|
67 |
+
elt.round(freq="foo")
|
68 |
+
|
69 |
+
msg = "<MonthEnd> is a non-fixed frequency"
|
70 |
+
with pytest.raises(ValueError, match=msg):
|
71 |
+
rng.round(freq="ME")
|
72 |
+
with pytest.raises(ValueError, match=msg):
|
73 |
+
elt.round(freq="ME")
|
74 |
+
|
75 |
+
def test_round2(self, tz_naive_fixture):
|
76 |
+
tz = tz_naive_fixture
|
77 |
+
# GH#14440 & GH#15578
|
78 |
+
index = DatetimeIndex(["2016-10-17 12:00:00.0015"], tz=tz).as_unit("ns")
|
79 |
+
result = index.round("ms")
|
80 |
+
expected = DatetimeIndex(["2016-10-17 12:00:00.002000"], tz=tz).as_unit("ns")
|
81 |
+
tm.assert_index_equal(result, expected)
|
82 |
+
|
83 |
+
for freq in ["us", "ns"]:
|
84 |
+
tm.assert_index_equal(index, index.round(freq))
|
85 |
+
|
86 |
+
def test_round3(self, tz_naive_fixture):
|
87 |
+
tz = tz_naive_fixture
|
88 |
+
index = DatetimeIndex(["2016-10-17 12:00:00.00149"], tz=tz).as_unit("ns")
|
89 |
+
result = index.round("ms")
|
90 |
+
expected = DatetimeIndex(["2016-10-17 12:00:00.001000"], tz=tz).as_unit("ns")
|
91 |
+
tm.assert_index_equal(result, expected)
|
92 |
+
|
93 |
+
def test_round4(self, tz_naive_fixture):
|
94 |
+
index = DatetimeIndex(["2016-10-17 12:00:00.001501031"], dtype="M8[ns]")
|
95 |
+
result = index.round("10ns")
|
96 |
+
expected = DatetimeIndex(["2016-10-17 12:00:00.001501030"], dtype="M8[ns]")
|
97 |
+
tm.assert_index_equal(result, expected)
|
98 |
+
|
99 |
+
ts = "2016-10-17 12:00:00.001501031"
|
100 |
+
dti = DatetimeIndex([ts], dtype="M8[ns]")
|
101 |
+
with tm.assert_produces_warning(False):
|
102 |
+
dti.round("1010ns")
|
103 |
+
|
104 |
+
def test_no_rounding_occurs(self, tz_naive_fixture):
|
105 |
+
# GH 21262
|
106 |
+
tz = tz_naive_fixture
|
107 |
+
rng = date_range(start="2016-01-01", periods=5, freq="2Min", tz=tz)
|
108 |
+
|
109 |
+
expected_rng = DatetimeIndex(
|
110 |
+
[
|
111 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
112 |
+
Timestamp("2016-01-01 00:02:00", tz=tz),
|
113 |
+
Timestamp("2016-01-01 00:04:00", tz=tz),
|
114 |
+
Timestamp("2016-01-01 00:06:00", tz=tz),
|
115 |
+
Timestamp("2016-01-01 00:08:00", tz=tz),
|
116 |
+
]
|
117 |
+
).as_unit("ns")
|
118 |
+
|
119 |
+
result = rng.round(freq="2min")
|
120 |
+
tm.assert_index_equal(result, expected_rng)
|
121 |
+
|
122 |
+
@pytest.mark.parametrize(
|
123 |
+
"test_input, rounder, freq, expected",
|
124 |
+
[
|
125 |
+
(["2117-01-01 00:00:45"], "floor", "15s", ["2117-01-01 00:00:45"]),
|
126 |
+
(["2117-01-01 00:00:45"], "ceil", "15s", ["2117-01-01 00:00:45"]),
|
127 |
+
(
|
128 |
+
["2117-01-01 00:00:45.000000012"],
|
129 |
+
"floor",
|
130 |
+
"10ns",
|
131 |
+
["2117-01-01 00:00:45.000000010"],
|
132 |
+
),
|
133 |
+
(
|
134 |
+
["1823-01-01 00:00:01.000000012"],
|
135 |
+
"ceil",
|
136 |
+
"10ns",
|
137 |
+
["1823-01-01 00:00:01.000000020"],
|
138 |
+
),
|
139 |
+
(["1823-01-01 00:00:01"], "floor", "1s", ["1823-01-01 00:00:01"]),
|
140 |
+
(["1823-01-01 00:00:01"], "ceil", "1s", ["1823-01-01 00:00:01"]),
|
141 |
+
(["2018-01-01 00:15:00"], "ceil", "15min", ["2018-01-01 00:15:00"]),
|
142 |
+
(["2018-01-01 00:15:00"], "floor", "15min", ["2018-01-01 00:15:00"]),
|
143 |
+
(["1823-01-01 03:00:00"], "ceil", "3h", ["1823-01-01 03:00:00"]),
|
144 |
+
(["1823-01-01 03:00:00"], "floor", "3h", ["1823-01-01 03:00:00"]),
|
145 |
+
(
|
146 |
+
("NaT", "1823-01-01 00:00:01"),
|
147 |
+
"floor",
|
148 |
+
"1s",
|
149 |
+
("NaT", "1823-01-01 00:00:01"),
|
150 |
+
),
|
151 |
+
(
|
152 |
+
("NaT", "1823-01-01 00:00:01"),
|
153 |
+
"ceil",
|
154 |
+
"1s",
|
155 |
+
("NaT", "1823-01-01 00:00:01"),
|
156 |
+
),
|
157 |
+
],
|
158 |
+
)
|
159 |
+
def test_ceil_floor_edge(self, test_input, rounder, freq, expected):
|
160 |
+
dt = DatetimeIndex(list(test_input))
|
161 |
+
func = getattr(dt, rounder)
|
162 |
+
result = func(freq)
|
163 |
+
expected = DatetimeIndex(list(expected))
|
164 |
+
assert expected.equals(result)
|
165 |
+
|
166 |
+
@pytest.mark.parametrize(
|
167 |
+
"start, index_freq, periods",
|
168 |
+
[("2018-01-01", "12h", 25), ("2018-01-01 0:0:0.124999", "1ns", 1000)],
|
169 |
+
)
|
170 |
+
@pytest.mark.parametrize(
|
171 |
+
"round_freq",
|
172 |
+
[
|
173 |
+
"2ns",
|
174 |
+
"3ns",
|
175 |
+
"4ns",
|
176 |
+
"5ns",
|
177 |
+
"6ns",
|
178 |
+
"7ns",
|
179 |
+
"250ns",
|
180 |
+
"500ns",
|
181 |
+
"750ns",
|
182 |
+
"1us",
|
183 |
+
"19us",
|
184 |
+
"250us",
|
185 |
+
"500us",
|
186 |
+
"750us",
|
187 |
+
"1s",
|
188 |
+
"2s",
|
189 |
+
"3s",
|
190 |
+
"12h",
|
191 |
+
"1D",
|
192 |
+
],
|
193 |
+
)
|
194 |
+
def test_round_int64(self, start, index_freq, periods, round_freq):
|
195 |
+
dt = date_range(start=start, freq=index_freq, periods=periods)
|
196 |
+
unit = to_offset(round_freq).nanos
|
197 |
+
|
198 |
+
# test floor
|
199 |
+
result = dt.floor(round_freq)
|
200 |
+
diff = dt.asi8 - result.asi8
|
201 |
+
mod = result.asi8 % unit
|
202 |
+
assert (mod == 0).all(), f"floor not a {round_freq} multiple"
|
203 |
+
assert (0 <= diff).all() and (diff < unit).all(), "floor error"
|
204 |
+
|
205 |
+
# test ceil
|
206 |
+
result = dt.ceil(round_freq)
|
207 |
+
diff = result.asi8 - dt.asi8
|
208 |
+
mod = result.asi8 % unit
|
209 |
+
assert (mod == 0).all(), f"ceil not a {round_freq} multiple"
|
210 |
+
assert (0 <= diff).all() and (diff < unit).all(), "ceil error"
|
211 |
+
|
212 |
+
# test round
|
213 |
+
result = dt.round(round_freq)
|
214 |
+
diff = abs(result.asi8 - dt.asi8)
|
215 |
+
mod = result.asi8 % unit
|
216 |
+
assert (mod == 0).all(), f"round not a {round_freq} multiple"
|
217 |
+
assert (diff <= unit // 2).all(), "round error"
|
218 |
+
if unit % 2 == 0:
|
219 |
+
assert (
|
220 |
+
result.asi8[diff == unit // 2] % 2 == 0
|
221 |
+
).all(), "round half to even error"
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_shift.py
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
|
3 |
+
import pytest
|
4 |
+
import pytz
|
5 |
+
|
6 |
+
from pandas.errors import NullFrequencyError
|
7 |
+
|
8 |
+
import pandas as pd
|
9 |
+
from pandas import (
|
10 |
+
DatetimeIndex,
|
11 |
+
Series,
|
12 |
+
date_range,
|
13 |
+
)
|
14 |
+
import pandas._testing as tm
|
15 |
+
|
16 |
+
START, END = datetime(2009, 1, 1), datetime(2010, 1, 1)
|
17 |
+
|
18 |
+
|
19 |
+
class TestDatetimeIndexShift:
|
20 |
+
# -------------------------------------------------------------
|
21 |
+
# DatetimeIndex.shift is used in integer addition
|
22 |
+
|
23 |
+
def test_dti_shift_tzaware(self, tz_naive_fixture, unit):
|
24 |
+
# GH#9903
|
25 |
+
tz = tz_naive_fixture
|
26 |
+
idx = DatetimeIndex([], name="xxx", tz=tz).as_unit(unit)
|
27 |
+
tm.assert_index_equal(idx.shift(0, freq="h"), idx)
|
28 |
+
tm.assert_index_equal(idx.shift(3, freq="h"), idx)
|
29 |
+
|
30 |
+
idx = DatetimeIndex(
|
31 |
+
["2011-01-01 10:00", "2011-01-01 11:00", "2011-01-01 12:00"],
|
32 |
+
name="xxx",
|
33 |
+
tz=tz,
|
34 |
+
freq="h",
|
35 |
+
).as_unit(unit)
|
36 |
+
tm.assert_index_equal(idx.shift(0, freq="h"), idx)
|
37 |
+
exp = DatetimeIndex(
|
38 |
+
["2011-01-01 13:00", "2011-01-01 14:00", "2011-01-01 15:00"],
|
39 |
+
name="xxx",
|
40 |
+
tz=tz,
|
41 |
+
freq="h",
|
42 |
+
).as_unit(unit)
|
43 |
+
tm.assert_index_equal(idx.shift(3, freq="h"), exp)
|
44 |
+
exp = DatetimeIndex(
|
45 |
+
["2011-01-01 07:00", "2011-01-01 08:00", "2011-01-01 09:00"],
|
46 |
+
name="xxx",
|
47 |
+
tz=tz,
|
48 |
+
freq="h",
|
49 |
+
).as_unit(unit)
|
50 |
+
tm.assert_index_equal(idx.shift(-3, freq="h"), exp)
|
51 |
+
|
52 |
+
def test_dti_shift_freqs(self, unit):
|
53 |
+
# test shift for DatetimeIndex and non DatetimeIndex
|
54 |
+
# GH#8083
|
55 |
+
drange = date_range("20130101", periods=5, unit=unit)
|
56 |
+
result = drange.shift(1)
|
57 |
+
expected = DatetimeIndex(
|
58 |
+
["2013-01-02", "2013-01-03", "2013-01-04", "2013-01-05", "2013-01-06"],
|
59 |
+
dtype=f"M8[{unit}]",
|
60 |
+
freq="D",
|
61 |
+
)
|
62 |
+
tm.assert_index_equal(result, expected)
|
63 |
+
|
64 |
+
result = drange.shift(-1)
|
65 |
+
expected = DatetimeIndex(
|
66 |
+
["2012-12-31", "2013-01-01", "2013-01-02", "2013-01-03", "2013-01-04"],
|
67 |
+
dtype=f"M8[{unit}]",
|
68 |
+
freq="D",
|
69 |
+
)
|
70 |
+
tm.assert_index_equal(result, expected)
|
71 |
+
|
72 |
+
result = drange.shift(3, freq="2D")
|
73 |
+
expected = DatetimeIndex(
|
74 |
+
["2013-01-07", "2013-01-08", "2013-01-09", "2013-01-10", "2013-01-11"],
|
75 |
+
dtype=f"M8[{unit}]",
|
76 |
+
freq="D",
|
77 |
+
)
|
78 |
+
tm.assert_index_equal(result, expected)
|
79 |
+
|
80 |
+
def test_dti_shift_int(self, unit):
|
81 |
+
rng = date_range("1/1/2000", periods=20, unit=unit)
|
82 |
+
|
83 |
+
result = rng + 5 * rng.freq
|
84 |
+
expected = rng.shift(5)
|
85 |
+
tm.assert_index_equal(result, expected)
|
86 |
+
|
87 |
+
result = rng - 5 * rng.freq
|
88 |
+
expected = rng.shift(-5)
|
89 |
+
tm.assert_index_equal(result, expected)
|
90 |
+
|
91 |
+
def test_dti_shift_no_freq(self, unit):
|
92 |
+
# GH#19147
|
93 |
+
dti = DatetimeIndex(["2011-01-01 10:00", "2011-01-01"], freq=None).as_unit(unit)
|
94 |
+
with pytest.raises(NullFrequencyError, match="Cannot shift with no freq"):
|
95 |
+
dti.shift(2)
|
96 |
+
|
97 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
98 |
+
def test_dti_shift_localized(self, tzstr, unit):
|
99 |
+
dr = date_range("2011/1/1", "2012/1/1", freq="W-FRI", unit=unit)
|
100 |
+
dr_tz = dr.tz_localize(tzstr)
|
101 |
+
|
102 |
+
result = dr_tz.shift(1, "10min")
|
103 |
+
assert result.tz == dr_tz.tz
|
104 |
+
|
105 |
+
def test_dti_shift_across_dst(self, unit):
|
106 |
+
# GH 8616
|
107 |
+
idx = date_range(
|
108 |
+
"2013-11-03", tz="America/Chicago", periods=7, freq="h", unit=unit
|
109 |
+
)
|
110 |
+
ser = Series(index=idx[:-1], dtype=object)
|
111 |
+
result = ser.shift(freq="h")
|
112 |
+
expected = Series(index=idx[1:], dtype=object)
|
113 |
+
tm.assert_series_equal(result, expected)
|
114 |
+
|
115 |
+
@pytest.mark.parametrize(
|
116 |
+
"shift, result_time",
|
117 |
+
[
|
118 |
+
[0, "2014-11-14 00:00:00"],
|
119 |
+
[-1, "2014-11-13 23:00:00"],
|
120 |
+
[1, "2014-11-14 01:00:00"],
|
121 |
+
],
|
122 |
+
)
|
123 |
+
def test_dti_shift_near_midnight(self, shift, result_time, unit):
|
124 |
+
# GH 8616
|
125 |
+
dt = datetime(2014, 11, 14, 0)
|
126 |
+
dt_est = pytz.timezone("EST").localize(dt)
|
127 |
+
idx = DatetimeIndex([dt_est]).as_unit(unit)
|
128 |
+
ser = Series(data=[1], index=idx)
|
129 |
+
result = ser.shift(shift, freq="h")
|
130 |
+
exp_index = DatetimeIndex([result_time], tz="EST").as_unit(unit)
|
131 |
+
expected = Series(1, index=exp_index)
|
132 |
+
tm.assert_series_equal(result, expected)
|
133 |
+
|
134 |
+
def test_shift_periods(self, unit):
|
135 |
+
# GH#22458 : argument 'n' was deprecated in favor of 'periods'
|
136 |
+
idx = date_range(start=START, end=END, periods=3, unit=unit)
|
137 |
+
tm.assert_index_equal(idx.shift(periods=0), idx)
|
138 |
+
tm.assert_index_equal(idx.shift(0), idx)
|
139 |
+
|
140 |
+
@pytest.mark.parametrize("freq", ["B", "C"])
|
141 |
+
def test_shift_bday(self, freq, unit):
|
142 |
+
rng = date_range(START, END, freq=freq, unit=unit)
|
143 |
+
shifted = rng.shift(5)
|
144 |
+
assert shifted[0] == rng[5]
|
145 |
+
assert shifted.freq == rng.freq
|
146 |
+
|
147 |
+
shifted = rng.shift(-5)
|
148 |
+
assert shifted[5] == rng[0]
|
149 |
+
assert shifted.freq == rng.freq
|
150 |
+
|
151 |
+
shifted = rng.shift(0)
|
152 |
+
assert shifted[0] == rng[0]
|
153 |
+
assert shifted.freq == rng.freq
|
154 |
+
|
155 |
+
def test_shift_bmonth(self, unit):
|
156 |
+
rng = date_range(START, END, freq=pd.offsets.BMonthEnd(), unit=unit)
|
157 |
+
shifted = rng.shift(1, freq=pd.offsets.BDay())
|
158 |
+
assert shifted[0] == rng[0] + pd.offsets.BDay()
|
159 |
+
|
160 |
+
rng = date_range(START, END, freq=pd.offsets.BMonthEnd(), unit=unit)
|
161 |
+
with tm.assert_produces_warning(pd.errors.PerformanceWarning):
|
162 |
+
shifted = rng.shift(1, freq=pd.offsets.CDay())
|
163 |
+
assert shifted[0] == rng[0] + pd.offsets.CDay()
|
164 |
+
|
165 |
+
def test_shift_empty(self, unit):
|
166 |
+
# GH#14811
|
167 |
+
dti = date_range(start="2016-10-21", end="2016-10-21", freq="BME", unit=unit)
|
168 |
+
result = dti.shift(1)
|
169 |
+
tm.assert_index_equal(result, dti)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_snap.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas import (
|
4 |
+
DatetimeIndex,
|
5 |
+
date_range,
|
6 |
+
)
|
7 |
+
import pandas._testing as tm
|
8 |
+
|
9 |
+
|
10 |
+
@pytest.mark.parametrize("tz", [None, "Asia/Shanghai", "Europe/Berlin"])
|
11 |
+
@pytest.mark.parametrize("name", [None, "my_dti"])
|
12 |
+
@pytest.mark.parametrize("unit", ["ns", "us", "ms", "s"])
|
13 |
+
def test_dti_snap(name, tz, unit):
|
14 |
+
dti = DatetimeIndex(
|
15 |
+
[
|
16 |
+
"1/1/2002",
|
17 |
+
"1/2/2002",
|
18 |
+
"1/3/2002",
|
19 |
+
"1/4/2002",
|
20 |
+
"1/5/2002",
|
21 |
+
"1/6/2002",
|
22 |
+
"1/7/2002",
|
23 |
+
],
|
24 |
+
name=name,
|
25 |
+
tz=tz,
|
26 |
+
freq="D",
|
27 |
+
)
|
28 |
+
dti = dti.as_unit(unit)
|
29 |
+
|
30 |
+
result = dti.snap(freq="W-MON")
|
31 |
+
expected = date_range("12/31/2001", "1/7/2002", name=name, tz=tz, freq="w-mon")
|
32 |
+
expected = expected.repeat([3, 4])
|
33 |
+
expected = expected.as_unit(unit)
|
34 |
+
tm.assert_index_equal(result, expected)
|
35 |
+
assert result.tz == expected.tz
|
36 |
+
assert result.freq is None
|
37 |
+
assert expected.freq is None
|
38 |
+
|
39 |
+
result = dti.snap(freq="B")
|
40 |
+
|
41 |
+
expected = date_range("1/1/2002", "1/7/2002", name=name, tz=tz, freq="b")
|
42 |
+
expected = expected.repeat([1, 1, 1, 2, 2])
|
43 |
+
expected = expected.as_unit(unit)
|
44 |
+
tm.assert_index_equal(result, expected)
|
45 |
+
assert result.tz == expected.tz
|
46 |
+
assert result.freq is None
|
47 |
+
assert expected.freq is None
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_frame.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pandas import (
|
2 |
+
DataFrame,
|
3 |
+
Index,
|
4 |
+
date_range,
|
5 |
+
)
|
6 |
+
import pandas._testing as tm
|
7 |
+
|
8 |
+
|
9 |
+
class TestToFrame:
|
10 |
+
def test_to_frame_datetime_tz(self):
|
11 |
+
# GH#25809
|
12 |
+
idx = date_range(start="2019-01-01", end="2019-01-30", freq="D", tz="UTC")
|
13 |
+
result = idx.to_frame()
|
14 |
+
expected = DataFrame(idx, index=idx)
|
15 |
+
tm.assert_frame_equal(result, expected)
|
16 |
+
|
17 |
+
def test_to_frame_respects_none_name(self):
|
18 |
+
# GH#44212 if we explicitly pass name=None, then that should be respected,
|
19 |
+
# not changed to 0
|
20 |
+
# GH-45448 this is first deprecated to only change in the future
|
21 |
+
idx = date_range(start="2019-01-01", end="2019-01-30", freq="D", tz="UTC")
|
22 |
+
result = idx.to_frame(name=None)
|
23 |
+
exp_idx = Index([None], dtype=object)
|
24 |
+
tm.assert_index_equal(exp_idx, result.columns)
|
25 |
+
|
26 |
+
result = idx.rename("foo").to_frame(name=None)
|
27 |
+
exp_idx = Index([None], dtype=object)
|
28 |
+
tm.assert_index_equal(exp_idx, result.columns)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_julian_date.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
from pandas import (
|
4 |
+
Index,
|
5 |
+
Timestamp,
|
6 |
+
date_range,
|
7 |
+
)
|
8 |
+
import pandas._testing as tm
|
9 |
+
|
10 |
+
|
11 |
+
class TestDateTimeIndexToJulianDate:
|
12 |
+
def test_1700(self):
|
13 |
+
dr = date_range(start=Timestamp("1710-10-01"), periods=5, freq="D")
|
14 |
+
r1 = Index([x.to_julian_date() for x in dr])
|
15 |
+
r2 = dr.to_julian_date()
|
16 |
+
assert isinstance(r2, Index) and r2.dtype == np.float64
|
17 |
+
tm.assert_index_equal(r1, r2)
|
18 |
+
|
19 |
+
def test_2000(self):
|
20 |
+
dr = date_range(start=Timestamp("2000-02-27"), periods=5, freq="D")
|
21 |
+
r1 = Index([x.to_julian_date() for x in dr])
|
22 |
+
r2 = dr.to_julian_date()
|
23 |
+
assert isinstance(r2, Index) and r2.dtype == np.float64
|
24 |
+
tm.assert_index_equal(r1, r2)
|
25 |
+
|
26 |
+
def test_hour(self):
|
27 |
+
dr = date_range(start=Timestamp("2000-02-27"), periods=5, freq="h")
|
28 |
+
r1 = Index([x.to_julian_date() for x in dr])
|
29 |
+
r2 = dr.to_julian_date()
|
30 |
+
assert isinstance(r2, Index) and r2.dtype == np.float64
|
31 |
+
tm.assert_index_equal(r1, r2)
|
32 |
+
|
33 |
+
def test_minute(self):
|
34 |
+
dr = date_range(start=Timestamp("2000-02-27"), periods=5, freq="min")
|
35 |
+
r1 = Index([x.to_julian_date() for x in dr])
|
36 |
+
r2 = dr.to_julian_date()
|
37 |
+
assert isinstance(r2, Index) and r2.dtype == np.float64
|
38 |
+
tm.assert_index_equal(r1, r2)
|
39 |
+
|
40 |
+
def test_second(self):
|
41 |
+
dr = date_range(start=Timestamp("2000-02-27"), periods=5, freq="s")
|
42 |
+
r1 = Index([x.to_julian_date() for x in dr])
|
43 |
+
r2 = dr.to_julian_date()
|
44 |
+
assert isinstance(r2, Index) and r2.dtype == np.float64
|
45 |
+
tm.assert_index_equal(r1, r2)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_pydatetime.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import (
|
2 |
+
datetime,
|
3 |
+
timezone,
|
4 |
+
)
|
5 |
+
|
6 |
+
import dateutil.parser
|
7 |
+
import dateutil.tz
|
8 |
+
from dateutil.tz import tzlocal
|
9 |
+
import numpy as np
|
10 |
+
|
11 |
+
from pandas import (
|
12 |
+
DatetimeIndex,
|
13 |
+
date_range,
|
14 |
+
to_datetime,
|
15 |
+
)
|
16 |
+
import pandas._testing as tm
|
17 |
+
from pandas.tests.indexes.datetimes.test_timezones import FixedOffset
|
18 |
+
|
19 |
+
fixed_off = FixedOffset(-420, "-07:00")
|
20 |
+
|
21 |
+
|
22 |
+
class TestToPyDatetime:
|
23 |
+
def test_dti_to_pydatetime(self):
|
24 |
+
dt = dateutil.parser.parse("2012-06-13T01:39:00Z")
|
25 |
+
dt = dt.replace(tzinfo=tzlocal())
|
26 |
+
|
27 |
+
arr = np.array([dt], dtype=object)
|
28 |
+
|
29 |
+
result = to_datetime(arr, utc=True)
|
30 |
+
assert result.tz is timezone.utc
|
31 |
+
|
32 |
+
rng = date_range("2012-11-03 03:00", "2012-11-05 03:00", tz=tzlocal())
|
33 |
+
arr = rng.to_pydatetime()
|
34 |
+
result = to_datetime(arr, utc=True)
|
35 |
+
assert result.tz is timezone.utc
|
36 |
+
|
37 |
+
def test_dti_to_pydatetime_fizedtz(self):
|
38 |
+
dates = np.array(
|
39 |
+
[
|
40 |
+
datetime(2000, 1, 1, tzinfo=fixed_off),
|
41 |
+
datetime(2000, 1, 2, tzinfo=fixed_off),
|
42 |
+
datetime(2000, 1, 3, tzinfo=fixed_off),
|
43 |
+
]
|
44 |
+
)
|
45 |
+
dti = DatetimeIndex(dates)
|
46 |
+
|
47 |
+
result = dti.to_pydatetime()
|
48 |
+
tm.assert_numpy_array_equal(dates, result)
|
49 |
+
|
50 |
+
result = dti._mpl_repr()
|
51 |
+
tm.assert_numpy_array_equal(dates, result)
|
env-llmeval/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_tz_convert.py
ADDED
@@ -0,0 +1,283 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
|
3 |
+
import dateutil.tz
|
4 |
+
from dateutil.tz import gettz
|
5 |
+
import numpy as np
|
6 |
+
import pytest
|
7 |
+
import pytz
|
8 |
+
|
9 |
+
from pandas._libs.tslibs import timezones
|
10 |
+
|
11 |
+
from pandas import (
|
12 |
+
DatetimeIndex,
|
13 |
+
Index,
|
14 |
+
NaT,
|
15 |
+
Timestamp,
|
16 |
+
date_range,
|
17 |
+
offsets,
|
18 |
+
)
|
19 |
+
import pandas._testing as tm
|
20 |
+
|
21 |
+
|
22 |
+
class TestTZConvert:
|
23 |
+
def test_tz_convert_nat(self):
|
24 |
+
# GH#5546
|
25 |
+
dates = [NaT]
|
26 |
+
idx = DatetimeIndex(dates)
|
27 |
+
idx = idx.tz_localize("US/Pacific")
|
28 |
+
tm.assert_index_equal(idx, DatetimeIndex(dates, tz="US/Pacific"))
|
29 |
+
idx = idx.tz_convert("US/Eastern")
|
30 |
+
tm.assert_index_equal(idx, DatetimeIndex(dates, tz="US/Eastern"))
|
31 |
+
idx = idx.tz_convert("UTC")
|
32 |
+
tm.assert_index_equal(idx, DatetimeIndex(dates, tz="UTC"))
|
33 |
+
|
34 |
+
dates = ["2010-12-01 00:00", "2010-12-02 00:00", NaT]
|
35 |
+
idx = DatetimeIndex(dates)
|
36 |
+
idx = idx.tz_localize("US/Pacific")
|
37 |
+
tm.assert_index_equal(idx, DatetimeIndex(dates, tz="US/Pacific"))
|
38 |
+
idx = idx.tz_convert("US/Eastern")
|
39 |
+
expected = ["2010-12-01 03:00", "2010-12-02 03:00", NaT]
|
40 |
+
tm.assert_index_equal(idx, DatetimeIndex(expected, tz="US/Eastern"))
|
41 |
+
|
42 |
+
idx = idx + offsets.Hour(5)
|
43 |
+
expected = ["2010-12-01 08:00", "2010-12-02 08:00", NaT]
|
44 |
+
tm.assert_index_equal(idx, DatetimeIndex(expected, tz="US/Eastern"))
|
45 |
+
idx = idx.tz_convert("US/Pacific")
|
46 |
+
expected = ["2010-12-01 05:00", "2010-12-02 05:00", NaT]
|
47 |
+
tm.assert_index_equal(idx, DatetimeIndex(expected, tz="US/Pacific"))
|
48 |
+
|
49 |
+
idx = idx + np.timedelta64(3, "h")
|
50 |
+
expected = ["2010-12-01 08:00", "2010-12-02 08:00", NaT]
|
51 |
+
tm.assert_index_equal(idx, DatetimeIndex(expected, tz="US/Pacific"))
|
52 |
+
|
53 |
+
idx = idx.tz_convert("US/Eastern")
|
54 |
+
expected = ["2010-12-01 11:00", "2010-12-02 11:00", NaT]
|
55 |
+
tm.assert_index_equal(idx, DatetimeIndex(expected, tz="US/Eastern"))
|
56 |
+
|
57 |
+
@pytest.mark.parametrize("prefix", ["", "dateutil/"])
|
58 |
+
def test_dti_tz_convert_compat_timestamp(self, prefix):
|
59 |
+
strdates = ["1/1/2012", "3/1/2012", "4/1/2012"]
|
60 |
+
idx = DatetimeIndex(strdates, tz=prefix + "US/Eastern")
|
61 |
+
|
62 |
+
conv = idx[0].tz_convert(prefix + "US/Pacific")
|
63 |
+
expected = idx.tz_convert(prefix + "US/Pacific")[0]
|
64 |
+
|
65 |
+
assert conv == expected
|
66 |
+
|
67 |
+
def test_dti_tz_convert_hour_overflow_dst(self):
|
68 |
+
# Regression test for GH#13306
|
69 |
+
|
70 |
+
# sorted case US/Eastern -> UTC
|
71 |
+
ts = ["2008-05-12 09:50:00", "2008-12-12 09:50:35", "2009-05-12 09:50:32"]
|
72 |
+
tt = DatetimeIndex(ts).tz_localize("US/Eastern")
|
73 |
+
ut = tt.tz_convert("UTC")
|
74 |
+
expected = Index([13, 14, 13], dtype=np.int32)
|
75 |
+
tm.assert_index_equal(ut.hour, expected)
|
76 |
+
|
77 |
+
# sorted case UTC -> US/Eastern
|
78 |
+
ts = ["2008-05-12 13:50:00", "2008-12-12 14:50:35", "2009-05-12 13:50:32"]
|
79 |
+
tt = DatetimeIndex(ts).tz_localize("UTC")
|
80 |
+
ut = tt.tz_convert("US/Eastern")
|
81 |
+
expected = Index([9, 9, 9], dtype=np.int32)
|
82 |
+
tm.assert_index_equal(ut.hour, expected)
|
83 |
+
|
84 |
+
# unsorted case US/Eastern -> UTC
|
85 |
+
ts = ["2008-05-12 09:50:00", "2008-12-12 09:50:35", "2008-05-12 09:50:32"]
|
86 |
+
tt = DatetimeIndex(ts).tz_localize("US/Eastern")
|
87 |
+
ut = tt.tz_convert("UTC")
|
88 |
+
expected = Index([13, 14, 13], dtype=np.int32)
|
89 |
+
tm.assert_index_equal(ut.hour, expected)
|
90 |
+
|
91 |
+
# unsorted case UTC -> US/Eastern
|
92 |
+
ts = ["2008-05-12 13:50:00", "2008-12-12 14:50:35", "2008-05-12 13:50:32"]
|
93 |
+
tt = DatetimeIndex(ts).tz_localize("UTC")
|
94 |
+
ut = tt.tz_convert("US/Eastern")
|
95 |
+
expected = Index([9, 9, 9], dtype=np.int32)
|
96 |
+
tm.assert_index_equal(ut.hour, expected)
|
97 |
+
|
98 |
+
@pytest.mark.parametrize("tz", ["US/Eastern", "dateutil/US/Eastern"])
|
99 |
+
def test_dti_tz_convert_hour_overflow_dst_timestamps(self, tz):
|
100 |
+
# Regression test for GH#13306
|
101 |
+
|
102 |
+
# sorted case US/Eastern -> UTC
|
103 |
+
ts = [
|
104 |
+
Timestamp("2008-05-12 09:50:00", tz=tz),
|
105 |
+
Timestamp("2008-12-12 09:50:35", tz=tz),
|
106 |
+
Timestamp("2009-05-12 09:50:32", tz=tz),
|
107 |
+
]
|
108 |
+
tt = DatetimeIndex(ts)
|
109 |
+
ut = tt.tz_convert("UTC")
|
110 |
+
expected = Index([13, 14, 13], dtype=np.int32)
|
111 |
+
tm.assert_index_equal(ut.hour, expected)
|
112 |
+
|
113 |
+
# sorted case UTC -> US/Eastern
|
114 |
+
ts = [
|
115 |
+
Timestamp("2008-05-12 13:50:00", tz="UTC"),
|
116 |
+
Timestamp("2008-12-12 14:50:35", tz="UTC"),
|
117 |
+
Timestamp("2009-05-12 13:50:32", tz="UTC"),
|
118 |
+
]
|
119 |
+
tt = DatetimeIndex(ts)
|
120 |
+
ut = tt.tz_convert("US/Eastern")
|
121 |
+
expected = Index([9, 9, 9], dtype=np.int32)
|
122 |
+
tm.assert_index_equal(ut.hour, expected)
|
123 |
+
|
124 |
+
# unsorted case US/Eastern -> UTC
|
125 |
+
ts = [
|
126 |
+
Timestamp("2008-05-12 09:50:00", tz=tz),
|
127 |
+
Timestamp("2008-12-12 09:50:35", tz=tz),
|
128 |
+
Timestamp("2008-05-12 09:50:32", tz=tz),
|
129 |
+
]
|
130 |
+
tt = DatetimeIndex(ts)
|
131 |
+
ut = tt.tz_convert("UTC")
|
132 |
+
expected = Index([13, 14, 13], dtype=np.int32)
|
133 |
+
tm.assert_index_equal(ut.hour, expected)
|
134 |
+
|
135 |
+
# unsorted case UTC -> US/Eastern
|
136 |
+
ts = [
|
137 |
+
Timestamp("2008-05-12 13:50:00", tz="UTC"),
|
138 |
+
Timestamp("2008-12-12 14:50:35", tz="UTC"),
|
139 |
+
Timestamp("2008-05-12 13:50:32", tz="UTC"),
|
140 |
+
]
|
141 |
+
tt = DatetimeIndex(ts)
|
142 |
+
ut = tt.tz_convert("US/Eastern")
|
143 |
+
expected = Index([9, 9, 9], dtype=np.int32)
|
144 |
+
tm.assert_index_equal(ut.hour, expected)
|
145 |
+
|
146 |
+
@pytest.mark.parametrize("freq, n", [("h", 1), ("min", 60), ("s", 3600)])
|
147 |
+
def test_dti_tz_convert_trans_pos_plus_1__bug(self, freq, n):
|
148 |
+
# Regression test for tslib.tz_convert(vals, tz1, tz2).
|
149 |
+
# See GH#4496 for details.
|
150 |
+
idx = date_range(datetime(2011, 3, 26, 23), datetime(2011, 3, 27, 1), freq=freq)
|
151 |
+
idx = idx.tz_localize("UTC")
|
152 |
+
idx = idx.tz_convert("Europe/Moscow")
|
153 |
+
|
154 |
+
expected = np.repeat(np.array([3, 4, 5]), np.array([n, n, 1]))
|
155 |
+
tm.assert_index_equal(idx.hour, Index(expected, dtype=np.int32))
|
156 |
+
|
157 |
+
def test_dti_tz_convert_dst(self):
|
158 |
+
for freq, n in [("h", 1), ("min", 60), ("s", 3600)]:
|
159 |
+
# Start DST
|
160 |
+
idx = date_range(
|
161 |
+
"2014-03-08 23:00", "2014-03-09 09:00", freq=freq, tz="UTC"
|
162 |
+
)
|
163 |
+
idx = idx.tz_convert("US/Eastern")
|
164 |
+
expected = np.repeat(
|
165 |
+
np.array([18, 19, 20, 21, 22, 23, 0, 1, 3, 4, 5]),
|
166 |
+
np.array([n, n, n, n, n, n, n, n, n, n, 1]),
|
167 |
+
)
|
168 |
+
tm.assert_index_equal(idx.hour, Index(expected, dtype=np.int32))
|
169 |
+
|
170 |
+
idx = date_range(
|
171 |
+
"2014-03-08 18:00", "2014-03-09 05:00", freq=freq, tz="US/Eastern"
|
172 |
+
)
|
173 |
+
idx = idx.tz_convert("UTC")
|
174 |
+
expected = np.repeat(
|
175 |
+
np.array([23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
|
176 |
+
np.array([n, n, n, n, n, n, n, n, n, n, 1]),
|
177 |
+
)
|
178 |
+
tm.assert_index_equal(idx.hour, Index(expected, dtype=np.int32))
|
179 |
+
|
180 |
+
# End DST
|
181 |
+
idx = date_range(
|
182 |
+
"2014-11-01 23:00", "2014-11-02 09:00", freq=freq, tz="UTC"
|
183 |
+
)
|
184 |
+
idx = idx.tz_convert("US/Eastern")
|
185 |
+
expected = np.repeat(
|
186 |
+
np.array([19, 20, 21, 22, 23, 0, 1, 1, 2, 3, 4]),
|
187 |
+
np.array([n, n, n, n, n, n, n, n, n, n, 1]),
|
188 |
+
)
|
189 |
+
tm.assert_index_equal(idx.hour, Index(expected, dtype=np.int32))
|
190 |
+
|
191 |
+
idx = date_range(
|
192 |
+
"2014-11-01 18:00", "2014-11-02 05:00", freq=freq, tz="US/Eastern"
|
193 |
+
)
|
194 |
+
idx = idx.tz_convert("UTC")
|
195 |
+
expected = np.repeat(
|
196 |
+
np.array([22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
|
197 |
+
np.array([n, n, n, n, n, n, n, n, n, n, n, n, 1]),
|
198 |
+
)
|
199 |
+
tm.assert_index_equal(idx.hour, Index(expected, dtype=np.int32))
|
200 |
+
|
201 |
+
# daily
|
202 |
+
# Start DST
|
203 |
+
idx = date_range("2014-03-08 00:00", "2014-03-09 00:00", freq="D", tz="UTC")
|
204 |
+
idx = idx.tz_convert("US/Eastern")
|
205 |
+
tm.assert_index_equal(idx.hour, Index([19, 19], dtype=np.int32))
|
206 |
+
|
207 |
+
idx = date_range(
|
208 |
+
"2014-03-08 00:00", "2014-03-09 00:00", freq="D", tz="US/Eastern"
|
209 |
+
)
|
210 |
+
idx = idx.tz_convert("UTC")
|
211 |
+
tm.assert_index_equal(idx.hour, Index([5, 5], dtype=np.int32))
|
212 |
+
|
213 |
+
# End DST
|
214 |
+
idx = date_range("2014-11-01 00:00", "2014-11-02 00:00", freq="D", tz="UTC")
|
215 |
+
idx = idx.tz_convert("US/Eastern")
|
216 |
+
tm.assert_index_equal(idx.hour, Index([20, 20], dtype=np.int32))
|
217 |
+
|
218 |
+
idx = date_range(
|
219 |
+
"2014-11-01 00:00", "2014-11-02 000:00", freq="D", tz="US/Eastern"
|
220 |
+
)
|
221 |
+
idx = idx.tz_convert("UTC")
|
222 |
+
tm.assert_index_equal(idx.hour, Index([4, 4], dtype=np.int32))
|
223 |
+
|
224 |
+
def test_tz_convert_roundtrip(self, tz_aware_fixture):
|
225 |
+
tz = tz_aware_fixture
|
226 |
+
idx1 = date_range(start="2014-01-01", end="2014-12-31", freq="ME", tz="UTC")
|
227 |
+
exp1 = date_range(start="2014-01-01", end="2014-12-31", freq="ME")
|
228 |
+
|
229 |
+
idx2 = date_range(start="2014-01-01", end="2014-12-31", freq="D", tz="UTC")
|
230 |
+
exp2 = date_range(start="2014-01-01", end="2014-12-31", freq="D")
|
231 |
+
|
232 |
+
idx3 = date_range(start="2014-01-01", end="2014-03-01", freq="h", tz="UTC")
|
233 |
+
exp3 = date_range(start="2014-01-01", end="2014-03-01", freq="h")
|
234 |
+
|
235 |
+
idx4 = date_range(start="2014-08-01", end="2014-10-31", freq="min", tz="UTC")
|
236 |
+
exp4 = date_range(start="2014-08-01", end="2014-10-31", freq="min")
|
237 |
+
|
238 |
+
for idx, expected in [(idx1, exp1), (idx2, exp2), (idx3, exp3), (idx4, exp4)]:
|
239 |
+
converted = idx.tz_convert(tz)
|
240 |
+
reset = converted.tz_convert(None)
|
241 |
+
tm.assert_index_equal(reset, expected)
|
242 |
+
assert reset.tzinfo is None
|
243 |
+
expected = converted.tz_convert("UTC").tz_localize(None)
|
244 |
+
expected = expected._with_freq("infer")
|
245 |
+
tm.assert_index_equal(reset, expected)
|
246 |
+
|
247 |
+
def test_dti_tz_convert_tzlocal(self):
|
248 |
+
# GH#13583
|
249 |
+
# tz_convert doesn't affect to internal
|
250 |
+
dti = date_range(start="2001-01-01", end="2001-03-01", tz="UTC")
|
251 |
+
dti2 = dti.tz_convert(dateutil.tz.tzlocal())
|
252 |
+
tm.assert_numpy_array_equal(dti2.asi8, dti.asi8)
|
253 |
+
|
254 |
+
dti = date_range(start="2001-01-01", end="2001-03-01", tz=dateutil.tz.tzlocal())
|
255 |
+
dti2 = dti.tz_convert(None)
|
256 |
+
tm.assert_numpy_array_equal(dti2.asi8, dti.asi8)
|
257 |
+
|
258 |
+
@pytest.mark.parametrize(
|
259 |
+
"tz",
|
260 |
+
[
|
261 |
+
"US/Eastern",
|
262 |
+
"dateutil/US/Eastern",
|
263 |
+
pytz.timezone("US/Eastern"),
|
264 |
+
gettz("US/Eastern"),
|
265 |
+
],
|
266 |
+
)
|
267 |
+
def test_dti_tz_convert_utc_to_local_no_modify(self, tz):
|
268 |
+
rng = date_range("3/11/2012", "3/12/2012", freq="h", tz="utc")
|
269 |
+
rng_eastern = rng.tz_convert(tz)
|
270 |
+
|
271 |
+
# Values are unmodified
|
272 |
+
tm.assert_numpy_array_equal(rng.asi8, rng_eastern.asi8)
|
273 |
+
|
274 |
+
assert timezones.tz_compare(rng_eastern.tz, timezones.maybe_get_tz(tz))
|
275 |
+
|
276 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
277 |
+
def test_tz_convert_unsorted(self, tzstr):
|
278 |
+
dr = date_range("2012-03-09", freq="h", periods=100, tz="utc")
|
279 |
+
dr = dr.tz_convert(tzstr)
|
280 |
+
|
281 |
+
result = dr[::-1].hour
|
282 |
+
exp = dr.hour[::-1]
|
283 |
+
tm.assert_almost_equal(result, exp)
|