Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_formats.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_indexing.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_setops.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_timezones.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__init__.py +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/__init__.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_asof.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_astype.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_delete.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_factorize.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_fillna.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_insert.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_isocalendar.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_map.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_normalize.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_repeat.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_resolution.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_round.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_shift.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_snap.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_frame.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_julian_date.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_period.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_pydatetime.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_series.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_tz_convert.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_tz_localize.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_unique.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_asof.py +30 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_astype.py +335 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_delete.py +141 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_factorize.py +125 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_fillna.py +62 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_insert.py +265 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_isocalendar.py +28 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_map.py +47 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_normalize.py +95 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_repeat.py +83 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_resolution.py +31 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_round.py +221 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_shift.py +169 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_snap.py +47 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_frame.py +28 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_julian_date.py +45 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_period.py +225 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_pydatetime.py +51 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_series.py +18 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_tz_convert.py +283 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_tz_localize.py +402 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_unique.py +77 -0
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_formats.cpython-310.pyc
ADDED
Binary file (11 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_indexing.cpython-310.pyc
ADDED
Binary file (22.6 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_setops.cpython-310.pyc
ADDED
Binary file (18 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_timezones.cpython-310.pyc
ADDED
Binary file (7.66 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__init__.py
ADDED
File without changes
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (211 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_asof.cpython-310.pyc
ADDED
Binary file (1.17 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_astype.cpython-310.pyc
ADDED
Binary file (11.2 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_delete.cpython-310.pyc
ADDED
Binary file (3.23 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_factorize.cpython-310.pyc
ADDED
Binary file (3.61 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_fillna.cpython-310.pyc
ADDED
Binary file (1.34 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_insert.cpython-310.pyc
ADDED
Binary file (6.94 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_isocalendar.cpython-310.pyc
ADDED
Binary file (1.12 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_map.cpython-310.pyc
ADDED
Binary file (2.37 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_normalize.cpython-310.pyc
ADDED
Binary file (2.59 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_repeat.cpython-310.pyc
ADDED
Binary file (2.46 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_resolution.cpython-310.pyc
ADDED
Binary file (961 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_round.cpython-310.pyc
ADDED
Binary file (6.03 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_shift.cpython-310.pyc
ADDED
Binary file (5.72 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_snap.cpython-310.pyc
ADDED
Binary file (1.17 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_frame.cpython-310.pyc
ADDED
Binary file (1.25 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_julian_date.cpython-310.pyc
ADDED
Binary file (2.26 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_period.cpython-310.pyc
ADDED
Binary file (7.4 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_pydatetime.cpython-310.pyc
ADDED
Binary file (1.69 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_series.cpython-310.pyc
ADDED
Binary file (948 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_tz_convert.cpython-310.pyc
ADDED
Binary file (7.57 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_tz_localize.cpython-310.pyc
ADDED
Binary file (11.1 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_unique.cpython-310.pyc
ADDED
Binary file (2.32 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_asof.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import timedelta
|
2 |
+
|
3 |
+
from pandas import (
|
4 |
+
Index,
|
5 |
+
Timestamp,
|
6 |
+
date_range,
|
7 |
+
isna,
|
8 |
+
)
|
9 |
+
|
10 |
+
|
11 |
+
class TestAsOf:
|
12 |
+
def test_asof_partial(self):
|
13 |
+
index = date_range("2010-01-01", periods=2, freq="ME")
|
14 |
+
expected = Timestamp("2010-02-28")
|
15 |
+
result = index.asof("2010-02")
|
16 |
+
assert result == expected
|
17 |
+
assert not isinstance(result, Index)
|
18 |
+
|
19 |
+
def test_asof(self):
|
20 |
+
index = date_range("2020-01-01", periods=10)
|
21 |
+
|
22 |
+
dt = index[0]
|
23 |
+
assert index.asof(dt) == dt
|
24 |
+
assert isna(index.asof(dt - timedelta(1)))
|
25 |
+
|
26 |
+
dt = index[-1]
|
27 |
+
assert index.asof(dt + timedelta(1)) == dt
|
28 |
+
|
29 |
+
dt = index[0].to_pydatetime()
|
30 |
+
assert isinstance(index.asof(dt), Timestamp)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_astype.py
ADDED
@@ -0,0 +1,335 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
|
3 |
+
import dateutil
|
4 |
+
import numpy as np
|
5 |
+
import pytest
|
6 |
+
import pytz
|
7 |
+
|
8 |
+
import pandas as pd
|
9 |
+
from pandas import (
|
10 |
+
DatetimeIndex,
|
11 |
+
Index,
|
12 |
+
NaT,
|
13 |
+
PeriodIndex,
|
14 |
+
Timestamp,
|
15 |
+
date_range,
|
16 |
+
)
|
17 |
+
import pandas._testing as tm
|
18 |
+
|
19 |
+
|
20 |
+
class TestDatetimeIndex:
|
21 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
22 |
+
def test_dti_astype_asobject_around_dst_transition(self, tzstr):
|
23 |
+
# GH#1345
|
24 |
+
|
25 |
+
# dates around a dst transition
|
26 |
+
rng = date_range("2/13/2010", "5/6/2010", tz=tzstr)
|
27 |
+
|
28 |
+
objs = rng.astype(object)
|
29 |
+
for i, x in enumerate(objs):
|
30 |
+
exval = rng[i]
|
31 |
+
assert x == exval
|
32 |
+
assert x.tzinfo == exval.tzinfo
|
33 |
+
|
34 |
+
objs = rng.astype(object)
|
35 |
+
for i, x in enumerate(objs):
|
36 |
+
exval = rng[i]
|
37 |
+
assert x == exval
|
38 |
+
assert x.tzinfo == exval.tzinfo
|
39 |
+
|
40 |
+
def test_astype(self):
|
41 |
+
# GH 13149, GH 13209
|
42 |
+
idx = DatetimeIndex(
|
43 |
+
["2016-05-16", "NaT", NaT, np.nan], dtype="M8[ns]", name="idx"
|
44 |
+
)
|
45 |
+
|
46 |
+
result = idx.astype(object)
|
47 |
+
expected = Index(
|
48 |
+
[Timestamp("2016-05-16")] + [NaT] * 3, dtype=object, name="idx"
|
49 |
+
)
|
50 |
+
tm.assert_index_equal(result, expected)
|
51 |
+
|
52 |
+
result = idx.astype(np.int64)
|
53 |
+
expected = Index(
|
54 |
+
[1463356800000000000] + [-9223372036854775808] * 3,
|
55 |
+
dtype=np.int64,
|
56 |
+
name="idx",
|
57 |
+
)
|
58 |
+
tm.assert_index_equal(result, expected)
|
59 |
+
|
60 |
+
def test_astype2(self):
|
61 |
+
rng = date_range("1/1/2000", periods=10, name="idx")
|
62 |
+
result = rng.astype("i8")
|
63 |
+
tm.assert_index_equal(result, Index(rng.asi8, name="idx"))
|
64 |
+
tm.assert_numpy_array_equal(result.values, rng.asi8)
|
65 |
+
|
66 |
+
def test_astype_uint(self):
|
67 |
+
arr = date_range("2000", periods=2, name="idx")
|
68 |
+
|
69 |
+
with pytest.raises(TypeError, match=r"Do obj.astype\('int64'\)"):
|
70 |
+
arr.astype("uint64")
|
71 |
+
with pytest.raises(TypeError, match=r"Do obj.astype\('int64'\)"):
|
72 |
+
arr.astype("uint32")
|
73 |
+
|
74 |
+
def test_astype_with_tz(self):
|
75 |
+
# with tz
|
76 |
+
rng = date_range("1/1/2000", periods=10, tz="US/Eastern")
|
77 |
+
msg = "Cannot use .astype to convert from timezone-aware"
|
78 |
+
with pytest.raises(TypeError, match=msg):
|
79 |
+
# deprecated
|
80 |
+
rng.astype("datetime64[ns]")
|
81 |
+
with pytest.raises(TypeError, match=msg):
|
82 |
+
# check DatetimeArray while we're here deprecated
|
83 |
+
rng._data.astype("datetime64[ns]")
|
84 |
+
|
85 |
+
def test_astype_tzaware_to_tzaware(self):
|
86 |
+
# GH 18951: tz-aware to tz-aware
|
87 |
+
idx = date_range("20170101", periods=4, tz="US/Pacific")
|
88 |
+
result = idx.astype("datetime64[ns, US/Eastern]")
|
89 |
+
expected = date_range("20170101 03:00:00", periods=4, tz="US/Eastern")
|
90 |
+
tm.assert_index_equal(result, expected)
|
91 |
+
assert result.freq == expected.freq
|
92 |
+
|
93 |
+
def test_astype_tznaive_to_tzaware(self):
|
94 |
+
# GH 18951: tz-naive to tz-aware
|
95 |
+
idx = date_range("20170101", periods=4)
|
96 |
+
idx = idx._with_freq(None) # tz_localize does not preserve freq
|
97 |
+
msg = "Cannot use .astype to convert from timezone-naive"
|
98 |
+
with pytest.raises(TypeError, match=msg):
|
99 |
+
# dt64->dt64tz deprecated
|
100 |
+
idx.astype("datetime64[ns, US/Eastern]")
|
101 |
+
with pytest.raises(TypeError, match=msg):
|
102 |
+
# dt64->dt64tz deprecated
|
103 |
+
idx._data.astype("datetime64[ns, US/Eastern]")
|
104 |
+
|
105 |
+
def test_astype_str_nat(self):
|
106 |
+
# GH 13149, GH 13209
|
107 |
+
# verify that we are returning NaT as a string (and not unicode)
|
108 |
+
|
109 |
+
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.nan])
|
110 |
+
result = idx.astype(str)
|
111 |
+
expected = Index(["2016-05-16", "NaT", "NaT", "NaT"], dtype=object)
|
112 |
+
tm.assert_index_equal(result, expected)
|
113 |
+
|
114 |
+
def test_astype_str(self):
|
115 |
+
# test astype string - #10442
|
116 |
+
dti = date_range("2012-01-01", periods=4, name="test_name")
|
117 |
+
result = dti.astype(str)
|
118 |
+
expected = Index(
|
119 |
+
["2012-01-01", "2012-01-02", "2012-01-03", "2012-01-04"],
|
120 |
+
name="test_name",
|
121 |
+
dtype=object,
|
122 |
+
)
|
123 |
+
tm.assert_index_equal(result, expected)
|
124 |
+
|
125 |
+
def test_astype_str_tz_and_name(self):
|
126 |
+
# test astype string with tz and name
|
127 |
+
dti = date_range("2012-01-01", periods=3, name="test_name", tz="US/Eastern")
|
128 |
+
result = dti.astype(str)
|
129 |
+
expected = Index(
|
130 |
+
[
|
131 |
+
"2012-01-01 00:00:00-05:00",
|
132 |
+
"2012-01-02 00:00:00-05:00",
|
133 |
+
"2012-01-03 00:00:00-05:00",
|
134 |
+
],
|
135 |
+
name="test_name",
|
136 |
+
dtype=object,
|
137 |
+
)
|
138 |
+
tm.assert_index_equal(result, expected)
|
139 |
+
|
140 |
+
def test_astype_str_freq_and_name(self):
|
141 |
+
# test astype string with freqH and name
|
142 |
+
dti = date_range("1/1/2011", periods=3, freq="h", name="test_name")
|
143 |
+
result = dti.astype(str)
|
144 |
+
expected = Index(
|
145 |
+
["2011-01-01 00:00:00", "2011-01-01 01:00:00", "2011-01-01 02:00:00"],
|
146 |
+
name="test_name",
|
147 |
+
dtype=object,
|
148 |
+
)
|
149 |
+
tm.assert_index_equal(result, expected)
|
150 |
+
|
151 |
+
def test_astype_str_freq_and_tz(self):
|
152 |
+
# test astype string with freqH and timezone
|
153 |
+
dti = date_range(
|
154 |
+
"3/6/2012 00:00", periods=2, freq="h", tz="Europe/London", name="test_name"
|
155 |
+
)
|
156 |
+
result = dti.astype(str)
|
157 |
+
expected = Index(
|
158 |
+
["2012-03-06 00:00:00+00:00", "2012-03-06 01:00:00+00:00"],
|
159 |
+
dtype=object,
|
160 |
+
name="test_name",
|
161 |
+
)
|
162 |
+
tm.assert_index_equal(result, expected)
|
163 |
+
|
164 |
+
def test_astype_datetime64(self):
|
165 |
+
# GH 13149, GH 13209
|
166 |
+
idx = DatetimeIndex(
|
167 |
+
["2016-05-16", "NaT", NaT, np.nan], dtype="M8[ns]", name="idx"
|
168 |
+
)
|
169 |
+
|
170 |
+
result = idx.astype("datetime64[ns]")
|
171 |
+
tm.assert_index_equal(result, idx)
|
172 |
+
assert result is not idx
|
173 |
+
|
174 |
+
result = idx.astype("datetime64[ns]", copy=False)
|
175 |
+
tm.assert_index_equal(result, idx)
|
176 |
+
assert result is idx
|
177 |
+
|
178 |
+
idx_tz = DatetimeIndex(["2016-05-16", "NaT", NaT, np.nan], tz="EST", name="idx")
|
179 |
+
msg = "Cannot use .astype to convert from timezone-aware"
|
180 |
+
with pytest.raises(TypeError, match=msg):
|
181 |
+
# dt64tz->dt64 deprecated
|
182 |
+
result = idx_tz.astype("datetime64[ns]")
|
183 |
+
|
184 |
+
def test_astype_object(self):
|
185 |
+
rng = date_range("1/1/2000", periods=20)
|
186 |
+
|
187 |
+
casted = rng.astype("O")
|
188 |
+
exp_values = list(rng)
|
189 |
+
|
190 |
+
tm.assert_index_equal(casted, Index(exp_values, dtype=np.object_))
|
191 |
+
assert casted.tolist() == exp_values
|
192 |
+
|
193 |
+
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo"])
|
194 |
+
def test_astype_object_tz(self, tz):
|
195 |
+
idx = date_range(start="2013-01-01", periods=4, freq="ME", name="idx", tz=tz)
|
196 |
+
expected_list = [
|
197 |
+
Timestamp("2013-01-31", tz=tz),
|
198 |
+
Timestamp("2013-02-28", tz=tz),
|
199 |
+
Timestamp("2013-03-31", tz=tz),
|
200 |
+
Timestamp("2013-04-30", tz=tz),
|
201 |
+
]
|
202 |
+
expected = Index(expected_list, dtype=object, name="idx")
|
203 |
+
result = idx.astype(object)
|
204 |
+
tm.assert_index_equal(result, expected)
|
205 |
+
assert idx.tolist() == expected_list
|
206 |
+
|
207 |
+
def test_astype_object_with_nat(self):
|
208 |
+
idx = DatetimeIndex(
|
209 |
+
[datetime(2013, 1, 1), datetime(2013, 1, 2), NaT, datetime(2013, 1, 4)],
|
210 |
+
name="idx",
|
211 |
+
)
|
212 |
+
expected_list = [
|
213 |
+
Timestamp("2013-01-01"),
|
214 |
+
Timestamp("2013-01-02"),
|
215 |
+
NaT,
|
216 |
+
Timestamp("2013-01-04"),
|
217 |
+
]
|
218 |
+
expected = Index(expected_list, dtype=object, name="idx")
|
219 |
+
result = idx.astype(object)
|
220 |
+
tm.assert_index_equal(result, expected)
|
221 |
+
assert idx.tolist() == expected_list
|
222 |
+
|
223 |
+
@pytest.mark.parametrize(
|
224 |
+
"dtype",
|
225 |
+
[float, "timedelta64", "timedelta64[ns]", "datetime64", "datetime64[D]"],
|
226 |
+
)
|
227 |
+
def test_astype_raises(self, dtype):
|
228 |
+
# GH 13149, GH 13209
|
229 |
+
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.nan])
|
230 |
+
msg = "Cannot cast DatetimeIndex to dtype"
|
231 |
+
if dtype == "datetime64":
|
232 |
+
msg = "Casting to unit-less dtype 'datetime64' is not supported"
|
233 |
+
with pytest.raises(TypeError, match=msg):
|
234 |
+
idx.astype(dtype)
|
235 |
+
|
236 |
+
def test_index_convert_to_datetime_array(self):
|
237 |
+
def _check_rng(rng):
|
238 |
+
converted = rng.to_pydatetime()
|
239 |
+
assert isinstance(converted, np.ndarray)
|
240 |
+
for x, stamp in zip(converted, rng):
|
241 |
+
assert isinstance(x, datetime)
|
242 |
+
assert x == stamp.to_pydatetime()
|
243 |
+
assert x.tzinfo == stamp.tzinfo
|
244 |
+
|
245 |
+
rng = date_range("20090415", "20090519")
|
246 |
+
rng_eastern = date_range("20090415", "20090519", tz="US/Eastern")
|
247 |
+
rng_utc = date_range("20090415", "20090519", tz="utc")
|
248 |
+
|
249 |
+
_check_rng(rng)
|
250 |
+
_check_rng(rng_eastern)
|
251 |
+
_check_rng(rng_utc)
|
252 |
+
|
253 |
+
def test_index_convert_to_datetime_array_explicit_pytz(self):
|
254 |
+
def _check_rng(rng):
|
255 |
+
converted = rng.to_pydatetime()
|
256 |
+
assert isinstance(converted, np.ndarray)
|
257 |
+
for x, stamp in zip(converted, rng):
|
258 |
+
assert isinstance(x, datetime)
|
259 |
+
assert x == stamp.to_pydatetime()
|
260 |
+
assert x.tzinfo == stamp.tzinfo
|
261 |
+
|
262 |
+
rng = date_range("20090415", "20090519")
|
263 |
+
rng_eastern = date_range("20090415", "20090519", tz=pytz.timezone("US/Eastern"))
|
264 |
+
rng_utc = date_range("20090415", "20090519", tz=pytz.utc)
|
265 |
+
|
266 |
+
_check_rng(rng)
|
267 |
+
_check_rng(rng_eastern)
|
268 |
+
_check_rng(rng_utc)
|
269 |
+
|
270 |
+
def test_index_convert_to_datetime_array_dateutil(self):
|
271 |
+
def _check_rng(rng):
|
272 |
+
converted = rng.to_pydatetime()
|
273 |
+
assert isinstance(converted, np.ndarray)
|
274 |
+
for x, stamp in zip(converted, rng):
|
275 |
+
assert isinstance(x, datetime)
|
276 |
+
assert x == stamp.to_pydatetime()
|
277 |
+
assert x.tzinfo == stamp.tzinfo
|
278 |
+
|
279 |
+
rng = date_range("20090415", "20090519")
|
280 |
+
rng_eastern = date_range("20090415", "20090519", tz="dateutil/US/Eastern")
|
281 |
+
rng_utc = date_range("20090415", "20090519", tz=dateutil.tz.tzutc())
|
282 |
+
|
283 |
+
_check_rng(rng)
|
284 |
+
_check_rng(rng_eastern)
|
285 |
+
_check_rng(rng_utc)
|
286 |
+
|
287 |
+
@pytest.mark.parametrize(
|
288 |
+
"tz, dtype",
|
289 |
+
[["US/Pacific", "datetime64[ns, US/Pacific]"], [None, "datetime64[ns]"]],
|
290 |
+
)
|
291 |
+
def test_integer_index_astype_datetime(self, tz, dtype):
|
292 |
+
# GH 20997, 20964, 24559
|
293 |
+
val = [Timestamp("2018-01-01", tz=tz).as_unit("ns")._value]
|
294 |
+
result = Index(val, name="idx").astype(dtype)
|
295 |
+
expected = DatetimeIndex(["2018-01-01"], tz=tz, name="idx").as_unit("ns")
|
296 |
+
tm.assert_index_equal(result, expected)
|
297 |
+
|
298 |
+
def test_dti_astype_period(self):
|
299 |
+
idx = DatetimeIndex([NaT, "2011-01-01", "2011-02-01"], name="idx")
|
300 |
+
|
301 |
+
res = idx.astype("period[M]")
|
302 |
+
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx")
|
303 |
+
tm.assert_index_equal(res, exp)
|
304 |
+
|
305 |
+
res = idx.astype("period[3M]")
|
306 |
+
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx")
|
307 |
+
tm.assert_index_equal(res, exp)
|
308 |
+
|
309 |
+
|
310 |
+
class TestAstype:
|
311 |
+
@pytest.mark.parametrize("tz", [None, "US/Central"])
|
312 |
+
def test_astype_category(self, tz):
|
313 |
+
obj = date_range("2000", periods=2, tz=tz, name="idx")
|
314 |
+
result = obj.astype("category")
|
315 |
+
dti = DatetimeIndex(["2000-01-01", "2000-01-02"], tz=tz).as_unit("ns")
|
316 |
+
expected = pd.CategoricalIndex(
|
317 |
+
dti,
|
318 |
+
name="idx",
|
319 |
+
)
|
320 |
+
tm.assert_index_equal(result, expected)
|
321 |
+
|
322 |
+
result = obj._data.astype("category")
|
323 |
+
expected = expected.values
|
324 |
+
tm.assert_categorical_equal(result, expected)
|
325 |
+
|
326 |
+
@pytest.mark.parametrize("tz", [None, "US/Central"])
|
327 |
+
def test_astype_array_fallback(self, tz):
|
328 |
+
obj = date_range("2000", periods=2, tz=tz, name="idx")
|
329 |
+
result = obj.astype(bool)
|
330 |
+
expected = Index(np.array([True, True]), name="idx")
|
331 |
+
tm.assert_index_equal(result, expected)
|
332 |
+
|
333 |
+
result = obj._data.astype(bool)
|
334 |
+
expected = np.array([True, True])
|
335 |
+
tm.assert_numpy_array_equal(result, expected)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_delete.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas import (
|
4 |
+
DatetimeIndex,
|
5 |
+
Series,
|
6 |
+
date_range,
|
7 |
+
)
|
8 |
+
import pandas._testing as tm
|
9 |
+
|
10 |
+
|
11 |
+
class TestDelete:
|
12 |
+
def test_delete(self, unit):
|
13 |
+
idx = date_range(
|
14 |
+
start="2000-01-01", periods=5, freq="ME", name="idx", unit=unit
|
15 |
+
)
|
16 |
+
|
17 |
+
# preserve freq
|
18 |
+
expected_0 = date_range(
|
19 |
+
start="2000-02-01", periods=4, freq="ME", name="idx", unit=unit
|
20 |
+
)
|
21 |
+
expected_4 = date_range(
|
22 |
+
start="2000-01-01", periods=4, freq="ME", name="idx", unit=unit
|
23 |
+
)
|
24 |
+
|
25 |
+
# reset freq to None
|
26 |
+
expected_1 = DatetimeIndex(
|
27 |
+
["2000-01-31", "2000-03-31", "2000-04-30", "2000-05-31"],
|
28 |
+
freq=None,
|
29 |
+
name="idx",
|
30 |
+
).as_unit(unit)
|
31 |
+
|
32 |
+
cases = {
|
33 |
+
0: expected_0,
|
34 |
+
-5: expected_0,
|
35 |
+
-1: expected_4,
|
36 |
+
4: expected_4,
|
37 |
+
1: expected_1,
|
38 |
+
}
|
39 |
+
for n, expected in cases.items():
|
40 |
+
result = idx.delete(n)
|
41 |
+
tm.assert_index_equal(result, expected)
|
42 |
+
assert result.name == expected.name
|
43 |
+
assert result.freq == expected.freq
|
44 |
+
|
45 |
+
with pytest.raises((IndexError, ValueError), match="out of bounds"):
|
46 |
+
# either depending on numpy version
|
47 |
+
idx.delete(5)
|
48 |
+
|
49 |
+
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo", "US/Pacific"])
|
50 |
+
def test_delete2(self, tz):
|
51 |
+
idx = date_range(
|
52 |
+
start="2000-01-01 09:00", periods=10, freq="h", name="idx", tz=tz
|
53 |
+
)
|
54 |
+
|
55 |
+
expected = date_range(
|
56 |
+
start="2000-01-01 10:00", periods=9, freq="h", name="idx", tz=tz
|
57 |
+
)
|
58 |
+
result = idx.delete(0)
|
59 |
+
tm.assert_index_equal(result, expected)
|
60 |
+
assert result.name == expected.name
|
61 |
+
assert result.freqstr == "h"
|
62 |
+
assert result.tz == expected.tz
|
63 |
+
|
64 |
+
expected = date_range(
|
65 |
+
start="2000-01-01 09:00", periods=9, freq="h", name="idx", tz=tz
|
66 |
+
)
|
67 |
+
result = idx.delete(-1)
|
68 |
+
tm.assert_index_equal(result, expected)
|
69 |
+
assert result.name == expected.name
|
70 |
+
assert result.freqstr == "h"
|
71 |
+
assert result.tz == expected.tz
|
72 |
+
|
73 |
+
def test_delete_slice(self, unit):
|
74 |
+
idx = date_range(
|
75 |
+
start="2000-01-01", periods=10, freq="D", name="idx", unit=unit
|
76 |
+
)
|
77 |
+
|
78 |
+
# preserve freq
|
79 |
+
expected_0_2 = date_range(
|
80 |
+
start="2000-01-04", periods=7, freq="D", name="idx", unit=unit
|
81 |
+
)
|
82 |
+
expected_7_9 = date_range(
|
83 |
+
start="2000-01-01", periods=7, freq="D", name="idx", unit=unit
|
84 |
+
)
|
85 |
+
|
86 |
+
# reset freq to None
|
87 |
+
expected_3_5 = DatetimeIndex(
|
88 |
+
[
|
89 |
+
"2000-01-01",
|
90 |
+
"2000-01-02",
|
91 |
+
"2000-01-03",
|
92 |
+
"2000-01-07",
|
93 |
+
"2000-01-08",
|
94 |
+
"2000-01-09",
|
95 |
+
"2000-01-10",
|
96 |
+
],
|
97 |
+
freq=None,
|
98 |
+
name="idx",
|
99 |
+
).as_unit(unit)
|
100 |
+
|
101 |
+
cases = {
|
102 |
+
(0, 1, 2): expected_0_2,
|
103 |
+
(7, 8, 9): expected_7_9,
|
104 |
+
(3, 4, 5): expected_3_5,
|
105 |
+
}
|
106 |
+
for n, expected in cases.items():
|
107 |
+
result = idx.delete(n)
|
108 |
+
tm.assert_index_equal(result, expected)
|
109 |
+
assert result.name == expected.name
|
110 |
+
assert result.freq == expected.freq
|
111 |
+
|
112 |
+
result = idx.delete(slice(n[0], n[-1] + 1))
|
113 |
+
tm.assert_index_equal(result, expected)
|
114 |
+
assert result.name == expected.name
|
115 |
+
assert result.freq == expected.freq
|
116 |
+
|
117 |
+
# TODO: belongs in Series.drop tests?
|
118 |
+
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo", "US/Pacific"])
|
119 |
+
def test_delete_slice2(self, tz, unit):
|
120 |
+
dti = date_range(
|
121 |
+
"2000-01-01 09:00", periods=10, freq="h", name="idx", tz=tz, unit=unit
|
122 |
+
)
|
123 |
+
ts = Series(
|
124 |
+
1,
|
125 |
+
index=dti,
|
126 |
+
)
|
127 |
+
# preserve freq
|
128 |
+
result = ts.drop(ts.index[:5]).index
|
129 |
+
expected = dti[5:]
|
130 |
+
tm.assert_index_equal(result, expected)
|
131 |
+
assert result.name == expected.name
|
132 |
+
assert result.freq == expected.freq
|
133 |
+
assert result.tz == expected.tz
|
134 |
+
|
135 |
+
# reset freq to None
|
136 |
+
result = ts.drop(ts.index[[1, 3, 5, 7, 9]]).index
|
137 |
+
expected = dti[::2]._with_freq(None)
|
138 |
+
tm.assert_index_equal(result, expected)
|
139 |
+
assert result.name == expected.name
|
140 |
+
assert result.freq == expected.freq
|
141 |
+
assert result.tz == expected.tz
|
llmeval-env/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"))
|
llmeval-env/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)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_insert.py
ADDED
@@ -0,0 +1,265 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import pytest
|
5 |
+
import pytz
|
6 |
+
|
7 |
+
from pandas import (
|
8 |
+
NA,
|
9 |
+
DatetimeIndex,
|
10 |
+
Index,
|
11 |
+
NaT,
|
12 |
+
Timestamp,
|
13 |
+
date_range,
|
14 |
+
)
|
15 |
+
import pandas._testing as tm
|
16 |
+
|
17 |
+
|
18 |
+
class TestInsert:
|
19 |
+
@pytest.mark.parametrize("null", [None, np.nan, np.datetime64("NaT"), NaT, NA])
|
20 |
+
@pytest.mark.parametrize("tz", [None, "UTC", "US/Eastern"])
|
21 |
+
def test_insert_nat(self, tz, null):
|
22 |
+
# GH#16537, GH#18295 (test missing)
|
23 |
+
|
24 |
+
idx = DatetimeIndex(["2017-01-01"], tz=tz)
|
25 |
+
expected = DatetimeIndex(["NaT", "2017-01-01"], tz=tz)
|
26 |
+
if tz is not None and isinstance(null, np.datetime64):
|
27 |
+
expected = Index([null, idx[0]], dtype=object)
|
28 |
+
|
29 |
+
res = idx.insert(0, null)
|
30 |
+
tm.assert_index_equal(res, expected)
|
31 |
+
|
32 |
+
@pytest.mark.parametrize("tz", [None, "UTC", "US/Eastern"])
|
33 |
+
def test_insert_invalid_na(self, tz):
|
34 |
+
idx = DatetimeIndex(["2017-01-01"], tz=tz)
|
35 |
+
|
36 |
+
item = np.timedelta64("NaT")
|
37 |
+
result = idx.insert(0, item)
|
38 |
+
expected = Index([item] + list(idx), dtype=object)
|
39 |
+
tm.assert_index_equal(result, expected)
|
40 |
+
|
41 |
+
def test_insert_empty_preserves_freq(self, tz_naive_fixture):
|
42 |
+
# GH#33573
|
43 |
+
tz = tz_naive_fixture
|
44 |
+
dti = DatetimeIndex([], tz=tz, freq="D")
|
45 |
+
item = Timestamp("2017-04-05").tz_localize(tz)
|
46 |
+
|
47 |
+
result = dti.insert(0, item)
|
48 |
+
assert result.freq == dti.freq
|
49 |
+
|
50 |
+
# But not when we insert an item that doesn't conform to freq
|
51 |
+
dti = DatetimeIndex([], tz=tz, freq="W-THU")
|
52 |
+
result = dti.insert(0, item)
|
53 |
+
assert result.freq is None
|
54 |
+
|
55 |
+
def test_insert(self, unit):
|
56 |
+
idx = DatetimeIndex(
|
57 |
+
["2000-01-04", "2000-01-01", "2000-01-02"], name="idx"
|
58 |
+
).as_unit(unit)
|
59 |
+
|
60 |
+
result = idx.insert(2, datetime(2000, 1, 5))
|
61 |
+
exp = DatetimeIndex(
|
62 |
+
["2000-01-04", "2000-01-01", "2000-01-05", "2000-01-02"], name="idx"
|
63 |
+
).as_unit(unit)
|
64 |
+
tm.assert_index_equal(result, exp)
|
65 |
+
|
66 |
+
# insertion of non-datetime should coerce to object index
|
67 |
+
result = idx.insert(1, "inserted")
|
68 |
+
expected = Index(
|
69 |
+
[
|
70 |
+
datetime(2000, 1, 4),
|
71 |
+
"inserted",
|
72 |
+
datetime(2000, 1, 1),
|
73 |
+
datetime(2000, 1, 2),
|
74 |
+
],
|
75 |
+
name="idx",
|
76 |
+
)
|
77 |
+
assert not isinstance(result, DatetimeIndex)
|
78 |
+
tm.assert_index_equal(result, expected)
|
79 |
+
assert result.name == expected.name
|
80 |
+
|
81 |
+
def test_insert2(self, unit):
|
82 |
+
idx = date_range("1/1/2000", periods=3, freq="ME", name="idx", unit=unit)
|
83 |
+
|
84 |
+
# preserve freq
|
85 |
+
expected_0 = DatetimeIndex(
|
86 |
+
["1999-12-31", "2000-01-31", "2000-02-29", "2000-03-31"],
|
87 |
+
name="idx",
|
88 |
+
freq="ME",
|
89 |
+
).as_unit(unit)
|
90 |
+
expected_3 = DatetimeIndex(
|
91 |
+
["2000-01-31", "2000-02-29", "2000-03-31", "2000-04-30"],
|
92 |
+
name="idx",
|
93 |
+
freq="ME",
|
94 |
+
).as_unit(unit)
|
95 |
+
|
96 |
+
# reset freq to None
|
97 |
+
expected_1_nofreq = DatetimeIndex(
|
98 |
+
["2000-01-31", "2000-01-31", "2000-02-29", "2000-03-31"],
|
99 |
+
name="idx",
|
100 |
+
freq=None,
|
101 |
+
).as_unit(unit)
|
102 |
+
expected_3_nofreq = DatetimeIndex(
|
103 |
+
["2000-01-31", "2000-02-29", "2000-03-31", "2000-01-02"],
|
104 |
+
name="idx",
|
105 |
+
freq=None,
|
106 |
+
).as_unit(unit)
|
107 |
+
|
108 |
+
cases = [
|
109 |
+
(0, datetime(1999, 12, 31), expected_0),
|
110 |
+
(-3, datetime(1999, 12, 31), expected_0),
|
111 |
+
(3, datetime(2000, 4, 30), expected_3),
|
112 |
+
(1, datetime(2000, 1, 31), expected_1_nofreq),
|
113 |
+
(3, datetime(2000, 1, 2), expected_3_nofreq),
|
114 |
+
]
|
115 |
+
|
116 |
+
for n, d, expected in cases:
|
117 |
+
result = idx.insert(n, d)
|
118 |
+
tm.assert_index_equal(result, expected)
|
119 |
+
assert result.name == expected.name
|
120 |
+
assert result.freq == expected.freq
|
121 |
+
|
122 |
+
def test_insert3(self, unit):
|
123 |
+
idx = date_range("1/1/2000", periods=3, freq="ME", name="idx", unit=unit)
|
124 |
+
|
125 |
+
# reset freq to None
|
126 |
+
result = idx.insert(3, datetime(2000, 1, 2))
|
127 |
+
expected = DatetimeIndex(
|
128 |
+
["2000-01-31", "2000-02-29", "2000-03-31", "2000-01-02"],
|
129 |
+
name="idx",
|
130 |
+
freq=None,
|
131 |
+
).as_unit(unit)
|
132 |
+
tm.assert_index_equal(result, expected)
|
133 |
+
assert result.name == expected.name
|
134 |
+
assert result.freq is None
|
135 |
+
|
136 |
+
def test_insert4(self, unit):
|
137 |
+
for tz in ["US/Pacific", "Asia/Singapore"]:
|
138 |
+
idx = date_range(
|
139 |
+
"1/1/2000 09:00", periods=6, freq="h", tz=tz, name="idx", unit=unit
|
140 |
+
)
|
141 |
+
# preserve freq
|
142 |
+
expected = date_range(
|
143 |
+
"1/1/2000 09:00", periods=7, freq="h", tz=tz, name="idx", unit=unit
|
144 |
+
)
|
145 |
+
for d in [
|
146 |
+
Timestamp("2000-01-01 15:00", tz=tz),
|
147 |
+
pytz.timezone(tz).localize(datetime(2000, 1, 1, 15)),
|
148 |
+
]:
|
149 |
+
result = idx.insert(6, d)
|
150 |
+
tm.assert_index_equal(result, expected)
|
151 |
+
assert result.name == expected.name
|
152 |
+
assert result.freq == expected.freq
|
153 |
+
assert result.tz == expected.tz
|
154 |
+
|
155 |
+
expected = DatetimeIndex(
|
156 |
+
[
|
157 |
+
"2000-01-01 09:00",
|
158 |
+
"2000-01-01 10:00",
|
159 |
+
"2000-01-01 11:00",
|
160 |
+
"2000-01-01 12:00",
|
161 |
+
"2000-01-01 13:00",
|
162 |
+
"2000-01-01 14:00",
|
163 |
+
"2000-01-01 10:00",
|
164 |
+
],
|
165 |
+
name="idx",
|
166 |
+
tz=tz,
|
167 |
+
freq=None,
|
168 |
+
).as_unit(unit)
|
169 |
+
# reset freq to None
|
170 |
+
for d in [
|
171 |
+
Timestamp("2000-01-01 10:00", tz=tz),
|
172 |
+
pytz.timezone(tz).localize(datetime(2000, 1, 1, 10)),
|
173 |
+
]:
|
174 |
+
result = idx.insert(6, d)
|
175 |
+
tm.assert_index_equal(result, expected)
|
176 |
+
assert result.name == expected.name
|
177 |
+
assert result.tz == expected.tz
|
178 |
+
assert result.freq is None
|
179 |
+
|
180 |
+
# TODO: also changes DataFrame.__setitem__ with expansion
|
181 |
+
def test_insert_mismatched_tzawareness(self):
|
182 |
+
# see GH#7299
|
183 |
+
idx = date_range("1/1/2000", periods=3, freq="D", tz="Asia/Tokyo", name="idx")
|
184 |
+
|
185 |
+
# mismatched tz-awareness
|
186 |
+
item = Timestamp("2000-01-04")
|
187 |
+
result = idx.insert(3, item)
|
188 |
+
expected = Index(
|
189 |
+
list(idx[:3]) + [item] + list(idx[3:]), dtype=object, name="idx"
|
190 |
+
)
|
191 |
+
tm.assert_index_equal(result, expected)
|
192 |
+
|
193 |
+
# mismatched tz-awareness
|
194 |
+
item = datetime(2000, 1, 4)
|
195 |
+
result = idx.insert(3, item)
|
196 |
+
expected = Index(
|
197 |
+
list(idx[:3]) + [item] + list(idx[3:]), dtype=object, name="idx"
|
198 |
+
)
|
199 |
+
tm.assert_index_equal(result, expected)
|
200 |
+
|
201 |
+
# TODO: also changes DataFrame.__setitem__ with expansion
|
202 |
+
def test_insert_mismatched_tz(self):
|
203 |
+
# see GH#7299
|
204 |
+
# pre-2.0 with mismatched tzs we would cast to object
|
205 |
+
idx = date_range("1/1/2000", periods=3, freq="D", tz="Asia/Tokyo", name="idx")
|
206 |
+
|
207 |
+
# mismatched tz -> cast to object (could reasonably cast to same tz or UTC)
|
208 |
+
item = Timestamp("2000-01-04", tz="US/Eastern")
|
209 |
+
result = idx.insert(3, item)
|
210 |
+
expected = Index(
|
211 |
+
list(idx[:3]) + [item.tz_convert(idx.tz)] + list(idx[3:]),
|
212 |
+
name="idx",
|
213 |
+
)
|
214 |
+
assert expected.dtype == idx.dtype
|
215 |
+
tm.assert_index_equal(result, expected)
|
216 |
+
|
217 |
+
item = datetime(2000, 1, 4, tzinfo=pytz.timezone("US/Eastern"))
|
218 |
+
result = idx.insert(3, item)
|
219 |
+
expected = Index(
|
220 |
+
list(idx[:3]) + [item.astimezone(idx.tzinfo)] + list(idx[3:]),
|
221 |
+
name="idx",
|
222 |
+
)
|
223 |
+
assert expected.dtype == idx.dtype
|
224 |
+
tm.assert_index_equal(result, expected)
|
225 |
+
|
226 |
+
@pytest.mark.parametrize(
|
227 |
+
"item", [0, np.int64(0), np.float64(0), np.array(0), np.timedelta64(456)]
|
228 |
+
)
|
229 |
+
def test_insert_mismatched_types_raises(self, tz_aware_fixture, item):
|
230 |
+
# GH#33703 dont cast these to dt64
|
231 |
+
tz = tz_aware_fixture
|
232 |
+
dti = date_range("2019-11-04", periods=9, freq="-1D", name=9, tz=tz)
|
233 |
+
|
234 |
+
result = dti.insert(1, item)
|
235 |
+
|
236 |
+
if isinstance(item, np.ndarray):
|
237 |
+
assert item.item() == 0
|
238 |
+
expected = Index([dti[0], 0] + list(dti[1:]), dtype=object, name=9)
|
239 |
+
else:
|
240 |
+
expected = Index([dti[0], item] + list(dti[1:]), dtype=object, name=9)
|
241 |
+
|
242 |
+
tm.assert_index_equal(result, expected)
|
243 |
+
|
244 |
+
def test_insert_castable_str(self, tz_aware_fixture):
|
245 |
+
# GH#33703
|
246 |
+
tz = tz_aware_fixture
|
247 |
+
dti = date_range("2019-11-04", periods=3, freq="-1D", name=9, tz=tz)
|
248 |
+
|
249 |
+
value = "2019-11-05"
|
250 |
+
result = dti.insert(0, value)
|
251 |
+
|
252 |
+
ts = Timestamp(value).tz_localize(tz)
|
253 |
+
expected = DatetimeIndex([ts] + list(dti), dtype=dti.dtype, name=9)
|
254 |
+
tm.assert_index_equal(result, expected)
|
255 |
+
|
256 |
+
def test_insert_non_castable_str(self, tz_aware_fixture):
|
257 |
+
# GH#33703
|
258 |
+
tz = tz_aware_fixture
|
259 |
+
dti = date_range("2019-11-04", periods=3, freq="-1D", name=9, tz=tz)
|
260 |
+
|
261 |
+
value = "foo"
|
262 |
+
result = dti.insert(0, value)
|
263 |
+
|
264 |
+
expected = Index(["foo"] + list(dti), dtype=object, name=9)
|
265 |
+
tm.assert_index_equal(result, expected)
|
llmeval-env/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
|
llmeval-env/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)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_normalize.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dateutil.tz import tzlocal
|
2 |
+
import numpy as np
|
3 |
+
import pytest
|
4 |
+
|
5 |
+
import pandas.util._test_decorators as td
|
6 |
+
|
7 |
+
from pandas import (
|
8 |
+
DatetimeIndex,
|
9 |
+
NaT,
|
10 |
+
Timestamp,
|
11 |
+
date_range,
|
12 |
+
)
|
13 |
+
import pandas._testing as tm
|
14 |
+
|
15 |
+
|
16 |
+
class TestNormalize:
|
17 |
+
def test_normalize(self):
|
18 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D")
|
19 |
+
|
20 |
+
result = rng.normalize()
|
21 |
+
expected = date_range("1/1/2000", periods=10, freq="D")
|
22 |
+
tm.assert_index_equal(result, expected)
|
23 |
+
|
24 |
+
arr_ns = np.array([1380585623454345752, 1380585612343234312]).astype(
|
25 |
+
"datetime64[ns]"
|
26 |
+
)
|
27 |
+
rng_ns = DatetimeIndex(arr_ns)
|
28 |
+
rng_ns_normalized = rng_ns.normalize()
|
29 |
+
|
30 |
+
arr_ns = np.array([1380585600000000000, 1380585600000000000]).astype(
|
31 |
+
"datetime64[ns]"
|
32 |
+
)
|
33 |
+
expected = DatetimeIndex(arr_ns)
|
34 |
+
tm.assert_index_equal(rng_ns_normalized, expected)
|
35 |
+
|
36 |
+
assert result.is_normalized
|
37 |
+
assert not rng.is_normalized
|
38 |
+
|
39 |
+
def test_normalize_nat(self):
|
40 |
+
dti = DatetimeIndex([NaT, Timestamp("2018-01-01 01:00:00")])
|
41 |
+
result = dti.normalize()
|
42 |
+
expected = DatetimeIndex([NaT, Timestamp("2018-01-01")])
|
43 |
+
tm.assert_index_equal(result, expected)
|
44 |
+
|
45 |
+
def test_normalize_tz(self):
|
46 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz="US/Eastern")
|
47 |
+
|
48 |
+
result = rng.normalize() # does not preserve freq
|
49 |
+
expected = date_range("1/1/2000", periods=10, freq="D", tz="US/Eastern")
|
50 |
+
tm.assert_index_equal(result, expected._with_freq(None))
|
51 |
+
|
52 |
+
assert result.is_normalized
|
53 |
+
assert not rng.is_normalized
|
54 |
+
|
55 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz="UTC")
|
56 |
+
|
57 |
+
result = rng.normalize()
|
58 |
+
expected = date_range("1/1/2000", periods=10, freq="D", tz="UTC")
|
59 |
+
tm.assert_index_equal(result, expected)
|
60 |
+
|
61 |
+
assert result.is_normalized
|
62 |
+
assert not rng.is_normalized
|
63 |
+
|
64 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz=tzlocal())
|
65 |
+
result = rng.normalize() # does not preserve freq
|
66 |
+
expected = date_range("1/1/2000", periods=10, freq="D", tz=tzlocal())
|
67 |
+
tm.assert_index_equal(result, expected._with_freq(None))
|
68 |
+
|
69 |
+
assert result.is_normalized
|
70 |
+
assert not rng.is_normalized
|
71 |
+
|
72 |
+
@td.skip_if_windows
|
73 |
+
@pytest.mark.parametrize(
|
74 |
+
"timezone",
|
75 |
+
[
|
76 |
+
"US/Pacific",
|
77 |
+
"US/Eastern",
|
78 |
+
"UTC",
|
79 |
+
"Asia/Kolkata",
|
80 |
+
"Asia/Shanghai",
|
81 |
+
"Australia/Canberra",
|
82 |
+
],
|
83 |
+
)
|
84 |
+
def test_normalize_tz_local(self, timezone):
|
85 |
+
# GH#13459
|
86 |
+
with tm.set_timezone(timezone):
|
87 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz=tzlocal())
|
88 |
+
|
89 |
+
result = rng.normalize()
|
90 |
+
expected = date_range("1/1/2000", periods=10, freq="D", tz=tzlocal())
|
91 |
+
expected = expected._with_freq(None)
|
92 |
+
tm.assert_index_equal(result, expected)
|
93 |
+
|
94 |
+
assert result.is_normalized
|
95 |
+
assert not rng.is_normalized
|
llmeval-env/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)
|
llmeval-env/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
|
llmeval-env/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"
|
llmeval-env/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)
|
llmeval-env/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
|
llmeval-env/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)
|
llmeval-env/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)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_period.py
ADDED
@@ -0,0 +1,225 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import dateutil.tz
|
2 |
+
from dateutil.tz import tzlocal
|
3 |
+
import pytest
|
4 |
+
import pytz
|
5 |
+
|
6 |
+
from pandas._libs.tslibs.ccalendar import MONTHS
|
7 |
+
from pandas._libs.tslibs.offsets import MonthEnd
|
8 |
+
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG
|
9 |
+
|
10 |
+
from pandas import (
|
11 |
+
DatetimeIndex,
|
12 |
+
Period,
|
13 |
+
PeriodIndex,
|
14 |
+
Timestamp,
|
15 |
+
date_range,
|
16 |
+
period_range,
|
17 |
+
)
|
18 |
+
import pandas._testing as tm
|
19 |
+
|
20 |
+
|
21 |
+
class TestToPeriod:
|
22 |
+
def test_dti_to_period(self):
|
23 |
+
dti = date_range(start="1/1/2005", end="12/1/2005", freq="ME")
|
24 |
+
pi1 = dti.to_period()
|
25 |
+
pi2 = dti.to_period(freq="D")
|
26 |
+
pi3 = dti.to_period(freq="3D")
|
27 |
+
|
28 |
+
assert pi1[0] == Period("Jan 2005", freq="M")
|
29 |
+
assert pi2[0] == Period("1/31/2005", freq="D")
|
30 |
+
assert pi3[0] == Period("1/31/2005", freq="3D")
|
31 |
+
|
32 |
+
assert pi1[-1] == Period("Nov 2005", freq="M")
|
33 |
+
assert pi2[-1] == Period("11/30/2005", freq="D")
|
34 |
+
assert pi3[-1], Period("11/30/2005", freq="3D")
|
35 |
+
|
36 |
+
tm.assert_index_equal(pi1, period_range("1/1/2005", "11/1/2005", freq="M"))
|
37 |
+
tm.assert_index_equal(
|
38 |
+
pi2, period_range("1/1/2005", "11/1/2005", freq="M").asfreq("D")
|
39 |
+
)
|
40 |
+
tm.assert_index_equal(
|
41 |
+
pi3, period_range("1/1/2005", "11/1/2005", freq="M").asfreq("3D")
|
42 |
+
)
|
43 |
+
|
44 |
+
@pytest.mark.parametrize("month", MONTHS)
|
45 |
+
def test_to_period_quarterly(self, month):
|
46 |
+
# make sure we can make the round trip
|
47 |
+
freq = f"Q-{month}"
|
48 |
+
rng = period_range("1989Q3", "1991Q3", freq=freq)
|
49 |
+
stamps = rng.to_timestamp()
|
50 |
+
result = stamps.to_period(freq)
|
51 |
+
tm.assert_index_equal(rng, result)
|
52 |
+
|
53 |
+
@pytest.mark.parametrize("off", ["BQE", "QS", "BQS"])
|
54 |
+
def test_to_period_quarterlyish(self, off):
|
55 |
+
rng = date_range("01-Jan-2012", periods=8, freq=off)
|
56 |
+
prng = rng.to_period()
|
57 |
+
assert prng.freq == "QE-DEC"
|
58 |
+
|
59 |
+
@pytest.mark.parametrize("off", ["BYE", "YS", "BYS"])
|
60 |
+
def test_to_period_annualish(self, off):
|
61 |
+
rng = date_range("01-Jan-2012", periods=8, freq=off)
|
62 |
+
prng = rng.to_period()
|
63 |
+
assert prng.freq == "YE-DEC"
|
64 |
+
|
65 |
+
def test_to_period_monthish(self):
|
66 |
+
offsets = ["MS", "BME"]
|
67 |
+
for off in offsets:
|
68 |
+
rng = date_range("01-Jan-2012", periods=8, freq=off)
|
69 |
+
prng = rng.to_period()
|
70 |
+
assert prng.freqstr == "M"
|
71 |
+
|
72 |
+
rng = date_range("01-Jan-2012", periods=8, freq="ME")
|
73 |
+
prng = rng.to_period()
|
74 |
+
assert prng.freqstr == "M"
|
75 |
+
|
76 |
+
with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG):
|
77 |
+
date_range("01-Jan-2012", periods=8, freq="EOM")
|
78 |
+
|
79 |
+
@pytest.mark.parametrize(
|
80 |
+
"freq_offset, freq_period",
|
81 |
+
[
|
82 |
+
("2ME", "2M"),
|
83 |
+
(MonthEnd(2), MonthEnd(2)),
|
84 |
+
],
|
85 |
+
)
|
86 |
+
def test_dti_to_period_2monthish(self, freq_offset, freq_period):
|
87 |
+
dti = date_range("2020-01-01", periods=3, freq=freq_offset)
|
88 |
+
pi = dti.to_period()
|
89 |
+
|
90 |
+
tm.assert_index_equal(pi, period_range("2020-01", "2020-05", freq=freq_period))
|
91 |
+
|
92 |
+
@pytest.mark.parametrize(
|
93 |
+
"freq, freq_depr",
|
94 |
+
[
|
95 |
+
("2ME", "2M"),
|
96 |
+
("2QE", "2Q"),
|
97 |
+
("2QE-SEP", "2Q-SEP"),
|
98 |
+
("1YE", "1Y"),
|
99 |
+
("2YE-MAR", "2Y-MAR"),
|
100 |
+
("1YE", "1A"),
|
101 |
+
("2YE-MAR", "2A-MAR"),
|
102 |
+
],
|
103 |
+
)
|
104 |
+
def test_to_period_frequency_M_Q_Y_A_deprecated(self, freq, freq_depr):
|
105 |
+
# GH#9586
|
106 |
+
msg = f"'{freq_depr[1:]}' is deprecated and will be removed "
|
107 |
+
f"in a future version, please use '{freq[1:]}' instead."
|
108 |
+
|
109 |
+
rng = date_range("01-Jan-2012", periods=8, freq=freq)
|
110 |
+
prng = rng.to_period()
|
111 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
112 |
+
assert prng.freq == freq_depr
|
113 |
+
|
114 |
+
def test_to_period_infer(self):
|
115 |
+
# https://github.com/pandas-dev/pandas/issues/33358
|
116 |
+
rng = date_range(
|
117 |
+
start="2019-12-22 06:40:00+00:00",
|
118 |
+
end="2019-12-22 08:45:00+00:00",
|
119 |
+
freq="5min",
|
120 |
+
)
|
121 |
+
|
122 |
+
with tm.assert_produces_warning(UserWarning):
|
123 |
+
pi1 = rng.to_period("5min")
|
124 |
+
|
125 |
+
with tm.assert_produces_warning(UserWarning):
|
126 |
+
pi2 = rng.to_period()
|
127 |
+
|
128 |
+
tm.assert_index_equal(pi1, pi2)
|
129 |
+
|
130 |
+
@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning")
|
131 |
+
def test_period_dt64_round_trip(self):
|
132 |
+
dti = date_range("1/1/2000", "1/7/2002", freq="B")
|
133 |
+
pi = dti.to_period()
|
134 |
+
tm.assert_index_equal(pi.to_timestamp(), dti)
|
135 |
+
|
136 |
+
dti = date_range("1/1/2000", "1/7/2002", freq="B")
|
137 |
+
pi = dti.to_period(freq="h")
|
138 |
+
tm.assert_index_equal(pi.to_timestamp(), dti)
|
139 |
+
|
140 |
+
def test_to_period_millisecond(self):
|
141 |
+
index = DatetimeIndex(
|
142 |
+
[
|
143 |
+
Timestamp("2007-01-01 10:11:12.123456Z"),
|
144 |
+
Timestamp("2007-01-01 10:11:13.789123Z"),
|
145 |
+
]
|
146 |
+
)
|
147 |
+
|
148 |
+
with tm.assert_produces_warning(UserWarning):
|
149 |
+
# warning that timezone info will be lost
|
150 |
+
period = index.to_period(freq="ms")
|
151 |
+
assert 2 == len(period)
|
152 |
+
assert period[0] == Period("2007-01-01 10:11:12.123Z", "ms")
|
153 |
+
assert period[1] == Period("2007-01-01 10:11:13.789Z", "ms")
|
154 |
+
|
155 |
+
def test_to_period_microsecond(self):
|
156 |
+
index = DatetimeIndex(
|
157 |
+
[
|
158 |
+
Timestamp("2007-01-01 10:11:12.123456Z"),
|
159 |
+
Timestamp("2007-01-01 10:11:13.789123Z"),
|
160 |
+
]
|
161 |
+
)
|
162 |
+
|
163 |
+
with tm.assert_produces_warning(UserWarning):
|
164 |
+
# warning that timezone info will be lost
|
165 |
+
period = index.to_period(freq="us")
|
166 |
+
assert 2 == len(period)
|
167 |
+
assert period[0] == Period("2007-01-01 10:11:12.123456Z", "us")
|
168 |
+
assert period[1] == Period("2007-01-01 10:11:13.789123Z", "us")
|
169 |
+
|
170 |
+
@pytest.mark.parametrize(
|
171 |
+
"tz",
|
172 |
+
["US/Eastern", pytz.utc, tzlocal(), "dateutil/US/Eastern", dateutil.tz.tzutc()],
|
173 |
+
)
|
174 |
+
def test_to_period_tz(self, tz):
|
175 |
+
ts = date_range("1/1/2000", "2/1/2000", tz=tz)
|
176 |
+
|
177 |
+
with tm.assert_produces_warning(UserWarning):
|
178 |
+
# GH#21333 warning that timezone info will be lost
|
179 |
+
# filter warning about freq deprecation
|
180 |
+
|
181 |
+
result = ts.to_period()[0]
|
182 |
+
expected = ts[0].to_period(ts.freq)
|
183 |
+
|
184 |
+
assert result == expected
|
185 |
+
|
186 |
+
expected = date_range("1/1/2000", "2/1/2000").to_period()
|
187 |
+
|
188 |
+
with tm.assert_produces_warning(UserWarning):
|
189 |
+
# GH#21333 warning that timezone info will be lost
|
190 |
+
result = ts.to_period(ts.freq)
|
191 |
+
|
192 |
+
tm.assert_index_equal(result, expected)
|
193 |
+
|
194 |
+
@pytest.mark.parametrize("tz", ["Etc/GMT-1", "Etc/GMT+1"])
|
195 |
+
def test_to_period_tz_utc_offset_consistency(self, tz):
|
196 |
+
# GH#22905
|
197 |
+
ts = date_range("1/1/2000", "2/1/2000", tz="Etc/GMT-1")
|
198 |
+
with tm.assert_produces_warning(UserWarning):
|
199 |
+
result = ts.to_period()[0]
|
200 |
+
expected = ts[0].to_period(ts.freq)
|
201 |
+
assert result == expected
|
202 |
+
|
203 |
+
def test_to_period_nofreq(self):
|
204 |
+
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-04"])
|
205 |
+
msg = "You must pass a freq argument as current index has none."
|
206 |
+
with pytest.raises(ValueError, match=msg):
|
207 |
+
idx.to_period()
|
208 |
+
|
209 |
+
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="infer")
|
210 |
+
assert idx.freqstr == "D"
|
211 |
+
expected = PeriodIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="D")
|
212 |
+
tm.assert_index_equal(idx.to_period(), expected)
|
213 |
+
|
214 |
+
# GH#7606
|
215 |
+
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"])
|
216 |
+
assert idx.freqstr is None
|
217 |
+
tm.assert_index_equal(idx.to_period(), expected)
|
218 |
+
|
219 |
+
@pytest.mark.parametrize("freq", ["2BMS", "1SME-15"])
|
220 |
+
def test_to_period_offsets_not_supported(self, freq):
|
221 |
+
# GH#56243
|
222 |
+
msg = f"{freq[1:]} is not supported as period frequency"
|
223 |
+
ts = date_range("1/1/2012", periods=4, freq=freq)
|
224 |
+
with pytest.raises(ValueError, match=msg):
|
225 |
+
ts.to_period()
|
llmeval-env/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)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_series.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
from pandas import (
|
4 |
+
DatetimeIndex,
|
5 |
+
Series,
|
6 |
+
)
|
7 |
+
import pandas._testing as tm
|
8 |
+
|
9 |
+
|
10 |
+
class TestToSeries:
|
11 |
+
def test_to_series(self):
|
12 |
+
naive = DatetimeIndex(["2013-1-1 13:00", "2013-1-2 14:00"], name="B")
|
13 |
+
idx = naive.tz_localize("US/Pacific")
|
14 |
+
|
15 |
+
expected = Series(np.array(idx.tolist(), dtype="object"), name="B")
|
16 |
+
result = idx.to_series(index=[0, 1])
|
17 |
+
assert expected.dtype == idx.dtype
|
18 |
+
tm.assert_series_equal(result, expected)
|
llmeval-env/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)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_tz_localize.py
ADDED
@@ -0,0 +1,402 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import (
|
2 |
+
datetime,
|
3 |
+
timedelta,
|
4 |
+
)
|
5 |
+
|
6 |
+
import dateutil.tz
|
7 |
+
from dateutil.tz import gettz
|
8 |
+
import numpy as np
|
9 |
+
import pytest
|
10 |
+
import pytz
|
11 |
+
|
12 |
+
from pandas import (
|
13 |
+
DatetimeIndex,
|
14 |
+
Timestamp,
|
15 |
+
bdate_range,
|
16 |
+
date_range,
|
17 |
+
offsets,
|
18 |
+
to_datetime,
|
19 |
+
)
|
20 |
+
import pandas._testing as tm
|
21 |
+
|
22 |
+
try:
|
23 |
+
from zoneinfo import ZoneInfo
|
24 |
+
except ImportError:
|
25 |
+
# Cannot assign to a type [misc]
|
26 |
+
ZoneInfo = None # type: ignore[misc, assignment]
|
27 |
+
|
28 |
+
|
29 |
+
easts = [pytz.timezone("US/Eastern"), gettz("US/Eastern")]
|
30 |
+
if ZoneInfo is not None:
|
31 |
+
try:
|
32 |
+
tz = ZoneInfo("US/Eastern")
|
33 |
+
except KeyError:
|
34 |
+
# no tzdata
|
35 |
+
pass
|
36 |
+
else:
|
37 |
+
easts.append(tz)
|
38 |
+
|
39 |
+
|
40 |
+
class TestTZLocalize:
|
41 |
+
def test_tz_localize_invalidates_freq(self):
|
42 |
+
# we only preserve freq in unambiguous cases
|
43 |
+
|
44 |
+
# if localized to US/Eastern, this crosses a DST transition
|
45 |
+
dti = date_range("2014-03-08 23:00", "2014-03-09 09:00", freq="h")
|
46 |
+
assert dti.freq == "h"
|
47 |
+
|
48 |
+
result = dti.tz_localize(None) # no-op
|
49 |
+
assert result.freq == "h"
|
50 |
+
|
51 |
+
result = dti.tz_localize("UTC") # unambiguous freq preservation
|
52 |
+
assert result.freq == "h"
|
53 |
+
|
54 |
+
result = dti.tz_localize("US/Eastern", nonexistent="shift_forward")
|
55 |
+
assert result.freq is None
|
56 |
+
assert result.inferred_freq is None # i.e. we are not _too_ strict here
|
57 |
+
|
58 |
+
# Case where we _can_ keep freq because we're length==1
|
59 |
+
dti2 = dti[:1]
|
60 |
+
result = dti2.tz_localize("US/Eastern")
|
61 |
+
assert result.freq == "h"
|
62 |
+
|
63 |
+
def test_tz_localize_utc_copies(self, utc_fixture):
|
64 |
+
# GH#46460
|
65 |
+
times = ["2015-03-08 01:00", "2015-03-08 02:00", "2015-03-08 03:00"]
|
66 |
+
index = DatetimeIndex(times)
|
67 |
+
|
68 |
+
res = index.tz_localize(utc_fixture)
|
69 |
+
assert not tm.shares_memory(res, index)
|
70 |
+
|
71 |
+
res2 = index._data.tz_localize(utc_fixture)
|
72 |
+
assert not tm.shares_memory(index._data, res2)
|
73 |
+
|
74 |
+
def test_dti_tz_localize_nonexistent_raise_coerce(self):
|
75 |
+
# GH#13057
|
76 |
+
times = ["2015-03-08 01:00", "2015-03-08 02:00", "2015-03-08 03:00"]
|
77 |
+
index = DatetimeIndex(times)
|
78 |
+
tz = "US/Eastern"
|
79 |
+
with pytest.raises(pytz.NonExistentTimeError, match="|".join(times)):
|
80 |
+
index.tz_localize(tz=tz)
|
81 |
+
|
82 |
+
with pytest.raises(pytz.NonExistentTimeError, match="|".join(times)):
|
83 |
+
index.tz_localize(tz=tz, nonexistent="raise")
|
84 |
+
|
85 |
+
result = index.tz_localize(tz=tz, nonexistent="NaT")
|
86 |
+
test_times = ["2015-03-08 01:00-05:00", "NaT", "2015-03-08 03:00-04:00"]
|
87 |
+
dti = to_datetime(test_times, utc=True)
|
88 |
+
expected = dti.tz_convert("US/Eastern")
|
89 |
+
tm.assert_index_equal(result, expected)
|
90 |
+
|
91 |
+
@pytest.mark.parametrize("tz", easts)
|
92 |
+
def test_dti_tz_localize_ambiguous_infer(self, tz):
|
93 |
+
# November 6, 2011, fall back, repeat 2 AM hour
|
94 |
+
# With no repeated hours, we cannot infer the transition
|
95 |
+
dr = date_range(datetime(2011, 11, 6, 0), periods=5, freq=offsets.Hour())
|
96 |
+
with pytest.raises(pytz.AmbiguousTimeError, match="Cannot infer dst time"):
|
97 |
+
dr.tz_localize(tz)
|
98 |
+
|
99 |
+
@pytest.mark.parametrize("tz", easts)
|
100 |
+
def test_dti_tz_localize_ambiguous_infer2(self, tz, unit):
|
101 |
+
# With repeated hours, we can infer the transition
|
102 |
+
dr = date_range(
|
103 |
+
datetime(2011, 11, 6, 0), periods=5, freq=offsets.Hour(), tz=tz, unit=unit
|
104 |
+
)
|
105 |
+
times = [
|
106 |
+
"11/06/2011 00:00",
|
107 |
+
"11/06/2011 01:00",
|
108 |
+
"11/06/2011 01:00",
|
109 |
+
"11/06/2011 02:00",
|
110 |
+
"11/06/2011 03:00",
|
111 |
+
]
|
112 |
+
di = DatetimeIndex(times).as_unit(unit)
|
113 |
+
result = di.tz_localize(tz, ambiguous="infer")
|
114 |
+
expected = dr._with_freq(None)
|
115 |
+
tm.assert_index_equal(result, expected)
|
116 |
+
result2 = DatetimeIndex(times, tz=tz, ambiguous="infer").as_unit(unit)
|
117 |
+
tm.assert_index_equal(result2, expected)
|
118 |
+
|
119 |
+
@pytest.mark.parametrize("tz", easts)
|
120 |
+
def test_dti_tz_localize_ambiguous_infer3(self, tz):
|
121 |
+
# When there is no dst transition, nothing special happens
|
122 |
+
dr = date_range(datetime(2011, 6, 1, 0), periods=10, freq=offsets.Hour())
|
123 |
+
localized = dr.tz_localize(tz)
|
124 |
+
localized_infer = dr.tz_localize(tz, ambiguous="infer")
|
125 |
+
tm.assert_index_equal(localized, localized_infer)
|
126 |
+
|
127 |
+
@pytest.mark.parametrize("tz", easts)
|
128 |
+
def test_dti_tz_localize_ambiguous_times(self, tz):
|
129 |
+
# March 13, 2011, spring forward, skip from 2 AM to 3 AM
|
130 |
+
dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3, freq=offsets.Hour())
|
131 |
+
with pytest.raises(pytz.NonExistentTimeError, match="2011-03-13 02:30:00"):
|
132 |
+
dr.tz_localize(tz)
|
133 |
+
|
134 |
+
# after dst transition, it works
|
135 |
+
dr = date_range(
|
136 |
+
datetime(2011, 3, 13, 3, 30), periods=3, freq=offsets.Hour(), tz=tz
|
137 |
+
)
|
138 |
+
|
139 |
+
# November 6, 2011, fall back, repeat 2 AM hour
|
140 |
+
dr = date_range(datetime(2011, 11, 6, 1, 30), periods=3, freq=offsets.Hour())
|
141 |
+
with pytest.raises(pytz.AmbiguousTimeError, match="Cannot infer dst time"):
|
142 |
+
dr.tz_localize(tz)
|
143 |
+
|
144 |
+
# UTC is OK
|
145 |
+
dr = date_range(
|
146 |
+
datetime(2011, 3, 13), periods=48, freq=offsets.Minute(30), tz=pytz.utc
|
147 |
+
)
|
148 |
+
|
149 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
150 |
+
def test_dti_tz_localize_pass_dates_to_utc(self, tzstr):
|
151 |
+
strdates = ["1/1/2012", "3/1/2012", "4/1/2012"]
|
152 |
+
|
153 |
+
idx = DatetimeIndex(strdates)
|
154 |
+
conv = idx.tz_localize(tzstr)
|
155 |
+
|
156 |
+
fromdates = DatetimeIndex(strdates, tz=tzstr)
|
157 |
+
|
158 |
+
assert conv.tz == fromdates.tz
|
159 |
+
tm.assert_numpy_array_equal(conv.values, fromdates.values)
|
160 |
+
|
161 |
+
@pytest.mark.parametrize("prefix", ["", "dateutil/"])
|
162 |
+
def test_dti_tz_localize(self, prefix):
|
163 |
+
tzstr = prefix + "US/Eastern"
|
164 |
+
dti = date_range(start="1/1/2005", end="1/1/2005 0:00:30.256", freq="ms")
|
165 |
+
dti2 = dti.tz_localize(tzstr)
|
166 |
+
|
167 |
+
dti_utc = date_range(
|
168 |
+
start="1/1/2005 05:00", end="1/1/2005 5:00:30.256", freq="ms", tz="utc"
|
169 |
+
)
|
170 |
+
|
171 |
+
tm.assert_numpy_array_equal(dti2.values, dti_utc.values)
|
172 |
+
|
173 |
+
dti3 = dti2.tz_convert(prefix + "US/Pacific")
|
174 |
+
tm.assert_numpy_array_equal(dti3.values, dti_utc.values)
|
175 |
+
|
176 |
+
dti = date_range(start="11/6/2011 1:59", end="11/6/2011 2:00", freq="ms")
|
177 |
+
with pytest.raises(pytz.AmbiguousTimeError, match="Cannot infer dst time"):
|
178 |
+
dti.tz_localize(tzstr)
|
179 |
+
|
180 |
+
dti = date_range(start="3/13/2011 1:59", end="3/13/2011 2:00", freq="ms")
|
181 |
+
with pytest.raises(pytz.NonExistentTimeError, match="2011-03-13 02:00:00"):
|
182 |
+
dti.tz_localize(tzstr)
|
183 |
+
|
184 |
+
@pytest.mark.parametrize(
|
185 |
+
"tz",
|
186 |
+
[
|
187 |
+
"US/Eastern",
|
188 |
+
"dateutil/US/Eastern",
|
189 |
+
pytz.timezone("US/Eastern"),
|
190 |
+
gettz("US/Eastern"),
|
191 |
+
],
|
192 |
+
)
|
193 |
+
def test_dti_tz_localize_utc_conversion(self, tz):
|
194 |
+
# Localizing to time zone should:
|
195 |
+
# 1) check for DST ambiguities
|
196 |
+
# 2) convert to UTC
|
197 |
+
|
198 |
+
rng = date_range("3/10/2012", "3/11/2012", freq="30min")
|
199 |
+
|
200 |
+
converted = rng.tz_localize(tz)
|
201 |
+
expected_naive = rng + offsets.Hour(5)
|
202 |
+
tm.assert_numpy_array_equal(converted.asi8, expected_naive.asi8)
|
203 |
+
|
204 |
+
# DST ambiguity, this should fail
|
205 |
+
rng = date_range("3/11/2012", "3/12/2012", freq="30min")
|
206 |
+
# Is this really how it should fail??
|
207 |
+
with pytest.raises(pytz.NonExistentTimeError, match="2012-03-11 02:00:00"):
|
208 |
+
rng.tz_localize(tz)
|
209 |
+
|
210 |
+
def test_dti_tz_localize_roundtrip(self, tz_aware_fixture):
|
211 |
+
# note: this tz tests that a tz-naive index can be localized
|
212 |
+
# and de-localized successfully, when there are no DST transitions
|
213 |
+
# in the range.
|
214 |
+
idx = date_range(start="2014-06-01", end="2014-08-30", freq="15min")
|
215 |
+
tz = tz_aware_fixture
|
216 |
+
localized = idx.tz_localize(tz)
|
217 |
+
# can't localize a tz-aware object
|
218 |
+
with pytest.raises(
|
219 |
+
TypeError, match="Already tz-aware, use tz_convert to convert"
|
220 |
+
):
|
221 |
+
localized.tz_localize(tz)
|
222 |
+
reset = localized.tz_localize(None)
|
223 |
+
assert reset.tzinfo is None
|
224 |
+
expected = idx._with_freq(None)
|
225 |
+
tm.assert_index_equal(reset, expected)
|
226 |
+
|
227 |
+
def test_dti_tz_localize_naive(self):
|
228 |
+
rng = date_range("1/1/2011", periods=100, freq="h")
|
229 |
+
|
230 |
+
conv = rng.tz_localize("US/Pacific")
|
231 |
+
exp = date_range("1/1/2011", periods=100, freq="h", tz="US/Pacific")
|
232 |
+
|
233 |
+
tm.assert_index_equal(conv, exp._with_freq(None))
|
234 |
+
|
235 |
+
def test_dti_tz_localize_tzlocal(self):
|
236 |
+
# GH#13583
|
237 |
+
offset = dateutil.tz.tzlocal().utcoffset(datetime(2011, 1, 1))
|
238 |
+
offset = int(offset.total_seconds() * 1000000000)
|
239 |
+
|
240 |
+
dti = date_range(start="2001-01-01", end="2001-03-01")
|
241 |
+
dti2 = dti.tz_localize(dateutil.tz.tzlocal())
|
242 |
+
tm.assert_numpy_array_equal(dti2.asi8 + offset, dti.asi8)
|
243 |
+
|
244 |
+
dti = date_range(start="2001-01-01", end="2001-03-01", tz=dateutil.tz.tzlocal())
|
245 |
+
dti2 = dti.tz_localize(None)
|
246 |
+
tm.assert_numpy_array_equal(dti2.asi8 - offset, dti.asi8)
|
247 |
+
|
248 |
+
@pytest.mark.parametrize("tz", easts)
|
249 |
+
def test_dti_tz_localize_ambiguous_nat(self, tz):
|
250 |
+
times = [
|
251 |
+
"11/06/2011 00:00",
|
252 |
+
"11/06/2011 01:00",
|
253 |
+
"11/06/2011 01:00",
|
254 |
+
"11/06/2011 02:00",
|
255 |
+
"11/06/2011 03:00",
|
256 |
+
]
|
257 |
+
di = DatetimeIndex(times)
|
258 |
+
localized = di.tz_localize(tz, ambiguous="NaT")
|
259 |
+
|
260 |
+
times = [
|
261 |
+
"11/06/2011 00:00",
|
262 |
+
np.nan,
|
263 |
+
np.nan,
|
264 |
+
"11/06/2011 02:00",
|
265 |
+
"11/06/2011 03:00",
|
266 |
+
]
|
267 |
+
di_test = DatetimeIndex(times, tz="US/Eastern")
|
268 |
+
|
269 |
+
# left dtype is datetime64[ns, US/Eastern]
|
270 |
+
# right is datetime64[ns, tzfile('/usr/share/zoneinfo/US/Eastern')]
|
271 |
+
tm.assert_numpy_array_equal(di_test.values, localized.values)
|
272 |
+
|
273 |
+
@pytest.mark.parametrize("tz", easts)
|
274 |
+
def test_dti_tz_localize_ambiguous_flags(self, tz, unit):
|
275 |
+
# November 6, 2011, fall back, repeat 2 AM hour
|
276 |
+
|
277 |
+
# Pass in flags to determine right dst transition
|
278 |
+
dr = date_range(
|
279 |
+
datetime(2011, 11, 6, 0), periods=5, freq=offsets.Hour(), tz=tz, unit=unit
|
280 |
+
)
|
281 |
+
times = [
|
282 |
+
"11/06/2011 00:00",
|
283 |
+
"11/06/2011 01:00",
|
284 |
+
"11/06/2011 01:00",
|
285 |
+
"11/06/2011 02:00",
|
286 |
+
"11/06/2011 03:00",
|
287 |
+
]
|
288 |
+
|
289 |
+
# Test tz_localize
|
290 |
+
di = DatetimeIndex(times).as_unit(unit)
|
291 |
+
is_dst = [1, 1, 0, 0, 0]
|
292 |
+
localized = di.tz_localize(tz, ambiguous=is_dst)
|
293 |
+
expected = dr._with_freq(None)
|
294 |
+
tm.assert_index_equal(expected, localized)
|
295 |
+
|
296 |
+
result = DatetimeIndex(times, tz=tz, ambiguous=is_dst).as_unit(unit)
|
297 |
+
tm.assert_index_equal(result, expected)
|
298 |
+
|
299 |
+
localized = di.tz_localize(tz, ambiguous=np.array(is_dst))
|
300 |
+
tm.assert_index_equal(dr, localized)
|
301 |
+
|
302 |
+
localized = di.tz_localize(tz, ambiguous=np.array(is_dst).astype("bool"))
|
303 |
+
tm.assert_index_equal(dr, localized)
|
304 |
+
|
305 |
+
# Test constructor
|
306 |
+
localized = DatetimeIndex(times, tz=tz, ambiguous=is_dst).as_unit(unit)
|
307 |
+
tm.assert_index_equal(dr, localized)
|
308 |
+
|
309 |
+
# Test duplicate times where inferring the dst fails
|
310 |
+
times += times
|
311 |
+
di = DatetimeIndex(times).as_unit(unit)
|
312 |
+
|
313 |
+
# When the sizes are incompatible, make sure error is raised
|
314 |
+
msg = "Length of ambiguous bool-array must be the same size as vals"
|
315 |
+
with pytest.raises(Exception, match=msg):
|
316 |
+
di.tz_localize(tz, ambiguous=is_dst)
|
317 |
+
|
318 |
+
# When sizes are compatible and there are repeats ('infer' won't work)
|
319 |
+
is_dst = np.hstack((is_dst, is_dst))
|
320 |
+
localized = di.tz_localize(tz, ambiguous=is_dst)
|
321 |
+
dr = dr.append(dr)
|
322 |
+
tm.assert_index_equal(dr, localized)
|
323 |
+
|
324 |
+
@pytest.mark.parametrize("tz", easts)
|
325 |
+
def test_dti_tz_localize_ambiguous_flags2(self, tz, unit):
|
326 |
+
# When there is no dst transition, nothing special happens
|
327 |
+
dr = date_range(datetime(2011, 6, 1, 0), periods=10, freq=offsets.Hour())
|
328 |
+
is_dst = np.array([1] * 10)
|
329 |
+
localized = dr.tz_localize(tz)
|
330 |
+
localized_is_dst = dr.tz_localize(tz, ambiguous=is_dst)
|
331 |
+
tm.assert_index_equal(localized, localized_is_dst)
|
332 |
+
|
333 |
+
def test_dti_tz_localize_bdate_range(self):
|
334 |
+
dr = bdate_range("1/1/2009", "1/1/2010")
|
335 |
+
dr_utc = bdate_range("1/1/2009", "1/1/2010", tz=pytz.utc)
|
336 |
+
localized = dr.tz_localize(pytz.utc)
|
337 |
+
tm.assert_index_equal(dr_utc, localized)
|
338 |
+
|
339 |
+
@pytest.mark.parametrize(
|
340 |
+
"start_ts, tz, end_ts, shift",
|
341 |
+
[
|
342 |
+
["2015-03-29 02:20:00", "Europe/Warsaw", "2015-03-29 03:00:00", "forward"],
|
343 |
+
[
|
344 |
+
"2015-03-29 02:20:00",
|
345 |
+
"Europe/Warsaw",
|
346 |
+
"2015-03-29 01:59:59.999999999",
|
347 |
+
"backward",
|
348 |
+
],
|
349 |
+
[
|
350 |
+
"2015-03-29 02:20:00",
|
351 |
+
"Europe/Warsaw",
|
352 |
+
"2015-03-29 03:20:00",
|
353 |
+
timedelta(hours=1),
|
354 |
+
],
|
355 |
+
[
|
356 |
+
"2015-03-29 02:20:00",
|
357 |
+
"Europe/Warsaw",
|
358 |
+
"2015-03-29 01:20:00",
|
359 |
+
timedelta(hours=-1),
|
360 |
+
],
|
361 |
+
["2018-03-11 02:33:00", "US/Pacific", "2018-03-11 03:00:00", "forward"],
|
362 |
+
[
|
363 |
+
"2018-03-11 02:33:00",
|
364 |
+
"US/Pacific",
|
365 |
+
"2018-03-11 01:59:59.999999999",
|
366 |
+
"backward",
|
367 |
+
],
|
368 |
+
[
|
369 |
+
"2018-03-11 02:33:00",
|
370 |
+
"US/Pacific",
|
371 |
+
"2018-03-11 03:33:00",
|
372 |
+
timedelta(hours=1),
|
373 |
+
],
|
374 |
+
[
|
375 |
+
"2018-03-11 02:33:00",
|
376 |
+
"US/Pacific",
|
377 |
+
"2018-03-11 01:33:00",
|
378 |
+
timedelta(hours=-1),
|
379 |
+
],
|
380 |
+
],
|
381 |
+
)
|
382 |
+
@pytest.mark.parametrize("tz_type", ["", "dateutil/"])
|
383 |
+
def test_dti_tz_localize_nonexistent_shift(
|
384 |
+
self, start_ts, tz, end_ts, shift, tz_type, unit
|
385 |
+
):
|
386 |
+
# GH#8917
|
387 |
+
tz = tz_type + tz
|
388 |
+
if isinstance(shift, str):
|
389 |
+
shift = "shift_" + shift
|
390 |
+
dti = DatetimeIndex([Timestamp(start_ts)]).as_unit(unit)
|
391 |
+
result = dti.tz_localize(tz, nonexistent=shift)
|
392 |
+
expected = DatetimeIndex([Timestamp(end_ts)]).tz_localize(tz).as_unit(unit)
|
393 |
+
tm.assert_index_equal(result, expected)
|
394 |
+
|
395 |
+
@pytest.mark.parametrize("offset", [-1, 1])
|
396 |
+
def test_dti_tz_localize_nonexistent_shift_invalid(self, offset, warsaw):
|
397 |
+
# GH#8917
|
398 |
+
tz = warsaw
|
399 |
+
dti = DatetimeIndex([Timestamp("2015-03-29 02:20:00")])
|
400 |
+
msg = "The provided timedelta will relocalize on a nonexistent time"
|
401 |
+
with pytest.raises(ValueError, match=msg):
|
402 |
+
dti.tz_localize(tz, nonexistent=timedelta(seconds=offset))
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_unique.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import (
|
2 |
+
datetime,
|
3 |
+
timedelta,
|
4 |
+
)
|
5 |
+
|
6 |
+
from pandas import (
|
7 |
+
DatetimeIndex,
|
8 |
+
NaT,
|
9 |
+
Timestamp,
|
10 |
+
)
|
11 |
+
import pandas._testing as tm
|
12 |
+
|
13 |
+
|
14 |
+
def test_unique(tz_naive_fixture):
|
15 |
+
idx = DatetimeIndex(["2017"] * 2, tz=tz_naive_fixture)
|
16 |
+
expected = idx[:1]
|
17 |
+
|
18 |
+
result = idx.unique()
|
19 |
+
tm.assert_index_equal(result, expected)
|
20 |
+
# GH#21737
|
21 |
+
# Ensure the underlying data is consistent
|
22 |
+
assert result[0] == expected[0]
|
23 |
+
|
24 |
+
|
25 |
+
def test_index_unique(rand_series_with_duplicate_datetimeindex):
|
26 |
+
dups = rand_series_with_duplicate_datetimeindex
|
27 |
+
index = dups.index
|
28 |
+
|
29 |
+
uniques = index.unique()
|
30 |
+
expected = DatetimeIndex(
|
31 |
+
[
|
32 |
+
datetime(2000, 1, 2),
|
33 |
+
datetime(2000, 1, 3),
|
34 |
+
datetime(2000, 1, 4),
|
35 |
+
datetime(2000, 1, 5),
|
36 |
+
],
|
37 |
+
dtype=index.dtype,
|
38 |
+
)
|
39 |
+
assert uniques.dtype == index.dtype # sanity
|
40 |
+
tm.assert_index_equal(uniques, expected)
|
41 |
+
assert index.nunique() == 4
|
42 |
+
|
43 |
+
# GH#2563
|
44 |
+
assert isinstance(uniques, DatetimeIndex)
|
45 |
+
|
46 |
+
dups_local = index.tz_localize("US/Eastern")
|
47 |
+
dups_local.name = "foo"
|
48 |
+
result = dups_local.unique()
|
49 |
+
expected = DatetimeIndex(expected, name="foo")
|
50 |
+
expected = expected.tz_localize("US/Eastern")
|
51 |
+
assert result.tz is not None
|
52 |
+
assert result.name == "foo"
|
53 |
+
tm.assert_index_equal(result, expected)
|
54 |
+
|
55 |
+
|
56 |
+
def test_index_unique2():
|
57 |
+
# NaT, note this is excluded
|
58 |
+
arr = [1370745748 + t for t in range(20)] + [NaT._value]
|
59 |
+
idx = DatetimeIndex(arr * 3)
|
60 |
+
tm.assert_index_equal(idx.unique(), DatetimeIndex(arr))
|
61 |
+
assert idx.nunique() == 20
|
62 |
+
assert idx.nunique(dropna=False) == 21
|
63 |
+
|
64 |
+
|
65 |
+
def test_index_unique3():
|
66 |
+
arr = [
|
67 |
+
Timestamp("2013-06-09 02:42:28") + timedelta(seconds=t) for t in range(20)
|
68 |
+
] + [NaT]
|
69 |
+
idx = DatetimeIndex(arr * 3)
|
70 |
+
tm.assert_index_equal(idx.unique(), DatetimeIndex(arr))
|
71 |
+
assert idx.nunique() == 20
|
72 |
+
assert idx.nunique(dropna=False) == 21
|
73 |
+
|
74 |
+
|
75 |
+
def test_is_unique_monotonic(rand_series_with_duplicate_datetimeindex):
|
76 |
+
index = rand_series_with_duplicate_datetimeindex.index
|
77 |
+
assert not index.is_unique
|