Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_asof.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_npfuncs.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_ops.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_scalar_compat.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_setops.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_unique.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_frame.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_series.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_astype.py +310 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_factorize.py +125 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_fillna.py +62 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_insert.py +256 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_repeat.py +78 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_shift.py +162 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_snap.py +47 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_frame.py +28 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_period.py +188 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_series.py +18 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_asof.py +31 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_constructors.py +1098 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_date_range.py +1258 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_datetime.py +198 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_datetimelike.py +39 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_delete.py +138 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_formats.py +267 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_freq_attr.py +61 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_indexing.py +712 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_join.py +155 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_map.py +47 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_misc.py +305 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_npfuncs.py +13 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_ops.py +85 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_partial_slicing.py +457 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_pickle.py +45 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_reindex.py +56 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_scalar_compat.py +347 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_setops.py +597 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_timezones.py +1213 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_unique.py +76 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_constructors.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_formats.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_freq_attr.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_indexing.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_join.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_monotonic.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_partial_slicing.cpython-310.pyc +0 -0
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_asof.cpython-310.pyc
ADDED
|
Binary file (1.19 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_npfuncs.cpython-310.pyc
ADDED
|
Binary file (811 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_ops.cpython-310.pyc
ADDED
|
Binary file (2.81 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_scalar_compat.cpython-310.pyc
ADDED
|
Binary file (10.1 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_setops.cpython-310.pyc
ADDED
|
Binary file (15.8 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/__pycache__/test_unique.cpython-310.pyc
ADDED
|
Binary file (2.29 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (194 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_frame.cpython-310.pyc
ADDED
|
Binary file (1.23 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/__pycache__/test_to_series.cpython-310.pyc
ADDED
|
Binary file (931 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_astype.py
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
def test_astype(self):
|
| 22 |
+
# GH 13149, GH 13209
|
| 23 |
+
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.NaN], name="idx")
|
| 24 |
+
|
| 25 |
+
result = idx.astype(object)
|
| 26 |
+
expected = Index(
|
| 27 |
+
[Timestamp("2016-05-16")] + [NaT] * 3, dtype=object, name="idx"
|
| 28 |
+
)
|
| 29 |
+
tm.assert_index_equal(result, expected)
|
| 30 |
+
|
| 31 |
+
result = idx.astype(np.int64)
|
| 32 |
+
expected = Index(
|
| 33 |
+
[1463356800000000000] + [-9223372036854775808] * 3,
|
| 34 |
+
dtype=np.int64,
|
| 35 |
+
name="idx",
|
| 36 |
+
)
|
| 37 |
+
tm.assert_index_equal(result, expected)
|
| 38 |
+
|
| 39 |
+
rng = date_range("1/1/2000", periods=10, name="idx")
|
| 40 |
+
result = rng.astype("i8")
|
| 41 |
+
tm.assert_index_equal(result, Index(rng.asi8, name="idx"))
|
| 42 |
+
tm.assert_numpy_array_equal(result.values, rng.asi8)
|
| 43 |
+
|
| 44 |
+
def test_astype_uint(self):
|
| 45 |
+
arr = date_range("2000", periods=2, name="idx")
|
| 46 |
+
|
| 47 |
+
with pytest.raises(TypeError, match=r"Do obj.astype\('int64'\)"):
|
| 48 |
+
arr.astype("uint64")
|
| 49 |
+
with pytest.raises(TypeError, match=r"Do obj.astype\('int64'\)"):
|
| 50 |
+
arr.astype("uint32")
|
| 51 |
+
|
| 52 |
+
def test_astype_with_tz(self):
|
| 53 |
+
# with tz
|
| 54 |
+
rng = date_range("1/1/2000", periods=10, tz="US/Eastern")
|
| 55 |
+
msg = "Cannot use .astype to convert from timezone-aware"
|
| 56 |
+
with pytest.raises(TypeError, match=msg):
|
| 57 |
+
# deprecated
|
| 58 |
+
rng.astype("datetime64[ns]")
|
| 59 |
+
with pytest.raises(TypeError, match=msg):
|
| 60 |
+
# check DatetimeArray while we're here deprecated
|
| 61 |
+
rng._data.astype("datetime64[ns]")
|
| 62 |
+
|
| 63 |
+
def test_astype_tzaware_to_tzaware(self):
|
| 64 |
+
# GH 18951: tz-aware to tz-aware
|
| 65 |
+
idx = date_range("20170101", periods=4, tz="US/Pacific")
|
| 66 |
+
result = idx.astype("datetime64[ns, US/Eastern]")
|
| 67 |
+
expected = date_range("20170101 03:00:00", periods=4, tz="US/Eastern")
|
| 68 |
+
tm.assert_index_equal(result, expected)
|
| 69 |
+
assert result.freq == expected.freq
|
| 70 |
+
|
| 71 |
+
def test_astype_tznaive_to_tzaware(self):
|
| 72 |
+
# GH 18951: tz-naive to tz-aware
|
| 73 |
+
idx = date_range("20170101", periods=4)
|
| 74 |
+
idx = idx._with_freq(None) # tz_localize does not preserve freq
|
| 75 |
+
msg = "Cannot use .astype to convert from timezone-naive"
|
| 76 |
+
with pytest.raises(TypeError, match=msg):
|
| 77 |
+
# dt64->dt64tz deprecated
|
| 78 |
+
idx.astype("datetime64[ns, US/Eastern]")
|
| 79 |
+
with pytest.raises(TypeError, match=msg):
|
| 80 |
+
# dt64->dt64tz deprecated
|
| 81 |
+
idx._data.astype("datetime64[ns, US/Eastern]")
|
| 82 |
+
|
| 83 |
+
def test_astype_str_nat(self):
|
| 84 |
+
# GH 13149, GH 13209
|
| 85 |
+
# verify that we are returning NaT as a string (and not unicode)
|
| 86 |
+
|
| 87 |
+
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.NaN])
|
| 88 |
+
result = idx.astype(str)
|
| 89 |
+
expected = Index(["2016-05-16", "NaT", "NaT", "NaT"], dtype=object)
|
| 90 |
+
tm.assert_index_equal(result, expected)
|
| 91 |
+
|
| 92 |
+
def test_astype_str(self):
|
| 93 |
+
# test astype string - #10442
|
| 94 |
+
dti = date_range("2012-01-01", periods=4, name="test_name")
|
| 95 |
+
result = dti.astype(str)
|
| 96 |
+
expected = Index(
|
| 97 |
+
["2012-01-01", "2012-01-02", "2012-01-03", "2012-01-04"],
|
| 98 |
+
name="test_name",
|
| 99 |
+
dtype=object,
|
| 100 |
+
)
|
| 101 |
+
tm.assert_index_equal(result, expected)
|
| 102 |
+
|
| 103 |
+
def test_astype_str_tz_and_name(self):
|
| 104 |
+
# test astype string with tz and name
|
| 105 |
+
dti = date_range("2012-01-01", periods=3, name="test_name", tz="US/Eastern")
|
| 106 |
+
result = dti.astype(str)
|
| 107 |
+
expected = Index(
|
| 108 |
+
[
|
| 109 |
+
"2012-01-01 00:00:00-05:00",
|
| 110 |
+
"2012-01-02 00:00:00-05:00",
|
| 111 |
+
"2012-01-03 00:00:00-05:00",
|
| 112 |
+
],
|
| 113 |
+
name="test_name",
|
| 114 |
+
dtype=object,
|
| 115 |
+
)
|
| 116 |
+
tm.assert_index_equal(result, expected)
|
| 117 |
+
|
| 118 |
+
def test_astype_str_freq_and_name(self):
|
| 119 |
+
# test astype string with freqH and name
|
| 120 |
+
dti = date_range("1/1/2011", periods=3, freq="H", name="test_name")
|
| 121 |
+
result = dti.astype(str)
|
| 122 |
+
expected = Index(
|
| 123 |
+
["2011-01-01 00:00:00", "2011-01-01 01:00:00", "2011-01-01 02:00:00"],
|
| 124 |
+
name="test_name",
|
| 125 |
+
dtype=object,
|
| 126 |
+
)
|
| 127 |
+
tm.assert_index_equal(result, expected)
|
| 128 |
+
|
| 129 |
+
def test_astype_str_freq_and_tz(self):
|
| 130 |
+
# test astype string with freqH and timezone
|
| 131 |
+
dti = date_range(
|
| 132 |
+
"3/6/2012 00:00", periods=2, freq="H", tz="Europe/London", name="test_name"
|
| 133 |
+
)
|
| 134 |
+
result = dti.astype(str)
|
| 135 |
+
expected = Index(
|
| 136 |
+
["2012-03-06 00:00:00+00:00", "2012-03-06 01:00:00+00:00"],
|
| 137 |
+
dtype=object,
|
| 138 |
+
name="test_name",
|
| 139 |
+
)
|
| 140 |
+
tm.assert_index_equal(result, expected)
|
| 141 |
+
|
| 142 |
+
def test_astype_datetime64(self):
|
| 143 |
+
# GH 13149, GH 13209
|
| 144 |
+
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.NaN], name="idx")
|
| 145 |
+
|
| 146 |
+
result = idx.astype("datetime64[ns]")
|
| 147 |
+
tm.assert_index_equal(result, idx)
|
| 148 |
+
assert result is not idx
|
| 149 |
+
|
| 150 |
+
result = idx.astype("datetime64[ns]", copy=False)
|
| 151 |
+
tm.assert_index_equal(result, idx)
|
| 152 |
+
assert result is idx
|
| 153 |
+
|
| 154 |
+
idx_tz = DatetimeIndex(["2016-05-16", "NaT", NaT, np.NaN], tz="EST", name="idx")
|
| 155 |
+
msg = "Cannot use .astype to convert from timezone-aware"
|
| 156 |
+
with pytest.raises(TypeError, match=msg):
|
| 157 |
+
# dt64tz->dt64 deprecated
|
| 158 |
+
result = idx_tz.astype("datetime64[ns]")
|
| 159 |
+
|
| 160 |
+
def test_astype_object(self):
|
| 161 |
+
rng = date_range("1/1/2000", periods=20)
|
| 162 |
+
|
| 163 |
+
casted = rng.astype("O")
|
| 164 |
+
exp_values = list(rng)
|
| 165 |
+
|
| 166 |
+
tm.assert_index_equal(casted, Index(exp_values, dtype=np.object_))
|
| 167 |
+
assert casted.tolist() == exp_values
|
| 168 |
+
|
| 169 |
+
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo"])
|
| 170 |
+
def test_astype_object_tz(self, tz):
|
| 171 |
+
idx = date_range(start="2013-01-01", periods=4, freq="M", name="idx", tz=tz)
|
| 172 |
+
expected_list = [
|
| 173 |
+
Timestamp("2013-01-31", tz=tz),
|
| 174 |
+
Timestamp("2013-02-28", tz=tz),
|
| 175 |
+
Timestamp("2013-03-31", tz=tz),
|
| 176 |
+
Timestamp("2013-04-30", tz=tz),
|
| 177 |
+
]
|
| 178 |
+
expected = Index(expected_list, dtype=object, name="idx")
|
| 179 |
+
result = idx.astype(object)
|
| 180 |
+
tm.assert_index_equal(result, expected)
|
| 181 |
+
assert idx.tolist() == expected_list
|
| 182 |
+
|
| 183 |
+
def test_astype_object_with_nat(self):
|
| 184 |
+
idx = DatetimeIndex(
|
| 185 |
+
[datetime(2013, 1, 1), datetime(2013, 1, 2), NaT, datetime(2013, 1, 4)],
|
| 186 |
+
name="idx",
|
| 187 |
+
)
|
| 188 |
+
expected_list = [
|
| 189 |
+
Timestamp("2013-01-01"),
|
| 190 |
+
Timestamp("2013-01-02"),
|
| 191 |
+
NaT,
|
| 192 |
+
Timestamp("2013-01-04"),
|
| 193 |
+
]
|
| 194 |
+
expected = Index(expected_list, dtype=object, name="idx")
|
| 195 |
+
result = idx.astype(object)
|
| 196 |
+
tm.assert_index_equal(result, expected)
|
| 197 |
+
assert idx.tolist() == expected_list
|
| 198 |
+
|
| 199 |
+
@pytest.mark.parametrize(
|
| 200 |
+
"dtype",
|
| 201 |
+
[float, "timedelta64", "timedelta64[ns]", "datetime64", "datetime64[D]"],
|
| 202 |
+
)
|
| 203 |
+
def test_astype_raises(self, dtype):
|
| 204 |
+
# GH 13149, GH 13209
|
| 205 |
+
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.NaN])
|
| 206 |
+
msg = "Cannot cast DatetimeIndex to dtype"
|
| 207 |
+
if dtype == "datetime64":
|
| 208 |
+
msg = "Casting to unit-less dtype 'datetime64' is not supported"
|
| 209 |
+
with pytest.raises(TypeError, match=msg):
|
| 210 |
+
idx.astype(dtype)
|
| 211 |
+
|
| 212 |
+
def test_index_convert_to_datetime_array(self):
|
| 213 |
+
def _check_rng(rng):
|
| 214 |
+
converted = rng.to_pydatetime()
|
| 215 |
+
assert isinstance(converted, np.ndarray)
|
| 216 |
+
for x, stamp in zip(converted, rng):
|
| 217 |
+
assert isinstance(x, datetime)
|
| 218 |
+
assert x == stamp.to_pydatetime()
|
| 219 |
+
assert x.tzinfo == stamp.tzinfo
|
| 220 |
+
|
| 221 |
+
rng = date_range("20090415", "20090519")
|
| 222 |
+
rng_eastern = date_range("20090415", "20090519", tz="US/Eastern")
|
| 223 |
+
rng_utc = date_range("20090415", "20090519", tz="utc")
|
| 224 |
+
|
| 225 |
+
_check_rng(rng)
|
| 226 |
+
_check_rng(rng_eastern)
|
| 227 |
+
_check_rng(rng_utc)
|
| 228 |
+
|
| 229 |
+
def test_index_convert_to_datetime_array_explicit_pytz(self):
|
| 230 |
+
def _check_rng(rng):
|
| 231 |
+
converted = rng.to_pydatetime()
|
| 232 |
+
assert isinstance(converted, np.ndarray)
|
| 233 |
+
for x, stamp in zip(converted, rng):
|
| 234 |
+
assert isinstance(x, datetime)
|
| 235 |
+
assert x == stamp.to_pydatetime()
|
| 236 |
+
assert x.tzinfo == stamp.tzinfo
|
| 237 |
+
|
| 238 |
+
rng = date_range("20090415", "20090519")
|
| 239 |
+
rng_eastern = date_range("20090415", "20090519", tz=pytz.timezone("US/Eastern"))
|
| 240 |
+
rng_utc = date_range("20090415", "20090519", tz=pytz.utc)
|
| 241 |
+
|
| 242 |
+
_check_rng(rng)
|
| 243 |
+
_check_rng(rng_eastern)
|
| 244 |
+
_check_rng(rng_utc)
|
| 245 |
+
|
| 246 |
+
def test_index_convert_to_datetime_array_dateutil(self):
|
| 247 |
+
def _check_rng(rng):
|
| 248 |
+
converted = rng.to_pydatetime()
|
| 249 |
+
assert isinstance(converted, np.ndarray)
|
| 250 |
+
for x, stamp in zip(converted, rng):
|
| 251 |
+
assert isinstance(x, datetime)
|
| 252 |
+
assert x == stamp.to_pydatetime()
|
| 253 |
+
assert x.tzinfo == stamp.tzinfo
|
| 254 |
+
|
| 255 |
+
rng = date_range("20090415", "20090519")
|
| 256 |
+
rng_eastern = date_range("20090415", "20090519", tz="dateutil/US/Eastern")
|
| 257 |
+
rng_utc = date_range("20090415", "20090519", tz=dateutil.tz.tzutc())
|
| 258 |
+
|
| 259 |
+
_check_rng(rng)
|
| 260 |
+
_check_rng(rng_eastern)
|
| 261 |
+
_check_rng(rng_utc)
|
| 262 |
+
|
| 263 |
+
@pytest.mark.parametrize(
|
| 264 |
+
"tz, dtype",
|
| 265 |
+
[["US/Pacific", "datetime64[ns, US/Pacific]"], [None, "datetime64[ns]"]],
|
| 266 |
+
)
|
| 267 |
+
def test_integer_index_astype_datetime(self, tz, dtype):
|
| 268 |
+
# GH 20997, 20964, 24559
|
| 269 |
+
val = [Timestamp("2018-01-01", tz=tz).as_unit("ns")._value]
|
| 270 |
+
result = Index(val, name="idx").astype(dtype)
|
| 271 |
+
expected = DatetimeIndex(["2018-01-01"], tz=tz, name="idx")
|
| 272 |
+
tm.assert_index_equal(result, expected)
|
| 273 |
+
|
| 274 |
+
def test_dti_astype_period(self):
|
| 275 |
+
idx = DatetimeIndex([NaT, "2011-01-01", "2011-02-01"], name="idx")
|
| 276 |
+
|
| 277 |
+
res = idx.astype("period[M]")
|
| 278 |
+
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx")
|
| 279 |
+
tm.assert_index_equal(res, exp)
|
| 280 |
+
|
| 281 |
+
res = idx.astype("period[3M]")
|
| 282 |
+
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx")
|
| 283 |
+
tm.assert_index_equal(res, exp)
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
class TestAstype:
|
| 287 |
+
@pytest.mark.parametrize("tz", [None, "US/Central"])
|
| 288 |
+
def test_astype_category(self, tz):
|
| 289 |
+
obj = date_range("2000", periods=2, tz=tz, name="idx")
|
| 290 |
+
result = obj.astype("category")
|
| 291 |
+
expected = pd.CategoricalIndex(
|
| 292 |
+
[Timestamp("2000-01-01", tz=tz), Timestamp("2000-01-02", tz=tz)],
|
| 293 |
+
name="idx",
|
| 294 |
+
)
|
| 295 |
+
tm.assert_index_equal(result, expected)
|
| 296 |
+
|
| 297 |
+
result = obj._data.astype("category")
|
| 298 |
+
expected = expected.values
|
| 299 |
+
tm.assert_categorical_equal(result, expected)
|
| 300 |
+
|
| 301 |
+
@pytest.mark.parametrize("tz", [None, "US/Central"])
|
| 302 |
+
def test_astype_array_fallback(self, tz):
|
| 303 |
+
obj = date_range("2000", periods=2, tz=tz, name="idx")
|
| 304 |
+
result = obj.astype(bool)
|
| 305 |
+
expected = Index(np.array([True, True]), name="idx")
|
| 306 |
+
tm.assert_index_equal(result, expected)
|
| 307 |
+
|
| 308 |
+
result = obj._data.astype(bool)
|
| 309 |
+
expected = np.array([True, True])
|
| 310 |
+
tm.assert_numpy_array_equal(result, expected)
|
videochat2/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="M", 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"))
|
videochat2/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)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_insert.py
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
| 56 |
+
idx = DatetimeIndex(["2000-01-04", "2000-01-01", "2000-01-02"], name="idx")
|
| 57 |
+
|
| 58 |
+
result = idx.insert(2, datetime(2000, 1, 5))
|
| 59 |
+
exp = DatetimeIndex(
|
| 60 |
+
["2000-01-04", "2000-01-01", "2000-01-05", "2000-01-02"], name="idx"
|
| 61 |
+
)
|
| 62 |
+
tm.assert_index_equal(result, exp)
|
| 63 |
+
|
| 64 |
+
# insertion of non-datetime should coerce to object index
|
| 65 |
+
result = idx.insert(1, "inserted")
|
| 66 |
+
expected = Index(
|
| 67 |
+
[
|
| 68 |
+
datetime(2000, 1, 4),
|
| 69 |
+
"inserted",
|
| 70 |
+
datetime(2000, 1, 1),
|
| 71 |
+
datetime(2000, 1, 2),
|
| 72 |
+
],
|
| 73 |
+
name="idx",
|
| 74 |
+
)
|
| 75 |
+
assert not isinstance(result, DatetimeIndex)
|
| 76 |
+
tm.assert_index_equal(result, expected)
|
| 77 |
+
assert result.name == expected.name
|
| 78 |
+
|
| 79 |
+
idx = date_range("1/1/2000", periods=3, freq="M", name="idx")
|
| 80 |
+
|
| 81 |
+
# preserve freq
|
| 82 |
+
expected_0 = DatetimeIndex(
|
| 83 |
+
["1999-12-31", "2000-01-31", "2000-02-29", "2000-03-31"],
|
| 84 |
+
name="idx",
|
| 85 |
+
freq="M",
|
| 86 |
+
)
|
| 87 |
+
expected_3 = DatetimeIndex(
|
| 88 |
+
["2000-01-31", "2000-02-29", "2000-03-31", "2000-04-30"],
|
| 89 |
+
name="idx",
|
| 90 |
+
freq="M",
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# reset freq to None
|
| 94 |
+
expected_1_nofreq = DatetimeIndex(
|
| 95 |
+
["2000-01-31", "2000-01-31", "2000-02-29", "2000-03-31"],
|
| 96 |
+
name="idx",
|
| 97 |
+
freq=None,
|
| 98 |
+
)
|
| 99 |
+
expected_3_nofreq = DatetimeIndex(
|
| 100 |
+
["2000-01-31", "2000-02-29", "2000-03-31", "2000-01-02"],
|
| 101 |
+
name="idx",
|
| 102 |
+
freq=None,
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
cases = [
|
| 106 |
+
(0, datetime(1999, 12, 31), expected_0),
|
| 107 |
+
(-3, datetime(1999, 12, 31), expected_0),
|
| 108 |
+
(3, datetime(2000, 4, 30), expected_3),
|
| 109 |
+
(1, datetime(2000, 1, 31), expected_1_nofreq),
|
| 110 |
+
(3, datetime(2000, 1, 2), expected_3_nofreq),
|
| 111 |
+
]
|
| 112 |
+
|
| 113 |
+
for n, d, expected in cases:
|
| 114 |
+
result = idx.insert(n, d)
|
| 115 |
+
tm.assert_index_equal(result, expected)
|
| 116 |
+
assert result.name == expected.name
|
| 117 |
+
assert result.freq == expected.freq
|
| 118 |
+
|
| 119 |
+
# reset freq to None
|
| 120 |
+
result = idx.insert(3, datetime(2000, 1, 2))
|
| 121 |
+
expected = DatetimeIndex(
|
| 122 |
+
["2000-01-31", "2000-02-29", "2000-03-31", "2000-01-02"],
|
| 123 |
+
name="idx",
|
| 124 |
+
freq=None,
|
| 125 |
+
)
|
| 126 |
+
tm.assert_index_equal(result, expected)
|
| 127 |
+
assert result.name == expected.name
|
| 128 |
+
assert result.freq is None
|
| 129 |
+
|
| 130 |
+
for tz in ["US/Pacific", "Asia/Singapore"]:
|
| 131 |
+
idx = date_range("1/1/2000 09:00", periods=6, freq="H", tz=tz, name="idx")
|
| 132 |
+
# preserve freq
|
| 133 |
+
expected = date_range(
|
| 134 |
+
"1/1/2000 09:00", periods=7, freq="H", tz=tz, name="idx"
|
| 135 |
+
)
|
| 136 |
+
for d in [
|
| 137 |
+
Timestamp("2000-01-01 15:00", tz=tz),
|
| 138 |
+
pytz.timezone(tz).localize(datetime(2000, 1, 1, 15)),
|
| 139 |
+
]:
|
| 140 |
+
result = idx.insert(6, d)
|
| 141 |
+
tm.assert_index_equal(result, expected)
|
| 142 |
+
assert result.name == expected.name
|
| 143 |
+
assert result.freq == expected.freq
|
| 144 |
+
assert result.tz == expected.tz
|
| 145 |
+
|
| 146 |
+
expected = DatetimeIndex(
|
| 147 |
+
[
|
| 148 |
+
"2000-01-01 09:00",
|
| 149 |
+
"2000-01-01 10:00",
|
| 150 |
+
"2000-01-01 11:00",
|
| 151 |
+
"2000-01-01 12:00",
|
| 152 |
+
"2000-01-01 13:00",
|
| 153 |
+
"2000-01-01 14:00",
|
| 154 |
+
"2000-01-01 10:00",
|
| 155 |
+
],
|
| 156 |
+
name="idx",
|
| 157 |
+
tz=tz,
|
| 158 |
+
freq=None,
|
| 159 |
+
)
|
| 160 |
+
# reset freq to None
|
| 161 |
+
for d in [
|
| 162 |
+
Timestamp("2000-01-01 10:00", tz=tz),
|
| 163 |
+
pytz.timezone(tz).localize(datetime(2000, 1, 1, 10)),
|
| 164 |
+
]:
|
| 165 |
+
result = idx.insert(6, d)
|
| 166 |
+
tm.assert_index_equal(result, expected)
|
| 167 |
+
assert result.name == expected.name
|
| 168 |
+
assert result.tz == expected.tz
|
| 169 |
+
assert result.freq is None
|
| 170 |
+
|
| 171 |
+
# TODO: also changes DataFrame.__setitem__ with expansion
|
| 172 |
+
def test_insert_mismatched_tzawareness(self):
|
| 173 |
+
# see GH#7299
|
| 174 |
+
idx = date_range("1/1/2000", periods=3, freq="D", tz="Asia/Tokyo", name="idx")
|
| 175 |
+
|
| 176 |
+
# mismatched tz-awareness
|
| 177 |
+
item = Timestamp("2000-01-04")
|
| 178 |
+
result = idx.insert(3, item)
|
| 179 |
+
expected = Index(
|
| 180 |
+
list(idx[:3]) + [item] + list(idx[3:]), dtype=object, name="idx"
|
| 181 |
+
)
|
| 182 |
+
tm.assert_index_equal(result, expected)
|
| 183 |
+
|
| 184 |
+
# mismatched tz-awareness
|
| 185 |
+
item = datetime(2000, 1, 4)
|
| 186 |
+
result = idx.insert(3, item)
|
| 187 |
+
expected = Index(
|
| 188 |
+
list(idx[:3]) + [item] + list(idx[3:]), dtype=object, name="idx"
|
| 189 |
+
)
|
| 190 |
+
tm.assert_index_equal(result, expected)
|
| 191 |
+
|
| 192 |
+
# TODO: also changes DataFrame.__setitem__ with expansion
|
| 193 |
+
def test_insert_mismatched_tz(self):
|
| 194 |
+
# see GH#7299
|
| 195 |
+
# pre-2.0 with mismatched tzs we would cast to object
|
| 196 |
+
idx = date_range("1/1/2000", periods=3, freq="D", tz="Asia/Tokyo", name="idx")
|
| 197 |
+
|
| 198 |
+
# mismatched tz -> cast to object (could reasonably cast to same tz or UTC)
|
| 199 |
+
item = Timestamp("2000-01-04", tz="US/Eastern")
|
| 200 |
+
result = idx.insert(3, item)
|
| 201 |
+
expected = Index(
|
| 202 |
+
list(idx[:3]) + [item.tz_convert(idx.tz)] + list(idx[3:]),
|
| 203 |
+
name="idx",
|
| 204 |
+
)
|
| 205 |
+
assert expected.dtype == idx.dtype
|
| 206 |
+
tm.assert_index_equal(result, expected)
|
| 207 |
+
|
| 208 |
+
item = datetime(2000, 1, 4, tzinfo=pytz.timezone("US/Eastern"))
|
| 209 |
+
result = idx.insert(3, item)
|
| 210 |
+
expected = Index(
|
| 211 |
+
list(idx[:3]) + [item.astimezone(idx.tzinfo)] + list(idx[3:]),
|
| 212 |
+
name="idx",
|
| 213 |
+
)
|
| 214 |
+
assert expected.dtype == idx.dtype
|
| 215 |
+
tm.assert_index_equal(result, expected)
|
| 216 |
+
|
| 217 |
+
@pytest.mark.parametrize(
|
| 218 |
+
"item", [0, np.int64(0), np.float64(0), np.array(0), np.timedelta64(456)]
|
| 219 |
+
)
|
| 220 |
+
def test_insert_mismatched_types_raises(self, tz_aware_fixture, item):
|
| 221 |
+
# GH#33703 dont cast these to dt64
|
| 222 |
+
tz = tz_aware_fixture
|
| 223 |
+
dti = date_range("2019-11-04", periods=9, freq="-1D", name=9, tz=tz)
|
| 224 |
+
|
| 225 |
+
result = dti.insert(1, item)
|
| 226 |
+
|
| 227 |
+
if isinstance(item, np.ndarray):
|
| 228 |
+
assert item.item() == 0
|
| 229 |
+
expected = Index([dti[0], 0] + list(dti[1:]), dtype=object, name=9)
|
| 230 |
+
else:
|
| 231 |
+
expected = Index([dti[0], item] + list(dti[1:]), dtype=object, name=9)
|
| 232 |
+
|
| 233 |
+
tm.assert_index_equal(result, expected)
|
| 234 |
+
|
| 235 |
+
def test_insert_castable_str(self, tz_aware_fixture):
|
| 236 |
+
# GH#33703
|
| 237 |
+
tz = tz_aware_fixture
|
| 238 |
+
dti = date_range("2019-11-04", periods=3, freq="-1D", name=9, tz=tz)
|
| 239 |
+
|
| 240 |
+
value = "2019-11-05"
|
| 241 |
+
result = dti.insert(0, value)
|
| 242 |
+
|
| 243 |
+
ts = Timestamp(value).tz_localize(tz)
|
| 244 |
+
expected = DatetimeIndex([ts] + list(dti), dtype=dti.dtype, name=9)
|
| 245 |
+
tm.assert_index_equal(result, expected)
|
| 246 |
+
|
| 247 |
+
def test_insert_non_castable_str(self, tz_aware_fixture):
|
| 248 |
+
# GH#33703
|
| 249 |
+
tz = tz_aware_fixture
|
| 250 |
+
dti = date_range("2019-11-04", periods=3, freq="-1D", name=9, tz=tz)
|
| 251 |
+
|
| 252 |
+
value = "foo"
|
| 253 |
+
result = dti.insert(0, value)
|
| 254 |
+
|
| 255 |
+
expected = Index(["foo"] + list(dti), dtype=object, name=9)
|
| 256 |
+
tm.assert_index_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_repeat.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
tz = tz_naive_fixture
|
| 15 |
+
rng = date_range("1/1/2000", "1/1/2001")
|
| 16 |
+
|
| 17 |
+
result = rng.repeat(5)
|
| 18 |
+
assert result.freq is None
|
| 19 |
+
assert len(result) == 5 * len(rng)
|
| 20 |
+
|
| 21 |
+
index = date_range("2001-01-01", periods=2, freq="D", tz=tz)
|
| 22 |
+
exp = DatetimeIndex(
|
| 23 |
+
["2001-01-01", "2001-01-01", "2001-01-02", "2001-01-02"], tz=tz
|
| 24 |
+
)
|
| 25 |
+
for res in [index.repeat(2), np.repeat(index, 2)]:
|
| 26 |
+
tm.assert_index_equal(res, exp)
|
| 27 |
+
assert res.freq is None
|
| 28 |
+
|
| 29 |
+
index = date_range("2001-01-01", periods=2, freq="2D", tz=tz)
|
| 30 |
+
exp = DatetimeIndex(
|
| 31 |
+
["2001-01-01", "2001-01-01", "2001-01-03", "2001-01-03"], tz=tz
|
| 32 |
+
)
|
| 33 |
+
for res in [index.repeat(2), np.repeat(index, 2)]:
|
| 34 |
+
tm.assert_index_equal(res, exp)
|
| 35 |
+
assert res.freq is None
|
| 36 |
+
|
| 37 |
+
index = DatetimeIndex(["2001-01-01", "NaT", "2003-01-01"], tz=tz)
|
| 38 |
+
exp = DatetimeIndex(
|
| 39 |
+
[
|
| 40 |
+
"2001-01-01",
|
| 41 |
+
"2001-01-01",
|
| 42 |
+
"2001-01-01",
|
| 43 |
+
"NaT",
|
| 44 |
+
"NaT",
|
| 45 |
+
"NaT",
|
| 46 |
+
"2003-01-01",
|
| 47 |
+
"2003-01-01",
|
| 48 |
+
"2003-01-01",
|
| 49 |
+
],
|
| 50 |
+
tz=tz,
|
| 51 |
+
)
|
| 52 |
+
for res in [index.repeat(3), np.repeat(index, 3)]:
|
| 53 |
+
tm.assert_index_equal(res, exp)
|
| 54 |
+
assert res.freq is None
|
| 55 |
+
|
| 56 |
+
def test_repeat(self, tz_naive_fixture):
|
| 57 |
+
tz = tz_naive_fixture
|
| 58 |
+
reps = 2
|
| 59 |
+
msg = "the 'axis' parameter is not supported"
|
| 60 |
+
|
| 61 |
+
rng = date_range(start="2016-01-01", periods=2, freq="30Min", tz=tz)
|
| 62 |
+
|
| 63 |
+
expected_rng = DatetimeIndex(
|
| 64 |
+
[
|
| 65 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
| 66 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
| 67 |
+
Timestamp("2016-01-01 00:30:00", tz=tz),
|
| 68 |
+
Timestamp("2016-01-01 00:30:00", tz=tz),
|
| 69 |
+
]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
res = rng.repeat(reps)
|
| 73 |
+
tm.assert_index_equal(res, expected_rng)
|
| 74 |
+
assert res.freq is None
|
| 75 |
+
|
| 76 |
+
tm.assert_index_equal(np.repeat(rng, reps), expected_rng)
|
| 77 |
+
with pytest.raises(ValueError, match=msg):
|
| 78 |
+
np.repeat(rng, reps, axis=1)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_shift.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
| 24 |
+
# GH#9903
|
| 25 |
+
tz = tz_naive_fixture
|
| 26 |
+
idx = DatetimeIndex([], name="xxx", tz=tz)
|
| 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 |
+
)
|
| 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 |
+
)
|
| 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 |
+
)
|
| 50 |
+
tm.assert_index_equal(idx.shift(-3, freq="H"), exp)
|
| 51 |
+
|
| 52 |
+
def test_dti_shift_freqs(self):
|
| 53 |
+
# test shift for DatetimeIndex and non DatetimeIndex
|
| 54 |
+
# GH#8083
|
| 55 |
+
drange = date_range("20130101", periods=5)
|
| 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 |
+
freq="D",
|
| 60 |
+
)
|
| 61 |
+
tm.assert_index_equal(result, expected)
|
| 62 |
+
|
| 63 |
+
result = drange.shift(-1)
|
| 64 |
+
expected = DatetimeIndex(
|
| 65 |
+
["2012-12-31", "2013-01-01", "2013-01-02", "2013-01-03", "2013-01-04"],
|
| 66 |
+
freq="D",
|
| 67 |
+
)
|
| 68 |
+
tm.assert_index_equal(result, expected)
|
| 69 |
+
|
| 70 |
+
result = drange.shift(3, freq="2D")
|
| 71 |
+
expected = DatetimeIndex(
|
| 72 |
+
["2013-01-07", "2013-01-08", "2013-01-09", "2013-01-10", "2013-01-11"],
|
| 73 |
+
freq="D",
|
| 74 |
+
)
|
| 75 |
+
tm.assert_index_equal(result, expected)
|
| 76 |
+
|
| 77 |
+
def test_dti_shift_int(self):
|
| 78 |
+
rng = date_range("1/1/2000", periods=20)
|
| 79 |
+
|
| 80 |
+
result = rng + 5 * rng.freq
|
| 81 |
+
expected = rng.shift(5)
|
| 82 |
+
tm.assert_index_equal(result, expected)
|
| 83 |
+
|
| 84 |
+
result = rng - 5 * rng.freq
|
| 85 |
+
expected = rng.shift(-5)
|
| 86 |
+
tm.assert_index_equal(result, expected)
|
| 87 |
+
|
| 88 |
+
def test_dti_shift_no_freq(self):
|
| 89 |
+
# GH#19147
|
| 90 |
+
dti = DatetimeIndex(["2011-01-01 10:00", "2011-01-01"], freq=None)
|
| 91 |
+
with pytest.raises(NullFrequencyError, match="Cannot shift with no freq"):
|
| 92 |
+
dti.shift(2)
|
| 93 |
+
|
| 94 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 95 |
+
def test_dti_shift_localized(self, tzstr):
|
| 96 |
+
dr = date_range("2011/1/1", "2012/1/1", freq="W-FRI")
|
| 97 |
+
dr_tz = dr.tz_localize(tzstr)
|
| 98 |
+
|
| 99 |
+
result = dr_tz.shift(1, "10T")
|
| 100 |
+
assert result.tz == dr_tz.tz
|
| 101 |
+
|
| 102 |
+
def test_dti_shift_across_dst(self):
|
| 103 |
+
# GH 8616
|
| 104 |
+
idx = date_range("2013-11-03", tz="America/Chicago", periods=7, freq="H")
|
| 105 |
+
s = Series(index=idx[:-1], dtype=object)
|
| 106 |
+
result = s.shift(freq="H")
|
| 107 |
+
expected = Series(index=idx[1:], dtype=object)
|
| 108 |
+
tm.assert_series_equal(result, expected)
|
| 109 |
+
|
| 110 |
+
@pytest.mark.parametrize(
|
| 111 |
+
"shift, result_time",
|
| 112 |
+
[
|
| 113 |
+
[0, "2014-11-14 00:00:00"],
|
| 114 |
+
[-1, "2014-11-13 23:00:00"],
|
| 115 |
+
[1, "2014-11-14 01:00:00"],
|
| 116 |
+
],
|
| 117 |
+
)
|
| 118 |
+
def test_dti_shift_near_midnight(self, shift, result_time):
|
| 119 |
+
# GH 8616
|
| 120 |
+
dt = datetime(2014, 11, 14, 0)
|
| 121 |
+
dt_est = pytz.timezone("EST").localize(dt)
|
| 122 |
+
s = Series(data=[1], index=[dt_est])
|
| 123 |
+
result = s.shift(shift, freq="H")
|
| 124 |
+
expected = Series(1, index=DatetimeIndex([result_time], tz="EST"))
|
| 125 |
+
tm.assert_series_equal(result, expected)
|
| 126 |
+
|
| 127 |
+
def test_shift_periods(self):
|
| 128 |
+
# GH#22458 : argument 'n' was deprecated in favor of 'periods'
|
| 129 |
+
idx = date_range(start=START, end=END, periods=3)
|
| 130 |
+
tm.assert_index_equal(idx.shift(periods=0), idx)
|
| 131 |
+
tm.assert_index_equal(idx.shift(0), idx)
|
| 132 |
+
|
| 133 |
+
@pytest.mark.parametrize("freq", ["B", "C"])
|
| 134 |
+
def test_shift_bday(self, freq):
|
| 135 |
+
rng = date_range(START, END, freq=freq)
|
| 136 |
+
shifted = rng.shift(5)
|
| 137 |
+
assert shifted[0] == rng[5]
|
| 138 |
+
assert shifted.freq == rng.freq
|
| 139 |
+
|
| 140 |
+
shifted = rng.shift(-5)
|
| 141 |
+
assert shifted[5] == rng[0]
|
| 142 |
+
assert shifted.freq == rng.freq
|
| 143 |
+
|
| 144 |
+
shifted = rng.shift(0)
|
| 145 |
+
assert shifted[0] == rng[0]
|
| 146 |
+
assert shifted.freq == rng.freq
|
| 147 |
+
|
| 148 |
+
def test_shift_bmonth(self):
|
| 149 |
+
rng = date_range(START, END, freq=pd.offsets.BMonthEnd())
|
| 150 |
+
shifted = rng.shift(1, freq=pd.offsets.BDay())
|
| 151 |
+
assert shifted[0] == rng[0] + pd.offsets.BDay()
|
| 152 |
+
|
| 153 |
+
rng = date_range(START, END, freq=pd.offsets.BMonthEnd())
|
| 154 |
+
with tm.assert_produces_warning(pd.errors.PerformanceWarning):
|
| 155 |
+
shifted = rng.shift(1, freq=pd.offsets.CDay())
|
| 156 |
+
assert shifted[0] == rng[0] + pd.offsets.CDay()
|
| 157 |
+
|
| 158 |
+
def test_shift_empty(self):
|
| 159 |
+
# GH#14811
|
| 160 |
+
dti = date_range(start="2016-10-21", end="2016-10-21", freq="BM")
|
| 161 |
+
result = dti.shift(1)
|
| 162 |
+
tm.assert_index_equal(result, dti)
|
videochat2/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
|
videochat2/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)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/methods/test_to_period.py
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
|
| 3 |
+
import dateutil.tz
|
| 4 |
+
from dateutil.tz import tzlocal
|
| 5 |
+
import pytest
|
| 6 |
+
import pytz
|
| 7 |
+
|
| 8 |
+
from pandas._libs.tslibs.ccalendar import MONTHS
|
| 9 |
+
from pandas._libs.tslibs.period import INVALID_FREQ_ERR_MSG
|
| 10 |
+
|
| 11 |
+
from pandas import (
|
| 12 |
+
DatetimeIndex,
|
| 13 |
+
Period,
|
| 14 |
+
PeriodIndex,
|
| 15 |
+
Timestamp,
|
| 16 |
+
date_range,
|
| 17 |
+
period_range,
|
| 18 |
+
)
|
| 19 |
+
import pandas._testing as tm
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class TestToPeriod:
|
| 23 |
+
def test_dti_to_period(self):
|
| 24 |
+
dti = date_range(start="1/1/2005", end="12/1/2005", freq="M")
|
| 25 |
+
pi1 = dti.to_period()
|
| 26 |
+
pi2 = dti.to_period(freq="D")
|
| 27 |
+
pi3 = dti.to_period(freq="3D")
|
| 28 |
+
|
| 29 |
+
assert pi1[0] == Period("Jan 2005", freq="M")
|
| 30 |
+
assert pi2[0] == Period("1/31/2005", freq="D")
|
| 31 |
+
assert pi3[0] == Period("1/31/2005", freq="3D")
|
| 32 |
+
|
| 33 |
+
assert pi1[-1] == Period("Nov 2005", freq="M")
|
| 34 |
+
assert pi2[-1] == Period("11/30/2005", freq="D")
|
| 35 |
+
assert pi3[-1], Period("11/30/2005", freq="3D")
|
| 36 |
+
|
| 37 |
+
tm.assert_index_equal(pi1, period_range("1/1/2005", "11/1/2005", freq="M"))
|
| 38 |
+
tm.assert_index_equal(
|
| 39 |
+
pi2, period_range("1/1/2005", "11/1/2005", freq="M").asfreq("D")
|
| 40 |
+
)
|
| 41 |
+
tm.assert_index_equal(
|
| 42 |
+
pi3, period_range("1/1/2005", "11/1/2005", freq="M").asfreq("3D")
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
@pytest.mark.parametrize("month", MONTHS)
|
| 46 |
+
def test_to_period_quarterly(self, month):
|
| 47 |
+
# make sure we can make the round trip
|
| 48 |
+
freq = f"Q-{month}"
|
| 49 |
+
rng = period_range("1989Q3", "1991Q3", freq=freq)
|
| 50 |
+
stamps = rng.to_timestamp()
|
| 51 |
+
result = stamps.to_period(freq)
|
| 52 |
+
tm.assert_index_equal(rng, result)
|
| 53 |
+
|
| 54 |
+
@pytest.mark.parametrize("off", ["BQ", "QS", "BQS"])
|
| 55 |
+
def test_to_period_quarterlyish(self, off):
|
| 56 |
+
rng = date_range("01-Jan-2012", periods=8, freq=off)
|
| 57 |
+
prng = rng.to_period()
|
| 58 |
+
assert prng.freq == "Q-DEC"
|
| 59 |
+
|
| 60 |
+
@pytest.mark.parametrize("off", ["BA", "AS", "BAS"])
|
| 61 |
+
def test_to_period_annualish(self, off):
|
| 62 |
+
rng = date_range("01-Jan-2012", periods=8, freq=off)
|
| 63 |
+
prng = rng.to_period()
|
| 64 |
+
assert prng.freq == "A-DEC"
|
| 65 |
+
|
| 66 |
+
def test_to_period_monthish(self):
|
| 67 |
+
offsets = ["MS", "BM"]
|
| 68 |
+
for off in offsets:
|
| 69 |
+
rng = date_range("01-Jan-2012", periods=8, freq=off)
|
| 70 |
+
prng = rng.to_period()
|
| 71 |
+
assert prng.freq == "M"
|
| 72 |
+
|
| 73 |
+
rng = date_range("01-Jan-2012", periods=8, freq="M")
|
| 74 |
+
prng = rng.to_period()
|
| 75 |
+
assert prng.freq == "M"
|
| 76 |
+
|
| 77 |
+
with pytest.raises(ValueError, match=INVALID_FREQ_ERR_MSG):
|
| 78 |
+
date_range("01-Jan-2012", periods=8, freq="EOM")
|
| 79 |
+
|
| 80 |
+
def test_to_period_infer(self):
|
| 81 |
+
# https://github.com/pandas-dev/pandas/issues/33358
|
| 82 |
+
rng = date_range(
|
| 83 |
+
start="2019-12-22 06:40:00+00:00",
|
| 84 |
+
end="2019-12-22 08:45:00+00:00",
|
| 85 |
+
freq="5min",
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
with tm.assert_produces_warning(None):
|
| 89 |
+
# Using simple filter because we are not checking for the warning here
|
| 90 |
+
warnings.simplefilter("ignore", UserWarning)
|
| 91 |
+
|
| 92 |
+
pi1 = rng.to_period("5min")
|
| 93 |
+
|
| 94 |
+
with tm.assert_produces_warning(None):
|
| 95 |
+
# Using simple filter because we are not checking for the warning here
|
| 96 |
+
warnings.simplefilter("ignore", UserWarning)
|
| 97 |
+
|
| 98 |
+
pi2 = rng.to_period()
|
| 99 |
+
|
| 100 |
+
tm.assert_index_equal(pi1, pi2)
|
| 101 |
+
|
| 102 |
+
def test_period_dt64_round_trip(self):
|
| 103 |
+
dti = date_range("1/1/2000", "1/7/2002", freq="B")
|
| 104 |
+
pi = dti.to_period()
|
| 105 |
+
tm.assert_index_equal(pi.to_timestamp(), dti)
|
| 106 |
+
|
| 107 |
+
dti = date_range("1/1/2000", "1/7/2002", freq="B")
|
| 108 |
+
pi = dti.to_period(freq="H")
|
| 109 |
+
tm.assert_index_equal(pi.to_timestamp(), dti)
|
| 110 |
+
|
| 111 |
+
def test_to_period_millisecond(self):
|
| 112 |
+
index = DatetimeIndex(
|
| 113 |
+
[
|
| 114 |
+
Timestamp("2007-01-01 10:11:12.123456Z"),
|
| 115 |
+
Timestamp("2007-01-01 10:11:13.789123Z"),
|
| 116 |
+
]
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
with tm.assert_produces_warning(UserWarning):
|
| 120 |
+
# warning that timezone info will be lost
|
| 121 |
+
period = index.to_period(freq="L")
|
| 122 |
+
assert 2 == len(period)
|
| 123 |
+
assert period[0] == Period("2007-01-01 10:11:12.123Z", "L")
|
| 124 |
+
assert period[1] == Period("2007-01-01 10:11:13.789Z", "L")
|
| 125 |
+
|
| 126 |
+
def test_to_period_microsecond(self):
|
| 127 |
+
index = DatetimeIndex(
|
| 128 |
+
[
|
| 129 |
+
Timestamp("2007-01-01 10:11:12.123456Z"),
|
| 130 |
+
Timestamp("2007-01-01 10:11:13.789123Z"),
|
| 131 |
+
]
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
with tm.assert_produces_warning(UserWarning):
|
| 135 |
+
# warning that timezone info will be lost
|
| 136 |
+
period = index.to_period(freq="U")
|
| 137 |
+
assert 2 == len(period)
|
| 138 |
+
assert period[0] == Period("2007-01-01 10:11:12.123456Z", "U")
|
| 139 |
+
assert period[1] == Period("2007-01-01 10:11:13.789123Z", "U")
|
| 140 |
+
|
| 141 |
+
@pytest.mark.parametrize(
|
| 142 |
+
"tz",
|
| 143 |
+
["US/Eastern", pytz.utc, tzlocal(), "dateutil/US/Eastern", dateutil.tz.tzutc()],
|
| 144 |
+
)
|
| 145 |
+
def test_to_period_tz(self, tz):
|
| 146 |
+
ts = date_range("1/1/2000", "2/1/2000", tz=tz)
|
| 147 |
+
|
| 148 |
+
with tm.assert_produces_warning(UserWarning):
|
| 149 |
+
# GH#21333 warning that timezone info will be lost
|
| 150 |
+
# filter warning about freq deprecation
|
| 151 |
+
|
| 152 |
+
result = ts.to_period()[0]
|
| 153 |
+
expected = ts[0].to_period(ts.freq)
|
| 154 |
+
|
| 155 |
+
assert result == expected
|
| 156 |
+
|
| 157 |
+
expected = date_range("1/1/2000", "2/1/2000").to_period()
|
| 158 |
+
|
| 159 |
+
with tm.assert_produces_warning(UserWarning):
|
| 160 |
+
# GH#21333 warning that timezone info will be lost
|
| 161 |
+
result = ts.to_period(ts.freq)
|
| 162 |
+
|
| 163 |
+
tm.assert_index_equal(result, expected)
|
| 164 |
+
|
| 165 |
+
@pytest.mark.parametrize("tz", ["Etc/GMT-1", "Etc/GMT+1"])
|
| 166 |
+
def test_to_period_tz_utc_offset_consistency(self, tz):
|
| 167 |
+
# GH#22905
|
| 168 |
+
ts = date_range("1/1/2000", "2/1/2000", tz="Etc/GMT-1")
|
| 169 |
+
with tm.assert_produces_warning(UserWarning):
|
| 170 |
+
result = ts.to_period()[0]
|
| 171 |
+
expected = ts[0].to_period(ts.freq)
|
| 172 |
+
assert result == expected
|
| 173 |
+
|
| 174 |
+
def test_to_period_nofreq(self):
|
| 175 |
+
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-04"])
|
| 176 |
+
msg = "You must pass a freq argument as current index has none."
|
| 177 |
+
with pytest.raises(ValueError, match=msg):
|
| 178 |
+
idx.to_period()
|
| 179 |
+
|
| 180 |
+
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="infer")
|
| 181 |
+
assert idx.freqstr == "D"
|
| 182 |
+
expected = PeriodIndex(["2000-01-01", "2000-01-02", "2000-01-03"], freq="D")
|
| 183 |
+
tm.assert_index_equal(idx.to_period(), expected)
|
| 184 |
+
|
| 185 |
+
# GH#7606
|
| 186 |
+
idx = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"])
|
| 187 |
+
assert idx.freqstr is None
|
| 188 |
+
tm.assert_index_equal(idx.to_period(), expected)
|
videochat2/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)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_asof.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import timedelta
|
| 2 |
+
|
| 3 |
+
from pandas import (
|
| 4 |
+
Index,
|
| 5 |
+
Timestamp,
|
| 6 |
+
date_range,
|
| 7 |
+
isna,
|
| 8 |
+
)
|
| 9 |
+
import pandas._testing as tm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestAsOf:
|
| 13 |
+
def test_asof_partial(self):
|
| 14 |
+
index = date_range("2010-01-01", periods=2, freq="m")
|
| 15 |
+
expected = Timestamp("2010-02-28")
|
| 16 |
+
result = index.asof("2010-02")
|
| 17 |
+
assert result == expected
|
| 18 |
+
assert not isinstance(result, Index)
|
| 19 |
+
|
| 20 |
+
def test_asof(self):
|
| 21 |
+
index = tm.makeDateIndex(100)
|
| 22 |
+
|
| 23 |
+
dt = index[0]
|
| 24 |
+
assert index.asof(dt) == dt
|
| 25 |
+
assert isna(index.asof(dt - timedelta(1)))
|
| 26 |
+
|
| 27 |
+
dt = index[-1]
|
| 28 |
+
assert index.asof(dt + timedelta(1)) == dt
|
| 29 |
+
|
| 30 |
+
dt = index[0].to_pydatetime()
|
| 31 |
+
assert isinstance(index.asof(dt), Timestamp)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_constructors.py
ADDED
|
@@ -0,0 +1,1098 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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edelta,
|
| 4 |
+
timezone,
|
| 5 |
+
)
|
| 6 |
+
from functools import partial
|
| 7 |
+
from operator import attrgetter
|
| 8 |
+
|
| 9 |
+
import dateutil
|
| 10 |
+
import numpy as np
|
| 11 |
+
import pytest
|
| 12 |
+
import pytz
|
| 13 |
+
|
| 14 |
+
from pandas._libs.tslibs import (
|
| 15 |
+
OutOfBoundsDatetime,
|
| 16 |
+
astype_overflowsafe,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
import pandas as pd
|
| 20 |
+
from pandas import (
|
| 21 |
+
DatetimeIndex,
|
| 22 |
+
Index,
|
| 23 |
+
Timestamp,
|
| 24 |
+
date_range,
|
| 25 |
+
offsets,
|
| 26 |
+
to_datetime,
|
| 27 |
+
)
|
| 28 |
+
import pandas._testing as tm
|
| 29 |
+
from pandas.core.arrays import (
|
| 30 |
+
DatetimeArray,
|
| 31 |
+
period_array,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class TestDatetimeIndex:
|
| 36 |
+
def test_from_dt64_unsupported_unit(self):
|
| 37 |
+
# GH#49292
|
| 38 |
+
val = np.datetime64(1, "D")
|
| 39 |
+
result = DatetimeIndex([val], tz="US/Pacific")
|
| 40 |
+
|
| 41 |
+
expected = DatetimeIndex([val.astype("M8[s]")], tz="US/Pacific")
|
| 42 |
+
tm.assert_index_equal(result, expected)
|
| 43 |
+
|
| 44 |
+
def test_explicit_tz_none(self):
|
| 45 |
+
# GH#48659
|
| 46 |
+
dti = date_range("2016-01-01", periods=10, tz="UTC")
|
| 47 |
+
|
| 48 |
+
msg = "Passed data is timezone-aware, incompatible with 'tz=None'"
|
| 49 |
+
with pytest.raises(ValueError, match=msg):
|
| 50 |
+
DatetimeIndex(dti, tz=None)
|
| 51 |
+
|
| 52 |
+
with pytest.raises(ValueError, match=msg):
|
| 53 |
+
DatetimeIndex(np.array(dti), tz=None)
|
| 54 |
+
|
| 55 |
+
msg = "Cannot pass both a timezone-aware dtype and tz=None"
|
| 56 |
+
with pytest.raises(ValueError, match=msg):
|
| 57 |
+
DatetimeIndex([], dtype="M8[ns, UTC]", tz=None)
|
| 58 |
+
|
| 59 |
+
@pytest.mark.parametrize(
|
| 60 |
+
"dt_cls", [DatetimeIndex, DatetimeArray._from_sequence_not_strict]
|
| 61 |
+
)
|
| 62 |
+
def test_freq_validation_with_nat(self, dt_cls):
|
| 63 |
+
# GH#11587 make sure we get a useful error message when generate_range
|
| 64 |
+
# raises
|
| 65 |
+
msg = (
|
| 66 |
+
"Inferred frequency None from passed values does not conform "
|
| 67 |
+
"to passed frequency D"
|
| 68 |
+
)
|
| 69 |
+
with pytest.raises(ValueError, match=msg):
|
| 70 |
+
dt_cls([pd.NaT, Timestamp("2011-01-01")], freq="D")
|
| 71 |
+
with pytest.raises(ValueError, match=msg):
|
| 72 |
+
dt_cls([pd.NaT, Timestamp("2011-01-01")._value], freq="D")
|
| 73 |
+
|
| 74 |
+
# TODO: better place for tests shared by DTI/TDI?
|
| 75 |
+
@pytest.mark.parametrize(
|
| 76 |
+
"index",
|
| 77 |
+
[
|
| 78 |
+
date_range("2016-01-01", periods=5, tz="US/Pacific"),
|
| 79 |
+
pd.timedelta_range("1 Day", periods=5),
|
| 80 |
+
],
|
| 81 |
+
)
|
| 82 |
+
def test_shallow_copy_inherits_array_freq(self, index):
|
| 83 |
+
# If we pass a DTA/TDA to shallow_copy and dont specify a freq,
|
| 84 |
+
# we should inherit the array's freq, not our own.
|
| 85 |
+
array = index._data
|
| 86 |
+
|
| 87 |
+
arr = array[[0, 3, 2, 4, 1]]
|
| 88 |
+
assert arr.freq is None
|
| 89 |
+
|
| 90 |
+
result = index._shallow_copy(arr)
|
| 91 |
+
assert result.freq is None
|
| 92 |
+
|
| 93 |
+
def test_categorical_preserves_tz(self):
|
| 94 |
+
# GH#18664 retain tz when going DTI-->Categorical-->DTI
|
| 95 |
+
dti = DatetimeIndex(
|
| 96 |
+
[pd.NaT, "2015-01-01", "1999-04-06 15:14:13", "2015-01-01"], tz="US/Eastern"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
for dtobj in [dti, dti._data]:
|
| 100 |
+
# works for DatetimeIndex or DatetimeArray
|
| 101 |
+
|
| 102 |
+
ci = pd.CategoricalIndex(dtobj)
|
| 103 |
+
carr = pd.Categorical(dtobj)
|
| 104 |
+
cser = pd.Series(ci)
|
| 105 |
+
|
| 106 |
+
for obj in [ci, carr, cser]:
|
| 107 |
+
result = DatetimeIndex(obj)
|
| 108 |
+
tm.assert_index_equal(result, dti)
|
| 109 |
+
|
| 110 |
+
def test_dti_with_period_data_raises(self):
|
| 111 |
+
# GH#23675
|
| 112 |
+
data = pd.PeriodIndex(["2016Q1", "2016Q2"], freq="Q")
|
| 113 |
+
|
| 114 |
+
with pytest.raises(TypeError, match="PeriodDtype data is invalid"):
|
| 115 |
+
DatetimeIndex(data)
|
| 116 |
+
|
| 117 |
+
with pytest.raises(TypeError, match="PeriodDtype data is invalid"):
|
| 118 |
+
to_datetime(data)
|
| 119 |
+
|
| 120 |
+
with pytest.raises(TypeError, match="PeriodDtype data is invalid"):
|
| 121 |
+
DatetimeIndex(period_array(data))
|
| 122 |
+
|
| 123 |
+
with pytest.raises(TypeError, match="PeriodDtype data is invalid"):
|
| 124 |
+
to_datetime(period_array(data))
|
| 125 |
+
|
| 126 |
+
def test_dti_with_timedelta64_data_raises(self):
|
| 127 |
+
# GH#23675 deprecated, enforrced in GH#29794
|
| 128 |
+
data = np.array([0], dtype="m8[ns]")
|
| 129 |
+
msg = r"timedelta64\[ns\] cannot be converted to datetime64"
|
| 130 |
+
with pytest.raises(TypeError, match=msg):
|
| 131 |
+
DatetimeIndex(data)
|
| 132 |
+
|
| 133 |
+
with pytest.raises(TypeError, match=msg):
|
| 134 |
+
to_datetime(data)
|
| 135 |
+
|
| 136 |
+
with pytest.raises(TypeError, match=msg):
|
| 137 |
+
DatetimeIndex(pd.TimedeltaIndex(data))
|
| 138 |
+
|
| 139 |
+
with pytest.raises(TypeError, match=msg):
|
| 140 |
+
to_datetime(pd.TimedeltaIndex(data))
|
| 141 |
+
|
| 142 |
+
def test_constructor_from_sparse_array(self):
|
| 143 |
+
# https://github.com/pandas-dev/pandas/issues/35843
|
| 144 |
+
values = [
|
| 145 |
+
Timestamp("2012-05-01T01:00:00.000000"),
|
| 146 |
+
Timestamp("2016-05-01T01:00:00.000000"),
|
| 147 |
+
]
|
| 148 |
+
arr = pd.arrays.SparseArray(values)
|
| 149 |
+
result = Index(arr)
|
| 150 |
+
assert type(result) is Index
|
| 151 |
+
assert result.dtype == arr.dtype
|
| 152 |
+
|
| 153 |
+
def test_construction_caching(self):
|
| 154 |
+
df = pd.DataFrame(
|
| 155 |
+
{
|
| 156 |
+
"dt": date_range("20130101", periods=3),
|
| 157 |
+
"dttz": date_range("20130101", periods=3, tz="US/Eastern"),
|
| 158 |
+
"dt_with_null": [
|
| 159 |
+
Timestamp("20130101"),
|
| 160 |
+
pd.NaT,
|
| 161 |
+
Timestamp("20130103"),
|
| 162 |
+
],
|
| 163 |
+
"dtns": date_range("20130101", periods=3, freq="ns"),
|
| 164 |
+
}
|
| 165 |
+
)
|
| 166 |
+
assert df.dttz.dtype.tz.zone == "US/Eastern"
|
| 167 |
+
|
| 168 |
+
@pytest.mark.parametrize(
|
| 169 |
+
"kwargs",
|
| 170 |
+
[{"tz": "dtype.tz"}, {"dtype": "dtype"}, {"dtype": "dtype", "tz": "dtype.tz"}],
|
| 171 |
+
)
|
| 172 |
+
def test_construction_with_alt(self, kwargs, tz_aware_fixture):
|
| 173 |
+
tz = tz_aware_fixture
|
| 174 |
+
i = date_range("20130101", periods=5, freq="H", tz=tz)
|
| 175 |
+
kwargs = {key: attrgetter(val)(i) for key, val in kwargs.items()}
|
| 176 |
+
result = DatetimeIndex(i, **kwargs)
|
| 177 |
+
tm.assert_index_equal(i, result)
|
| 178 |
+
|
| 179 |
+
@pytest.mark.parametrize(
|
| 180 |
+
"kwargs",
|
| 181 |
+
[{"tz": "dtype.tz"}, {"dtype": "dtype"}, {"dtype": "dtype", "tz": "dtype.tz"}],
|
| 182 |
+
)
|
| 183 |
+
def test_construction_with_alt_tz_localize(self, kwargs, tz_aware_fixture):
|
| 184 |
+
tz = tz_aware_fixture
|
| 185 |
+
i = date_range("20130101", periods=5, freq="H", tz=tz)
|
| 186 |
+
i = i._with_freq(None)
|
| 187 |
+
kwargs = {key: attrgetter(val)(i) for key, val in kwargs.items()}
|
| 188 |
+
|
| 189 |
+
if "tz" in kwargs:
|
| 190 |
+
result = DatetimeIndex(i.asi8, tz="UTC").tz_convert(kwargs["tz"])
|
| 191 |
+
|
| 192 |
+
expected = DatetimeIndex(i, **kwargs)
|
| 193 |
+
tm.assert_index_equal(result, expected)
|
| 194 |
+
|
| 195 |
+
# localize into the provided tz
|
| 196 |
+
i2 = DatetimeIndex(i.tz_localize(None).asi8, tz="UTC")
|
| 197 |
+
expected = i.tz_localize(None).tz_localize("UTC")
|
| 198 |
+
tm.assert_index_equal(i2, expected)
|
| 199 |
+
|
| 200 |
+
# incompat tz/dtype
|
| 201 |
+
msg = "cannot supply both a tz and a dtype with a tz"
|
| 202 |
+
with pytest.raises(ValueError, match=msg):
|
| 203 |
+
DatetimeIndex(i.tz_localize(None).asi8, dtype=i.dtype, tz="US/Pacific")
|
| 204 |
+
|
| 205 |
+
def test_construction_index_with_mixed_timezones(self):
|
| 206 |
+
# gh-11488: no tz results in DatetimeIndex
|
| 207 |
+
result = Index([Timestamp("2011-01-01"), Timestamp("2011-01-02")], name="idx")
|
| 208 |
+
exp = DatetimeIndex(
|
| 209 |
+
[Timestamp("2011-01-01"), Timestamp("2011-01-02")], name="idx"
|
| 210 |
+
)
|
| 211 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 212 |
+
assert isinstance(result, DatetimeIndex)
|
| 213 |
+
assert result.tz is None
|
| 214 |
+
|
| 215 |
+
# same tz results in DatetimeIndex
|
| 216 |
+
result = Index(
|
| 217 |
+
[
|
| 218 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo"),
|
| 219 |
+
Timestamp("2011-01-02 10:00", tz="Asia/Tokyo"),
|
| 220 |
+
],
|
| 221 |
+
name="idx",
|
| 222 |
+
)
|
| 223 |
+
exp = DatetimeIndex(
|
| 224 |
+
[Timestamp("2011-01-01 10:00"), Timestamp("2011-01-02 10:00")],
|
| 225 |
+
tz="Asia/Tokyo",
|
| 226 |
+
name="idx",
|
| 227 |
+
)
|
| 228 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 229 |
+
assert isinstance(result, DatetimeIndex)
|
| 230 |
+
assert result.tz is not None
|
| 231 |
+
assert result.tz == exp.tz
|
| 232 |
+
|
| 233 |
+
# same tz results in DatetimeIndex (DST)
|
| 234 |
+
result = Index(
|
| 235 |
+
[
|
| 236 |
+
Timestamp("2011-01-01 10:00", tz="US/Eastern"),
|
| 237 |
+
Timestamp("2011-08-01 10:00", tz="US/Eastern"),
|
| 238 |
+
],
|
| 239 |
+
name="idx",
|
| 240 |
+
)
|
| 241 |
+
exp = DatetimeIndex(
|
| 242 |
+
[Timestamp("2011-01-01 10:00"), Timestamp("2011-08-01 10:00")],
|
| 243 |
+
tz="US/Eastern",
|
| 244 |
+
name="idx",
|
| 245 |
+
)
|
| 246 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 247 |
+
assert isinstance(result, DatetimeIndex)
|
| 248 |
+
assert result.tz is not None
|
| 249 |
+
assert result.tz == exp.tz
|
| 250 |
+
|
| 251 |
+
# Different tz results in Index(dtype=object)
|
| 252 |
+
result = Index(
|
| 253 |
+
[
|
| 254 |
+
Timestamp("2011-01-01 10:00"),
|
| 255 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 256 |
+
],
|
| 257 |
+
name="idx",
|
| 258 |
+
)
|
| 259 |
+
exp = Index(
|
| 260 |
+
[
|
| 261 |
+
Timestamp("2011-01-01 10:00"),
|
| 262 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 263 |
+
],
|
| 264 |
+
dtype="object",
|
| 265 |
+
name="idx",
|
| 266 |
+
)
|
| 267 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 268 |
+
assert not isinstance(result, DatetimeIndex)
|
| 269 |
+
|
| 270 |
+
result = Index(
|
| 271 |
+
[
|
| 272 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo"),
|
| 273 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 274 |
+
],
|
| 275 |
+
name="idx",
|
| 276 |
+
)
|
| 277 |
+
exp = Index(
|
| 278 |
+
[
|
| 279 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo"),
|
| 280 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 281 |
+
],
|
| 282 |
+
dtype="object",
|
| 283 |
+
name="idx",
|
| 284 |
+
)
|
| 285 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 286 |
+
assert not isinstance(result, DatetimeIndex)
|
| 287 |
+
|
| 288 |
+
# length = 1
|
| 289 |
+
result = Index([Timestamp("2011-01-01")], name="idx")
|
| 290 |
+
exp = DatetimeIndex([Timestamp("2011-01-01")], name="idx")
|
| 291 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 292 |
+
assert isinstance(result, DatetimeIndex)
|
| 293 |
+
assert result.tz is None
|
| 294 |
+
|
| 295 |
+
# length = 1 with tz
|
| 296 |
+
result = Index([Timestamp("2011-01-01 10:00", tz="Asia/Tokyo")], name="idx")
|
| 297 |
+
exp = DatetimeIndex(
|
| 298 |
+
[Timestamp("2011-01-01 10:00")], tz="Asia/Tokyo", name="idx"
|
| 299 |
+
)
|
| 300 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 301 |
+
assert isinstance(result, DatetimeIndex)
|
| 302 |
+
assert result.tz is not None
|
| 303 |
+
assert result.tz == exp.tz
|
| 304 |
+
|
| 305 |
+
def test_construction_index_with_mixed_timezones_with_NaT(self):
|
| 306 |
+
# see gh-11488
|
| 307 |
+
result = Index(
|
| 308 |
+
[pd.NaT, Timestamp("2011-01-01"), pd.NaT, Timestamp("2011-01-02")],
|
| 309 |
+
name="idx",
|
| 310 |
+
)
|
| 311 |
+
exp = DatetimeIndex(
|
| 312 |
+
[pd.NaT, Timestamp("2011-01-01"), pd.NaT, Timestamp("2011-01-02")],
|
| 313 |
+
name="idx",
|
| 314 |
+
)
|
| 315 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 316 |
+
assert isinstance(result, DatetimeIndex)
|
| 317 |
+
assert result.tz is None
|
| 318 |
+
|
| 319 |
+
# Same tz results in DatetimeIndex
|
| 320 |
+
result = Index(
|
| 321 |
+
[
|
| 322 |
+
pd.NaT,
|
| 323 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo"),
|
| 324 |
+
pd.NaT,
|
| 325 |
+
Timestamp("2011-01-02 10:00", tz="Asia/Tokyo"),
|
| 326 |
+
],
|
| 327 |
+
name="idx",
|
| 328 |
+
)
|
| 329 |
+
exp = DatetimeIndex(
|
| 330 |
+
[
|
| 331 |
+
pd.NaT,
|
| 332 |
+
Timestamp("2011-01-01 10:00"),
|
| 333 |
+
pd.NaT,
|
| 334 |
+
Timestamp("2011-01-02 10:00"),
|
| 335 |
+
],
|
| 336 |
+
tz="Asia/Tokyo",
|
| 337 |
+
name="idx",
|
| 338 |
+
)
|
| 339 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 340 |
+
assert isinstance(result, DatetimeIndex)
|
| 341 |
+
assert result.tz is not None
|
| 342 |
+
assert result.tz == exp.tz
|
| 343 |
+
|
| 344 |
+
# same tz results in DatetimeIndex (DST)
|
| 345 |
+
result = Index(
|
| 346 |
+
[
|
| 347 |
+
Timestamp("2011-01-01 10:00", tz="US/Eastern"),
|
| 348 |
+
pd.NaT,
|
| 349 |
+
Timestamp("2011-08-01 10:00", tz="US/Eastern"),
|
| 350 |
+
],
|
| 351 |
+
name="idx",
|
| 352 |
+
)
|
| 353 |
+
exp = DatetimeIndex(
|
| 354 |
+
[Timestamp("2011-01-01 10:00"), pd.NaT, Timestamp("2011-08-01 10:00")],
|
| 355 |
+
tz="US/Eastern",
|
| 356 |
+
name="idx",
|
| 357 |
+
)
|
| 358 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 359 |
+
assert isinstance(result, DatetimeIndex)
|
| 360 |
+
assert result.tz is not None
|
| 361 |
+
assert result.tz == exp.tz
|
| 362 |
+
|
| 363 |
+
# different tz results in Index(dtype=object)
|
| 364 |
+
result = Index(
|
| 365 |
+
[
|
| 366 |
+
pd.NaT,
|
| 367 |
+
Timestamp("2011-01-01 10:00"),
|
| 368 |
+
pd.NaT,
|
| 369 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 370 |
+
],
|
| 371 |
+
name="idx",
|
| 372 |
+
)
|
| 373 |
+
exp = Index(
|
| 374 |
+
[
|
| 375 |
+
pd.NaT,
|
| 376 |
+
Timestamp("2011-01-01 10:00"),
|
| 377 |
+
pd.NaT,
|
| 378 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 379 |
+
],
|
| 380 |
+
dtype="object",
|
| 381 |
+
name="idx",
|
| 382 |
+
)
|
| 383 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 384 |
+
assert not isinstance(result, DatetimeIndex)
|
| 385 |
+
|
| 386 |
+
result = Index(
|
| 387 |
+
[
|
| 388 |
+
pd.NaT,
|
| 389 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo"),
|
| 390 |
+
pd.NaT,
|
| 391 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 392 |
+
],
|
| 393 |
+
name="idx",
|
| 394 |
+
)
|
| 395 |
+
exp = Index(
|
| 396 |
+
[
|
| 397 |
+
pd.NaT,
|
| 398 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo"),
|
| 399 |
+
pd.NaT,
|
| 400 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 401 |
+
],
|
| 402 |
+
dtype="object",
|
| 403 |
+
name="idx",
|
| 404 |
+
)
|
| 405 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 406 |
+
assert not isinstance(result, DatetimeIndex)
|
| 407 |
+
|
| 408 |
+
# all NaT
|
| 409 |
+
result = Index([pd.NaT, pd.NaT], name="idx")
|
| 410 |
+
exp = DatetimeIndex([pd.NaT, pd.NaT], name="idx")
|
| 411 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 412 |
+
assert isinstance(result, DatetimeIndex)
|
| 413 |
+
assert result.tz is None
|
| 414 |
+
|
| 415 |
+
def test_construction_dti_with_mixed_timezones(self):
|
| 416 |
+
# GH 11488 (not changed, added explicit tests)
|
| 417 |
+
|
| 418 |
+
# no tz results in DatetimeIndex
|
| 419 |
+
result = DatetimeIndex(
|
| 420 |
+
[Timestamp("2011-01-01"), Timestamp("2011-01-02")], name="idx"
|
| 421 |
+
)
|
| 422 |
+
exp = DatetimeIndex(
|
| 423 |
+
[Timestamp("2011-01-01"), Timestamp("2011-01-02")], name="idx"
|
| 424 |
+
)
|
| 425 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 426 |
+
assert isinstance(result, DatetimeIndex)
|
| 427 |
+
|
| 428 |
+
# same tz results in DatetimeIndex
|
| 429 |
+
result = DatetimeIndex(
|
| 430 |
+
[
|
| 431 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo"),
|
| 432 |
+
Timestamp("2011-01-02 10:00", tz="Asia/Tokyo"),
|
| 433 |
+
],
|
| 434 |
+
name="idx",
|
| 435 |
+
)
|
| 436 |
+
exp = DatetimeIndex(
|
| 437 |
+
[Timestamp("2011-01-01 10:00"), Timestamp("2011-01-02 10:00")],
|
| 438 |
+
tz="Asia/Tokyo",
|
| 439 |
+
name="idx",
|
| 440 |
+
)
|
| 441 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 442 |
+
assert isinstance(result, DatetimeIndex)
|
| 443 |
+
|
| 444 |
+
# same tz results in DatetimeIndex (DST)
|
| 445 |
+
result = DatetimeIndex(
|
| 446 |
+
[
|
| 447 |
+
Timestamp("2011-01-01 10:00", tz="US/Eastern"),
|
| 448 |
+
Timestamp("2011-08-01 10:00", tz="US/Eastern"),
|
| 449 |
+
],
|
| 450 |
+
name="idx",
|
| 451 |
+
)
|
| 452 |
+
exp = DatetimeIndex(
|
| 453 |
+
[Timestamp("2011-01-01 10:00"), Timestamp("2011-08-01 10:00")],
|
| 454 |
+
tz="US/Eastern",
|
| 455 |
+
name="idx",
|
| 456 |
+
)
|
| 457 |
+
tm.assert_index_equal(result, exp, exact=True)
|
| 458 |
+
assert isinstance(result, DatetimeIndex)
|
| 459 |
+
|
| 460 |
+
# tz mismatch affecting to tz-aware raises TypeError/ValueError
|
| 461 |
+
|
| 462 |
+
msg = "cannot be converted to datetime64"
|
| 463 |
+
with pytest.raises(ValueError, match=msg):
|
| 464 |
+
DatetimeIndex(
|
| 465 |
+
[
|
| 466 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo"),
|
| 467 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 468 |
+
],
|
| 469 |
+
name="idx",
|
| 470 |
+
)
|
| 471 |
+
|
| 472 |
+
# pre-2.0 this raised bc of awareness mismatch. in 2.0 with a tz#
|
| 473 |
+
# specified we behave as if this was called pointwise, so
|
| 474 |
+
# the naive Timestamp is treated as a wall time.
|
| 475 |
+
dti = DatetimeIndex(
|
| 476 |
+
[
|
| 477 |
+
Timestamp("2011-01-01 10:00"),
|
| 478 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 479 |
+
],
|
| 480 |
+
tz="Asia/Tokyo",
|
| 481 |
+
name="idx",
|
| 482 |
+
)
|
| 483 |
+
expected = DatetimeIndex(
|
| 484 |
+
[
|
| 485 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo"),
|
| 486 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern").tz_convert("Asia/Tokyo"),
|
| 487 |
+
],
|
| 488 |
+
tz="Asia/Tokyo",
|
| 489 |
+
name="idx",
|
| 490 |
+
)
|
| 491 |
+
tm.assert_index_equal(dti, expected)
|
| 492 |
+
|
| 493 |
+
# pre-2.0 mixed-tz scalars raised even if a tz/dtype was specified.
|
| 494 |
+
# as of 2.0 we successfully return the requested tz/dtype
|
| 495 |
+
dti = DatetimeIndex(
|
| 496 |
+
[
|
| 497 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo"),
|
| 498 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 499 |
+
],
|
| 500 |
+
tz="US/Eastern",
|
| 501 |
+
name="idx",
|
| 502 |
+
)
|
| 503 |
+
expected = DatetimeIndex(
|
| 504 |
+
[
|
| 505 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo").tz_convert("US/Eastern"),
|
| 506 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 507 |
+
],
|
| 508 |
+
tz="US/Eastern",
|
| 509 |
+
name="idx",
|
| 510 |
+
)
|
| 511 |
+
tm.assert_index_equal(dti, expected)
|
| 512 |
+
|
| 513 |
+
# same thing but pass dtype instead of tz
|
| 514 |
+
dti = DatetimeIndex(
|
| 515 |
+
[
|
| 516 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo"),
|
| 517 |
+
Timestamp("2011-01-02 10:00", tz="US/Eastern"),
|
| 518 |
+
],
|
| 519 |
+
dtype="M8[ns, US/Eastern]",
|
| 520 |
+
name="idx",
|
| 521 |
+
)
|
| 522 |
+
tm.assert_index_equal(dti, expected)
|
| 523 |
+
|
| 524 |
+
def test_construction_base_constructor(self):
|
| 525 |
+
arr = [Timestamp("2011-01-01"), pd.NaT, Timestamp("2011-01-03")]
|
| 526 |
+
tm.assert_index_equal(Index(arr), DatetimeIndex(arr))
|
| 527 |
+
tm.assert_index_equal(Index(np.array(arr)), DatetimeIndex(np.array(arr)))
|
| 528 |
+
|
| 529 |
+
arr = [np.nan, pd.NaT, Timestamp("2011-01-03")]
|
| 530 |
+
tm.assert_index_equal(Index(arr), DatetimeIndex(arr))
|
| 531 |
+
tm.assert_index_equal(Index(np.array(arr)), DatetimeIndex(np.array(arr)))
|
| 532 |
+
|
| 533 |
+
def test_construction_outofbounds(self):
|
| 534 |
+
# GH 13663
|
| 535 |
+
dates = [
|
| 536 |
+
datetime(3000, 1, 1),
|
| 537 |
+
datetime(4000, 1, 1),
|
| 538 |
+
datetime(5000, 1, 1),
|
| 539 |
+
datetime(6000, 1, 1),
|
| 540 |
+
]
|
| 541 |
+
exp = Index(dates, dtype=object)
|
| 542 |
+
# coerces to object
|
| 543 |
+
tm.assert_index_equal(Index(dates), exp)
|
| 544 |
+
|
| 545 |
+
msg = "^Out of bounds nanosecond timestamp: 3000-01-01 00:00:00, at position 0$"
|
| 546 |
+
with pytest.raises(OutOfBoundsDatetime, match=msg):
|
| 547 |
+
# can't create DatetimeIndex
|
| 548 |
+
DatetimeIndex(dates)
|
| 549 |
+
|
| 550 |
+
def test_construction_with_ndarray(self):
|
| 551 |
+
# GH 5152
|
| 552 |
+
dates = [datetime(2013, 10, 7), datetime(2013, 10, 8), datetime(2013, 10, 9)]
|
| 553 |
+
data = DatetimeIndex(dates, freq=offsets.BDay()).values
|
| 554 |
+
result = DatetimeIndex(data, freq=offsets.BDay())
|
| 555 |
+
expected = DatetimeIndex(["2013-10-07", "2013-10-08", "2013-10-09"], freq="B")
|
| 556 |
+
tm.assert_index_equal(result, expected)
|
| 557 |
+
|
| 558 |
+
def test_integer_values_and_tz_interpreted_as_utc(self):
|
| 559 |
+
# GH-24559
|
| 560 |
+
val = np.datetime64("2000-01-01 00:00:00", "ns")
|
| 561 |
+
values = np.array([val.view("i8")])
|
| 562 |
+
|
| 563 |
+
result = DatetimeIndex(values).tz_localize("US/Central")
|
| 564 |
+
|
| 565 |
+
expected = DatetimeIndex(["2000-01-01T00:00:00"], tz="US/Central")
|
| 566 |
+
tm.assert_index_equal(result, expected)
|
| 567 |
+
|
| 568 |
+
# but UTC is *not* deprecated.
|
| 569 |
+
with tm.assert_produces_warning(None):
|
| 570 |
+
result = DatetimeIndex(values, tz="UTC")
|
| 571 |
+
expected = DatetimeIndex(["2000-01-01T00:00:00"], tz="US/Central")
|
| 572 |
+
|
| 573 |
+
def test_constructor_coverage(self):
|
| 574 |
+
rng = date_range("1/1/2000", periods=10.5)
|
| 575 |
+
exp = date_range("1/1/2000", periods=10)
|
| 576 |
+
tm.assert_index_equal(rng, exp)
|
| 577 |
+
|
| 578 |
+
msg = "periods must be a number, got foo"
|
| 579 |
+
with pytest.raises(TypeError, match=msg):
|
| 580 |
+
date_range(start="1/1/2000", periods="foo", freq="D")
|
| 581 |
+
|
| 582 |
+
msg = r"DatetimeIndex\(\.\.\.\) must be called with a collection"
|
| 583 |
+
with pytest.raises(TypeError, match=msg):
|
| 584 |
+
DatetimeIndex("1/1/2000")
|
| 585 |
+
|
| 586 |
+
# generator expression
|
| 587 |
+
gen = (datetime(2000, 1, 1) + timedelta(i) for i in range(10))
|
| 588 |
+
result = DatetimeIndex(gen)
|
| 589 |
+
expected = DatetimeIndex(
|
| 590 |
+
[datetime(2000, 1, 1) + timedelta(i) for i in range(10)]
|
| 591 |
+
)
|
| 592 |
+
tm.assert_index_equal(result, expected)
|
| 593 |
+
|
| 594 |
+
# NumPy string array
|
| 595 |
+
strings = np.array(["2000-01-01", "2000-01-02", "2000-01-03"])
|
| 596 |
+
result = DatetimeIndex(strings)
|
| 597 |
+
expected = DatetimeIndex(strings.astype("O"))
|
| 598 |
+
tm.assert_index_equal(result, expected)
|
| 599 |
+
|
| 600 |
+
from_ints = DatetimeIndex(expected.asi8)
|
| 601 |
+
tm.assert_index_equal(from_ints, expected)
|
| 602 |
+
|
| 603 |
+
# string with NaT
|
| 604 |
+
strings = np.array(["2000-01-01", "2000-01-02", "NaT"])
|
| 605 |
+
result = DatetimeIndex(strings)
|
| 606 |
+
expected = DatetimeIndex(strings.astype("O"))
|
| 607 |
+
tm.assert_index_equal(result, expected)
|
| 608 |
+
|
| 609 |
+
from_ints = DatetimeIndex(expected.asi8)
|
| 610 |
+
tm.assert_index_equal(from_ints, expected)
|
| 611 |
+
|
| 612 |
+
# non-conforming
|
| 613 |
+
msg = (
|
| 614 |
+
"Inferred frequency None from passed values does not conform "
|
| 615 |
+
"to passed frequency D"
|
| 616 |
+
)
|
| 617 |
+
with pytest.raises(ValueError, match=msg):
|
| 618 |
+
DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-04"], freq="D")
|
| 619 |
+
|
| 620 |
+
msg = (
|
| 621 |
+
"Of the four parameters: start, end, periods, and freq, exactly "
|
| 622 |
+
"three must be specified"
|
| 623 |
+
)
|
| 624 |
+
with pytest.raises(ValueError, match=msg):
|
| 625 |
+
date_range(start="2011-01-01", freq="b")
|
| 626 |
+
with pytest.raises(ValueError, match=msg):
|
| 627 |
+
date_range(end="2011-01-01", freq="B")
|
| 628 |
+
with pytest.raises(ValueError, match=msg):
|
| 629 |
+
date_range(periods=10, freq="D")
|
| 630 |
+
|
| 631 |
+
@pytest.mark.parametrize("freq", ["AS", "W-SUN"])
|
| 632 |
+
def test_constructor_datetime64_tzformat(self, freq):
|
| 633 |
+
# see GH#6572: ISO 8601 format results in stdlib timezone object
|
| 634 |
+
idx = date_range(
|
| 635 |
+
"2013-01-01T00:00:00-05:00", "2016-01-01T23:59:59-05:00", freq=freq
|
| 636 |
+
)
|
| 637 |
+
expected = date_range(
|
| 638 |
+
"2013-01-01T00:00:00",
|
| 639 |
+
"2016-01-01T23:59:59",
|
| 640 |
+
freq=freq,
|
| 641 |
+
tz=timezone(timedelta(minutes=-300)),
|
| 642 |
+
)
|
| 643 |
+
tm.assert_index_equal(idx, expected)
|
| 644 |
+
# Unable to use `US/Eastern` because of DST
|
| 645 |
+
expected_i8 = date_range(
|
| 646 |
+
"2013-01-01T00:00:00", "2016-01-01T23:59:59", freq=freq, tz="America/Lima"
|
| 647 |
+
)
|
| 648 |
+
tm.assert_numpy_array_equal(idx.asi8, expected_i8.asi8)
|
| 649 |
+
|
| 650 |
+
idx = date_range(
|
| 651 |
+
"2013-01-01T00:00:00+09:00", "2016-01-01T23:59:59+09:00", freq=freq
|
| 652 |
+
)
|
| 653 |
+
expected = date_range(
|
| 654 |
+
"2013-01-01T00:00:00",
|
| 655 |
+
"2016-01-01T23:59:59",
|
| 656 |
+
freq=freq,
|
| 657 |
+
tz=timezone(timedelta(minutes=540)),
|
| 658 |
+
)
|
| 659 |
+
tm.assert_index_equal(idx, expected)
|
| 660 |
+
expected_i8 = date_range(
|
| 661 |
+
"2013-01-01T00:00:00", "2016-01-01T23:59:59", freq=freq, tz="Asia/Tokyo"
|
| 662 |
+
)
|
| 663 |
+
tm.assert_numpy_array_equal(idx.asi8, expected_i8.asi8)
|
| 664 |
+
|
| 665 |
+
# Non ISO 8601 format results in dateutil.tz.tzoffset
|
| 666 |
+
idx = date_range("2013/1/1 0:00:00-5:00", "2016/1/1 23:59:59-5:00", freq=freq)
|
| 667 |
+
expected = date_range(
|
| 668 |
+
"2013-01-01T00:00:00",
|
| 669 |
+
"2016-01-01T23:59:59",
|
| 670 |
+
freq=freq,
|
| 671 |
+
tz=timezone(timedelta(minutes=-300)),
|
| 672 |
+
)
|
| 673 |
+
tm.assert_index_equal(idx, expected)
|
| 674 |
+
# Unable to use `US/Eastern` because of DST
|
| 675 |
+
expected_i8 = date_range(
|
| 676 |
+
"2013-01-01T00:00:00", "2016-01-01T23:59:59", freq=freq, tz="America/Lima"
|
| 677 |
+
)
|
| 678 |
+
tm.assert_numpy_array_equal(idx.asi8, expected_i8.asi8)
|
| 679 |
+
|
| 680 |
+
idx = date_range("2013/1/1 0:00:00+9:00", "2016/1/1 23:59:59+09:00", freq=freq)
|
| 681 |
+
expected = date_range(
|
| 682 |
+
"2013-01-01T00:00:00",
|
| 683 |
+
"2016-01-01T23:59:59",
|
| 684 |
+
freq=freq,
|
| 685 |
+
tz=timezone(timedelta(minutes=540)),
|
| 686 |
+
)
|
| 687 |
+
tm.assert_index_equal(idx, expected)
|
| 688 |
+
expected_i8 = date_range(
|
| 689 |
+
"2013-01-01T00:00:00", "2016-01-01T23:59:59", freq=freq, tz="Asia/Tokyo"
|
| 690 |
+
)
|
| 691 |
+
tm.assert_numpy_array_equal(idx.asi8, expected_i8.asi8)
|
| 692 |
+
|
| 693 |
+
def test_constructor_dtype(self):
|
| 694 |
+
# passing a dtype with a tz should localize
|
| 695 |
+
idx = DatetimeIndex(
|
| 696 |
+
["2013-01-01", "2013-01-02"], dtype="datetime64[ns, US/Eastern]"
|
| 697 |
+
)
|
| 698 |
+
expected = DatetimeIndex(["2013-01-01", "2013-01-02"]).tz_localize("US/Eastern")
|
| 699 |
+
tm.assert_index_equal(idx, expected)
|
| 700 |
+
|
| 701 |
+
idx = DatetimeIndex(["2013-01-01", "2013-01-02"], tz="US/Eastern")
|
| 702 |
+
tm.assert_index_equal(idx, expected)
|
| 703 |
+
|
| 704 |
+
def test_constructor_dtype_tz_mismatch_raises(self):
|
| 705 |
+
# if we already have a tz and its not the same, then raise
|
| 706 |
+
idx = DatetimeIndex(
|
| 707 |
+
["2013-01-01", "2013-01-02"], dtype="datetime64[ns, US/Eastern]"
|
| 708 |
+
)
|
| 709 |
+
|
| 710 |
+
msg = (
|
| 711 |
+
"cannot supply both a tz and a timezone-naive dtype "
|
| 712 |
+
r"\(i\.e\. datetime64\[ns\]\)"
|
| 713 |
+
)
|
| 714 |
+
with pytest.raises(ValueError, match=msg):
|
| 715 |
+
DatetimeIndex(idx, dtype="datetime64[ns]")
|
| 716 |
+
|
| 717 |
+
# this is effectively trying to convert tz's
|
| 718 |
+
msg = "data is already tz-aware US/Eastern, unable to set specified tz: CET"
|
| 719 |
+
with pytest.raises(TypeError, match=msg):
|
| 720 |
+
DatetimeIndex(idx, dtype="datetime64[ns, CET]")
|
| 721 |
+
msg = "cannot supply both a tz and a dtype with a tz"
|
| 722 |
+
with pytest.raises(ValueError, match=msg):
|
| 723 |
+
DatetimeIndex(idx, tz="CET", dtype="datetime64[ns, US/Eastern]")
|
| 724 |
+
|
| 725 |
+
result = DatetimeIndex(idx, dtype="datetime64[ns, US/Eastern]")
|
| 726 |
+
tm.assert_index_equal(idx, result)
|
| 727 |
+
|
| 728 |
+
@pytest.mark.parametrize("dtype", [object, np.int32, np.int64])
|
| 729 |
+
def test_constructor_invalid_dtype_raises(self, dtype):
|
| 730 |
+
# GH 23986
|
| 731 |
+
msg = "Unexpected value for 'dtype'"
|
| 732 |
+
with pytest.raises(ValueError, match=msg):
|
| 733 |
+
DatetimeIndex([1, 2], dtype=dtype)
|
| 734 |
+
|
| 735 |
+
def test_constructor_name(self):
|
| 736 |
+
idx = date_range(start="2000-01-01", periods=1, freq="A", name="TEST")
|
| 737 |
+
assert idx.name == "TEST"
|
| 738 |
+
|
| 739 |
+
def test_000constructor_resolution(self):
|
| 740 |
+
# 2252
|
| 741 |
+
t1 = Timestamp((1352934390 * 1000000000) + 1000000 + 1000 + 1)
|
| 742 |
+
idx = DatetimeIndex([t1])
|
| 743 |
+
|
| 744 |
+
assert idx.nanosecond[0] == t1.nanosecond
|
| 745 |
+
|
| 746 |
+
def test_disallow_setting_tz(self):
|
| 747 |
+
# GH 3746
|
| 748 |
+
dti = DatetimeIndex(["2010"], tz="UTC")
|
| 749 |
+
msg = "Cannot directly set timezone"
|
| 750 |
+
with pytest.raises(AttributeError, match=msg):
|
| 751 |
+
dti.tz = pytz.timezone("US/Pacific")
|
| 752 |
+
|
| 753 |
+
@pytest.mark.parametrize(
|
| 754 |
+
"tz",
|
| 755 |
+
[
|
| 756 |
+
None,
|
| 757 |
+
"America/Los_Angeles",
|
| 758 |
+
pytz.timezone("America/Los_Angeles"),
|
| 759 |
+
Timestamp("2000", tz="America/Los_Angeles").tz,
|
| 760 |
+
],
|
| 761 |
+
)
|
| 762 |
+
def test_constructor_start_end_with_tz(self, tz):
|
| 763 |
+
# GH 18595
|
| 764 |
+
start = Timestamp("2013-01-01 06:00:00", tz="America/Los_Angeles")
|
| 765 |
+
end = Timestamp("2013-01-02 06:00:00", tz="America/Los_Angeles")
|
| 766 |
+
result = date_range(freq="D", start=start, end=end, tz=tz)
|
| 767 |
+
expected = DatetimeIndex(
|
| 768 |
+
["2013-01-01 06:00:00", "2013-01-02 06:00:00"],
|
| 769 |
+
tz="America/Los_Angeles",
|
| 770 |
+
freq="D",
|
| 771 |
+
)
|
| 772 |
+
tm.assert_index_equal(result, expected)
|
| 773 |
+
# Especially assert that the timezone is consistent for pytz
|
| 774 |
+
assert pytz.timezone("America/Los_Angeles") is result.tz
|
| 775 |
+
|
| 776 |
+
@pytest.mark.parametrize("tz", ["US/Pacific", "US/Eastern", "Asia/Tokyo"])
|
| 777 |
+
def test_constructor_with_non_normalized_pytz(self, tz):
|
| 778 |
+
# GH 18595
|
| 779 |
+
non_norm_tz = Timestamp("2010", tz=tz).tz
|
| 780 |
+
result = DatetimeIndex(["2010"], tz=non_norm_tz)
|
| 781 |
+
assert pytz.timezone(tz) is result.tz
|
| 782 |
+
|
| 783 |
+
def test_constructor_timestamp_near_dst(self):
|
| 784 |
+
# GH 20854
|
| 785 |
+
ts = [
|
| 786 |
+
Timestamp("2016-10-30 03:00:00+0300", tz="Europe/Helsinki"),
|
| 787 |
+
Timestamp("2016-10-30 03:00:00+0200", tz="Europe/Helsinki"),
|
| 788 |
+
]
|
| 789 |
+
result = DatetimeIndex(ts)
|
| 790 |
+
expected = DatetimeIndex([ts[0].to_pydatetime(), ts[1].to_pydatetime()])
|
| 791 |
+
tm.assert_index_equal(result, expected)
|
| 792 |
+
|
| 793 |
+
@pytest.mark.parametrize("klass", [Index, DatetimeIndex])
|
| 794 |
+
@pytest.mark.parametrize("box", [np.array, partial(np.array, dtype=object), list])
|
| 795 |
+
@pytest.mark.parametrize(
|
| 796 |
+
"tz, dtype",
|
| 797 |
+
[("US/Pacific", "datetime64[ns, US/Pacific]"), (None, "datetime64[ns]")],
|
| 798 |
+
)
|
| 799 |
+
def test_constructor_with_int_tz(self, klass, box, tz, dtype):
|
| 800 |
+
# GH 20997, 20964
|
| 801 |
+
ts = Timestamp("2018-01-01", tz=tz).as_unit("ns")
|
| 802 |
+
result = klass(box([ts._value]), dtype=dtype)
|
| 803 |
+
expected = klass([ts])
|
| 804 |
+
assert result == expected
|
| 805 |
+
|
| 806 |
+
def test_construction_int_rountrip(self, tz_naive_fixture):
|
| 807 |
+
# GH 12619, GH#24559
|
| 808 |
+
tz = tz_naive_fixture
|
| 809 |
+
|
| 810 |
+
result = 1293858000000000000
|
| 811 |
+
expected = DatetimeIndex([result], tz=tz).asi8[0]
|
| 812 |
+
assert result == expected
|
| 813 |
+
|
| 814 |
+
def test_construction_from_replaced_timestamps_with_dst(self):
|
| 815 |
+
# GH 18785
|
| 816 |
+
index = date_range(
|
| 817 |
+
Timestamp(2000, 1, 1),
|
| 818 |
+
Timestamp(2005, 1, 1),
|
| 819 |
+
freq="MS",
|
| 820 |
+
tz="Australia/Melbourne",
|
| 821 |
+
)
|
| 822 |
+
test = pd.DataFrame({"data": range(len(index))}, index=index)
|
| 823 |
+
test = test.resample("Y").mean()
|
| 824 |
+
result = DatetimeIndex([x.replace(month=6, day=1) for x in test.index])
|
| 825 |
+
expected = DatetimeIndex(
|
| 826 |
+
[
|
| 827 |
+
"2000-06-01 00:00:00",
|
| 828 |
+
"2001-06-01 00:00:00",
|
| 829 |
+
"2002-06-01 00:00:00",
|
| 830 |
+
"2003-06-01 00:00:00",
|
| 831 |
+
"2004-06-01 00:00:00",
|
| 832 |
+
"2005-06-01 00:00:00",
|
| 833 |
+
],
|
| 834 |
+
tz="Australia/Melbourne",
|
| 835 |
+
)
|
| 836 |
+
tm.assert_index_equal(result, expected)
|
| 837 |
+
|
| 838 |
+
def test_construction_with_tz_and_tz_aware_dti(self):
|
| 839 |
+
# GH 23579
|
| 840 |
+
dti = date_range("2016-01-01", periods=3, tz="US/Central")
|
| 841 |
+
msg = "data is already tz-aware US/Central, unable to set specified tz"
|
| 842 |
+
with pytest.raises(TypeError, match=msg):
|
| 843 |
+
DatetimeIndex(dti, tz="Asia/Tokyo")
|
| 844 |
+
|
| 845 |
+
def test_construction_with_nat_and_tzlocal(self):
|
| 846 |
+
tz = dateutil.tz.tzlocal()
|
| 847 |
+
result = DatetimeIndex(["2018", "NaT"], tz=tz)
|
| 848 |
+
expected = DatetimeIndex([Timestamp("2018", tz=tz), pd.NaT])
|
| 849 |
+
tm.assert_index_equal(result, expected)
|
| 850 |
+
|
| 851 |
+
def test_constructor_with_ambiguous_keyword_arg(self):
|
| 852 |
+
# GH 35297
|
| 853 |
+
|
| 854 |
+
expected = DatetimeIndex(
|
| 855 |
+
["2020-11-01 01:00:00", "2020-11-02 01:00:00"],
|
| 856 |
+
dtype="datetime64[ns, America/New_York]",
|
| 857 |
+
freq="D",
|
| 858 |
+
ambiguous=False,
|
| 859 |
+
)
|
| 860 |
+
|
| 861 |
+
# ambiguous keyword in start
|
| 862 |
+
timezone = "America/New_York"
|
| 863 |
+
start = Timestamp(year=2020, month=11, day=1, hour=1).tz_localize(
|
| 864 |
+
timezone, ambiguous=False
|
| 865 |
+
)
|
| 866 |
+
result = date_range(start=start, periods=2, ambiguous=False)
|
| 867 |
+
tm.assert_index_equal(result, expected)
|
| 868 |
+
|
| 869 |
+
# ambiguous keyword in end
|
| 870 |
+
timezone = "America/New_York"
|
| 871 |
+
end = Timestamp(year=2020, month=11, day=2, hour=1).tz_localize(
|
| 872 |
+
timezone, ambiguous=False
|
| 873 |
+
)
|
| 874 |
+
result = date_range(end=end, periods=2, ambiguous=False)
|
| 875 |
+
tm.assert_index_equal(result, expected)
|
| 876 |
+
|
| 877 |
+
def test_constructor_with_nonexistent_keyword_arg(self, warsaw):
|
| 878 |
+
# GH 35297
|
| 879 |
+
timezone = warsaw
|
| 880 |
+
|
| 881 |
+
# nonexistent keyword in start
|
| 882 |
+
start = Timestamp("2015-03-29 02:30:00").tz_localize(
|
| 883 |
+
timezone, nonexistent="shift_forward"
|
| 884 |
+
)
|
| 885 |
+
result = date_range(start=start, periods=2, freq="H")
|
| 886 |
+
expected = DatetimeIndex(
|
| 887 |
+
[
|
| 888 |
+
Timestamp("2015-03-29 03:00:00+02:00", tz=timezone),
|
| 889 |
+
Timestamp("2015-03-29 04:00:00+02:00", tz=timezone),
|
| 890 |
+
]
|
| 891 |
+
)
|
| 892 |
+
|
| 893 |
+
tm.assert_index_equal(result, expected)
|
| 894 |
+
|
| 895 |
+
# nonexistent keyword in end
|
| 896 |
+
end = Timestamp("2015-03-29 02:30:00").tz_localize(
|
| 897 |
+
timezone, nonexistent="shift_forward"
|
| 898 |
+
)
|
| 899 |
+
result = date_range(end=end, periods=2, freq="H")
|
| 900 |
+
expected = DatetimeIndex(
|
| 901 |
+
[
|
| 902 |
+
Timestamp("2015-03-29 01:00:00+01:00", tz=timezone),
|
| 903 |
+
Timestamp("2015-03-29 03:00:00+02:00", tz=timezone),
|
| 904 |
+
]
|
| 905 |
+
)
|
| 906 |
+
|
| 907 |
+
tm.assert_index_equal(result, expected)
|
| 908 |
+
|
| 909 |
+
def test_constructor_no_precision_raises(self):
|
| 910 |
+
# GH-24753, GH-24739
|
| 911 |
+
|
| 912 |
+
msg = "with no precision is not allowed"
|
| 913 |
+
with pytest.raises(ValueError, match=msg):
|
| 914 |
+
DatetimeIndex(["2000"], dtype="datetime64")
|
| 915 |
+
|
| 916 |
+
msg = "The 'datetime64' dtype has no unit. Please pass in"
|
| 917 |
+
with pytest.raises(ValueError, match=msg):
|
| 918 |
+
Index(["2000"], dtype="datetime64")
|
| 919 |
+
|
| 920 |
+
def test_constructor_wrong_precision_raises(self):
|
| 921 |
+
dti = DatetimeIndex(["2000"], dtype="datetime64[us]")
|
| 922 |
+
assert dti.dtype == "M8[us]"
|
| 923 |
+
assert dti[0] == Timestamp(2000, 1, 1)
|
| 924 |
+
|
| 925 |
+
def test_index_constructor_with_numpy_object_array_and_timestamp_tz_with_nan(self):
|
| 926 |
+
# GH 27011
|
| 927 |
+
result = Index(np.array([Timestamp("2019", tz="UTC"), np.nan], dtype=object))
|
| 928 |
+
expected = DatetimeIndex([Timestamp("2019", tz="UTC"), pd.NaT])
|
| 929 |
+
tm.assert_index_equal(result, expected)
|
| 930 |
+
|
| 931 |
+
|
| 932 |
+
class TestTimeSeries:
|
| 933 |
+
def test_dti_constructor_preserve_dti_freq(self):
|
| 934 |
+
rng = date_range("1/1/2000", "1/2/2000", freq="5min")
|
| 935 |
+
|
| 936 |
+
rng2 = DatetimeIndex(rng)
|
| 937 |
+
assert rng.freq == rng2.freq
|
| 938 |
+
|
| 939 |
+
def test_explicit_none_freq(self):
|
| 940 |
+
# Explicitly passing freq=None is respected
|
| 941 |
+
rng = date_range("1/1/2000", "1/2/2000", freq="5min")
|
| 942 |
+
|
| 943 |
+
result = DatetimeIndex(rng, freq=None)
|
| 944 |
+
assert result.freq is None
|
| 945 |
+
|
| 946 |
+
result = DatetimeIndex(rng._data, freq=None)
|
| 947 |
+
assert result.freq is None
|
| 948 |
+
|
| 949 |
+
dta = DatetimeArray(rng, freq=None)
|
| 950 |
+
assert dta.freq is None
|
| 951 |
+
|
| 952 |
+
def test_dti_constructor_years_only(self, tz_naive_fixture):
|
| 953 |
+
tz = tz_naive_fixture
|
| 954 |
+
# GH 6961
|
| 955 |
+
rng1 = date_range("2014", "2015", freq="M", tz=tz)
|
| 956 |
+
expected1 = date_range("2014-01-31", "2014-12-31", freq="M", tz=tz)
|
| 957 |
+
|
| 958 |
+
rng2 = date_range("2014", "2015", freq="MS", tz=tz)
|
| 959 |
+
expected2 = date_range("2014-01-01", "2015-01-01", freq="MS", tz=tz)
|
| 960 |
+
|
| 961 |
+
rng3 = date_range("2014", "2020", freq="A", tz=tz)
|
| 962 |
+
expected3 = date_range("2014-12-31", "2019-12-31", freq="A", tz=tz)
|
| 963 |
+
|
| 964 |
+
rng4 = date_range("2014", "2020", freq="AS", tz=tz)
|
| 965 |
+
expected4 = date_range("2014-01-01", "2020-01-01", freq="AS", tz=tz)
|
| 966 |
+
|
| 967 |
+
for rng, expected in [
|
| 968 |
+
(rng1, expected1),
|
| 969 |
+
(rng2, expected2),
|
| 970 |
+
(rng3, expected3),
|
| 971 |
+
(rng4, expected4),
|
| 972 |
+
]:
|
| 973 |
+
tm.assert_index_equal(rng, expected)
|
| 974 |
+
|
| 975 |
+
def test_dti_constructor_small_int(self, any_int_numpy_dtype):
|
| 976 |
+
# see gh-13721
|
| 977 |
+
exp = DatetimeIndex(
|
| 978 |
+
[
|
| 979 |
+
"1970-01-01 00:00:00.00000000",
|
| 980 |
+
"1970-01-01 00:00:00.00000001",
|
| 981 |
+
"1970-01-01 00:00:00.00000002",
|
| 982 |
+
]
|
| 983 |
+
)
|
| 984 |
+
|
| 985 |
+
arr = np.array([0, 10, 20], dtype=any_int_numpy_dtype)
|
| 986 |
+
tm.assert_index_equal(DatetimeIndex(arr), exp)
|
| 987 |
+
|
| 988 |
+
def test_ctor_str_intraday(self):
|
| 989 |
+
rng = DatetimeIndex(["1-1-2000 00:00:01"])
|
| 990 |
+
assert rng[0].second == 1
|
| 991 |
+
|
| 992 |
+
def test_is_(self):
|
| 993 |
+
dti = date_range(start="1/1/2005", end="12/1/2005", freq="M")
|
| 994 |
+
assert dti.is_(dti)
|
| 995 |
+
assert dti.is_(dti.view())
|
| 996 |
+
assert not dti.is_(dti.copy())
|
| 997 |
+
|
| 998 |
+
def test_index_cast_datetime64_other_units(self):
|
| 999 |
+
arr = np.arange(0, 100, 10, dtype=np.int64).view("M8[D]")
|
| 1000 |
+
idx = Index(arr)
|
| 1001 |
+
|
| 1002 |
+
assert (idx.values == astype_overflowsafe(arr, dtype=np.dtype("M8[ns]"))).all()
|
| 1003 |
+
|
| 1004 |
+
def test_constructor_int64_nocopy(self):
|
| 1005 |
+
# GH#1624
|
| 1006 |
+
arr = np.arange(1000, dtype=np.int64)
|
| 1007 |
+
index = DatetimeIndex(arr)
|
| 1008 |
+
|
| 1009 |
+
arr[50:100] = -1
|
| 1010 |
+
assert (index.asi8[50:100] == -1).all()
|
| 1011 |
+
|
| 1012 |
+
arr = np.arange(1000, dtype=np.int64)
|
| 1013 |
+
index = DatetimeIndex(arr, copy=True)
|
| 1014 |
+
|
| 1015 |
+
arr[50:100] = -1
|
| 1016 |
+
assert (index.asi8[50:100] != -1).all()
|
| 1017 |
+
|
| 1018 |
+
@pytest.mark.parametrize(
|
| 1019 |
+
"freq", ["M", "Q", "A", "D", "B", "BH", "T", "S", "L", "U", "H", "N", "C"]
|
| 1020 |
+
)
|
| 1021 |
+
def test_from_freq_recreate_from_data(self, freq):
|
| 1022 |
+
org = date_range(start="2001/02/01 09:00", freq=freq, periods=1)
|
| 1023 |
+
idx = DatetimeIndex(org, freq=freq)
|
| 1024 |
+
tm.assert_index_equal(idx, org)
|
| 1025 |
+
|
| 1026 |
+
org = date_range(
|
| 1027 |
+
start="2001/02/01 09:00", freq=freq, tz="US/Pacific", periods=1
|
| 1028 |
+
)
|
| 1029 |
+
idx = DatetimeIndex(org, freq=freq, tz="US/Pacific")
|
| 1030 |
+
tm.assert_index_equal(idx, org)
|
| 1031 |
+
|
| 1032 |
+
def test_datetimeindex_constructor_misc(self):
|
| 1033 |
+
arr = ["1/1/2005", "1/2/2005", "Jn 3, 2005", "2005-01-04"]
|
| 1034 |
+
msg = r"(\(')?Unknown datetime string format(:', 'Jn 3, 2005'\))?"
|
| 1035 |
+
with pytest.raises(ValueError, match=msg):
|
| 1036 |
+
DatetimeIndex(arr)
|
| 1037 |
+
|
| 1038 |
+
arr = ["1/1/2005", "1/2/2005", "1/3/2005", "2005-01-04"]
|
| 1039 |
+
idx1 = DatetimeIndex(arr)
|
| 1040 |
+
|
| 1041 |
+
arr = [datetime(2005, 1, 1), "1/2/2005", "1/3/2005", "2005-01-04"]
|
| 1042 |
+
idx2 = DatetimeIndex(arr)
|
| 1043 |
+
|
| 1044 |
+
arr = [Timestamp(datetime(2005, 1, 1)), "1/2/2005", "1/3/2005", "2005-01-04"]
|
| 1045 |
+
idx3 = DatetimeIndex(arr)
|
| 1046 |
+
|
| 1047 |
+
arr = np.array(["1/1/2005", "1/2/2005", "1/3/2005", "2005-01-04"], dtype="O")
|
| 1048 |
+
idx4 = DatetimeIndex(arr)
|
| 1049 |
+
|
| 1050 |
+
idx5 = DatetimeIndex(["12/05/2007", "25/01/2008"], dayfirst=True)
|
| 1051 |
+
idx6 = DatetimeIndex(
|
| 1052 |
+
["2007/05/12", "2008/01/25"], dayfirst=False, yearfirst=True
|
| 1053 |
+
)
|
| 1054 |
+
tm.assert_index_equal(idx5, idx6)
|
| 1055 |
+
|
| 1056 |
+
for other in [idx2, idx3, idx4]:
|
| 1057 |
+
assert (idx1.values == other.values).all()
|
| 1058 |
+
|
| 1059 |
+
sdate = datetime(1999, 12, 25)
|
| 1060 |
+
edate = datetime(2000, 1, 1)
|
| 1061 |
+
idx = date_range(start=sdate, freq="1B", periods=20)
|
| 1062 |
+
assert len(idx) == 20
|
| 1063 |
+
assert idx[0] == sdate + 0 * offsets.BDay()
|
| 1064 |
+
assert idx.freq == "B"
|
| 1065 |
+
|
| 1066 |
+
idx1 = date_range(start=sdate, end=edate, freq="W-SUN")
|
| 1067 |
+
idx2 = date_range(start=sdate, end=edate, freq=offsets.Week(weekday=6))
|
| 1068 |
+
assert len(idx1) == len(idx2)
|
| 1069 |
+
assert idx1.freq == idx2.freq
|
| 1070 |
+
|
| 1071 |
+
idx1 = date_range(start=sdate, end=edate, freq="QS")
|
| 1072 |
+
idx2 = date_range(
|
| 1073 |
+
start=sdate, end=edate, freq=offsets.QuarterBegin(startingMonth=1)
|
| 1074 |
+
)
|
| 1075 |
+
assert len(idx1) == len(idx2)
|
| 1076 |
+
assert idx1.freq == idx2.freq
|
| 1077 |
+
|
| 1078 |
+
idx1 = date_range(start=sdate, end=edate, freq="BQ")
|
| 1079 |
+
idx2 = date_range(
|
| 1080 |
+
start=sdate, end=edate, freq=offsets.BQuarterEnd(startingMonth=12)
|
| 1081 |
+
)
|
| 1082 |
+
assert len(idx1) == len(idx2)
|
| 1083 |
+
assert idx1.freq == idx2.freq
|
| 1084 |
+
|
| 1085 |
+
def test_pass_datetimeindex_to_index(self):
|
| 1086 |
+
# Bugs in #1396
|
| 1087 |
+
rng = date_range("1/1/2000", "3/1/2000")
|
| 1088 |
+
idx = Index(rng, dtype=object)
|
| 1089 |
+
|
| 1090 |
+
expected = Index(rng.to_pydatetime(), dtype=object)
|
| 1091 |
+
|
| 1092 |
+
tm.assert_numpy_array_equal(idx.values, expected.values)
|
| 1093 |
+
|
| 1094 |
+
def test_date_range_tuple_freq_raises(self):
|
| 1095 |
+
# GH#34703
|
| 1096 |
+
edate = datetime(2000, 1, 1)
|
| 1097 |
+
with pytest.raises(TypeError, match="pass as a string instead"):
|
| 1098 |
+
date_range(end=edate, freq=("D", 5), periods=20)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_date_range.py
ADDED
|
@@ -0,0 +1,1258 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
test date_range, bdate_range construction from the convenience range functions
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from datetime import (
|
| 6 |
+
datetime,
|
| 7 |
+
time,
|
| 8 |
+
timedelta,
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
import pytest
|
| 13 |
+
import pytz
|
| 14 |
+
from pytz import timezone
|
| 15 |
+
|
| 16 |
+
from pandas._libs.tslibs import timezones
|
| 17 |
+
from pandas._libs.tslibs.offsets import (
|
| 18 |
+
BDay,
|
| 19 |
+
CDay,
|
| 20 |
+
DateOffset,
|
| 21 |
+
MonthEnd,
|
| 22 |
+
prefix_mapping,
|
| 23 |
+
)
|
| 24 |
+
from pandas.errors import OutOfBoundsDatetime
|
| 25 |
+
import pandas.util._test_decorators as td
|
| 26 |
+
|
| 27 |
+
import pandas as pd
|
| 28 |
+
from pandas import (
|
| 29 |
+
DatetimeIndex,
|
| 30 |
+
Timedelta,
|
| 31 |
+
Timestamp,
|
| 32 |
+
bdate_range,
|
| 33 |
+
date_range,
|
| 34 |
+
offsets,
|
| 35 |
+
)
|
| 36 |
+
import pandas._testing as tm
|
| 37 |
+
from pandas.core.arrays.datetimes import _generate_range as generate_range
|
| 38 |
+
|
| 39 |
+
START, END = datetime(2009, 1, 1), datetime(2010, 1, 1)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def _get_expected_range(
|
| 43 |
+
begin_to_match,
|
| 44 |
+
end_to_match,
|
| 45 |
+
both_range,
|
| 46 |
+
inclusive_endpoints,
|
| 47 |
+
):
|
| 48 |
+
"""Helper to get expected range from a both inclusive range"""
|
| 49 |
+
left_match = begin_to_match == both_range[0]
|
| 50 |
+
right_match = end_to_match == both_range[-1]
|
| 51 |
+
|
| 52 |
+
if inclusive_endpoints == "left" and right_match:
|
| 53 |
+
expected_range = both_range[:-1]
|
| 54 |
+
elif inclusive_endpoints == "right" and left_match:
|
| 55 |
+
expected_range = both_range[1:]
|
| 56 |
+
elif inclusive_endpoints == "neither" and left_match and right_match:
|
| 57 |
+
expected_range = both_range[1:-1]
|
| 58 |
+
elif inclusive_endpoints == "neither" and right_match:
|
| 59 |
+
expected_range = both_range[:-1]
|
| 60 |
+
elif inclusive_endpoints == "neither" and left_match:
|
| 61 |
+
expected_range = both_range[1:]
|
| 62 |
+
elif inclusive_endpoints == "both":
|
| 63 |
+
expected_range = both_range[:]
|
| 64 |
+
else:
|
| 65 |
+
expected_range = both_range[:]
|
| 66 |
+
|
| 67 |
+
return expected_range
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class TestTimestampEquivDateRange:
|
| 71 |
+
# Older tests in TestTimeSeries constructed their `stamp` objects
|
| 72 |
+
# using `date_range` instead of the `Timestamp` constructor.
|
| 73 |
+
# TestTimestampEquivDateRange checks that these are equivalent in the
|
| 74 |
+
# pertinent cases.
|
| 75 |
+
|
| 76 |
+
def test_date_range_timestamp_equiv(self):
|
| 77 |
+
rng = date_range("20090415", "20090519", tz="US/Eastern")
|
| 78 |
+
stamp = rng[0]
|
| 79 |
+
|
| 80 |
+
ts = Timestamp("20090415", tz="US/Eastern")
|
| 81 |
+
assert ts == stamp
|
| 82 |
+
|
| 83 |
+
def test_date_range_timestamp_equiv_dateutil(self):
|
| 84 |
+
rng = date_range("20090415", "20090519", tz="dateutil/US/Eastern")
|
| 85 |
+
stamp = rng[0]
|
| 86 |
+
|
| 87 |
+
ts = Timestamp("20090415", tz="dateutil/US/Eastern")
|
| 88 |
+
assert ts == stamp
|
| 89 |
+
|
| 90 |
+
def test_date_range_timestamp_equiv_explicit_pytz(self):
|
| 91 |
+
rng = date_range("20090415", "20090519", tz=pytz.timezone("US/Eastern"))
|
| 92 |
+
stamp = rng[0]
|
| 93 |
+
|
| 94 |
+
ts = Timestamp("20090415", tz=pytz.timezone("US/Eastern"))
|
| 95 |
+
assert ts == stamp
|
| 96 |
+
|
| 97 |
+
@td.skip_if_windows
|
| 98 |
+
def test_date_range_timestamp_equiv_explicit_dateutil(self):
|
| 99 |
+
from pandas._libs.tslibs.timezones import dateutil_gettz as gettz
|
| 100 |
+
|
| 101 |
+
rng = date_range("20090415", "20090519", tz=gettz("US/Eastern"))
|
| 102 |
+
stamp = rng[0]
|
| 103 |
+
|
| 104 |
+
ts = Timestamp("20090415", tz=gettz("US/Eastern"))
|
| 105 |
+
assert ts == stamp
|
| 106 |
+
|
| 107 |
+
def test_date_range_timestamp_equiv_from_datetime_instance(self):
|
| 108 |
+
datetime_instance = datetime(2014, 3, 4)
|
| 109 |
+
# build a timestamp with a frequency, since then it supports
|
| 110 |
+
# addition/subtraction of integers
|
| 111 |
+
timestamp_instance = date_range(datetime_instance, periods=1, freq="D")[0]
|
| 112 |
+
|
| 113 |
+
ts = Timestamp(datetime_instance)
|
| 114 |
+
assert ts == timestamp_instance
|
| 115 |
+
|
| 116 |
+
def test_date_range_timestamp_equiv_preserve_frequency(self):
|
| 117 |
+
timestamp_instance = date_range("2014-03-05", periods=1, freq="D")[0]
|
| 118 |
+
ts = Timestamp("2014-03-05")
|
| 119 |
+
|
| 120 |
+
assert timestamp_instance == ts
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
class TestDateRanges:
|
| 124 |
+
@pytest.mark.parametrize("freq", ["N", "U", "L", "T", "S", "H", "D"])
|
| 125 |
+
def test_date_range_edges(self, freq):
|
| 126 |
+
# GH#13672
|
| 127 |
+
td = Timedelta(f"1{freq}")
|
| 128 |
+
ts = Timestamp("1970-01-01")
|
| 129 |
+
|
| 130 |
+
idx = date_range(
|
| 131 |
+
start=ts + td,
|
| 132 |
+
end=ts + 4 * td,
|
| 133 |
+
freq=freq,
|
| 134 |
+
)
|
| 135 |
+
exp = DatetimeIndex(
|
| 136 |
+
[ts + n * td for n in range(1, 5)],
|
| 137 |
+
freq=freq,
|
| 138 |
+
)
|
| 139 |
+
tm.assert_index_equal(idx, exp)
|
| 140 |
+
|
| 141 |
+
# start after end
|
| 142 |
+
idx = date_range(
|
| 143 |
+
start=ts + 4 * td,
|
| 144 |
+
end=ts + td,
|
| 145 |
+
freq=freq,
|
| 146 |
+
)
|
| 147 |
+
exp = DatetimeIndex([], freq=freq)
|
| 148 |
+
tm.assert_index_equal(idx, exp)
|
| 149 |
+
|
| 150 |
+
# start matches end
|
| 151 |
+
idx = date_range(
|
| 152 |
+
start=ts + td,
|
| 153 |
+
end=ts + td,
|
| 154 |
+
freq=freq,
|
| 155 |
+
)
|
| 156 |
+
exp = DatetimeIndex([ts + td], freq=freq)
|
| 157 |
+
tm.assert_index_equal(idx, exp)
|
| 158 |
+
|
| 159 |
+
def test_date_range_near_implementation_bound(self):
|
| 160 |
+
# GH#???
|
| 161 |
+
freq = Timedelta(1)
|
| 162 |
+
|
| 163 |
+
with pytest.raises(OutOfBoundsDatetime, match="Cannot generate range with"):
|
| 164 |
+
date_range(end=Timestamp.min, periods=2, freq=freq)
|
| 165 |
+
|
| 166 |
+
def test_date_range_nat(self):
|
| 167 |
+
# GH#11587
|
| 168 |
+
msg = "Neither `start` nor `end` can be NaT"
|
| 169 |
+
with pytest.raises(ValueError, match=msg):
|
| 170 |
+
date_range(start="2016-01-01", end=pd.NaT, freq="D")
|
| 171 |
+
with pytest.raises(ValueError, match=msg):
|
| 172 |
+
date_range(start=pd.NaT, end="2016-01-01", freq="D")
|
| 173 |
+
|
| 174 |
+
def test_date_range_multiplication_overflow(self):
|
| 175 |
+
# GH#24255
|
| 176 |
+
# check that overflows in calculating `addend = periods * stride`
|
| 177 |
+
# are caught
|
| 178 |
+
with tm.assert_produces_warning(None):
|
| 179 |
+
# we should _not_ be seeing a overflow RuntimeWarning
|
| 180 |
+
dti = date_range(start="1677-09-22", periods=213503, freq="D")
|
| 181 |
+
|
| 182 |
+
assert dti[0] == Timestamp("1677-09-22")
|
| 183 |
+
assert len(dti) == 213503
|
| 184 |
+
|
| 185 |
+
msg = "Cannot generate range with"
|
| 186 |
+
with pytest.raises(OutOfBoundsDatetime, match=msg):
|
| 187 |
+
date_range("1969-05-04", periods=200000000, freq="30000D")
|
| 188 |
+
|
| 189 |
+
def test_date_range_unsigned_overflow_handling(self):
|
| 190 |
+
# GH#24255
|
| 191 |
+
# case where `addend = periods * stride` overflows int64 bounds
|
| 192 |
+
# but not uint64 bounds
|
| 193 |
+
dti = date_range(start="1677-09-22", end="2262-04-11", freq="D")
|
| 194 |
+
|
| 195 |
+
dti2 = date_range(start=dti[0], periods=len(dti), freq="D")
|
| 196 |
+
assert dti2.equals(dti)
|
| 197 |
+
|
| 198 |
+
dti3 = date_range(end=dti[-1], periods=len(dti), freq="D")
|
| 199 |
+
assert dti3.equals(dti)
|
| 200 |
+
|
| 201 |
+
def test_date_range_int64_overflow_non_recoverable(self):
|
| 202 |
+
# GH#24255
|
| 203 |
+
# case with start later than 1970-01-01, overflow int64 but not uint64
|
| 204 |
+
msg = "Cannot generate range with"
|
| 205 |
+
with pytest.raises(OutOfBoundsDatetime, match=msg):
|
| 206 |
+
date_range(start="1970-02-01", periods=106752 * 24, freq="H")
|
| 207 |
+
|
| 208 |
+
# case with end before 1970-01-01, overflow int64 but not uint64
|
| 209 |
+
with pytest.raises(OutOfBoundsDatetime, match=msg):
|
| 210 |
+
date_range(end="1969-11-14", periods=106752 * 24, freq="H")
|
| 211 |
+
|
| 212 |
+
@pytest.mark.slow
|
| 213 |
+
def test_date_range_int64_overflow_stride_endpoint_different_signs(self):
|
| 214 |
+
# cases where stride * periods overflow int64 and stride/endpoint
|
| 215 |
+
# have different signs
|
| 216 |
+
start = Timestamp("2262-02-23")
|
| 217 |
+
end = Timestamp("1969-11-14")
|
| 218 |
+
|
| 219 |
+
expected = date_range(start=start, end=end, freq="-1H")
|
| 220 |
+
assert expected[0] == start
|
| 221 |
+
assert expected[-1] == end
|
| 222 |
+
|
| 223 |
+
dti = date_range(end=end, periods=len(expected), freq="-1H")
|
| 224 |
+
tm.assert_index_equal(dti, expected)
|
| 225 |
+
|
| 226 |
+
start2 = Timestamp("1970-02-01")
|
| 227 |
+
end2 = Timestamp("1677-10-22")
|
| 228 |
+
|
| 229 |
+
expected2 = date_range(start=start2, end=end2, freq="-1H")
|
| 230 |
+
assert expected2[0] == start2
|
| 231 |
+
assert expected2[-1] == end2
|
| 232 |
+
|
| 233 |
+
dti2 = date_range(start=start2, periods=len(expected2), freq="-1H")
|
| 234 |
+
tm.assert_index_equal(dti2, expected2)
|
| 235 |
+
|
| 236 |
+
def test_date_range_out_of_bounds(self):
|
| 237 |
+
# GH#14187
|
| 238 |
+
msg = "Cannot generate range"
|
| 239 |
+
with pytest.raises(OutOfBoundsDatetime, match=msg):
|
| 240 |
+
date_range("2016-01-01", periods=100000, freq="D")
|
| 241 |
+
with pytest.raises(OutOfBoundsDatetime, match=msg):
|
| 242 |
+
date_range(end="1763-10-12", periods=100000, freq="D")
|
| 243 |
+
|
| 244 |
+
def test_date_range_gen_error(self):
|
| 245 |
+
rng = date_range("1/1/2000 00:00", "1/1/2000 00:18", freq="5min")
|
| 246 |
+
assert len(rng) == 4
|
| 247 |
+
|
| 248 |
+
@pytest.mark.parametrize("freq", ["AS", "YS"])
|
| 249 |
+
def test_begin_year_alias(self, freq):
|
| 250 |
+
# see gh-9313
|
| 251 |
+
rng = date_range("1/1/2013", "7/1/2017", freq=freq)
|
| 252 |
+
exp = DatetimeIndex(
|
| 253 |
+
["2013-01-01", "2014-01-01", "2015-01-01", "2016-01-01", "2017-01-01"],
|
| 254 |
+
freq=freq,
|
| 255 |
+
)
|
| 256 |
+
tm.assert_index_equal(rng, exp)
|
| 257 |
+
|
| 258 |
+
@pytest.mark.parametrize("freq", ["A", "Y"])
|
| 259 |
+
def test_end_year_alias(self, freq):
|
| 260 |
+
# see gh-9313
|
| 261 |
+
rng = date_range("1/1/2013", "7/1/2017", freq=freq)
|
| 262 |
+
exp = DatetimeIndex(
|
| 263 |
+
["2013-12-31", "2014-12-31", "2015-12-31", "2016-12-31"], freq=freq
|
| 264 |
+
)
|
| 265 |
+
tm.assert_index_equal(rng, exp)
|
| 266 |
+
|
| 267 |
+
@pytest.mark.parametrize("freq", ["BA", "BY"])
|
| 268 |
+
def test_business_end_year_alias(self, freq):
|
| 269 |
+
# see gh-9313
|
| 270 |
+
rng = date_range("1/1/2013", "7/1/2017", freq=freq)
|
| 271 |
+
exp = DatetimeIndex(
|
| 272 |
+
["2013-12-31", "2014-12-31", "2015-12-31", "2016-12-30"], freq=freq
|
| 273 |
+
)
|
| 274 |
+
tm.assert_index_equal(rng, exp)
|
| 275 |
+
|
| 276 |
+
def test_date_range_negative_freq(self):
|
| 277 |
+
# GH 11018
|
| 278 |
+
rng = date_range("2011-12-31", freq="-2A", periods=3)
|
| 279 |
+
exp = DatetimeIndex(["2011-12-31", "2009-12-31", "2007-12-31"], freq="-2A")
|
| 280 |
+
tm.assert_index_equal(rng, exp)
|
| 281 |
+
assert rng.freq == "-2A"
|
| 282 |
+
|
| 283 |
+
rng = date_range("2011-01-31", freq="-2M", periods=3)
|
| 284 |
+
exp = DatetimeIndex(["2011-01-31", "2010-11-30", "2010-09-30"], freq="-2M")
|
| 285 |
+
tm.assert_index_equal(rng, exp)
|
| 286 |
+
assert rng.freq == "-2M"
|
| 287 |
+
|
| 288 |
+
def test_date_range_bms_bug(self):
|
| 289 |
+
# #1645
|
| 290 |
+
rng = date_range("1/1/2000", periods=10, freq="BMS")
|
| 291 |
+
|
| 292 |
+
ex_first = Timestamp("2000-01-03")
|
| 293 |
+
assert rng[0] == ex_first
|
| 294 |
+
|
| 295 |
+
def test_date_range_normalize(self):
|
| 296 |
+
snap = datetime.today()
|
| 297 |
+
n = 50
|
| 298 |
+
|
| 299 |
+
rng = date_range(snap, periods=n, normalize=False, freq="2D")
|
| 300 |
+
|
| 301 |
+
offset = timedelta(2)
|
| 302 |
+
values = DatetimeIndex([snap + i * offset for i in range(n)], freq=offset)
|
| 303 |
+
|
| 304 |
+
tm.assert_index_equal(rng, values)
|
| 305 |
+
|
| 306 |
+
rng = date_range("1/1/2000 08:15", periods=n, normalize=False, freq="B")
|
| 307 |
+
the_time = time(8, 15)
|
| 308 |
+
for val in rng:
|
| 309 |
+
assert val.time() == the_time
|
| 310 |
+
|
| 311 |
+
def test_date_range_fy5252(self):
|
| 312 |
+
dr = date_range(
|
| 313 |
+
start="2013-01-01",
|
| 314 |
+
periods=2,
|
| 315 |
+
freq=offsets.FY5253(startingMonth=1, weekday=3, variation="nearest"),
|
| 316 |
+
)
|
| 317 |
+
assert dr[0] == Timestamp("2013-01-31")
|
| 318 |
+
assert dr[1] == Timestamp("2014-01-30")
|
| 319 |
+
|
| 320 |
+
def test_date_range_ambiguous_arguments(self):
|
| 321 |
+
# #2538
|
| 322 |
+
start = datetime(2011, 1, 1, 5, 3, 40)
|
| 323 |
+
end = datetime(2011, 1, 1, 8, 9, 40)
|
| 324 |
+
|
| 325 |
+
msg = (
|
| 326 |
+
"Of the four parameters: start, end, periods, and "
|
| 327 |
+
"freq, exactly three must be specified"
|
| 328 |
+
)
|
| 329 |
+
with pytest.raises(ValueError, match=msg):
|
| 330 |
+
date_range(start, end, periods=10, freq="s")
|
| 331 |
+
|
| 332 |
+
def test_date_range_convenience_periods(self):
|
| 333 |
+
# GH 20808
|
| 334 |
+
result = date_range("2018-04-24", "2018-04-27", periods=3)
|
| 335 |
+
expected = DatetimeIndex(
|
| 336 |
+
["2018-04-24 00:00:00", "2018-04-25 12:00:00", "2018-04-27 00:00:00"],
|
| 337 |
+
freq=None,
|
| 338 |
+
)
|
| 339 |
+
|
| 340 |
+
tm.assert_index_equal(result, expected)
|
| 341 |
+
|
| 342 |
+
# Test if spacing remains linear if tz changes to dst in range
|
| 343 |
+
result = date_range(
|
| 344 |
+
"2018-04-01 01:00:00",
|
| 345 |
+
"2018-04-01 04:00:00",
|
| 346 |
+
tz="Australia/Sydney",
|
| 347 |
+
periods=3,
|
| 348 |
+
)
|
| 349 |
+
expected = DatetimeIndex(
|
| 350 |
+
[
|
| 351 |
+
Timestamp("2018-04-01 01:00:00+1100", tz="Australia/Sydney"),
|
| 352 |
+
Timestamp("2018-04-01 02:00:00+1000", tz="Australia/Sydney"),
|
| 353 |
+
Timestamp("2018-04-01 04:00:00+1000", tz="Australia/Sydney"),
|
| 354 |
+
]
|
| 355 |
+
)
|
| 356 |
+
tm.assert_index_equal(result, expected)
|
| 357 |
+
|
| 358 |
+
@pytest.mark.parametrize(
|
| 359 |
+
"start,end,result_tz",
|
| 360 |
+
[
|
| 361 |
+
["20180101", "20180103", "US/Eastern"],
|
| 362 |
+
[datetime(2018, 1, 1), datetime(2018, 1, 3), "US/Eastern"],
|
| 363 |
+
[Timestamp("20180101"), Timestamp("20180103"), "US/Eastern"],
|
| 364 |
+
[
|
| 365 |
+
Timestamp("20180101", tz="US/Eastern"),
|
| 366 |
+
Timestamp("20180103", tz="US/Eastern"),
|
| 367 |
+
"US/Eastern",
|
| 368 |
+
],
|
| 369 |
+
[
|
| 370 |
+
Timestamp("20180101", tz="US/Eastern"),
|
| 371 |
+
Timestamp("20180103", tz="US/Eastern"),
|
| 372 |
+
None,
|
| 373 |
+
],
|
| 374 |
+
],
|
| 375 |
+
)
|
| 376 |
+
def test_date_range_linspacing_tz(self, start, end, result_tz):
|
| 377 |
+
# GH 20983
|
| 378 |
+
result = date_range(start, end, periods=3, tz=result_tz)
|
| 379 |
+
expected = date_range("20180101", periods=3, freq="D", tz="US/Eastern")
|
| 380 |
+
tm.assert_index_equal(result, expected)
|
| 381 |
+
|
| 382 |
+
def test_date_range_businesshour(self):
|
| 383 |
+
idx = DatetimeIndex(
|
| 384 |
+
[
|
| 385 |
+
"2014-07-04 09:00",
|
| 386 |
+
"2014-07-04 10:00",
|
| 387 |
+
"2014-07-04 11:00",
|
| 388 |
+
"2014-07-04 12:00",
|
| 389 |
+
"2014-07-04 13:00",
|
| 390 |
+
"2014-07-04 14:00",
|
| 391 |
+
"2014-07-04 15:00",
|
| 392 |
+
"2014-07-04 16:00",
|
| 393 |
+
],
|
| 394 |
+
freq="BH",
|
| 395 |
+
)
|
| 396 |
+
rng = date_range("2014-07-04 09:00", "2014-07-04 16:00", freq="BH")
|
| 397 |
+
tm.assert_index_equal(idx, rng)
|
| 398 |
+
|
| 399 |
+
idx = DatetimeIndex(["2014-07-04 16:00", "2014-07-07 09:00"], freq="BH")
|
| 400 |
+
rng = date_range("2014-07-04 16:00", "2014-07-07 09:00", freq="BH")
|
| 401 |
+
tm.assert_index_equal(idx, rng)
|
| 402 |
+
|
| 403 |
+
idx = DatetimeIndex(
|
| 404 |
+
[
|
| 405 |
+
"2014-07-04 09:00",
|
| 406 |
+
"2014-07-04 10:00",
|
| 407 |
+
"2014-07-04 11:00",
|
| 408 |
+
"2014-07-04 12:00",
|
| 409 |
+
"2014-07-04 13:00",
|
| 410 |
+
"2014-07-04 14:00",
|
| 411 |
+
"2014-07-04 15:00",
|
| 412 |
+
"2014-07-04 16:00",
|
| 413 |
+
"2014-07-07 09:00",
|
| 414 |
+
"2014-07-07 10:00",
|
| 415 |
+
"2014-07-07 11:00",
|
| 416 |
+
"2014-07-07 12:00",
|
| 417 |
+
"2014-07-07 13:00",
|
| 418 |
+
"2014-07-07 14:00",
|
| 419 |
+
"2014-07-07 15:00",
|
| 420 |
+
"2014-07-07 16:00",
|
| 421 |
+
"2014-07-08 09:00",
|
| 422 |
+
"2014-07-08 10:00",
|
| 423 |
+
"2014-07-08 11:00",
|
| 424 |
+
"2014-07-08 12:00",
|
| 425 |
+
"2014-07-08 13:00",
|
| 426 |
+
"2014-07-08 14:00",
|
| 427 |
+
"2014-07-08 15:00",
|
| 428 |
+
"2014-07-08 16:00",
|
| 429 |
+
],
|
| 430 |
+
freq="BH",
|
| 431 |
+
)
|
| 432 |
+
rng = date_range("2014-07-04 09:00", "2014-07-08 16:00", freq="BH")
|
| 433 |
+
tm.assert_index_equal(idx, rng)
|
| 434 |
+
|
| 435 |
+
def test_date_range_timedelta(self):
|
| 436 |
+
start = "2020-01-01"
|
| 437 |
+
end = "2020-01-11"
|
| 438 |
+
rng1 = date_range(start, end, freq="3D")
|
| 439 |
+
rng2 = date_range(start, end, freq=timedelta(days=3))
|
| 440 |
+
tm.assert_index_equal(rng1, rng2)
|
| 441 |
+
|
| 442 |
+
def test_range_misspecified(self):
|
| 443 |
+
# GH #1095
|
| 444 |
+
msg = (
|
| 445 |
+
"Of the four parameters: start, end, periods, and "
|
| 446 |
+
"freq, exactly three must be specified"
|
| 447 |
+
)
|
| 448 |
+
|
| 449 |
+
with pytest.raises(ValueError, match=msg):
|
| 450 |
+
date_range(start="1/1/2000")
|
| 451 |
+
|
| 452 |
+
with pytest.raises(ValueError, match=msg):
|
| 453 |
+
date_range(end="1/1/2000")
|
| 454 |
+
|
| 455 |
+
with pytest.raises(ValueError, match=msg):
|
| 456 |
+
date_range(periods=10)
|
| 457 |
+
|
| 458 |
+
with pytest.raises(ValueError, match=msg):
|
| 459 |
+
date_range(start="1/1/2000", freq="H")
|
| 460 |
+
|
| 461 |
+
with pytest.raises(ValueError, match=msg):
|
| 462 |
+
date_range(end="1/1/2000", freq="H")
|
| 463 |
+
|
| 464 |
+
with pytest.raises(ValueError, match=msg):
|
| 465 |
+
date_range(periods=10, freq="H")
|
| 466 |
+
|
| 467 |
+
with pytest.raises(ValueError, match=msg):
|
| 468 |
+
date_range()
|
| 469 |
+
|
| 470 |
+
def test_compat_replace(self):
|
| 471 |
+
# https://github.com/statsmodels/statsmodels/issues/3349
|
| 472 |
+
# replace should take ints/longs for compat
|
| 473 |
+
result = date_range(Timestamp("1960-04-01 00:00:00"), periods=76, freq="QS-JAN")
|
| 474 |
+
assert len(result) == 76
|
| 475 |
+
|
| 476 |
+
def test_catch_infinite_loop(self):
|
| 477 |
+
offset = offsets.DateOffset(minute=5)
|
| 478 |
+
# blow up, don't loop forever
|
| 479 |
+
msg = "Offset <DateOffset: minute=5> did not increment date"
|
| 480 |
+
with pytest.raises(ValueError, match=msg):
|
| 481 |
+
date_range(datetime(2011, 11, 11), datetime(2011, 11, 12), freq=offset)
|
| 482 |
+
|
| 483 |
+
@pytest.mark.parametrize("periods", (1, 2))
|
| 484 |
+
def test_wom_len(self, periods):
|
| 485 |
+
# https://github.com/pandas-dev/pandas/issues/20517
|
| 486 |
+
res = date_range(start="20110101", periods=periods, freq="WOM-1MON")
|
| 487 |
+
assert len(res) == periods
|
| 488 |
+
|
| 489 |
+
def test_construct_over_dst(self):
|
| 490 |
+
# GH 20854
|
| 491 |
+
pre_dst = Timestamp("2010-11-07 01:00:00").tz_localize(
|
| 492 |
+
"US/Pacific", ambiguous=True
|
| 493 |
+
)
|
| 494 |
+
pst_dst = Timestamp("2010-11-07 01:00:00").tz_localize(
|
| 495 |
+
"US/Pacific", ambiguous=False
|
| 496 |
+
)
|
| 497 |
+
expect_data = [
|
| 498 |
+
Timestamp("2010-11-07 00:00:00", tz="US/Pacific"),
|
| 499 |
+
pre_dst,
|
| 500 |
+
pst_dst,
|
| 501 |
+
]
|
| 502 |
+
expected = DatetimeIndex(expect_data, freq="H")
|
| 503 |
+
result = date_range(start="2010-11-7", periods=3, freq="H", tz="US/Pacific")
|
| 504 |
+
tm.assert_index_equal(result, expected)
|
| 505 |
+
|
| 506 |
+
def test_construct_with_different_start_end_string_format(self):
|
| 507 |
+
# GH 12064
|
| 508 |
+
result = date_range(
|
| 509 |
+
"2013-01-01 00:00:00+09:00", "2013/01/01 02:00:00+09:00", freq="H"
|
| 510 |
+
)
|
| 511 |
+
expected = DatetimeIndex(
|
| 512 |
+
[
|
| 513 |
+
Timestamp("2013-01-01 00:00:00+09:00"),
|
| 514 |
+
Timestamp("2013-01-01 01:00:00+09:00"),
|
| 515 |
+
Timestamp("2013-01-01 02:00:00+09:00"),
|
| 516 |
+
],
|
| 517 |
+
freq="H",
|
| 518 |
+
)
|
| 519 |
+
tm.assert_index_equal(result, expected)
|
| 520 |
+
|
| 521 |
+
def test_error_with_zero_monthends(self):
|
| 522 |
+
msg = r"Offset <0 \* MonthEnds> did not increment date"
|
| 523 |
+
with pytest.raises(ValueError, match=msg):
|
| 524 |
+
date_range("1/1/2000", "1/1/2001", freq=MonthEnd(0))
|
| 525 |
+
|
| 526 |
+
def test_range_bug(self):
|
| 527 |
+
# GH #770
|
| 528 |
+
offset = DateOffset(months=3)
|
| 529 |
+
result = date_range("2011-1-1", "2012-1-31", freq=offset)
|
| 530 |
+
|
| 531 |
+
start = datetime(2011, 1, 1)
|
| 532 |
+
expected = DatetimeIndex([start + i * offset for i in range(5)], freq=offset)
|
| 533 |
+
tm.assert_index_equal(result, expected)
|
| 534 |
+
|
| 535 |
+
def test_range_tz_pytz(self):
|
| 536 |
+
# see gh-2906
|
| 537 |
+
tz = timezone("US/Eastern")
|
| 538 |
+
start = tz.localize(datetime(2011, 1, 1))
|
| 539 |
+
end = tz.localize(datetime(2011, 1, 3))
|
| 540 |
+
|
| 541 |
+
dr = date_range(start=start, periods=3)
|
| 542 |
+
assert dr.tz.zone == tz.zone
|
| 543 |
+
assert dr[0] == start
|
| 544 |
+
assert dr[2] == end
|
| 545 |
+
|
| 546 |
+
dr = date_range(end=end, periods=3)
|
| 547 |
+
assert dr.tz.zone == tz.zone
|
| 548 |
+
assert dr[0] == start
|
| 549 |
+
assert dr[2] == end
|
| 550 |
+
|
| 551 |
+
dr = date_range(start=start, end=end)
|
| 552 |
+
assert dr.tz.zone == tz.zone
|
| 553 |
+
assert dr[0] == start
|
| 554 |
+
assert dr[2] == end
|
| 555 |
+
|
| 556 |
+
@pytest.mark.parametrize(
|
| 557 |
+
"start, end",
|
| 558 |
+
[
|
| 559 |
+
[
|
| 560 |
+
Timestamp(datetime(2014, 3, 6), tz="US/Eastern"),
|
| 561 |
+
Timestamp(datetime(2014, 3, 12), tz="US/Eastern"),
|
| 562 |
+
],
|
| 563 |
+
[
|
| 564 |
+
Timestamp(datetime(2013, 11, 1), tz="US/Eastern"),
|
| 565 |
+
Timestamp(datetime(2013, 11, 6), tz="US/Eastern"),
|
| 566 |
+
],
|
| 567 |
+
],
|
| 568 |
+
)
|
| 569 |
+
def test_range_tz_dst_straddle_pytz(self, start, end):
|
| 570 |
+
dr = date_range(start, end, freq="D")
|
| 571 |
+
assert dr[0] == start
|
| 572 |
+
assert dr[-1] == end
|
| 573 |
+
assert np.all(dr.hour == 0)
|
| 574 |
+
|
| 575 |
+
dr = date_range(start, end, freq="D", tz="US/Eastern")
|
| 576 |
+
assert dr[0] == start
|
| 577 |
+
assert dr[-1] == end
|
| 578 |
+
assert np.all(dr.hour == 0)
|
| 579 |
+
|
| 580 |
+
dr = date_range(
|
| 581 |
+
start.replace(tzinfo=None),
|
| 582 |
+
end.replace(tzinfo=None),
|
| 583 |
+
freq="D",
|
| 584 |
+
tz="US/Eastern",
|
| 585 |
+
)
|
| 586 |
+
assert dr[0] == start
|
| 587 |
+
assert dr[-1] == end
|
| 588 |
+
assert np.all(dr.hour == 0)
|
| 589 |
+
|
| 590 |
+
def test_range_tz_dateutil(self):
|
| 591 |
+
# see gh-2906
|
| 592 |
+
|
| 593 |
+
# Use maybe_get_tz to fix filename in tz under dateutil.
|
| 594 |
+
from pandas._libs.tslibs.timezones import maybe_get_tz
|
| 595 |
+
|
| 596 |
+
tz = lambda x: maybe_get_tz("dateutil/" + x)
|
| 597 |
+
|
| 598 |
+
start = datetime(2011, 1, 1, tzinfo=tz("US/Eastern"))
|
| 599 |
+
end = datetime(2011, 1, 3, tzinfo=tz("US/Eastern"))
|
| 600 |
+
|
| 601 |
+
dr = date_range(start=start, periods=3)
|
| 602 |
+
assert dr.tz == tz("US/Eastern")
|
| 603 |
+
assert dr[0] == start
|
| 604 |
+
assert dr[2] == end
|
| 605 |
+
|
| 606 |
+
dr = date_range(end=end, periods=3)
|
| 607 |
+
assert dr.tz == tz("US/Eastern")
|
| 608 |
+
assert dr[0] == start
|
| 609 |
+
assert dr[2] == end
|
| 610 |
+
|
| 611 |
+
dr = date_range(start=start, end=end)
|
| 612 |
+
assert dr.tz == tz("US/Eastern")
|
| 613 |
+
assert dr[0] == start
|
| 614 |
+
assert dr[2] == end
|
| 615 |
+
|
| 616 |
+
@pytest.mark.parametrize("freq", ["1D", "3D", "2M", "7W", "3H", "A"])
|
| 617 |
+
def test_range_closed(self, freq, inclusive_endpoints_fixture):
|
| 618 |
+
begin = datetime(2011, 1, 1)
|
| 619 |
+
end = datetime(2014, 1, 1)
|
| 620 |
+
|
| 621 |
+
result_range = date_range(
|
| 622 |
+
begin, end, inclusive=inclusive_endpoints_fixture, freq=freq
|
| 623 |
+
)
|
| 624 |
+
both_range = date_range(begin, end, inclusive="both", freq=freq)
|
| 625 |
+
expected_range = _get_expected_range(
|
| 626 |
+
begin, end, both_range, inclusive_endpoints_fixture
|
| 627 |
+
)
|
| 628 |
+
|
| 629 |
+
tm.assert_index_equal(expected_range, result_range)
|
| 630 |
+
|
| 631 |
+
@pytest.mark.parametrize("freq", ["1D", "3D", "2M", "7W", "3H", "A"])
|
| 632 |
+
def test_range_closed_with_tz_aware_start_end(
|
| 633 |
+
self, freq, inclusive_endpoints_fixture
|
| 634 |
+
):
|
| 635 |
+
# GH12409, GH12684
|
| 636 |
+
begin = Timestamp("2011/1/1", tz="US/Eastern")
|
| 637 |
+
end = Timestamp("2014/1/1", tz="US/Eastern")
|
| 638 |
+
|
| 639 |
+
result_range = date_range(
|
| 640 |
+
begin, end, inclusive=inclusive_endpoints_fixture, freq=freq
|
| 641 |
+
)
|
| 642 |
+
both_range = date_range(begin, end, inclusive="both", freq=freq)
|
| 643 |
+
expected_range = _get_expected_range(
|
| 644 |
+
begin,
|
| 645 |
+
end,
|
| 646 |
+
both_range,
|
| 647 |
+
inclusive_endpoints_fixture,
|
| 648 |
+
)
|
| 649 |
+
|
| 650 |
+
tm.assert_index_equal(expected_range, result_range)
|
| 651 |
+
|
| 652 |
+
@pytest.mark.parametrize("freq", ["1D", "3D", "2M", "7W", "3H", "A"])
|
| 653 |
+
def test_range_with_tz_closed_with_tz_aware_start_end(
|
| 654 |
+
self, freq, inclusive_endpoints_fixture
|
| 655 |
+
):
|
| 656 |
+
begin = Timestamp("2011/1/1")
|
| 657 |
+
end = Timestamp("2014/1/1")
|
| 658 |
+
begintz = Timestamp("2011/1/1", tz="US/Eastern")
|
| 659 |
+
endtz = Timestamp("2014/1/1", tz="US/Eastern")
|
| 660 |
+
|
| 661 |
+
result_range = date_range(
|
| 662 |
+
begin,
|
| 663 |
+
end,
|
| 664 |
+
inclusive=inclusive_endpoints_fixture,
|
| 665 |
+
freq=freq,
|
| 666 |
+
tz="US/Eastern",
|
| 667 |
+
)
|
| 668 |
+
both_range = date_range(
|
| 669 |
+
begin, end, inclusive="both", freq=freq, tz="US/Eastern"
|
| 670 |
+
)
|
| 671 |
+
expected_range = _get_expected_range(
|
| 672 |
+
begintz,
|
| 673 |
+
endtz,
|
| 674 |
+
both_range,
|
| 675 |
+
inclusive_endpoints_fixture,
|
| 676 |
+
)
|
| 677 |
+
|
| 678 |
+
tm.assert_index_equal(expected_range, result_range)
|
| 679 |
+
|
| 680 |
+
def test_range_closed_boundary(self, inclusive_endpoints_fixture):
|
| 681 |
+
# GH#11804
|
| 682 |
+
right_boundary = date_range(
|
| 683 |
+
"2015-09-12",
|
| 684 |
+
"2015-12-01",
|
| 685 |
+
freq="QS-MAR",
|
| 686 |
+
inclusive=inclusive_endpoints_fixture,
|
| 687 |
+
)
|
| 688 |
+
left_boundary = date_range(
|
| 689 |
+
"2015-09-01",
|
| 690 |
+
"2015-09-12",
|
| 691 |
+
freq="QS-MAR",
|
| 692 |
+
inclusive=inclusive_endpoints_fixture,
|
| 693 |
+
)
|
| 694 |
+
both_boundary = date_range(
|
| 695 |
+
"2015-09-01",
|
| 696 |
+
"2015-12-01",
|
| 697 |
+
freq="QS-MAR",
|
| 698 |
+
inclusive=inclusive_endpoints_fixture,
|
| 699 |
+
)
|
| 700 |
+
neither_boundary = date_range(
|
| 701 |
+
"2015-09-11",
|
| 702 |
+
"2015-09-12",
|
| 703 |
+
freq="QS-MAR",
|
| 704 |
+
inclusive=inclusive_endpoints_fixture,
|
| 705 |
+
)
|
| 706 |
+
|
| 707 |
+
expected_right = both_boundary
|
| 708 |
+
expected_left = both_boundary
|
| 709 |
+
expected_both = both_boundary
|
| 710 |
+
|
| 711 |
+
if inclusive_endpoints_fixture == "right":
|
| 712 |
+
expected_left = both_boundary[1:]
|
| 713 |
+
elif inclusive_endpoints_fixture == "left":
|
| 714 |
+
expected_right = both_boundary[:-1]
|
| 715 |
+
elif inclusive_endpoints_fixture == "both":
|
| 716 |
+
expected_right = both_boundary[1:]
|
| 717 |
+
expected_left = both_boundary[:-1]
|
| 718 |
+
|
| 719 |
+
expected_neither = both_boundary[1:-1]
|
| 720 |
+
|
| 721 |
+
tm.assert_index_equal(right_boundary, expected_right)
|
| 722 |
+
tm.assert_index_equal(left_boundary, expected_left)
|
| 723 |
+
tm.assert_index_equal(both_boundary, expected_both)
|
| 724 |
+
tm.assert_index_equal(neither_boundary, expected_neither)
|
| 725 |
+
|
| 726 |
+
def test_years_only(self):
|
| 727 |
+
# GH 6961
|
| 728 |
+
dr = date_range("2014", "2015", freq="M")
|
| 729 |
+
assert dr[0] == datetime(2014, 1, 31)
|
| 730 |
+
assert dr[-1] == datetime(2014, 12, 31)
|
| 731 |
+
|
| 732 |
+
def test_freq_divides_end_in_nanos(self):
|
| 733 |
+
# GH 10885
|
| 734 |
+
result_1 = date_range("2005-01-12 10:00", "2005-01-12 16:00", freq="345min")
|
| 735 |
+
result_2 = date_range("2005-01-13 10:00", "2005-01-13 16:00", freq="345min")
|
| 736 |
+
expected_1 = DatetimeIndex(
|
| 737 |
+
["2005-01-12 10:00:00", "2005-01-12 15:45:00"],
|
| 738 |
+
dtype="datetime64[ns]",
|
| 739 |
+
freq="345T",
|
| 740 |
+
tz=None,
|
| 741 |
+
)
|
| 742 |
+
expected_2 = DatetimeIndex(
|
| 743 |
+
["2005-01-13 10:00:00", "2005-01-13 15:45:00"],
|
| 744 |
+
dtype="datetime64[ns]",
|
| 745 |
+
freq="345T",
|
| 746 |
+
tz=None,
|
| 747 |
+
)
|
| 748 |
+
tm.assert_index_equal(result_1, expected_1)
|
| 749 |
+
tm.assert_index_equal(result_2, expected_2)
|
| 750 |
+
|
| 751 |
+
def test_cached_range_bug(self):
|
| 752 |
+
rng = date_range("2010-09-01 05:00:00", periods=50, freq=DateOffset(hours=6))
|
| 753 |
+
assert len(rng) == 50
|
| 754 |
+
assert rng[0] == datetime(2010, 9, 1, 5)
|
| 755 |
+
|
| 756 |
+
def test_timezone_comparison_bug(self):
|
| 757 |
+
# smoke test
|
| 758 |
+
start = Timestamp("20130220 10:00", tz="US/Eastern")
|
| 759 |
+
result = date_range(start, periods=2, tz="US/Eastern")
|
| 760 |
+
assert len(result) == 2
|
| 761 |
+
|
| 762 |
+
def test_timezone_comparison_assert(self):
|
| 763 |
+
start = Timestamp("20130220 10:00", tz="US/Eastern")
|
| 764 |
+
msg = "Inferred time zone not equal to passed time zone"
|
| 765 |
+
with pytest.raises(AssertionError, match=msg):
|
| 766 |
+
date_range(start, periods=2, tz="Europe/Berlin")
|
| 767 |
+
|
| 768 |
+
def test_negative_non_tick_frequency_descending_dates(self, tz_aware_fixture):
|
| 769 |
+
# GH 23270
|
| 770 |
+
tz = tz_aware_fixture
|
| 771 |
+
result = date_range(start="2011-06-01", end="2011-01-01", freq="-1MS", tz=tz)
|
| 772 |
+
expected = date_range(end="2011-06-01", start="2011-01-01", freq="1MS", tz=tz)[
|
| 773 |
+
::-1
|
| 774 |
+
]
|
| 775 |
+
tm.assert_index_equal(result, expected)
|
| 776 |
+
|
| 777 |
+
def test_range_where_start_equal_end(self, inclusive_endpoints_fixture):
|
| 778 |
+
# GH 43394
|
| 779 |
+
start = "2021-09-02"
|
| 780 |
+
end = "2021-09-02"
|
| 781 |
+
result = date_range(
|
| 782 |
+
start=start, end=end, freq="D", inclusive=inclusive_endpoints_fixture
|
| 783 |
+
)
|
| 784 |
+
|
| 785 |
+
both_range = date_range(start=start, end=end, freq="D", inclusive="both")
|
| 786 |
+
if inclusive_endpoints_fixture == "neither":
|
| 787 |
+
expected = both_range[1:-1]
|
| 788 |
+
elif inclusive_endpoints_fixture in ("left", "right", "both"):
|
| 789 |
+
expected = both_range[:]
|
| 790 |
+
|
| 791 |
+
tm.assert_index_equal(result, expected)
|
| 792 |
+
|
| 793 |
+
|
| 794 |
+
class TestDateRangeTZ:
|
| 795 |
+
"""Tests for date_range with timezones"""
|
| 796 |
+
|
| 797 |
+
def test_hongkong_tz_convert(self):
|
| 798 |
+
# GH#1673 smoke test
|
| 799 |
+
dr = date_range("2012-01-01", "2012-01-10", freq="D", tz="Hongkong")
|
| 800 |
+
|
| 801 |
+
# it works!
|
| 802 |
+
dr.hour
|
| 803 |
+
|
| 804 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 805 |
+
def test_date_range_span_dst_transition(self, tzstr):
|
| 806 |
+
# GH#1778
|
| 807 |
+
|
| 808 |
+
# Standard -> Daylight Savings Time
|
| 809 |
+
dr = date_range("03/06/2012 00:00", periods=200, freq="W-FRI", tz="US/Eastern")
|
| 810 |
+
|
| 811 |
+
assert (dr.hour == 0).all()
|
| 812 |
+
|
| 813 |
+
dr = date_range("2012-11-02", periods=10, tz=tzstr)
|
| 814 |
+
result = dr.hour
|
| 815 |
+
expected = pd.Index([0] * 10, dtype="int32")
|
| 816 |
+
tm.assert_index_equal(result, expected)
|
| 817 |
+
|
| 818 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 819 |
+
def test_date_range_timezone_str_argument(self, tzstr):
|
| 820 |
+
tz = timezones.maybe_get_tz(tzstr)
|
| 821 |
+
result = date_range("1/1/2000", periods=10, tz=tzstr)
|
| 822 |
+
expected = date_range("1/1/2000", periods=10, tz=tz)
|
| 823 |
+
|
| 824 |
+
tm.assert_index_equal(result, expected)
|
| 825 |
+
|
| 826 |
+
def test_date_range_with_fixedoffset_noname(self):
|
| 827 |
+
from pandas.tests.indexes.datetimes.test_timezones import fixed_off_no_name
|
| 828 |
+
|
| 829 |
+
off = fixed_off_no_name
|
| 830 |
+
start = datetime(2012, 3, 11, 5, 0, 0, tzinfo=off)
|
| 831 |
+
end = datetime(2012, 6, 11, 5, 0, 0, tzinfo=off)
|
| 832 |
+
rng = date_range(start=start, end=end)
|
| 833 |
+
assert off == rng.tz
|
| 834 |
+
|
| 835 |
+
idx = pd.Index([start, end])
|
| 836 |
+
assert off == idx.tz
|
| 837 |
+
|
| 838 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 839 |
+
def test_date_range_with_tz(self, tzstr):
|
| 840 |
+
stamp = Timestamp("3/11/2012 05:00", tz=tzstr)
|
| 841 |
+
assert stamp.hour == 5
|
| 842 |
+
|
| 843 |
+
rng = date_range("3/11/2012 04:00", periods=10, freq="H", tz=tzstr)
|
| 844 |
+
|
| 845 |
+
assert stamp == rng[1]
|
| 846 |
+
|
| 847 |
+
|
| 848 |
+
class TestGenRangeGeneration:
|
| 849 |
+
def test_generate(self):
|
| 850 |
+
rng1 = list(generate_range(START, END, periods=None, offset=BDay(), unit="ns"))
|
| 851 |
+
rng2 = list(generate_range(START, END, periods=None, offset="B", unit="ns"))
|
| 852 |
+
assert rng1 == rng2
|
| 853 |
+
|
| 854 |
+
def test_generate_cday(self):
|
| 855 |
+
rng1 = list(generate_range(START, END, periods=None, offset=CDay(), unit="ns"))
|
| 856 |
+
rng2 = list(generate_range(START, END, periods=None, offset="C", unit="ns"))
|
| 857 |
+
assert rng1 == rng2
|
| 858 |
+
|
| 859 |
+
def test_1(self):
|
| 860 |
+
rng = list(
|
| 861 |
+
generate_range(
|
| 862 |
+
start=datetime(2009, 3, 25),
|
| 863 |
+
end=None,
|
| 864 |
+
periods=2,
|
| 865 |
+
offset=BDay(),
|
| 866 |
+
unit="ns",
|
| 867 |
+
)
|
| 868 |
+
)
|
| 869 |
+
expected = [datetime(2009, 3, 25), datetime(2009, 3, 26)]
|
| 870 |
+
assert rng == expected
|
| 871 |
+
|
| 872 |
+
def test_2(self):
|
| 873 |
+
rng = list(
|
| 874 |
+
generate_range(
|
| 875 |
+
start=datetime(2008, 1, 1),
|
| 876 |
+
end=datetime(2008, 1, 3),
|
| 877 |
+
periods=None,
|
| 878 |
+
offset=BDay(),
|
| 879 |
+
unit="ns",
|
| 880 |
+
)
|
| 881 |
+
)
|
| 882 |
+
expected = [datetime(2008, 1, 1), datetime(2008, 1, 2), datetime(2008, 1, 3)]
|
| 883 |
+
assert rng == expected
|
| 884 |
+
|
| 885 |
+
def test_3(self):
|
| 886 |
+
rng = list(
|
| 887 |
+
generate_range(
|
| 888 |
+
start=datetime(2008, 1, 5),
|
| 889 |
+
end=datetime(2008, 1, 6),
|
| 890 |
+
periods=None,
|
| 891 |
+
offset=BDay(),
|
| 892 |
+
unit="ns",
|
| 893 |
+
)
|
| 894 |
+
)
|
| 895 |
+
expected = []
|
| 896 |
+
assert rng == expected
|
| 897 |
+
|
| 898 |
+
def test_precision_finer_than_offset(self):
|
| 899 |
+
# GH#9907
|
| 900 |
+
result1 = date_range(
|
| 901 |
+
start="2015-04-15 00:00:03", end="2016-04-22 00:00:00", freq="Q"
|
| 902 |
+
)
|
| 903 |
+
result2 = date_range(
|
| 904 |
+
start="2015-04-15 00:00:03", end="2015-06-22 00:00:04", freq="W"
|
| 905 |
+
)
|
| 906 |
+
expected1_list = [
|
| 907 |
+
"2015-06-30 00:00:03",
|
| 908 |
+
"2015-09-30 00:00:03",
|
| 909 |
+
"2015-12-31 00:00:03",
|
| 910 |
+
"2016-03-31 00:00:03",
|
| 911 |
+
]
|
| 912 |
+
expected2_list = [
|
| 913 |
+
"2015-04-19 00:00:03",
|
| 914 |
+
"2015-04-26 00:00:03",
|
| 915 |
+
"2015-05-03 00:00:03",
|
| 916 |
+
"2015-05-10 00:00:03",
|
| 917 |
+
"2015-05-17 00:00:03",
|
| 918 |
+
"2015-05-24 00:00:03",
|
| 919 |
+
"2015-05-31 00:00:03",
|
| 920 |
+
"2015-06-07 00:00:03",
|
| 921 |
+
"2015-06-14 00:00:03",
|
| 922 |
+
"2015-06-21 00:00:03",
|
| 923 |
+
]
|
| 924 |
+
expected1 = DatetimeIndex(
|
| 925 |
+
expected1_list, dtype="datetime64[ns]", freq="Q-DEC", tz=None
|
| 926 |
+
)
|
| 927 |
+
expected2 = DatetimeIndex(
|
| 928 |
+
expected2_list, dtype="datetime64[ns]", freq="W-SUN", tz=None
|
| 929 |
+
)
|
| 930 |
+
tm.assert_index_equal(result1, expected1)
|
| 931 |
+
tm.assert_index_equal(result2, expected2)
|
| 932 |
+
|
| 933 |
+
dt1, dt2 = "2017-01-01", "2017-01-01"
|
| 934 |
+
tz1, tz2 = "US/Eastern", "Europe/London"
|
| 935 |
+
|
| 936 |
+
@pytest.mark.parametrize(
|
| 937 |
+
"start,end",
|
| 938 |
+
[
|
| 939 |
+
(Timestamp(dt1, tz=tz1), Timestamp(dt2)),
|
| 940 |
+
(Timestamp(dt1), Timestamp(dt2, tz=tz2)),
|
| 941 |
+
(Timestamp(dt1, tz=tz1), Timestamp(dt2, tz=tz2)),
|
| 942 |
+
(Timestamp(dt1, tz=tz2), Timestamp(dt2, tz=tz1)),
|
| 943 |
+
],
|
| 944 |
+
)
|
| 945 |
+
def test_mismatching_tz_raises_err(self, start, end):
|
| 946 |
+
# issue 18488
|
| 947 |
+
msg = "Start and end cannot both be tz-aware with different timezones"
|
| 948 |
+
with pytest.raises(TypeError, match=msg):
|
| 949 |
+
date_range(start, end)
|
| 950 |
+
with pytest.raises(TypeError, match=msg):
|
| 951 |
+
date_range(start, end, freq=BDay())
|
| 952 |
+
|
| 953 |
+
|
| 954 |
+
class TestBusinessDateRange:
|
| 955 |
+
def test_constructor(self):
|
| 956 |
+
bdate_range(START, END, freq=BDay())
|
| 957 |
+
bdate_range(START, periods=20, freq=BDay())
|
| 958 |
+
bdate_range(end=START, periods=20, freq=BDay())
|
| 959 |
+
|
| 960 |
+
msg = "periods must be a number, got B"
|
| 961 |
+
with pytest.raises(TypeError, match=msg):
|
| 962 |
+
date_range("2011-1-1", "2012-1-1", "B")
|
| 963 |
+
|
| 964 |
+
with pytest.raises(TypeError, match=msg):
|
| 965 |
+
bdate_range("2011-1-1", "2012-1-1", "B")
|
| 966 |
+
|
| 967 |
+
msg = "freq must be specified for bdate_range; use date_range instead"
|
| 968 |
+
with pytest.raises(TypeError, match=msg):
|
| 969 |
+
bdate_range(START, END, periods=10, freq=None)
|
| 970 |
+
|
| 971 |
+
def test_misc(self):
|
| 972 |
+
end = datetime(2009, 5, 13)
|
| 973 |
+
dr = bdate_range(end=end, periods=20)
|
| 974 |
+
firstDate = end - 19 * BDay()
|
| 975 |
+
|
| 976 |
+
assert len(dr) == 20
|
| 977 |
+
assert dr[0] == firstDate
|
| 978 |
+
assert dr[-1] == end
|
| 979 |
+
|
| 980 |
+
def test_date_parse_failure(self):
|
| 981 |
+
badly_formed_date = "2007/100/1"
|
| 982 |
+
|
| 983 |
+
msg = "Unknown datetime string format, unable to parse: 2007/100/1"
|
| 984 |
+
with pytest.raises(ValueError, match=msg):
|
| 985 |
+
Timestamp(badly_formed_date)
|
| 986 |
+
|
| 987 |
+
with pytest.raises(ValueError, match=msg):
|
| 988 |
+
bdate_range(start=badly_formed_date, periods=10)
|
| 989 |
+
|
| 990 |
+
with pytest.raises(ValueError, match=msg):
|
| 991 |
+
bdate_range(end=badly_formed_date, periods=10)
|
| 992 |
+
|
| 993 |
+
with pytest.raises(ValueError, match=msg):
|
| 994 |
+
bdate_range(badly_formed_date, badly_formed_date)
|
| 995 |
+
|
| 996 |
+
def test_daterange_bug_456(self):
|
| 997 |
+
# GH #456
|
| 998 |
+
rng1 = bdate_range("12/5/2011", "12/5/2011")
|
| 999 |
+
rng2 = bdate_range("12/2/2011", "12/5/2011")
|
| 1000 |
+
assert rng2._data.freq == BDay()
|
| 1001 |
+
|
| 1002 |
+
result = rng1.union(rng2)
|
| 1003 |
+
assert isinstance(result, DatetimeIndex)
|
| 1004 |
+
|
| 1005 |
+
@pytest.mark.parametrize("inclusive", ["left", "right", "neither", "both"])
|
| 1006 |
+
def test_bdays_and_open_boundaries(self, inclusive):
|
| 1007 |
+
# GH 6673
|
| 1008 |
+
start = "2018-07-21" # Saturday
|
| 1009 |
+
end = "2018-07-29" # Sunday
|
| 1010 |
+
result = date_range(start, end, freq="B", inclusive=inclusive)
|
| 1011 |
+
|
| 1012 |
+
bday_start = "2018-07-23" # Monday
|
| 1013 |
+
bday_end = "2018-07-27" # Friday
|
| 1014 |
+
expected = date_range(bday_start, bday_end, freq="D")
|
| 1015 |
+
tm.assert_index_equal(result, expected)
|
| 1016 |
+
# Note: we do _not_ expect the freqs to match here
|
| 1017 |
+
|
| 1018 |
+
def test_bday_near_overflow(self):
|
| 1019 |
+
# GH#24252 avoid doing unnecessary addition that _would_ overflow
|
| 1020 |
+
start = Timestamp.max.floor("D").to_pydatetime()
|
| 1021 |
+
rng = date_range(start, end=None, periods=1, freq="B")
|
| 1022 |
+
expected = DatetimeIndex([start], freq="B")
|
| 1023 |
+
tm.assert_index_equal(rng, expected)
|
| 1024 |
+
|
| 1025 |
+
def test_bday_overflow_error(self):
|
| 1026 |
+
# GH#24252 check that we get OutOfBoundsDatetime and not OverflowError
|
| 1027 |
+
msg = "Out of bounds nanosecond timestamp"
|
| 1028 |
+
start = Timestamp.max.floor("D").to_pydatetime()
|
| 1029 |
+
with pytest.raises(OutOfBoundsDatetime, match=msg):
|
| 1030 |
+
date_range(start, periods=2, freq="B")
|
| 1031 |
+
|
| 1032 |
+
|
| 1033 |
+
class TestCustomDateRange:
|
| 1034 |
+
def test_constructor(self):
|
| 1035 |
+
bdate_range(START, END, freq=CDay())
|
| 1036 |
+
bdate_range(START, periods=20, freq=CDay())
|
| 1037 |
+
bdate_range(end=START, periods=20, freq=CDay())
|
| 1038 |
+
|
| 1039 |
+
msg = "periods must be a number, got C"
|
| 1040 |
+
with pytest.raises(TypeError, match=msg):
|
| 1041 |
+
date_range("2011-1-1", "2012-1-1", "C")
|
| 1042 |
+
|
| 1043 |
+
with pytest.raises(TypeError, match=msg):
|
| 1044 |
+
bdate_range("2011-1-1", "2012-1-1", "C")
|
| 1045 |
+
|
| 1046 |
+
def test_misc(self):
|
| 1047 |
+
end = datetime(2009, 5, 13)
|
| 1048 |
+
dr = bdate_range(end=end, periods=20, freq="C")
|
| 1049 |
+
firstDate = end - 19 * CDay()
|
| 1050 |
+
|
| 1051 |
+
assert len(dr) == 20
|
| 1052 |
+
assert dr[0] == firstDate
|
| 1053 |
+
assert dr[-1] == end
|
| 1054 |
+
|
| 1055 |
+
def test_daterange_bug_456(self):
|
| 1056 |
+
# GH #456
|
| 1057 |
+
rng1 = bdate_range("12/5/2011", "12/5/2011", freq="C")
|
| 1058 |
+
rng2 = bdate_range("12/2/2011", "12/5/2011", freq="C")
|
| 1059 |
+
assert rng2._data.freq == CDay()
|
| 1060 |
+
|
| 1061 |
+
result = rng1.union(rng2)
|
| 1062 |
+
assert isinstance(result, DatetimeIndex)
|
| 1063 |
+
|
| 1064 |
+
def test_cdaterange(self):
|
| 1065 |
+
result = bdate_range("2013-05-01", periods=3, freq="C")
|
| 1066 |
+
expected = DatetimeIndex(["2013-05-01", "2013-05-02", "2013-05-03"], freq="C")
|
| 1067 |
+
tm.assert_index_equal(result, expected)
|
| 1068 |
+
assert result.freq == expected.freq
|
| 1069 |
+
|
| 1070 |
+
def test_cdaterange_weekmask(self):
|
| 1071 |
+
result = bdate_range(
|
| 1072 |
+
"2013-05-01", periods=3, freq="C", weekmask="Sun Mon Tue Wed Thu"
|
| 1073 |
+
)
|
| 1074 |
+
expected = DatetimeIndex(
|
| 1075 |
+
["2013-05-01", "2013-05-02", "2013-05-05"], freq=result.freq
|
| 1076 |
+
)
|
| 1077 |
+
tm.assert_index_equal(result, expected)
|
| 1078 |
+
assert result.freq == expected.freq
|
| 1079 |
+
|
| 1080 |
+
# raise with non-custom freq
|
| 1081 |
+
msg = (
|
| 1082 |
+
"a custom frequency string is required when holidays or "
|
| 1083 |
+
"weekmask are passed, got frequency B"
|
| 1084 |
+
)
|
| 1085 |
+
with pytest.raises(ValueError, match=msg):
|
| 1086 |
+
bdate_range("2013-05-01", periods=3, weekmask="Sun Mon Tue Wed Thu")
|
| 1087 |
+
|
| 1088 |
+
def test_cdaterange_holidays(self):
|
| 1089 |
+
result = bdate_range("2013-05-01", periods=3, freq="C", holidays=["2013-05-01"])
|
| 1090 |
+
expected = DatetimeIndex(
|
| 1091 |
+
["2013-05-02", "2013-05-03", "2013-05-06"], freq=result.freq
|
| 1092 |
+
)
|
| 1093 |
+
tm.assert_index_equal(result, expected)
|
| 1094 |
+
assert result.freq == expected.freq
|
| 1095 |
+
|
| 1096 |
+
# raise with non-custom freq
|
| 1097 |
+
msg = (
|
| 1098 |
+
"a custom frequency string is required when holidays or "
|
| 1099 |
+
"weekmask are passed, got frequency B"
|
| 1100 |
+
)
|
| 1101 |
+
with pytest.raises(ValueError, match=msg):
|
| 1102 |
+
bdate_range("2013-05-01", periods=3, holidays=["2013-05-01"])
|
| 1103 |
+
|
| 1104 |
+
def test_cdaterange_weekmask_and_holidays(self):
|
| 1105 |
+
result = bdate_range(
|
| 1106 |
+
"2013-05-01",
|
| 1107 |
+
periods=3,
|
| 1108 |
+
freq="C",
|
| 1109 |
+
weekmask="Sun Mon Tue Wed Thu",
|
| 1110 |
+
holidays=["2013-05-01"],
|
| 1111 |
+
)
|
| 1112 |
+
expected = DatetimeIndex(
|
| 1113 |
+
["2013-05-02", "2013-05-05", "2013-05-06"], freq=result.freq
|
| 1114 |
+
)
|
| 1115 |
+
tm.assert_index_equal(result, expected)
|
| 1116 |
+
assert result.freq == expected.freq
|
| 1117 |
+
|
| 1118 |
+
# raise with non-custom freq
|
| 1119 |
+
msg = (
|
| 1120 |
+
"a custom frequency string is required when holidays or "
|
| 1121 |
+
"weekmask are passed, got frequency B"
|
| 1122 |
+
)
|
| 1123 |
+
with pytest.raises(ValueError, match=msg):
|
| 1124 |
+
bdate_range(
|
| 1125 |
+
"2013-05-01",
|
| 1126 |
+
periods=3,
|
| 1127 |
+
weekmask="Sun Mon Tue Wed Thu",
|
| 1128 |
+
holidays=["2013-05-01"],
|
| 1129 |
+
)
|
| 1130 |
+
|
| 1131 |
+
@pytest.mark.parametrize(
|
| 1132 |
+
"freq", [freq for freq in prefix_mapping if freq.startswith("C")]
|
| 1133 |
+
)
|
| 1134 |
+
def test_all_custom_freq(self, freq):
|
| 1135 |
+
# should not raise
|
| 1136 |
+
bdate_range(
|
| 1137 |
+
START, END, freq=freq, weekmask="Mon Wed Fri", holidays=["2009-03-14"]
|
| 1138 |
+
)
|
| 1139 |
+
|
| 1140 |
+
bad_freq = freq + "FOO"
|
| 1141 |
+
msg = f"invalid custom frequency string: {bad_freq}"
|
| 1142 |
+
with pytest.raises(ValueError, match=msg):
|
| 1143 |
+
bdate_range(START, END, freq=bad_freq)
|
| 1144 |
+
|
| 1145 |
+
@pytest.mark.parametrize(
|
| 1146 |
+
"start_end",
|
| 1147 |
+
[
|
| 1148 |
+
("2018-01-01T00:00:01.000Z", "2018-01-03T00:00:01.000Z"),
|
| 1149 |
+
("2018-01-01T00:00:00.010Z", "2018-01-03T00:00:00.010Z"),
|
| 1150 |
+
("2001-01-01T00:00:00.010Z", "2001-01-03T00:00:00.010Z"),
|
| 1151 |
+
],
|
| 1152 |
+
)
|
| 1153 |
+
def test_range_with_millisecond_resolution(self, start_end):
|
| 1154 |
+
# https://github.com/pandas-dev/pandas/issues/24110
|
| 1155 |
+
start, end = start_end
|
| 1156 |
+
result = date_range(start=start, end=end, periods=2, inclusive="left")
|
| 1157 |
+
expected = DatetimeIndex([start])
|
| 1158 |
+
tm.assert_index_equal(result, expected)
|
| 1159 |
+
|
| 1160 |
+
@pytest.mark.parametrize(
|
| 1161 |
+
"start,period,expected",
|
| 1162 |
+
[
|
| 1163 |
+
("2022-07-23 00:00:00+02:00", 1, ["2022-07-25 00:00:00+02:00"]),
|
| 1164 |
+
("2022-07-22 00:00:00+02:00", 1, ["2022-07-22 00:00:00+02:00"]),
|
| 1165 |
+
(
|
| 1166 |
+
"2022-07-22 00:00:00+02:00",
|
| 1167 |
+
2,
|
| 1168 |
+
["2022-07-22 00:00:00+02:00", "2022-07-25 00:00:00+02:00"],
|
| 1169 |
+
),
|
| 1170 |
+
],
|
| 1171 |
+
)
|
| 1172 |
+
def test_range_with_timezone_and_custombusinessday(self, start, period, expected):
|
| 1173 |
+
# GH49441
|
| 1174 |
+
result = date_range(start=start, periods=period, freq="C")
|
| 1175 |
+
expected = DatetimeIndex(expected)
|
| 1176 |
+
tm.assert_index_equal(result, expected)
|
| 1177 |
+
|
| 1178 |
+
|
| 1179 |
+
def test_date_range_with_custom_holidays():
|
| 1180 |
+
# GH 30593
|
| 1181 |
+
freq = offsets.CustomBusinessHour(start="15:00", holidays=["2020-11-26"])
|
| 1182 |
+
result = date_range(start="2020-11-25 15:00", periods=4, freq=freq)
|
| 1183 |
+
expected = DatetimeIndex(
|
| 1184 |
+
[
|
| 1185 |
+
"2020-11-25 15:00:00",
|
| 1186 |
+
"2020-11-25 16:00:00",
|
| 1187 |
+
"2020-11-27 15:00:00",
|
| 1188 |
+
"2020-11-27 16:00:00",
|
| 1189 |
+
],
|
| 1190 |
+
freq=freq,
|
| 1191 |
+
)
|
| 1192 |
+
tm.assert_index_equal(result, expected)
|
| 1193 |
+
|
| 1194 |
+
|
| 1195 |
+
class TestDateRangeNonNano:
|
| 1196 |
+
def test_date_range_reso_validation(self):
|
| 1197 |
+
msg = "'unit' must be one of 's', 'ms', 'us', 'ns'"
|
| 1198 |
+
with pytest.raises(ValueError, match=msg):
|
| 1199 |
+
date_range("2016-01-01", "2016-03-04", periods=3, unit="h")
|
| 1200 |
+
|
| 1201 |
+
def test_date_range_freq_higher_than_reso(self):
|
| 1202 |
+
# freq being higher-resolution than reso is a problem
|
| 1203 |
+
msg = "Use a lower freq or a higher unit instead"
|
| 1204 |
+
with pytest.raises(ValueError, match=msg):
|
| 1205 |
+
# # TODO give a more useful or informative message?
|
| 1206 |
+
date_range("2016-01-01", "2016-01-02", freq="ns", unit="ms")
|
| 1207 |
+
|
| 1208 |
+
def test_date_range_freq_matches_reso(self):
|
| 1209 |
+
# GH#49106 matching reso is OK
|
| 1210 |
+
dti = date_range("2016-01-01", "2016-01-01 00:00:01", freq="ms", unit="ms")
|
| 1211 |
+
rng = np.arange(1_451_606_400_000, 1_451_606_401_001, dtype=np.int64)
|
| 1212 |
+
expected = DatetimeIndex(rng.view("M8[ms]"), freq="ms")
|
| 1213 |
+
tm.assert_index_equal(dti, expected)
|
| 1214 |
+
|
| 1215 |
+
dti = date_range("2016-01-01", "2016-01-01 00:00:01", freq="us", unit="us")
|
| 1216 |
+
rng = np.arange(1_451_606_400_000_000, 1_451_606_401_000_001, dtype=np.int64)
|
| 1217 |
+
expected = DatetimeIndex(rng.view("M8[us]"), freq="us")
|
| 1218 |
+
tm.assert_index_equal(dti, expected)
|
| 1219 |
+
|
| 1220 |
+
dti = date_range("2016-01-01", "2016-01-01 00:00:00.001", freq="ns", unit="ns")
|
| 1221 |
+
rng = np.arange(
|
| 1222 |
+
1_451_606_400_000_000_000, 1_451_606_400_001_000_001, dtype=np.int64
|
| 1223 |
+
)
|
| 1224 |
+
expected = DatetimeIndex(rng.view("M8[ns]"), freq="ns")
|
| 1225 |
+
tm.assert_index_equal(dti, expected)
|
| 1226 |
+
|
| 1227 |
+
def test_date_range_freq_lower_than_endpoints(self):
|
| 1228 |
+
start = Timestamp("2022-10-19 11:50:44.719781")
|
| 1229 |
+
end = Timestamp("2022-10-19 11:50:47.066458")
|
| 1230 |
+
|
| 1231 |
+
# start and end cannot be cast to "s" unit without lossy rounding,
|
| 1232 |
+
# so we do not allow this in date_range
|
| 1233 |
+
with pytest.raises(ValueError, match="Cannot losslessly convert units"):
|
| 1234 |
+
date_range(start, end, periods=3, unit="s")
|
| 1235 |
+
|
| 1236 |
+
# but we can losslessly cast to "us"
|
| 1237 |
+
dti = date_range(start, end, periods=2, unit="us")
|
| 1238 |
+
rng = np.array(
|
| 1239 |
+
[start.as_unit("us")._value, end.as_unit("us")._value], dtype=np.int64
|
| 1240 |
+
)
|
| 1241 |
+
expected = DatetimeIndex(rng.view("M8[us]"))
|
| 1242 |
+
tm.assert_index_equal(dti, expected)
|
| 1243 |
+
|
| 1244 |
+
def test_date_range_non_nano(self):
|
| 1245 |
+
start = np.datetime64("1066-10-14") # Battle of Hastings
|
| 1246 |
+
end = np.datetime64("2305-07-13") # Jean-Luc Picard's birthday
|
| 1247 |
+
|
| 1248 |
+
dti = date_range(start, end, freq="D", unit="s")
|
| 1249 |
+
assert dti.freq == "D"
|
| 1250 |
+
assert dti.dtype == "M8[s]"
|
| 1251 |
+
|
| 1252 |
+
exp = np.arange(
|
| 1253 |
+
start.astype("M8[s]").view("i8"),
|
| 1254 |
+
(end + 1).astype("M8[s]").view("i8"),
|
| 1255 |
+
24 * 3600,
|
| 1256 |
+
).view("M8[s]")
|
| 1257 |
+
|
| 1258 |
+
tm.assert_numpy_array_equal(dti.to_numpy(), exp)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_datetime.py
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import date
|
| 2 |
+
|
| 3 |
+
import dateutil
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from pandas import (
|
| 9 |
+
DataFrame,
|
| 10 |
+
DatetimeIndex,
|
| 11 |
+
Index,
|
| 12 |
+
Timestamp,
|
| 13 |
+
date_range,
|
| 14 |
+
offsets,
|
| 15 |
+
)
|
| 16 |
+
import pandas._testing as tm
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class TestDatetimeIndex:
|
| 20 |
+
def test_sub_datetime_preserves_freq(self, tz_naive_fixture):
|
| 21 |
+
# GH#48818
|
| 22 |
+
dti = date_range("2016-01-01", periods=12, tz=tz_naive_fixture)
|
| 23 |
+
|
| 24 |
+
res = dti - dti[0]
|
| 25 |
+
expected = pd.timedelta_range("0 Days", "11 Days")
|
| 26 |
+
tm.assert_index_equal(res, expected)
|
| 27 |
+
assert res.freq == expected.freq
|
| 28 |
+
|
| 29 |
+
@pytest.mark.xfail(
|
| 30 |
+
reason="The inherited freq is incorrect bc dti.freq is incorrect "
|
| 31 |
+
"https://github.com/pandas-dev/pandas/pull/48818/files#r982793461"
|
| 32 |
+
)
|
| 33 |
+
def test_sub_datetime_preserves_freq_across_dst(self):
|
| 34 |
+
# GH#48818
|
| 35 |
+
ts = Timestamp("2016-03-11", tz="US/Pacific")
|
| 36 |
+
dti = date_range(ts, periods=4)
|
| 37 |
+
|
| 38 |
+
res = dti - dti[0]
|
| 39 |
+
expected = pd.TimedeltaIndex(
|
| 40 |
+
[
|
| 41 |
+
pd.Timedelta(days=0),
|
| 42 |
+
pd.Timedelta(days=1),
|
| 43 |
+
pd.Timedelta(days=2),
|
| 44 |
+
pd.Timedelta(days=2, hours=23),
|
| 45 |
+
]
|
| 46 |
+
)
|
| 47 |
+
tm.assert_index_equal(res, expected)
|
| 48 |
+
assert res.freq == expected.freq
|
| 49 |
+
|
| 50 |
+
def test_time_overflow_for_32bit_machines(self):
|
| 51 |
+
# GH8943. On some machines NumPy defaults to np.int32 (for example,
|
| 52 |
+
# 32-bit Linux machines). In the function _generate_regular_range
|
| 53 |
+
# found in tseries/index.py, `periods` gets multiplied by `strides`
|
| 54 |
+
# (which has value 1e9) and since the max value for np.int32 is ~2e9,
|
| 55 |
+
# and since those machines won't promote np.int32 to np.int64, we get
|
| 56 |
+
# overflow.
|
| 57 |
+
periods = np.int_(1000)
|
| 58 |
+
|
| 59 |
+
idx1 = date_range(start="2000", periods=periods, freq="S")
|
| 60 |
+
assert len(idx1) == periods
|
| 61 |
+
|
| 62 |
+
idx2 = date_range(end="2000", periods=periods, freq="S")
|
| 63 |
+
assert len(idx2) == periods
|
| 64 |
+
|
| 65 |
+
def test_nat(self):
|
| 66 |
+
assert DatetimeIndex([np.nan])[0] is pd.NaT
|
| 67 |
+
|
| 68 |
+
def test_week_of_month_frequency(self):
|
| 69 |
+
# GH 5348: "ValueError: Could not evaluate WOM-1SUN" shouldn't raise
|
| 70 |
+
d1 = date(2002, 9, 1)
|
| 71 |
+
d2 = date(2013, 10, 27)
|
| 72 |
+
d3 = date(2012, 9, 30)
|
| 73 |
+
idx1 = DatetimeIndex([d1, d2])
|
| 74 |
+
idx2 = DatetimeIndex([d3])
|
| 75 |
+
result_append = idx1.append(idx2)
|
| 76 |
+
expected = DatetimeIndex([d1, d2, d3])
|
| 77 |
+
tm.assert_index_equal(result_append, expected)
|
| 78 |
+
result_union = idx1.union(idx2)
|
| 79 |
+
expected = DatetimeIndex([d1, d3, d2])
|
| 80 |
+
tm.assert_index_equal(result_union, expected)
|
| 81 |
+
|
| 82 |
+
# GH 5115
|
| 83 |
+
result = date_range("2013-1-1", periods=4, freq="WOM-1SAT")
|
| 84 |
+
dates = ["2013-01-05", "2013-02-02", "2013-03-02", "2013-04-06"]
|
| 85 |
+
expected = DatetimeIndex(dates, freq="WOM-1SAT")
|
| 86 |
+
tm.assert_index_equal(result, expected)
|
| 87 |
+
|
| 88 |
+
def test_append_nondatetimeindex(self):
|
| 89 |
+
rng = date_range("1/1/2000", periods=10)
|
| 90 |
+
idx = Index(["a", "b", "c", "d"])
|
| 91 |
+
|
| 92 |
+
result = rng.append(idx)
|
| 93 |
+
assert isinstance(result[0], Timestamp)
|
| 94 |
+
|
| 95 |
+
def test_iteration_preserves_tz(self):
|
| 96 |
+
# see gh-8890
|
| 97 |
+
index = date_range("2012-01-01", periods=3, freq="H", tz="US/Eastern")
|
| 98 |
+
|
| 99 |
+
for i, ts in enumerate(index):
|
| 100 |
+
result = ts
|
| 101 |
+
expected = index[i] # pylint: disable=unnecessary-list-index-lookup
|
| 102 |
+
assert result == expected
|
| 103 |
+
|
| 104 |
+
index = date_range(
|
| 105 |
+
"2012-01-01", periods=3, freq="H", tz=dateutil.tz.tzoffset(None, -28800)
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
for i, ts in enumerate(index):
|
| 109 |
+
result = ts
|
| 110 |
+
expected = index[i] # pylint: disable=unnecessary-list-index-lookup
|
| 111 |
+
assert result._repr_base == expected._repr_base
|
| 112 |
+
assert result == expected
|
| 113 |
+
|
| 114 |
+
# 9100
|
| 115 |
+
index = DatetimeIndex(
|
| 116 |
+
["2014-12-01 03:32:39.987000-08:00", "2014-12-01 04:12:34.987000-08:00"]
|
| 117 |
+
)
|
| 118 |
+
for i, ts in enumerate(index):
|
| 119 |
+
result = ts
|
| 120 |
+
expected = index[i] # pylint: disable=unnecessary-list-index-lookup
|
| 121 |
+
assert result._repr_base == expected._repr_base
|
| 122 |
+
assert result == expected
|
| 123 |
+
|
| 124 |
+
@pytest.mark.parametrize("periods", [0, 9999, 10000, 10001])
|
| 125 |
+
def test_iteration_over_chunksize(self, periods):
|
| 126 |
+
# GH21012
|
| 127 |
+
|
| 128 |
+
index = date_range("2000-01-01 00:00:00", periods=periods, freq="min")
|
| 129 |
+
num = 0
|
| 130 |
+
for stamp in index:
|
| 131 |
+
assert index[num] == stamp
|
| 132 |
+
num += 1
|
| 133 |
+
assert num == len(index)
|
| 134 |
+
|
| 135 |
+
def test_misc_coverage(self):
|
| 136 |
+
rng = date_range("1/1/2000", periods=5)
|
| 137 |
+
result = rng.groupby(rng.day)
|
| 138 |
+
assert isinstance(list(result.values())[0][0], Timestamp)
|
| 139 |
+
|
| 140 |
+
def test_groupby_function_tuple_1677(self):
|
| 141 |
+
df = DataFrame(np.random.rand(100), index=date_range("1/1/2000", periods=100))
|
| 142 |
+
monthly_group = df.groupby(lambda x: (x.year, x.month))
|
| 143 |
+
|
| 144 |
+
result = monthly_group.mean()
|
| 145 |
+
assert isinstance(result.index[0], tuple)
|
| 146 |
+
|
| 147 |
+
def assert_index_parameters(self, index):
|
| 148 |
+
assert index.freq == "40960N"
|
| 149 |
+
assert index.inferred_freq == "40960N"
|
| 150 |
+
|
| 151 |
+
def test_ns_index(self):
|
| 152 |
+
nsamples = 400
|
| 153 |
+
ns = int(1e9 / 24414)
|
| 154 |
+
dtstart = np.datetime64("2012-09-20T00:00:00")
|
| 155 |
+
|
| 156 |
+
dt = dtstart + np.arange(nsamples) * np.timedelta64(ns, "ns")
|
| 157 |
+
freq = ns * offsets.Nano()
|
| 158 |
+
index = DatetimeIndex(dt, freq=freq, name="time")
|
| 159 |
+
self.assert_index_parameters(index)
|
| 160 |
+
|
| 161 |
+
new_index = date_range(start=index[0], end=index[-1], freq=index.freq)
|
| 162 |
+
self.assert_index_parameters(new_index)
|
| 163 |
+
|
| 164 |
+
def test_asarray_tz_naive(self):
|
| 165 |
+
# This shouldn't produce a warning.
|
| 166 |
+
idx = date_range("2000", periods=2)
|
| 167 |
+
# M8[ns] by default
|
| 168 |
+
result = np.asarray(idx)
|
| 169 |
+
|
| 170 |
+
expected = np.array(["2000-01-01", "2000-01-02"], dtype="M8[ns]")
|
| 171 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 172 |
+
|
| 173 |
+
# optionally, object
|
| 174 |
+
result = np.asarray(idx, dtype=object)
|
| 175 |
+
|
| 176 |
+
expected = np.array([Timestamp("2000-01-01"), Timestamp("2000-01-02")])
|
| 177 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 178 |
+
|
| 179 |
+
def test_asarray_tz_aware(self):
|
| 180 |
+
tz = "US/Central"
|
| 181 |
+
idx = date_range("2000", periods=2, tz=tz)
|
| 182 |
+
expected = np.array(["2000-01-01T06", "2000-01-02T06"], dtype="M8[ns]")
|
| 183 |
+
result = np.asarray(idx, dtype="datetime64[ns]")
|
| 184 |
+
|
| 185 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 186 |
+
|
| 187 |
+
# Old behavior with no warning
|
| 188 |
+
result = np.asarray(idx, dtype="M8[ns]")
|
| 189 |
+
|
| 190 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 191 |
+
|
| 192 |
+
# Future behavior with no warning
|
| 193 |
+
expected = np.array(
|
| 194 |
+
[Timestamp("2000-01-01", tz=tz), Timestamp("2000-01-02", tz=tz)]
|
| 195 |
+
)
|
| 196 |
+
result = np.asarray(idx, dtype=object)
|
| 197 |
+
|
| 198 |
+
tm.assert_numpy_array_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_datetimelike.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" generic tests from the Datetimelike class """
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
DatetimeIndex,
|
| 6 |
+
date_range,
|
| 7 |
+
)
|
| 8 |
+
import pandas._testing as tm
|
| 9 |
+
from pandas.tests.indexes.datetimelike import DatetimeLike
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestDatetimeIndex(DatetimeLike):
|
| 13 |
+
_index_cls = DatetimeIndex
|
| 14 |
+
|
| 15 |
+
@pytest.fixture
|
| 16 |
+
def simple_index(self) -> DatetimeIndex:
|
| 17 |
+
return date_range("20130101", periods=5)
|
| 18 |
+
|
| 19 |
+
@pytest.fixture(
|
| 20 |
+
params=[tm.makeDateIndex(10), date_range("20130110", periods=10, freq="-1D")],
|
| 21 |
+
ids=["index_inc", "index_dec"],
|
| 22 |
+
)
|
| 23 |
+
def index(self, request):
|
| 24 |
+
return request.param
|
| 25 |
+
|
| 26 |
+
def test_format(self, simple_index):
|
| 27 |
+
# GH35439
|
| 28 |
+
idx = simple_index
|
| 29 |
+
expected = [f"{x:%Y-%m-%d}" for x in idx]
|
| 30 |
+
assert idx.format() == expected
|
| 31 |
+
|
| 32 |
+
def test_shift(self):
|
| 33 |
+
pass # handled in test_ops
|
| 34 |
+
|
| 35 |
+
def test_intersection(self):
|
| 36 |
+
pass # handled in test_setops
|
| 37 |
+
|
| 38 |
+
def test_union(self):
|
| 39 |
+
pass # handled in test_setops
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_delete.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
| 13 |
+
idx = date_range(start="2000-01-01", periods=5, freq="M", name="idx")
|
| 14 |
+
|
| 15 |
+
# preserve freq
|
| 16 |
+
expected_0 = date_range(start="2000-02-01", periods=4, freq="M", name="idx")
|
| 17 |
+
expected_4 = date_range(start="2000-01-01", periods=4, freq="M", name="idx")
|
| 18 |
+
|
| 19 |
+
# reset freq to None
|
| 20 |
+
expected_1 = DatetimeIndex(
|
| 21 |
+
["2000-01-31", "2000-03-31", "2000-04-30", "2000-05-31"],
|
| 22 |
+
freq=None,
|
| 23 |
+
name="idx",
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
cases = {
|
| 27 |
+
0: expected_0,
|
| 28 |
+
-5: expected_0,
|
| 29 |
+
-1: expected_4,
|
| 30 |
+
4: expected_4,
|
| 31 |
+
1: expected_1,
|
| 32 |
+
}
|
| 33 |
+
for n, expected in cases.items():
|
| 34 |
+
result = idx.delete(n)
|
| 35 |
+
tm.assert_index_equal(result, expected)
|
| 36 |
+
assert result.name == expected.name
|
| 37 |
+
assert result.freq == expected.freq
|
| 38 |
+
|
| 39 |
+
with pytest.raises((IndexError, ValueError), match="out of bounds"):
|
| 40 |
+
# either depending on numpy version
|
| 41 |
+
idx.delete(5)
|
| 42 |
+
|
| 43 |
+
for tz in [None, "Asia/Tokyo", "US/Pacific"]:
|
| 44 |
+
idx = date_range(
|
| 45 |
+
start="2000-01-01 09:00", periods=10, freq="H", name="idx", tz=tz
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
expected = date_range(
|
| 49 |
+
start="2000-01-01 10:00", periods=9, freq="H", name="idx", tz=tz
|
| 50 |
+
)
|
| 51 |
+
result = idx.delete(0)
|
| 52 |
+
tm.assert_index_equal(result, expected)
|
| 53 |
+
assert result.name == expected.name
|
| 54 |
+
assert result.freqstr == "H"
|
| 55 |
+
assert result.tz == expected.tz
|
| 56 |
+
|
| 57 |
+
expected = date_range(
|
| 58 |
+
start="2000-01-01 09:00", periods=9, freq="H", name="idx", tz=tz
|
| 59 |
+
)
|
| 60 |
+
result = idx.delete(-1)
|
| 61 |
+
tm.assert_index_equal(result, expected)
|
| 62 |
+
assert result.name == expected.name
|
| 63 |
+
assert result.freqstr == "H"
|
| 64 |
+
assert result.tz == expected.tz
|
| 65 |
+
|
| 66 |
+
def test_delete_slice(self):
|
| 67 |
+
idx = date_range(start="2000-01-01", periods=10, freq="D", name="idx")
|
| 68 |
+
|
| 69 |
+
# preserve freq
|
| 70 |
+
expected_0_2 = date_range(start="2000-01-04", periods=7, freq="D", name="idx")
|
| 71 |
+
expected_7_9 = date_range(start="2000-01-01", periods=7, freq="D", name="idx")
|
| 72 |
+
|
| 73 |
+
# reset freq to None
|
| 74 |
+
expected_3_5 = DatetimeIndex(
|
| 75 |
+
[
|
| 76 |
+
"2000-01-01",
|
| 77 |
+
"2000-01-02",
|
| 78 |
+
"2000-01-03",
|
| 79 |
+
"2000-01-07",
|
| 80 |
+
"2000-01-08",
|
| 81 |
+
"2000-01-09",
|
| 82 |
+
"2000-01-10",
|
| 83 |
+
],
|
| 84 |
+
freq=None,
|
| 85 |
+
name="idx",
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
cases = {
|
| 89 |
+
(0, 1, 2): expected_0_2,
|
| 90 |
+
(7, 8, 9): expected_7_9,
|
| 91 |
+
(3, 4, 5): expected_3_5,
|
| 92 |
+
}
|
| 93 |
+
for n, expected in cases.items():
|
| 94 |
+
result = idx.delete(n)
|
| 95 |
+
tm.assert_index_equal(result, expected)
|
| 96 |
+
assert result.name == expected.name
|
| 97 |
+
assert result.freq == expected.freq
|
| 98 |
+
|
| 99 |
+
result = idx.delete(slice(n[0], n[-1] + 1))
|
| 100 |
+
tm.assert_index_equal(result, expected)
|
| 101 |
+
assert result.name == expected.name
|
| 102 |
+
assert result.freq == expected.freq
|
| 103 |
+
|
| 104 |
+
for tz in [None, "Asia/Tokyo", "US/Pacific"]:
|
| 105 |
+
ts = Series(
|
| 106 |
+
1,
|
| 107 |
+
index=date_range(
|
| 108 |
+
"2000-01-01 09:00", periods=10, freq="H", name="idx", tz=tz
|
| 109 |
+
),
|
| 110 |
+
)
|
| 111 |
+
# preserve freq
|
| 112 |
+
result = ts.drop(ts.index[:5]).index
|
| 113 |
+
expected = date_range(
|
| 114 |
+
"2000-01-01 14:00", periods=5, freq="H", name="idx", tz=tz
|
| 115 |
+
)
|
| 116 |
+
tm.assert_index_equal(result, expected)
|
| 117 |
+
assert result.name == expected.name
|
| 118 |
+
assert result.freq == expected.freq
|
| 119 |
+
assert result.tz == expected.tz
|
| 120 |
+
|
| 121 |
+
# reset freq to None
|
| 122 |
+
result = ts.drop(ts.index[[1, 3, 5, 7, 9]]).index
|
| 123 |
+
expected = DatetimeIndex(
|
| 124 |
+
[
|
| 125 |
+
"2000-01-01 09:00",
|
| 126 |
+
"2000-01-01 11:00",
|
| 127 |
+
"2000-01-01 13:00",
|
| 128 |
+
"2000-01-01 15:00",
|
| 129 |
+
"2000-01-01 17:00",
|
| 130 |
+
],
|
| 131 |
+
freq=None,
|
| 132 |
+
name="idx",
|
| 133 |
+
tz=tz,
|
| 134 |
+
)
|
| 135 |
+
tm.assert_index_equal(result, expected)
|
| 136 |
+
assert result.name == expected.name
|
| 137 |
+
assert result.freq == expected.freq
|
| 138 |
+
assert result.tz == expected.tz
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_formats.py
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
|
| 3 |
+
import dateutil.tz
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pytest
|
| 6 |
+
import pytz
|
| 7 |
+
|
| 8 |
+
import pandas as pd
|
| 9 |
+
from pandas import (
|
| 10 |
+
DatetimeIndex,
|
| 11 |
+
Series,
|
| 12 |
+
)
|
| 13 |
+
import pandas._testing as tm
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_format_native_types():
|
| 17 |
+
index = pd.date_range(freq="1D", periods=3, start="2017-01-01")
|
| 18 |
+
|
| 19 |
+
# First, with no arguments.
|
| 20 |
+
expected = np.array(["2017-01-01", "2017-01-02", "2017-01-03"], dtype=object)
|
| 21 |
+
|
| 22 |
+
result = index._format_native_types()
|
| 23 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 24 |
+
|
| 25 |
+
# No NaN values, so na_rep has no effect
|
| 26 |
+
result = index._format_native_types(na_rep="pandas")
|
| 27 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 28 |
+
|
| 29 |
+
# Make sure date formatting works
|
| 30 |
+
expected = np.array(["01-2017-01", "01-2017-02", "01-2017-03"], dtype=object)
|
| 31 |
+
|
| 32 |
+
result = index._format_native_types(date_format="%m-%Y-%d")
|
| 33 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 34 |
+
|
| 35 |
+
# NULL object handling should work
|
| 36 |
+
index = DatetimeIndex(["2017-01-01", pd.NaT, "2017-01-03"])
|
| 37 |
+
expected = np.array(["2017-01-01", "NaT", "2017-01-03"], dtype=object)
|
| 38 |
+
|
| 39 |
+
result = index._format_native_types()
|
| 40 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 41 |
+
|
| 42 |
+
expected = np.array(["2017-01-01", "pandas", "2017-01-03"], dtype=object)
|
| 43 |
+
|
| 44 |
+
result = index._format_native_types(na_rep="pandas")
|
| 45 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 46 |
+
|
| 47 |
+
result = index._format_native_types(date_format="%Y-%m-%d %H:%M:%S.%f")
|
| 48 |
+
expected = np.array(
|
| 49 |
+
["2017-01-01 00:00:00.000000", "NaT", "2017-01-03 00:00:00.000000"],
|
| 50 |
+
dtype=object,
|
| 51 |
+
)
|
| 52 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 53 |
+
|
| 54 |
+
# invalid format
|
| 55 |
+
result = index._format_native_types(date_format="foo")
|
| 56 |
+
expected = np.array(["foo", "NaT", "foo"], dtype=object)
|
| 57 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class TestDatetimeIndexRendering:
|
| 61 |
+
def test_dti_repr_short(self):
|
| 62 |
+
dr = pd.date_range(start="1/1/2012", periods=1)
|
| 63 |
+
repr(dr)
|
| 64 |
+
|
| 65 |
+
dr = pd.date_range(start="1/1/2012", periods=2)
|
| 66 |
+
repr(dr)
|
| 67 |
+
|
| 68 |
+
dr = pd.date_range(start="1/1/2012", periods=3)
|
| 69 |
+
repr(dr)
|
| 70 |
+
|
| 71 |
+
@pytest.mark.parametrize("method", ["__repr__", "__str__"])
|
| 72 |
+
def test_dti_representation(self, method):
|
| 73 |
+
idxs = []
|
| 74 |
+
idxs.append(DatetimeIndex([], freq="D"))
|
| 75 |
+
idxs.append(DatetimeIndex(["2011-01-01"], freq="D"))
|
| 76 |
+
idxs.append(DatetimeIndex(["2011-01-01", "2011-01-02"], freq="D"))
|
| 77 |
+
idxs.append(DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03"], freq="D"))
|
| 78 |
+
idxs.append(
|
| 79 |
+
DatetimeIndex(
|
| 80 |
+
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"],
|
| 81 |
+
freq="H",
|
| 82 |
+
tz="Asia/Tokyo",
|
| 83 |
+
)
|
| 84 |
+
)
|
| 85 |
+
idxs.append(
|
| 86 |
+
DatetimeIndex(
|
| 87 |
+
["2011-01-01 09:00", "2011-01-01 10:00", pd.NaT], tz="US/Eastern"
|
| 88 |
+
)
|
| 89 |
+
)
|
| 90 |
+
idxs.append(
|
| 91 |
+
DatetimeIndex(["2011-01-01 09:00", "2011-01-01 10:00", pd.NaT], tz="UTC")
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
exp = []
|
| 95 |
+
exp.append("DatetimeIndex([], dtype='datetime64[ns]', freq='D')")
|
| 96 |
+
exp.append("DatetimeIndex(['2011-01-01'], dtype='datetime64[ns]', freq='D')")
|
| 97 |
+
exp.append(
|
| 98 |
+
"DatetimeIndex(['2011-01-01', '2011-01-02'], "
|
| 99 |
+
"dtype='datetime64[ns]', freq='D')"
|
| 100 |
+
)
|
| 101 |
+
exp.append(
|
| 102 |
+
"DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], "
|
| 103 |
+
"dtype='datetime64[ns]', freq='D')"
|
| 104 |
+
)
|
| 105 |
+
exp.append(
|
| 106 |
+
"DatetimeIndex(['2011-01-01 09:00:00+09:00', "
|
| 107 |
+
"'2011-01-01 10:00:00+09:00', '2011-01-01 11:00:00+09:00']"
|
| 108 |
+
", dtype='datetime64[ns, Asia/Tokyo]', freq='H')"
|
| 109 |
+
)
|
| 110 |
+
exp.append(
|
| 111 |
+
"DatetimeIndex(['2011-01-01 09:00:00-05:00', "
|
| 112 |
+
"'2011-01-01 10:00:00-05:00', 'NaT'], "
|
| 113 |
+
"dtype='datetime64[ns, US/Eastern]', freq=None)"
|
| 114 |
+
)
|
| 115 |
+
exp.append(
|
| 116 |
+
"DatetimeIndex(['2011-01-01 09:00:00+00:00', "
|
| 117 |
+
"'2011-01-01 10:00:00+00:00', 'NaT'], "
|
| 118 |
+
"dtype='datetime64[ns, UTC]', freq=None)"
|
| 119 |
+
""
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
with pd.option_context("display.width", 300):
|
| 123 |
+
for indx, expected in zip(idxs, exp):
|
| 124 |
+
result = getattr(indx, method)()
|
| 125 |
+
assert result == expected
|
| 126 |
+
|
| 127 |
+
def test_dti_representation_to_series(self):
|
| 128 |
+
idx1 = DatetimeIndex([], freq="D")
|
| 129 |
+
idx2 = DatetimeIndex(["2011-01-01"], freq="D")
|
| 130 |
+
idx3 = DatetimeIndex(["2011-01-01", "2011-01-02"], freq="D")
|
| 131 |
+
idx4 = DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03"], freq="D")
|
| 132 |
+
idx5 = DatetimeIndex(
|
| 133 |
+
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"],
|
| 134 |
+
freq="H",
|
| 135 |
+
tz="Asia/Tokyo",
|
| 136 |
+
)
|
| 137 |
+
idx6 = DatetimeIndex(
|
| 138 |
+
["2011-01-01 09:00", "2011-01-01 10:00", pd.NaT], tz="US/Eastern"
|
| 139 |
+
)
|
| 140 |
+
idx7 = DatetimeIndex(["2011-01-01 09:00", "2011-01-02 10:15"])
|
| 141 |
+
|
| 142 |
+
exp1 = """Series([], dtype: datetime64[ns])"""
|
| 143 |
+
|
| 144 |
+
exp2 = "0 2011-01-01\ndtype: datetime64[ns]"
|
| 145 |
+
|
| 146 |
+
exp3 = "0 2011-01-01\n1 2011-01-02\ndtype: datetime64[ns]"
|
| 147 |
+
|
| 148 |
+
exp4 = (
|
| 149 |
+
"0 2011-01-01\n"
|
| 150 |
+
"1 2011-01-02\n"
|
| 151 |
+
"2 2011-01-03\n"
|
| 152 |
+
"dtype: datetime64[ns]"
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
exp5 = (
|
| 156 |
+
"0 2011-01-01 09:00:00+09:00\n"
|
| 157 |
+
"1 2011-01-01 10:00:00+09:00\n"
|
| 158 |
+
"2 2011-01-01 11:00:00+09:00\n"
|
| 159 |
+
"dtype: datetime64[ns, Asia/Tokyo]"
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
exp6 = (
|
| 163 |
+
"0 2011-01-01 09:00:00-05:00\n"
|
| 164 |
+
"1 2011-01-01 10:00:00-05:00\n"
|
| 165 |
+
"2 NaT\n"
|
| 166 |
+
"dtype: datetime64[ns, US/Eastern]"
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
exp7 = (
|
| 170 |
+
"0 2011-01-01 09:00:00\n"
|
| 171 |
+
"1 2011-01-02 10:15:00\n"
|
| 172 |
+
"dtype: datetime64[ns]"
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
with pd.option_context("display.width", 300):
|
| 176 |
+
for idx, expected in zip(
|
| 177 |
+
[idx1, idx2, idx3, idx4, idx5, idx6, idx7],
|
| 178 |
+
[exp1, exp2, exp3, exp4, exp5, exp6, exp7],
|
| 179 |
+
):
|
| 180 |
+
result = repr(Series(idx))
|
| 181 |
+
assert result == expected
|
| 182 |
+
|
| 183 |
+
def test_dti_summary(self):
|
| 184 |
+
# GH#9116
|
| 185 |
+
idx1 = DatetimeIndex([], freq="D")
|
| 186 |
+
idx2 = DatetimeIndex(["2011-01-01"], freq="D")
|
| 187 |
+
idx3 = DatetimeIndex(["2011-01-01", "2011-01-02"], freq="D")
|
| 188 |
+
idx4 = DatetimeIndex(["2011-01-01", "2011-01-02", "2011-01-03"], freq="D")
|
| 189 |
+
idx5 = DatetimeIndex(
|
| 190 |
+
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"],
|
| 191 |
+
freq="H",
|
| 192 |
+
tz="Asia/Tokyo",
|
| 193 |
+
)
|
| 194 |
+
idx6 = DatetimeIndex(
|
| 195 |
+
["2011-01-01 09:00", "2011-01-01 10:00", pd.NaT], tz="US/Eastern"
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
exp1 = "DatetimeIndex: 0 entries\nFreq: D"
|
| 199 |
+
|
| 200 |
+
exp2 = "DatetimeIndex: 1 entries, 2011-01-01 to 2011-01-01\nFreq: D"
|
| 201 |
+
|
| 202 |
+
exp3 = "DatetimeIndex: 2 entries, 2011-01-01 to 2011-01-02\nFreq: D"
|
| 203 |
+
|
| 204 |
+
exp4 = "DatetimeIndex: 3 entries, 2011-01-01 to 2011-01-03\nFreq: D"
|
| 205 |
+
|
| 206 |
+
exp5 = (
|
| 207 |
+
"DatetimeIndex: 3 entries, 2011-01-01 09:00:00+09:00 "
|
| 208 |
+
"to 2011-01-01 11:00:00+09:00\n"
|
| 209 |
+
"Freq: H"
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
exp6 = """DatetimeIndex: 3 entries, 2011-01-01 09:00:00-05:00 to NaT"""
|
| 213 |
+
|
| 214 |
+
for idx, expected in zip(
|
| 215 |
+
[idx1, idx2, idx3, idx4, idx5, idx6], [exp1, exp2, exp3, exp4, exp5, exp6]
|
| 216 |
+
):
|
| 217 |
+
result = idx._summary()
|
| 218 |
+
assert result == expected
|
| 219 |
+
|
| 220 |
+
def test_dti_business_repr(self):
|
| 221 |
+
# only really care that it works
|
| 222 |
+
repr(pd.bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1)))
|
| 223 |
+
|
| 224 |
+
def test_dti_business_summary(self):
|
| 225 |
+
rng = pd.bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1))
|
| 226 |
+
rng._summary()
|
| 227 |
+
rng[2:2]._summary()
|
| 228 |
+
|
| 229 |
+
def test_dti_business_summary_pytz(self):
|
| 230 |
+
pd.bdate_range("1/1/2005", "1/1/2009", tz=pytz.utc)._summary()
|
| 231 |
+
|
| 232 |
+
def test_dti_business_summary_dateutil(self):
|
| 233 |
+
pd.bdate_range("1/1/2005", "1/1/2009", tz=dateutil.tz.tzutc())._summary()
|
| 234 |
+
|
| 235 |
+
def test_dti_custom_business_repr(self):
|
| 236 |
+
# only really care that it works
|
| 237 |
+
repr(pd.bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1), freq="C"))
|
| 238 |
+
|
| 239 |
+
def test_dti_custom_business_summary(self):
|
| 240 |
+
rng = pd.bdate_range(datetime(2009, 1, 1), datetime(2010, 1, 1), freq="C")
|
| 241 |
+
rng._summary()
|
| 242 |
+
rng[2:2]._summary()
|
| 243 |
+
|
| 244 |
+
def test_dti_custom_business_summary_pytz(self):
|
| 245 |
+
pd.bdate_range("1/1/2005", "1/1/2009", freq="C", tz=pytz.utc)._summary()
|
| 246 |
+
|
| 247 |
+
def test_dti_custom_business_summary_dateutil(self):
|
| 248 |
+
pd.bdate_range(
|
| 249 |
+
"1/1/2005", "1/1/2009", freq="C", tz=dateutil.tz.tzutc()
|
| 250 |
+
)._summary()
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
class TestFormat:
|
| 254 |
+
def test_format_with_name_time_info(self):
|
| 255 |
+
# bug I fixed 12/20/2011
|
| 256 |
+
dates = pd.date_range("2011-01-01 04:00:00", periods=10, name="something")
|
| 257 |
+
|
| 258 |
+
formatted = dates.format(name=True)
|
| 259 |
+
assert formatted[0] == "something"
|
| 260 |
+
|
| 261 |
+
def test_format_datetime_with_time(self):
|
| 262 |
+
dti = DatetimeIndex([datetime(2012, 2, 7), datetime(2012, 2, 7, 23)])
|
| 263 |
+
|
| 264 |
+
result = dti.format()
|
| 265 |
+
expected = ["2012-02-07 00:00:00", "2012-02-07 23:00:00"]
|
| 266 |
+
assert len(result) == 2
|
| 267 |
+
assert result == expected
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_freq_attr.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from pandas import (
|
| 4 |
+
DatetimeIndex,
|
| 5 |
+
date_range,
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
from pandas.tseries.offsets import (
|
| 9 |
+
BDay,
|
| 10 |
+
DateOffset,
|
| 11 |
+
Day,
|
| 12 |
+
Hour,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TestFreq:
|
| 17 |
+
def test_freq_setter_errors(self):
|
| 18 |
+
# GH#20678
|
| 19 |
+
idx = DatetimeIndex(["20180101", "20180103", "20180105"])
|
| 20 |
+
|
| 21 |
+
# setting with an incompatible freq
|
| 22 |
+
msg = (
|
| 23 |
+
"Inferred frequency 2D from passed values does not conform to "
|
| 24 |
+
"passed frequency 5D"
|
| 25 |
+
)
|
| 26 |
+
with pytest.raises(ValueError, match=msg):
|
| 27 |
+
idx._data.freq = "5D"
|
| 28 |
+
|
| 29 |
+
# setting with non-freq string
|
| 30 |
+
with pytest.raises(ValueError, match="Invalid frequency"):
|
| 31 |
+
idx._data.freq = "foo"
|
| 32 |
+
|
| 33 |
+
@pytest.mark.parametrize("values", [["20180101", "20180103", "20180105"], []])
|
| 34 |
+
@pytest.mark.parametrize("freq", ["2D", Day(2), "2B", BDay(2), "48H", Hour(48)])
|
| 35 |
+
@pytest.mark.parametrize("tz", [None, "US/Eastern"])
|
| 36 |
+
def test_freq_setter(self, values, freq, tz):
|
| 37 |
+
# GH#20678
|
| 38 |
+
idx = DatetimeIndex(values, tz=tz)
|
| 39 |
+
|
| 40 |
+
# can set to an offset, converting from string if necessary
|
| 41 |
+
idx._data.freq = freq
|
| 42 |
+
assert idx.freq == freq
|
| 43 |
+
assert isinstance(idx.freq, DateOffset)
|
| 44 |
+
|
| 45 |
+
# can reset to None
|
| 46 |
+
idx._data.freq = None
|
| 47 |
+
assert idx.freq is None
|
| 48 |
+
|
| 49 |
+
def test_freq_view_safe(self):
|
| 50 |
+
# Setting the freq for one DatetimeIndex shouldn't alter the freq
|
| 51 |
+
# for another that views the same data
|
| 52 |
+
|
| 53 |
+
dti = date_range("2016-01-01", periods=5)
|
| 54 |
+
dta = dti._data
|
| 55 |
+
|
| 56 |
+
dti2 = DatetimeIndex(dta)._with_freq(None)
|
| 57 |
+
assert dti2.freq is None
|
| 58 |
+
|
| 59 |
+
# Original was not altered
|
| 60 |
+
assert dti.freq == "D"
|
| 61 |
+
assert dta.freq == "D"
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_indexing.py
ADDED
|
@@ -0,0 +1,712 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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+
timedelta,
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
import pytest
|
| 10 |
+
|
| 11 |
+
import pandas as pd
|
| 12 |
+
from pandas import (
|
| 13 |
+
DatetimeIndex,
|
| 14 |
+
Index,
|
| 15 |
+
Timestamp,
|
| 16 |
+
bdate_range,
|
| 17 |
+
date_range,
|
| 18 |
+
notna,
|
| 19 |
+
)
|
| 20 |
+
import pandas._testing as tm
|
| 21 |
+
|
| 22 |
+
from pandas.tseries.frequencies import to_offset
|
| 23 |
+
|
| 24 |
+
START, END = datetime(2009, 1, 1), datetime(2010, 1, 1)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class TestGetItem:
|
| 28 |
+
def test_getitem_slice_keeps_name(self):
|
| 29 |
+
# GH4226
|
| 30 |
+
st = Timestamp("2013-07-01 00:00:00", tz="America/Los_Angeles")
|
| 31 |
+
et = Timestamp("2013-07-02 00:00:00", tz="America/Los_Angeles")
|
| 32 |
+
dr = date_range(st, et, freq="H", name="timebucket")
|
| 33 |
+
assert dr[1:].name == dr.name
|
| 34 |
+
|
| 35 |
+
def test_getitem(self):
|
| 36 |
+
idx1 = date_range("2011-01-01", "2011-01-31", freq="D", name="idx")
|
| 37 |
+
idx2 = date_range(
|
| 38 |
+
"2011-01-01", "2011-01-31", freq="D", tz="Asia/Tokyo", name="idx"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
for idx in [idx1, idx2]:
|
| 42 |
+
result = idx[0]
|
| 43 |
+
assert result == Timestamp("2011-01-01", tz=idx.tz)
|
| 44 |
+
|
| 45 |
+
result = idx[0:5]
|
| 46 |
+
expected = date_range(
|
| 47 |
+
"2011-01-01", "2011-01-05", freq="D", tz=idx.tz, name="idx"
|
| 48 |
+
)
|
| 49 |
+
tm.assert_index_equal(result, expected)
|
| 50 |
+
assert result.freq == expected.freq
|
| 51 |
+
|
| 52 |
+
result = idx[0:10:2]
|
| 53 |
+
expected = date_range(
|
| 54 |
+
"2011-01-01", "2011-01-09", freq="2D", tz=idx.tz, name="idx"
|
| 55 |
+
)
|
| 56 |
+
tm.assert_index_equal(result, expected)
|
| 57 |
+
assert result.freq == expected.freq
|
| 58 |
+
|
| 59 |
+
result = idx[-20:-5:3]
|
| 60 |
+
expected = date_range(
|
| 61 |
+
"2011-01-12", "2011-01-24", freq="3D", tz=idx.tz, name="idx"
|
| 62 |
+
)
|
| 63 |
+
tm.assert_index_equal(result, expected)
|
| 64 |
+
assert result.freq == expected.freq
|
| 65 |
+
|
| 66 |
+
result = idx[4::-1]
|
| 67 |
+
expected = DatetimeIndex(
|
| 68 |
+
["2011-01-05", "2011-01-04", "2011-01-03", "2011-01-02", "2011-01-01"],
|
| 69 |
+
freq="-1D",
|
| 70 |
+
tz=idx.tz,
|
| 71 |
+
name="idx",
|
| 72 |
+
)
|
| 73 |
+
tm.assert_index_equal(result, expected)
|
| 74 |
+
assert result.freq == expected.freq
|
| 75 |
+
|
| 76 |
+
@pytest.mark.parametrize("freq", ["B", "C"])
|
| 77 |
+
def test_dti_business_getitem(self, freq):
|
| 78 |
+
rng = bdate_range(START, END, freq=freq)
|
| 79 |
+
smaller = rng[:5]
|
| 80 |
+
exp = DatetimeIndex(rng.view(np.ndarray)[:5], freq=freq)
|
| 81 |
+
tm.assert_index_equal(smaller, exp)
|
| 82 |
+
assert smaller.freq == exp.freq
|
| 83 |
+
assert smaller.freq == rng.freq
|
| 84 |
+
|
| 85 |
+
sliced = rng[::5]
|
| 86 |
+
assert sliced.freq == to_offset(freq) * 5
|
| 87 |
+
|
| 88 |
+
fancy_indexed = rng[[4, 3, 2, 1, 0]]
|
| 89 |
+
assert len(fancy_indexed) == 5
|
| 90 |
+
assert isinstance(fancy_indexed, DatetimeIndex)
|
| 91 |
+
assert fancy_indexed.freq is None
|
| 92 |
+
|
| 93 |
+
# 32-bit vs. 64-bit platforms
|
| 94 |
+
assert rng[4] == rng[np.int_(4)]
|
| 95 |
+
|
| 96 |
+
@pytest.mark.parametrize("freq", ["B", "C"])
|
| 97 |
+
def test_dti_business_getitem_matplotlib_hackaround(self, freq):
|
| 98 |
+
rng = bdate_range(START, END, freq=freq)
|
| 99 |
+
with pytest.raises(ValueError, match="Multi-dimensional indexing"):
|
| 100 |
+
# GH#30588 multi-dimensional indexing deprecated
|
| 101 |
+
rng[:, None]
|
| 102 |
+
|
| 103 |
+
def test_getitem_int_list(self):
|
| 104 |
+
dti = date_range(start="1/1/2005", end="12/1/2005", freq="M")
|
| 105 |
+
dti2 = dti[[1, 3, 5]]
|
| 106 |
+
|
| 107 |
+
v1 = dti2[0]
|
| 108 |
+
v2 = dti2[1]
|
| 109 |
+
v3 = dti2[2]
|
| 110 |
+
|
| 111 |
+
assert v1 == Timestamp("2/28/2005")
|
| 112 |
+
assert v2 == Timestamp("4/30/2005")
|
| 113 |
+
assert v3 == Timestamp("6/30/2005")
|
| 114 |
+
|
| 115 |
+
# getitem with non-slice drops freq
|
| 116 |
+
assert dti2.freq is None
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
class TestWhere:
|
| 120 |
+
def test_where_doesnt_retain_freq(self):
|
| 121 |
+
dti = date_range("20130101", periods=3, freq="D", name="idx")
|
| 122 |
+
cond = [True, True, False]
|
| 123 |
+
expected = DatetimeIndex([dti[0], dti[1], dti[0]], freq=None, name="idx")
|
| 124 |
+
|
| 125 |
+
result = dti.where(cond, dti[::-1])
|
| 126 |
+
tm.assert_index_equal(result, expected)
|
| 127 |
+
|
| 128 |
+
def test_where_other(self):
|
| 129 |
+
# other is ndarray or Index
|
| 130 |
+
i = date_range("20130101", periods=3, tz="US/Eastern")
|
| 131 |
+
|
| 132 |
+
for arr in [np.nan, pd.NaT]:
|
| 133 |
+
result = i.where(notna(i), other=arr)
|
| 134 |
+
expected = i
|
| 135 |
+
tm.assert_index_equal(result, expected)
|
| 136 |
+
|
| 137 |
+
i2 = i.copy()
|
| 138 |
+
i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
|
| 139 |
+
result = i.where(notna(i2), i2)
|
| 140 |
+
tm.assert_index_equal(result, i2)
|
| 141 |
+
|
| 142 |
+
i2 = i.copy()
|
| 143 |
+
i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
|
| 144 |
+
result = i.where(notna(i2), i2._values)
|
| 145 |
+
tm.assert_index_equal(result, i2)
|
| 146 |
+
|
| 147 |
+
def test_where_invalid_dtypes(self):
|
| 148 |
+
dti = date_range("20130101", periods=3, tz="US/Eastern")
|
| 149 |
+
|
| 150 |
+
tail = dti[2:].tolist()
|
| 151 |
+
i2 = Index([pd.NaT, pd.NaT] + tail)
|
| 152 |
+
|
| 153 |
+
mask = notna(i2)
|
| 154 |
+
|
| 155 |
+
# passing tz-naive ndarray to tzaware DTI
|
| 156 |
+
result = dti.where(mask, i2.values)
|
| 157 |
+
expected = Index([pd.NaT.asm8, pd.NaT.asm8] + tail, dtype=object)
|
| 158 |
+
tm.assert_index_equal(result, expected)
|
| 159 |
+
|
| 160 |
+
# passing tz-aware DTI to tznaive DTI
|
| 161 |
+
naive = dti.tz_localize(None)
|
| 162 |
+
result = naive.where(mask, i2)
|
| 163 |
+
expected = Index([i2[0], i2[1]] + naive[2:].tolist(), dtype=object)
|
| 164 |
+
tm.assert_index_equal(result, expected)
|
| 165 |
+
|
| 166 |
+
pi = i2.tz_localize(None).to_period("D")
|
| 167 |
+
result = dti.where(mask, pi)
|
| 168 |
+
expected = Index([pi[0], pi[1]] + tail, dtype=object)
|
| 169 |
+
tm.assert_index_equal(result, expected)
|
| 170 |
+
|
| 171 |
+
tda = i2.asi8.view("timedelta64[ns]")
|
| 172 |
+
result = dti.where(mask, tda)
|
| 173 |
+
expected = Index([tda[0], tda[1]] + tail, dtype=object)
|
| 174 |
+
assert isinstance(expected[0], np.timedelta64)
|
| 175 |
+
tm.assert_index_equal(result, expected)
|
| 176 |
+
|
| 177 |
+
result = dti.where(mask, i2.asi8)
|
| 178 |
+
expected = Index([pd.NaT._value, pd.NaT._value] + tail, dtype=object)
|
| 179 |
+
assert isinstance(expected[0], int)
|
| 180 |
+
tm.assert_index_equal(result, expected)
|
| 181 |
+
|
| 182 |
+
# non-matching scalar
|
| 183 |
+
td = pd.Timedelta(days=4)
|
| 184 |
+
result = dti.where(mask, td)
|
| 185 |
+
expected = Index([td, td] + tail, dtype=object)
|
| 186 |
+
assert expected[0] is td
|
| 187 |
+
tm.assert_index_equal(result, expected)
|
| 188 |
+
|
| 189 |
+
def test_where_mismatched_nat(self, tz_aware_fixture):
|
| 190 |
+
tz = tz_aware_fixture
|
| 191 |
+
dti = date_range("2013-01-01", periods=3, tz=tz)
|
| 192 |
+
cond = np.array([True, False, True])
|
| 193 |
+
|
| 194 |
+
tdnat = np.timedelta64("NaT", "ns")
|
| 195 |
+
expected = Index([dti[0], tdnat, dti[2]], dtype=object)
|
| 196 |
+
assert expected[1] is tdnat
|
| 197 |
+
|
| 198 |
+
result = dti.where(cond, tdnat)
|
| 199 |
+
tm.assert_index_equal(result, expected)
|
| 200 |
+
|
| 201 |
+
def test_where_tz(self):
|
| 202 |
+
i = date_range("20130101", periods=3, tz="US/Eastern")
|
| 203 |
+
result = i.where(notna(i))
|
| 204 |
+
expected = i
|
| 205 |
+
tm.assert_index_equal(result, expected)
|
| 206 |
+
|
| 207 |
+
i2 = i.copy()
|
| 208 |
+
i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist())
|
| 209 |
+
result = i.where(notna(i2))
|
| 210 |
+
expected = i2
|
| 211 |
+
tm.assert_index_equal(result, expected)
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
class TestTake:
|
| 215 |
+
def test_take_nan_first_datetime(self):
|
| 216 |
+
index = DatetimeIndex([pd.NaT, Timestamp("20130101"), Timestamp("20130102")])
|
| 217 |
+
result = index.take([-1, 0, 1])
|
| 218 |
+
expected = DatetimeIndex([index[-1], index[0], index[1]])
|
| 219 |
+
tm.assert_index_equal(result, expected)
|
| 220 |
+
|
| 221 |
+
def test_take(self):
|
| 222 |
+
# GH#10295
|
| 223 |
+
idx1 = date_range("2011-01-01", "2011-01-31", freq="D", name="idx")
|
| 224 |
+
idx2 = date_range(
|
| 225 |
+
"2011-01-01", "2011-01-31", freq="D", tz="Asia/Tokyo", name="idx"
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
for idx in [idx1, idx2]:
|
| 229 |
+
result = idx.take([0])
|
| 230 |
+
assert result == Timestamp("2011-01-01", tz=idx.tz)
|
| 231 |
+
|
| 232 |
+
result = idx.take([0, 1, 2])
|
| 233 |
+
expected = date_range(
|
| 234 |
+
"2011-01-01", "2011-01-03", freq="D", tz=idx.tz, name="idx"
|
| 235 |
+
)
|
| 236 |
+
tm.assert_index_equal(result, expected)
|
| 237 |
+
assert result.freq == expected.freq
|
| 238 |
+
|
| 239 |
+
result = idx.take([0, 2, 4])
|
| 240 |
+
expected = date_range(
|
| 241 |
+
"2011-01-01", "2011-01-05", freq="2D", tz=idx.tz, name="idx"
|
| 242 |
+
)
|
| 243 |
+
tm.assert_index_equal(result, expected)
|
| 244 |
+
assert result.freq == expected.freq
|
| 245 |
+
|
| 246 |
+
result = idx.take([7, 4, 1])
|
| 247 |
+
expected = date_range(
|
| 248 |
+
"2011-01-08", "2011-01-02", freq="-3D", tz=idx.tz, name="idx"
|
| 249 |
+
)
|
| 250 |
+
tm.assert_index_equal(result, expected)
|
| 251 |
+
assert result.freq == expected.freq
|
| 252 |
+
|
| 253 |
+
result = idx.take([3, 2, 5])
|
| 254 |
+
expected = DatetimeIndex(
|
| 255 |
+
["2011-01-04", "2011-01-03", "2011-01-06"],
|
| 256 |
+
freq=None,
|
| 257 |
+
tz=idx.tz,
|
| 258 |
+
name="idx",
|
| 259 |
+
)
|
| 260 |
+
tm.assert_index_equal(result, expected)
|
| 261 |
+
assert result.freq is None
|
| 262 |
+
|
| 263 |
+
result = idx.take([-3, 2, 5])
|
| 264 |
+
expected = DatetimeIndex(
|
| 265 |
+
["2011-01-29", "2011-01-03", "2011-01-06"],
|
| 266 |
+
freq=None,
|
| 267 |
+
tz=idx.tz,
|
| 268 |
+
name="idx",
|
| 269 |
+
)
|
| 270 |
+
tm.assert_index_equal(result, expected)
|
| 271 |
+
assert result.freq is None
|
| 272 |
+
|
| 273 |
+
def test_take_invalid_kwargs(self):
|
| 274 |
+
idx = date_range("2011-01-01", "2011-01-31", freq="D", name="idx")
|
| 275 |
+
indices = [1, 6, 5, 9, 10, 13, 15, 3]
|
| 276 |
+
|
| 277 |
+
msg = r"take\(\) got an unexpected keyword argument 'foo'"
|
| 278 |
+
with pytest.raises(TypeError, match=msg):
|
| 279 |
+
idx.take(indices, foo=2)
|
| 280 |
+
|
| 281 |
+
msg = "the 'out' parameter is not supported"
|
| 282 |
+
with pytest.raises(ValueError, match=msg):
|
| 283 |
+
idx.take(indices, out=indices)
|
| 284 |
+
|
| 285 |
+
msg = "the 'mode' parameter is not supported"
|
| 286 |
+
with pytest.raises(ValueError, match=msg):
|
| 287 |
+
idx.take(indices, mode="clip")
|
| 288 |
+
|
| 289 |
+
# TODO: This method came from test_datetime; de-dup with version above
|
| 290 |
+
@pytest.mark.parametrize("tz", [None, "US/Eastern", "Asia/Tokyo"])
|
| 291 |
+
def test_take2(self, tz):
|
| 292 |
+
dates = [
|
| 293 |
+
datetime(2010, 1, 1, 14),
|
| 294 |
+
datetime(2010, 1, 1, 15),
|
| 295 |
+
datetime(2010, 1, 1, 17),
|
| 296 |
+
datetime(2010, 1, 1, 21),
|
| 297 |
+
]
|
| 298 |
+
|
| 299 |
+
idx = date_range(
|
| 300 |
+
start="2010-01-01 09:00",
|
| 301 |
+
end="2010-02-01 09:00",
|
| 302 |
+
freq="H",
|
| 303 |
+
tz=tz,
|
| 304 |
+
name="idx",
|
| 305 |
+
)
|
| 306 |
+
expected = DatetimeIndex(dates, freq=None, name="idx", tz=tz)
|
| 307 |
+
|
| 308 |
+
taken1 = idx.take([5, 6, 8, 12])
|
| 309 |
+
taken2 = idx[[5, 6, 8, 12]]
|
| 310 |
+
|
| 311 |
+
for taken in [taken1, taken2]:
|
| 312 |
+
tm.assert_index_equal(taken, expected)
|
| 313 |
+
assert isinstance(taken, DatetimeIndex)
|
| 314 |
+
assert taken.freq is None
|
| 315 |
+
assert taken.tz == expected.tz
|
| 316 |
+
assert taken.name == expected.name
|
| 317 |
+
|
| 318 |
+
def test_take_fill_value(self):
|
| 319 |
+
# GH#12631
|
| 320 |
+
idx = DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], name="xxx")
|
| 321 |
+
result = idx.take(np.array([1, 0, -1]))
|
| 322 |
+
expected = DatetimeIndex(["2011-02-01", "2011-01-01", "2011-03-01"], name="xxx")
|
| 323 |
+
tm.assert_index_equal(result, expected)
|
| 324 |
+
|
| 325 |
+
# fill_value
|
| 326 |
+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
| 327 |
+
expected = DatetimeIndex(["2011-02-01", "2011-01-01", "NaT"], name="xxx")
|
| 328 |
+
tm.assert_index_equal(result, expected)
|
| 329 |
+
|
| 330 |
+
# allow_fill=False
|
| 331 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
| 332 |
+
expected = DatetimeIndex(["2011-02-01", "2011-01-01", "2011-03-01"], name="xxx")
|
| 333 |
+
tm.assert_index_equal(result, expected)
|
| 334 |
+
|
| 335 |
+
msg = (
|
| 336 |
+
"When allow_fill=True and fill_value is not None, "
|
| 337 |
+
"all indices must be >= -1"
|
| 338 |
+
)
|
| 339 |
+
with pytest.raises(ValueError, match=msg):
|
| 340 |
+
idx.take(np.array([1, 0, -2]), fill_value=True)
|
| 341 |
+
with pytest.raises(ValueError, match=msg):
|
| 342 |
+
idx.take(np.array([1, 0, -5]), fill_value=True)
|
| 343 |
+
|
| 344 |
+
msg = "out of bounds"
|
| 345 |
+
with pytest.raises(IndexError, match=msg):
|
| 346 |
+
idx.take(np.array([1, -5]))
|
| 347 |
+
|
| 348 |
+
def test_take_fill_value_with_timezone(self):
|
| 349 |
+
idx = DatetimeIndex(
|
| 350 |
+
["2011-01-01", "2011-02-01", "2011-03-01"], name="xxx", tz="US/Eastern"
|
| 351 |
+
)
|
| 352 |
+
result = idx.take(np.array([1, 0, -1]))
|
| 353 |
+
expected = DatetimeIndex(
|
| 354 |
+
["2011-02-01", "2011-01-01", "2011-03-01"], name="xxx", tz="US/Eastern"
|
| 355 |
+
)
|
| 356 |
+
tm.assert_index_equal(result, expected)
|
| 357 |
+
|
| 358 |
+
# fill_value
|
| 359 |
+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
| 360 |
+
expected = DatetimeIndex(
|
| 361 |
+
["2011-02-01", "2011-01-01", "NaT"], name="xxx", tz="US/Eastern"
|
| 362 |
+
)
|
| 363 |
+
tm.assert_index_equal(result, expected)
|
| 364 |
+
|
| 365 |
+
# allow_fill=False
|
| 366 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
| 367 |
+
expected = DatetimeIndex(
|
| 368 |
+
["2011-02-01", "2011-01-01", "2011-03-01"], name="xxx", tz="US/Eastern"
|
| 369 |
+
)
|
| 370 |
+
tm.assert_index_equal(result, expected)
|
| 371 |
+
|
| 372 |
+
msg = (
|
| 373 |
+
"When allow_fill=True and fill_value is not None, "
|
| 374 |
+
"all indices must be >= -1"
|
| 375 |
+
)
|
| 376 |
+
with pytest.raises(ValueError, match=msg):
|
| 377 |
+
idx.take(np.array([1, 0, -2]), fill_value=True)
|
| 378 |
+
with pytest.raises(ValueError, match=msg):
|
| 379 |
+
idx.take(np.array([1, 0, -5]), fill_value=True)
|
| 380 |
+
|
| 381 |
+
msg = "out of bounds"
|
| 382 |
+
with pytest.raises(IndexError, match=msg):
|
| 383 |
+
idx.take(np.array([1, -5]))
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
class TestGetLoc:
|
| 387 |
+
def test_get_loc_key_unit_mismatch(self):
|
| 388 |
+
idx = date_range("2000-01-01", periods=3)
|
| 389 |
+
key = idx[1].as_unit("ms")
|
| 390 |
+
loc = idx.get_loc(key)
|
| 391 |
+
assert loc == 1
|
| 392 |
+
assert key in idx
|
| 393 |
+
|
| 394 |
+
def test_get_loc_key_unit_mismatch_not_castable(self):
|
| 395 |
+
dta = date_range("2000-01-01", periods=3)._data.astype("M8[s]")
|
| 396 |
+
dti = DatetimeIndex(dta)
|
| 397 |
+
key = dta[0].as_unit("ns") + pd.Timedelta(1)
|
| 398 |
+
|
| 399 |
+
with pytest.raises(
|
| 400 |
+
KeyError, match=r"Timestamp\('2000-01-01 00:00:00.000000001'\)"
|
| 401 |
+
):
|
| 402 |
+
dti.get_loc(key)
|
| 403 |
+
|
| 404 |
+
assert key not in dti
|
| 405 |
+
|
| 406 |
+
def test_get_loc_time_obj(self):
|
| 407 |
+
# time indexing
|
| 408 |
+
idx = date_range("2000-01-01", periods=24, freq="H")
|
| 409 |
+
|
| 410 |
+
result = idx.get_loc(time(12))
|
| 411 |
+
expected = np.array([12])
|
| 412 |
+
tm.assert_numpy_array_equal(result, expected, check_dtype=False)
|
| 413 |
+
|
| 414 |
+
result = idx.get_loc(time(12, 30))
|
| 415 |
+
expected = np.array([])
|
| 416 |
+
tm.assert_numpy_array_equal(result, expected, check_dtype=False)
|
| 417 |
+
|
| 418 |
+
def test_get_loc_time_obj2(self):
|
| 419 |
+
# GH#8667
|
| 420 |
+
|
| 421 |
+
from pandas._libs.index import _SIZE_CUTOFF
|
| 422 |
+
|
| 423 |
+
ns = _SIZE_CUTOFF + np.array([-100, 100], dtype=np.int64)
|
| 424 |
+
key = time(15, 11, 30)
|
| 425 |
+
start = key.hour * 3600 + key.minute * 60 + key.second
|
| 426 |
+
step = 24 * 3600
|
| 427 |
+
|
| 428 |
+
for n in ns:
|
| 429 |
+
idx = date_range("2014-11-26", periods=n, freq="S")
|
| 430 |
+
ts = pd.Series(np.random.randn(n), index=idx)
|
| 431 |
+
locs = np.arange(start, n, step, dtype=np.intp)
|
| 432 |
+
|
| 433 |
+
result = ts.index.get_loc(key)
|
| 434 |
+
tm.assert_numpy_array_equal(result, locs)
|
| 435 |
+
tm.assert_series_equal(ts[key], ts.iloc[locs])
|
| 436 |
+
|
| 437 |
+
left, right = ts.copy(), ts.copy()
|
| 438 |
+
left[key] *= -10
|
| 439 |
+
right.iloc[locs] *= -10
|
| 440 |
+
tm.assert_series_equal(left, right)
|
| 441 |
+
|
| 442 |
+
def test_get_loc_time_nat(self):
|
| 443 |
+
# GH#35114
|
| 444 |
+
# Case where key's total microseconds happens to match iNaT % 1e6 // 1000
|
| 445 |
+
tic = time(minute=12, second=43, microsecond=145224)
|
| 446 |
+
dti = DatetimeIndex([pd.NaT])
|
| 447 |
+
|
| 448 |
+
loc = dti.get_loc(tic)
|
| 449 |
+
expected = np.array([], dtype=np.intp)
|
| 450 |
+
tm.assert_numpy_array_equal(loc, expected)
|
| 451 |
+
|
| 452 |
+
def test_get_loc_nat(self):
|
| 453 |
+
# GH#20464
|
| 454 |
+
index = DatetimeIndex(["1/3/2000", "NaT"])
|
| 455 |
+
assert index.get_loc(pd.NaT) == 1
|
| 456 |
+
|
| 457 |
+
assert index.get_loc(None) == 1
|
| 458 |
+
|
| 459 |
+
assert index.get_loc(np.nan) == 1
|
| 460 |
+
|
| 461 |
+
assert index.get_loc(pd.NA) == 1
|
| 462 |
+
|
| 463 |
+
assert index.get_loc(np.datetime64("NaT")) == 1
|
| 464 |
+
|
| 465 |
+
with pytest.raises(KeyError, match="NaT"):
|
| 466 |
+
index.get_loc(np.timedelta64("NaT"))
|
| 467 |
+
|
| 468 |
+
@pytest.mark.parametrize("key", [pd.Timedelta(0), pd.Timedelta(1), timedelta(0)])
|
| 469 |
+
def test_get_loc_timedelta_invalid_key(self, key):
|
| 470 |
+
# GH#20464
|
| 471 |
+
dti = date_range("1970-01-01", periods=10)
|
| 472 |
+
msg = "Cannot index DatetimeIndex with [Tt]imedelta"
|
| 473 |
+
with pytest.raises(TypeError, match=msg):
|
| 474 |
+
dti.get_loc(key)
|
| 475 |
+
|
| 476 |
+
def test_get_loc_reasonable_key_error(self):
|
| 477 |
+
# GH#1062
|
| 478 |
+
index = DatetimeIndex(["1/3/2000"])
|
| 479 |
+
with pytest.raises(KeyError, match="2000"):
|
| 480 |
+
index.get_loc("1/1/2000")
|
| 481 |
+
|
| 482 |
+
def test_get_loc_year_str(self):
|
| 483 |
+
rng = date_range("1/1/2000", "1/1/2010")
|
| 484 |
+
|
| 485 |
+
result = rng.get_loc("2009")
|
| 486 |
+
expected = slice(3288, 3653)
|
| 487 |
+
assert result == expected
|
| 488 |
+
|
| 489 |
+
|
| 490 |
+
class TestContains:
|
| 491 |
+
def test_dti_contains_with_duplicates(self):
|
| 492 |
+
d = datetime(2011, 12, 5, 20, 30)
|
| 493 |
+
ix = DatetimeIndex([d, d])
|
| 494 |
+
assert d in ix
|
| 495 |
+
|
| 496 |
+
@pytest.mark.parametrize(
|
| 497 |
+
"vals",
|
| 498 |
+
[
|
| 499 |
+
[0, 1, 0],
|
| 500 |
+
[0, 0, -1],
|
| 501 |
+
[0, -1, -1],
|
| 502 |
+
["2015", "2015", "2016"],
|
| 503 |
+
["2015", "2015", "2014"],
|
| 504 |
+
],
|
| 505 |
+
)
|
| 506 |
+
def test_contains_nonunique(self, vals):
|
| 507 |
+
# GH#9512
|
| 508 |
+
idx = DatetimeIndex(vals)
|
| 509 |
+
assert idx[0] in idx
|
| 510 |
+
|
| 511 |
+
|
| 512 |
+
class TestGetIndexer:
|
| 513 |
+
def test_get_indexer_date_objs(self):
|
| 514 |
+
rng = date_range("1/1/2000", periods=20)
|
| 515 |
+
|
| 516 |
+
result = rng.get_indexer(rng.map(lambda x: x.date()))
|
| 517 |
+
expected = rng.get_indexer(rng)
|
| 518 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 519 |
+
|
| 520 |
+
def test_get_indexer(self):
|
| 521 |
+
idx = date_range("2000-01-01", periods=3)
|
| 522 |
+
exp = np.array([0, 1, 2], dtype=np.intp)
|
| 523 |
+
tm.assert_numpy_array_equal(idx.get_indexer(idx), exp)
|
| 524 |
+
|
| 525 |
+
target = idx[0] + pd.to_timedelta(["-1 hour", "12 hours", "1 day 1 hour"])
|
| 526 |
+
tm.assert_numpy_array_equal(
|
| 527 |
+
idx.get_indexer(target, "pad"), np.array([-1, 0, 1], dtype=np.intp)
|
| 528 |
+
)
|
| 529 |
+
tm.assert_numpy_array_equal(
|
| 530 |
+
idx.get_indexer(target, "backfill"), np.array([0, 1, 2], dtype=np.intp)
|
| 531 |
+
)
|
| 532 |
+
tm.assert_numpy_array_equal(
|
| 533 |
+
idx.get_indexer(target, "nearest"), np.array([0, 1, 1], dtype=np.intp)
|
| 534 |
+
)
|
| 535 |
+
tm.assert_numpy_array_equal(
|
| 536 |
+
idx.get_indexer(target, "nearest", tolerance=pd.Timedelta("1 hour")),
|
| 537 |
+
np.array([0, -1, 1], dtype=np.intp),
|
| 538 |
+
)
|
| 539 |
+
tol_raw = [
|
| 540 |
+
pd.Timedelta("1 hour"),
|
| 541 |
+
pd.Timedelta("1 hour"),
|
| 542 |
+
pd.Timedelta("1 hour").to_timedelta64(),
|
| 543 |
+
]
|
| 544 |
+
tm.assert_numpy_array_equal(
|
| 545 |
+
idx.get_indexer(
|
| 546 |
+
target, "nearest", tolerance=[np.timedelta64(x) for x in tol_raw]
|
| 547 |
+
),
|
| 548 |
+
np.array([0, -1, 1], dtype=np.intp),
|
| 549 |
+
)
|
| 550 |
+
tol_bad = [
|
| 551 |
+
pd.Timedelta("2 hour").to_timedelta64(),
|
| 552 |
+
pd.Timedelta("1 hour").to_timedelta64(),
|
| 553 |
+
"foo",
|
| 554 |
+
]
|
| 555 |
+
msg = "Could not convert 'foo' to NumPy timedelta"
|
| 556 |
+
with pytest.raises(ValueError, match=msg):
|
| 557 |
+
idx.get_indexer(target, "nearest", tolerance=tol_bad)
|
| 558 |
+
with pytest.raises(ValueError, match="abbreviation w/o a number"):
|
| 559 |
+
idx.get_indexer(idx[[0]], method="nearest", tolerance="foo")
|
| 560 |
+
|
| 561 |
+
@pytest.mark.parametrize(
|
| 562 |
+
"target",
|
| 563 |
+
[
|
| 564 |
+
[date(2020, 1, 1), Timestamp("2020-01-02")],
|
| 565 |
+
[Timestamp("2020-01-01"), date(2020, 1, 2)],
|
| 566 |
+
],
|
| 567 |
+
)
|
| 568 |
+
def test_get_indexer_mixed_dtypes(self, target):
|
| 569 |
+
# https://github.com/pandas-dev/pandas/issues/33741
|
| 570 |
+
values = DatetimeIndex([Timestamp("2020-01-01"), Timestamp("2020-01-02")])
|
| 571 |
+
result = values.get_indexer(target)
|
| 572 |
+
expected = np.array([0, 1], dtype=np.intp)
|
| 573 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 574 |
+
|
| 575 |
+
@pytest.mark.parametrize(
|
| 576 |
+
"target, positions",
|
| 577 |
+
[
|
| 578 |
+
([date(9999, 1, 1), Timestamp("2020-01-01")], [-1, 0]),
|
| 579 |
+
([Timestamp("2020-01-01"), date(9999, 1, 1)], [0, -1]),
|
| 580 |
+
([date(9999, 1, 1), date(9999, 1, 1)], [-1, -1]),
|
| 581 |
+
],
|
| 582 |
+
)
|
| 583 |
+
def test_get_indexer_out_of_bounds_date(self, target, positions):
|
| 584 |
+
values = DatetimeIndex([Timestamp("2020-01-01"), Timestamp("2020-01-02")])
|
| 585 |
+
|
| 586 |
+
result = values.get_indexer(target)
|
| 587 |
+
expected = np.array(positions, dtype=np.intp)
|
| 588 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 589 |
+
|
| 590 |
+
def test_get_indexer_pad_requires_monotonicity(self):
|
| 591 |
+
rng = date_range("1/1/2000", "3/1/2000", freq="B")
|
| 592 |
+
|
| 593 |
+
# neither monotonic increasing or decreasing
|
| 594 |
+
rng2 = rng[[1, 0, 2]]
|
| 595 |
+
|
| 596 |
+
msg = "index must be monotonic increasing or decreasing"
|
| 597 |
+
with pytest.raises(ValueError, match=msg):
|
| 598 |
+
rng2.get_indexer(rng, method="pad")
|
| 599 |
+
|
| 600 |
+
|
| 601 |
+
class TestMaybeCastSliceBound:
|
| 602 |
+
def test_maybe_cast_slice_bounds_empty(self):
|
| 603 |
+
# GH#14354
|
| 604 |
+
empty_idx = date_range(freq="1H", periods=0, end="2015")
|
| 605 |
+
|
| 606 |
+
right = empty_idx._maybe_cast_slice_bound("2015-01-02", "right")
|
| 607 |
+
exp = Timestamp("2015-01-02 23:59:59.999999999")
|
| 608 |
+
assert right == exp
|
| 609 |
+
|
| 610 |
+
left = empty_idx._maybe_cast_slice_bound("2015-01-02", "left")
|
| 611 |
+
exp = Timestamp("2015-01-02 00:00:00")
|
| 612 |
+
assert left == exp
|
| 613 |
+
|
| 614 |
+
def test_maybe_cast_slice_duplicate_monotonic(self):
|
| 615 |
+
# https://github.com/pandas-dev/pandas/issues/16515
|
| 616 |
+
idx = DatetimeIndex(["2017", "2017"])
|
| 617 |
+
result = idx._maybe_cast_slice_bound("2017-01-01", "left")
|
| 618 |
+
expected = Timestamp("2017-01-01")
|
| 619 |
+
assert result == expected
|
| 620 |
+
|
| 621 |
+
|
| 622 |
+
class TestGetSliceBounds:
|
| 623 |
+
@pytest.mark.parametrize("box", [date, datetime, Timestamp])
|
| 624 |
+
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
|
| 625 |
+
def test_get_slice_bounds_datetime_within(
|
| 626 |
+
self, box, side, expected, tz_aware_fixture
|
| 627 |
+
):
|
| 628 |
+
# GH 35690
|
| 629 |
+
tz = tz_aware_fixture
|
| 630 |
+
index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz)
|
| 631 |
+
key = box(year=2000, month=1, day=7)
|
| 632 |
+
|
| 633 |
+
if tz is not None:
|
| 634 |
+
with pytest.raises(TypeError, match="Cannot compare tz-naive"):
|
| 635 |
+
# GH#36148 we require tzawareness-compat as of 2.0
|
| 636 |
+
index.get_slice_bound(key, side=side)
|
| 637 |
+
else:
|
| 638 |
+
result = index.get_slice_bound(key, side=side)
|
| 639 |
+
assert result == expected
|
| 640 |
+
|
| 641 |
+
@pytest.mark.parametrize("box", [datetime, Timestamp])
|
| 642 |
+
@pytest.mark.parametrize("side", ["left", "right"])
|
| 643 |
+
@pytest.mark.parametrize("year, expected", [(1999, 0), (2020, 30)])
|
| 644 |
+
def test_get_slice_bounds_datetime_outside(
|
| 645 |
+
self, box, side, year, expected, tz_aware_fixture
|
| 646 |
+
):
|
| 647 |
+
# GH 35690
|
| 648 |
+
tz = tz_aware_fixture
|
| 649 |
+
index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz)
|
| 650 |
+
key = box(year=year, month=1, day=7)
|
| 651 |
+
|
| 652 |
+
if tz is not None:
|
| 653 |
+
with pytest.raises(TypeError, match="Cannot compare tz-naive"):
|
| 654 |
+
# GH#36148 we require tzawareness-compat as of 2.0
|
| 655 |
+
index.get_slice_bound(key, side=side)
|
| 656 |
+
else:
|
| 657 |
+
result = index.get_slice_bound(key, side=side)
|
| 658 |
+
assert result == expected
|
| 659 |
+
|
| 660 |
+
@pytest.mark.parametrize("box", [datetime, Timestamp])
|
| 661 |
+
def test_slice_datetime_locs(self, box, tz_aware_fixture):
|
| 662 |
+
# GH 34077
|
| 663 |
+
tz = tz_aware_fixture
|
| 664 |
+
index = DatetimeIndex(["2010-01-01", "2010-01-03"]).tz_localize(tz)
|
| 665 |
+
key = box(2010, 1, 1)
|
| 666 |
+
|
| 667 |
+
if tz is not None:
|
| 668 |
+
with pytest.raises(TypeError, match="Cannot compare tz-naive"):
|
| 669 |
+
# GH#36148 we require tzawareness-compat as of 2.0
|
| 670 |
+
index.slice_locs(key, box(2010, 1, 2))
|
| 671 |
+
else:
|
| 672 |
+
result = index.slice_locs(key, box(2010, 1, 2))
|
| 673 |
+
expected = (0, 1)
|
| 674 |
+
assert result == expected
|
| 675 |
+
|
| 676 |
+
|
| 677 |
+
class TestIndexerBetweenTime:
|
| 678 |
+
def test_indexer_between_time(self):
|
| 679 |
+
# GH#11818
|
| 680 |
+
rng = date_range("1/1/2000", "1/5/2000", freq="5min")
|
| 681 |
+
msg = r"Cannot convert arg \[datetime\.datetime\(2010, 1, 2, 1, 0\)\] to a time"
|
| 682 |
+
with pytest.raises(ValueError, match=msg):
|
| 683 |
+
rng.indexer_between_time(datetime(2010, 1, 2, 1), datetime(2010, 1, 2, 5))
|
| 684 |
+
|
| 685 |
+
@pytest.mark.parametrize("unit", ["us", "ms", "s"])
|
| 686 |
+
def test_indexer_between_time_non_nano(self, unit):
|
| 687 |
+
# For simple cases like this, the non-nano indexer_between_time
|
| 688 |
+
# should match the nano result
|
| 689 |
+
|
| 690 |
+
rng = date_range("1/1/2000", "1/5/2000", freq="5min")
|
| 691 |
+
arr_nano = rng._data._ndarray
|
| 692 |
+
|
| 693 |
+
arr = arr_nano.astype(f"M8[{unit}]")
|
| 694 |
+
|
| 695 |
+
dta = type(rng._data)._simple_new(arr, dtype=arr.dtype)
|
| 696 |
+
dti = DatetimeIndex(dta)
|
| 697 |
+
assert dti.dtype == arr.dtype
|
| 698 |
+
|
| 699 |
+
tic = time(1, 25)
|
| 700 |
+
toc = time(2, 29)
|
| 701 |
+
|
| 702 |
+
result = dti.indexer_between_time(tic, toc)
|
| 703 |
+
expected = rng.indexer_between_time(tic, toc)
|
| 704 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 705 |
+
|
| 706 |
+
# case with non-zero micros in arguments
|
| 707 |
+
tic = time(1, 25, 0, 45678)
|
| 708 |
+
toc = time(2, 29, 0, 1234)
|
| 709 |
+
|
| 710 |
+
result = dti.indexer_between_time(tic, toc)
|
| 711 |
+
expected = rng.indexer_between_time(tic, toc)
|
| 712 |
+
tm.assert_numpy_array_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_join.py
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import (
|
| 2 |
+
datetime,
|
| 3 |
+
timezone,
|
| 4 |
+
)
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
+
from pandas import (
|
| 10 |
+
DatetimeIndex,
|
| 11 |
+
Index,
|
| 12 |
+
Timestamp,
|
| 13 |
+
date_range,
|
| 14 |
+
to_datetime,
|
| 15 |
+
)
|
| 16 |
+
import pandas._testing as tm
|
| 17 |
+
|
| 18 |
+
from pandas.tseries.offsets import (
|
| 19 |
+
BDay,
|
| 20 |
+
BMonthEnd,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class TestJoin:
|
| 25 |
+
def test_does_not_convert_mixed_integer(self):
|
| 26 |
+
df = tm.makeCustomDataframe(
|
| 27 |
+
10,
|
| 28 |
+
10,
|
| 29 |
+
data_gen_f=lambda *args, **kwargs: np.random.randn(),
|
| 30 |
+
r_idx_type="i",
|
| 31 |
+
c_idx_type="dt",
|
| 32 |
+
)
|
| 33 |
+
cols = df.columns.join(df.index, how="outer")
|
| 34 |
+
joined = cols.join(df.columns)
|
| 35 |
+
assert cols.dtype == np.dtype("O")
|
| 36 |
+
assert cols.dtype == joined.dtype
|
| 37 |
+
tm.assert_numpy_array_equal(cols.values, joined.values)
|
| 38 |
+
|
| 39 |
+
def test_join_self(self, join_type):
|
| 40 |
+
index = date_range("1/1/2000", periods=10)
|
| 41 |
+
joined = index.join(index, how=join_type)
|
| 42 |
+
assert index is joined
|
| 43 |
+
|
| 44 |
+
def test_join_with_period_index(self, join_type):
|
| 45 |
+
df = tm.makeCustomDataframe(
|
| 46 |
+
10,
|
| 47 |
+
10,
|
| 48 |
+
data_gen_f=lambda *args: np.random.randint(2),
|
| 49 |
+
c_idx_type="p",
|
| 50 |
+
r_idx_type="dt",
|
| 51 |
+
)
|
| 52 |
+
s = df.iloc[:5, 0]
|
| 53 |
+
|
| 54 |
+
expected = df.columns.astype("O").join(s.index, how=join_type)
|
| 55 |
+
result = df.columns.join(s.index, how=join_type)
|
| 56 |
+
tm.assert_index_equal(expected, result)
|
| 57 |
+
|
| 58 |
+
def test_join_object_index(self):
|
| 59 |
+
rng = date_range("1/1/2000", periods=10)
|
| 60 |
+
idx = Index(["a", "b", "c", "d"])
|
| 61 |
+
|
| 62 |
+
result = rng.join(idx, how="outer")
|
| 63 |
+
assert isinstance(result[0], Timestamp)
|
| 64 |
+
|
| 65 |
+
def test_join_utc_convert(self, join_type):
|
| 66 |
+
rng = date_range("1/1/2011", periods=100, freq="H", tz="utc")
|
| 67 |
+
|
| 68 |
+
left = rng.tz_convert("US/Eastern")
|
| 69 |
+
right = rng.tz_convert("Europe/Berlin")
|
| 70 |
+
|
| 71 |
+
result = left.join(left[:-5], how=join_type)
|
| 72 |
+
assert isinstance(result, DatetimeIndex)
|
| 73 |
+
assert result.tz == left.tz
|
| 74 |
+
|
| 75 |
+
result = left.join(right[:-5], how=join_type)
|
| 76 |
+
assert isinstance(result, DatetimeIndex)
|
| 77 |
+
assert result.tz is timezone.utc
|
| 78 |
+
|
| 79 |
+
def test_datetimeindex_union_join_empty(self, sort):
|
| 80 |
+
dti = date_range(start="1/1/2001", end="2/1/2001", freq="D")
|
| 81 |
+
empty = Index([])
|
| 82 |
+
|
| 83 |
+
result = dti.union(empty, sort=sort)
|
| 84 |
+
expected = dti.astype("O")
|
| 85 |
+
tm.assert_index_equal(result, expected)
|
| 86 |
+
|
| 87 |
+
result = dti.join(empty)
|
| 88 |
+
assert isinstance(result, DatetimeIndex)
|
| 89 |
+
tm.assert_index_equal(result, dti)
|
| 90 |
+
|
| 91 |
+
def test_join_nonunique(self):
|
| 92 |
+
idx1 = to_datetime(["2012-11-06 16:00:11.477563", "2012-11-06 16:00:11.477563"])
|
| 93 |
+
idx2 = to_datetime(["2012-11-06 15:11:09.006507", "2012-11-06 15:11:09.006507"])
|
| 94 |
+
rs = idx1.join(idx2, how="outer")
|
| 95 |
+
assert rs.is_monotonic_increasing
|
| 96 |
+
|
| 97 |
+
@pytest.mark.parametrize("freq", ["B", "C"])
|
| 98 |
+
def test_outer_join(self, freq):
|
| 99 |
+
# should just behave as union
|
| 100 |
+
start, end = datetime(2009, 1, 1), datetime(2010, 1, 1)
|
| 101 |
+
rng = date_range(start=start, end=end, freq=freq)
|
| 102 |
+
|
| 103 |
+
# overlapping
|
| 104 |
+
left = rng[:10]
|
| 105 |
+
right = rng[5:10]
|
| 106 |
+
|
| 107 |
+
the_join = left.join(right, how="outer")
|
| 108 |
+
assert isinstance(the_join, DatetimeIndex)
|
| 109 |
+
|
| 110 |
+
# non-overlapping, gap in middle
|
| 111 |
+
left = rng[:5]
|
| 112 |
+
right = rng[10:]
|
| 113 |
+
|
| 114 |
+
the_join = left.join(right, how="outer")
|
| 115 |
+
assert isinstance(the_join, DatetimeIndex)
|
| 116 |
+
assert the_join.freq is None
|
| 117 |
+
|
| 118 |
+
# non-overlapping, no gap
|
| 119 |
+
left = rng[:5]
|
| 120 |
+
right = rng[5:10]
|
| 121 |
+
|
| 122 |
+
the_join = left.join(right, how="outer")
|
| 123 |
+
assert isinstance(the_join, DatetimeIndex)
|
| 124 |
+
|
| 125 |
+
# overlapping, but different offset
|
| 126 |
+
other = date_range(start, end, freq=BMonthEnd())
|
| 127 |
+
|
| 128 |
+
the_join = rng.join(other, how="outer")
|
| 129 |
+
assert isinstance(the_join, DatetimeIndex)
|
| 130 |
+
assert the_join.freq is None
|
| 131 |
+
|
| 132 |
+
def test_naive_aware_conflicts(self):
|
| 133 |
+
start, end = datetime(2009, 1, 1), datetime(2010, 1, 1)
|
| 134 |
+
naive = date_range(start, end, freq=BDay(), tz=None)
|
| 135 |
+
aware = date_range(start, end, freq=BDay(), tz="Asia/Hong_Kong")
|
| 136 |
+
|
| 137 |
+
msg = "tz-naive.*tz-aware"
|
| 138 |
+
with pytest.raises(TypeError, match=msg):
|
| 139 |
+
naive.join(aware)
|
| 140 |
+
|
| 141 |
+
with pytest.raises(TypeError, match=msg):
|
| 142 |
+
aware.join(naive)
|
| 143 |
+
|
| 144 |
+
@pytest.mark.parametrize("tz", [None, "US/Pacific"])
|
| 145 |
+
def test_join_preserves_freq(self, tz):
|
| 146 |
+
# GH#32157
|
| 147 |
+
dti = date_range("2016-01-01", periods=10, tz=tz)
|
| 148 |
+
result = dti[:5].join(dti[5:], how="outer")
|
| 149 |
+
assert result.freq == dti.freq
|
| 150 |
+
tm.assert_index_equal(result, dti)
|
| 151 |
+
|
| 152 |
+
result = dti[:5].join(dti[6:], how="outer")
|
| 153 |
+
assert result.freq is None
|
| 154 |
+
expected = dti.delete(5)
|
| 155 |
+
tm.assert_index_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/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], dtype="<U8")
|
| 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="M", 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)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_misc.py
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import calendar
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
import locale
|
| 4 |
+
import unicodedata
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from pandas import (
|
| 11 |
+
DatetimeIndex,
|
| 12 |
+
Index,
|
| 13 |
+
Timedelta,
|
| 14 |
+
Timestamp,
|
| 15 |
+
date_range,
|
| 16 |
+
offsets,
|
| 17 |
+
)
|
| 18 |
+
import pandas._testing as tm
|
| 19 |
+
from pandas.core.arrays import DatetimeArray
|
| 20 |
+
|
| 21 |
+
from pandas.tseries.frequencies import to_offset
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class TestDatetime64:
|
| 25 |
+
def test_no_millisecond_field(self):
|
| 26 |
+
msg = "type object 'DatetimeIndex' has no attribute 'millisecond'"
|
| 27 |
+
with pytest.raises(AttributeError, match=msg):
|
| 28 |
+
DatetimeIndex.millisecond
|
| 29 |
+
|
| 30 |
+
msg = "'DatetimeIndex' object has no attribute 'millisecond'"
|
| 31 |
+
with pytest.raises(AttributeError, match=msg):
|
| 32 |
+
DatetimeIndex([]).millisecond
|
| 33 |
+
|
| 34 |
+
def test_datetimeindex_accessors(self):
|
| 35 |
+
dti_naive = date_range(freq="D", start=datetime(1998, 1, 1), periods=365)
|
| 36 |
+
# GH#13303
|
| 37 |
+
dti_tz = date_range(
|
| 38 |
+
freq="D", start=datetime(1998, 1, 1), periods=365, tz="US/Eastern"
|
| 39 |
+
)
|
| 40 |
+
for dti in [dti_naive, dti_tz]:
|
| 41 |
+
assert dti.year[0] == 1998
|
| 42 |
+
assert dti.month[0] == 1
|
| 43 |
+
assert dti.day[0] == 1
|
| 44 |
+
assert dti.hour[0] == 0
|
| 45 |
+
assert dti.minute[0] == 0
|
| 46 |
+
assert dti.second[0] == 0
|
| 47 |
+
assert dti.microsecond[0] == 0
|
| 48 |
+
assert dti.dayofweek[0] == 3
|
| 49 |
+
|
| 50 |
+
assert dti.dayofyear[0] == 1
|
| 51 |
+
assert dti.dayofyear[120] == 121
|
| 52 |
+
|
| 53 |
+
assert dti.isocalendar().week[0] == 1
|
| 54 |
+
assert dti.isocalendar().week[120] == 18
|
| 55 |
+
|
| 56 |
+
assert dti.quarter[0] == 1
|
| 57 |
+
assert dti.quarter[120] == 2
|
| 58 |
+
|
| 59 |
+
assert dti.days_in_month[0] == 31
|
| 60 |
+
assert dti.days_in_month[90] == 30
|
| 61 |
+
|
| 62 |
+
assert dti.is_month_start[0]
|
| 63 |
+
assert not dti.is_month_start[1]
|
| 64 |
+
assert dti.is_month_start[31]
|
| 65 |
+
assert dti.is_quarter_start[0]
|
| 66 |
+
assert dti.is_quarter_start[90]
|
| 67 |
+
assert dti.is_year_start[0]
|
| 68 |
+
assert not dti.is_year_start[364]
|
| 69 |
+
assert not dti.is_month_end[0]
|
| 70 |
+
assert dti.is_month_end[30]
|
| 71 |
+
assert not dti.is_month_end[31]
|
| 72 |
+
assert dti.is_month_end[364]
|
| 73 |
+
assert not dti.is_quarter_end[0]
|
| 74 |
+
assert not dti.is_quarter_end[30]
|
| 75 |
+
assert dti.is_quarter_end[89]
|
| 76 |
+
assert dti.is_quarter_end[364]
|
| 77 |
+
assert not dti.is_year_end[0]
|
| 78 |
+
assert dti.is_year_end[364]
|
| 79 |
+
|
| 80 |
+
assert len(dti.year) == 365
|
| 81 |
+
assert len(dti.month) == 365
|
| 82 |
+
assert len(dti.day) == 365
|
| 83 |
+
assert len(dti.hour) == 365
|
| 84 |
+
assert len(dti.minute) == 365
|
| 85 |
+
assert len(dti.second) == 365
|
| 86 |
+
assert len(dti.microsecond) == 365
|
| 87 |
+
assert len(dti.dayofweek) == 365
|
| 88 |
+
assert len(dti.dayofyear) == 365
|
| 89 |
+
assert len(dti.isocalendar()) == 365
|
| 90 |
+
assert len(dti.quarter) == 365
|
| 91 |
+
assert len(dti.is_month_start) == 365
|
| 92 |
+
assert len(dti.is_month_end) == 365
|
| 93 |
+
assert len(dti.is_quarter_start) == 365
|
| 94 |
+
assert len(dti.is_quarter_end) == 365
|
| 95 |
+
assert len(dti.is_year_start) == 365
|
| 96 |
+
assert len(dti.is_year_end) == 365
|
| 97 |
+
|
| 98 |
+
dti.name = "name"
|
| 99 |
+
|
| 100 |
+
# non boolean accessors -> return Index
|
| 101 |
+
for accessor in DatetimeArray._field_ops:
|
| 102 |
+
res = getattr(dti, accessor)
|
| 103 |
+
assert len(res) == 365
|
| 104 |
+
assert isinstance(res, Index)
|
| 105 |
+
assert res.name == "name"
|
| 106 |
+
|
| 107 |
+
# boolean accessors -> return array
|
| 108 |
+
for accessor in DatetimeArray._bool_ops:
|
| 109 |
+
res = getattr(dti, accessor)
|
| 110 |
+
assert len(res) == 365
|
| 111 |
+
assert isinstance(res, np.ndarray)
|
| 112 |
+
|
| 113 |
+
# test boolean indexing
|
| 114 |
+
res = dti[dti.is_quarter_start]
|
| 115 |
+
exp = dti[[0, 90, 181, 273]]
|
| 116 |
+
tm.assert_index_equal(res, exp)
|
| 117 |
+
res = dti[dti.is_leap_year]
|
| 118 |
+
exp = DatetimeIndex([], freq="D", tz=dti.tz, name="name")
|
| 119 |
+
tm.assert_index_equal(res, exp)
|
| 120 |
+
|
| 121 |
+
def test_datetimeindex_accessors2(self):
|
| 122 |
+
dti = date_range(freq="BQ-FEB", start=datetime(1998, 1, 1), periods=4)
|
| 123 |
+
|
| 124 |
+
assert sum(dti.is_quarter_start) == 0
|
| 125 |
+
assert sum(dti.is_quarter_end) == 4
|
| 126 |
+
assert sum(dti.is_year_start) == 0
|
| 127 |
+
assert sum(dti.is_year_end) == 1
|
| 128 |
+
|
| 129 |
+
def test_datetimeindex_accessors3(self):
|
| 130 |
+
# Ensure is_start/end accessors throw ValueError for CustomBusinessDay,
|
| 131 |
+
bday_egypt = offsets.CustomBusinessDay(weekmask="Sun Mon Tue Wed Thu")
|
| 132 |
+
dti = date_range(datetime(2013, 4, 30), periods=5, freq=bday_egypt)
|
| 133 |
+
msg = "Custom business days is not supported by is_month_start"
|
| 134 |
+
with pytest.raises(ValueError, match=msg):
|
| 135 |
+
dti.is_month_start
|
| 136 |
+
|
| 137 |
+
def test_datetimeindex_accessors4(self):
|
| 138 |
+
dti = DatetimeIndex(["2000-01-01", "2000-01-02", "2000-01-03"])
|
| 139 |
+
|
| 140 |
+
assert dti.is_month_start[0] == 1
|
| 141 |
+
|
| 142 |
+
def test_datetimeindex_accessors5(self):
|
| 143 |
+
freq_m = to_offset("M")
|
| 144 |
+
bm = to_offset("BM")
|
| 145 |
+
qfeb = to_offset("Q-FEB")
|
| 146 |
+
qsfeb = to_offset("QS-FEB")
|
| 147 |
+
bq = to_offset("BQ")
|
| 148 |
+
bqs_apr = to_offset("BQS-APR")
|
| 149 |
+
as_nov = to_offset("AS-NOV")
|
| 150 |
+
|
| 151 |
+
tests = [
|
| 152 |
+
(freq_m.is_month_start(Timestamp("2013-06-01")), 1),
|
| 153 |
+
(bm.is_month_start(Timestamp("2013-06-01")), 0),
|
| 154 |
+
(freq_m.is_month_start(Timestamp("2013-06-03")), 0),
|
| 155 |
+
(bm.is_month_start(Timestamp("2013-06-03")), 1),
|
| 156 |
+
(qfeb.is_month_end(Timestamp("2013-02-28")), 1),
|
| 157 |
+
(qfeb.is_quarter_end(Timestamp("2013-02-28")), 1),
|
| 158 |
+
(qfeb.is_year_end(Timestamp("2013-02-28")), 1),
|
| 159 |
+
(qfeb.is_month_start(Timestamp("2013-03-01")), 1),
|
| 160 |
+
(qfeb.is_quarter_start(Timestamp("2013-03-01")), 1),
|
| 161 |
+
(qfeb.is_year_start(Timestamp("2013-03-01")), 1),
|
| 162 |
+
(qsfeb.is_month_end(Timestamp("2013-03-31")), 1),
|
| 163 |
+
(qsfeb.is_quarter_end(Timestamp("2013-03-31")), 0),
|
| 164 |
+
(qsfeb.is_year_end(Timestamp("2013-03-31")), 0),
|
| 165 |
+
(qsfeb.is_month_start(Timestamp("2013-02-01")), 1),
|
| 166 |
+
(qsfeb.is_quarter_start(Timestamp("2013-02-01")), 1),
|
| 167 |
+
(qsfeb.is_year_start(Timestamp("2013-02-01")), 1),
|
| 168 |
+
(bq.is_month_end(Timestamp("2013-06-30")), 0),
|
| 169 |
+
(bq.is_quarter_end(Timestamp("2013-06-30")), 0),
|
| 170 |
+
(bq.is_year_end(Timestamp("2013-06-30")), 0),
|
| 171 |
+
(bq.is_month_end(Timestamp("2013-06-28")), 1),
|
| 172 |
+
(bq.is_quarter_end(Timestamp("2013-06-28")), 1),
|
| 173 |
+
(bq.is_year_end(Timestamp("2013-06-28")), 0),
|
| 174 |
+
(bqs_apr.is_month_end(Timestamp("2013-06-30")), 0),
|
| 175 |
+
(bqs_apr.is_quarter_end(Timestamp("2013-06-30")), 0),
|
| 176 |
+
(bqs_apr.is_year_end(Timestamp("2013-06-30")), 0),
|
| 177 |
+
(bqs_apr.is_month_end(Timestamp("2013-06-28")), 1),
|
| 178 |
+
(bqs_apr.is_quarter_end(Timestamp("2013-06-28")), 1),
|
| 179 |
+
(bqs_apr.is_year_end(Timestamp("2013-03-29")), 1),
|
| 180 |
+
(as_nov.is_year_start(Timestamp("2013-11-01")), 1),
|
| 181 |
+
(as_nov.is_year_end(Timestamp("2013-10-31")), 1),
|
| 182 |
+
(Timestamp("2012-02-01").days_in_month, 29),
|
| 183 |
+
(Timestamp("2013-02-01").days_in_month, 28),
|
| 184 |
+
]
|
| 185 |
+
|
| 186 |
+
for ts, value in tests:
|
| 187 |
+
assert ts == value
|
| 188 |
+
|
| 189 |
+
def test_datetimeindex_accessors6(self):
|
| 190 |
+
# GH 6538: Check that DatetimeIndex and its TimeStamp elements
|
| 191 |
+
# return the same weekofyear accessor close to new year w/ tz
|
| 192 |
+
dates = ["2013/12/29", "2013/12/30", "2013/12/31"]
|
| 193 |
+
dates = DatetimeIndex(dates, tz="Europe/Brussels")
|
| 194 |
+
expected = [52, 1, 1]
|
| 195 |
+
assert dates.isocalendar().week.tolist() == expected
|
| 196 |
+
assert [d.weekofyear for d in dates] == expected
|
| 197 |
+
|
| 198 |
+
# GH 12806
|
| 199 |
+
# error: Unsupported operand types for + ("List[None]" and "List[str]")
|
| 200 |
+
@pytest.mark.parametrize(
|
| 201 |
+
"time_locale", [None] + tm.get_locales() # type: ignore[operator]
|
| 202 |
+
)
|
| 203 |
+
def test_datetime_name_accessors(self, time_locale):
|
| 204 |
+
# Test Monday -> Sunday and January -> December, in that sequence
|
| 205 |
+
if time_locale is None:
|
| 206 |
+
# If the time_locale is None, day-name and month_name should
|
| 207 |
+
# return the english attributes
|
| 208 |
+
expected_days = [
|
| 209 |
+
"Monday",
|
| 210 |
+
"Tuesday",
|
| 211 |
+
"Wednesday",
|
| 212 |
+
"Thursday",
|
| 213 |
+
"Friday",
|
| 214 |
+
"Saturday",
|
| 215 |
+
"Sunday",
|
| 216 |
+
]
|
| 217 |
+
expected_months = [
|
| 218 |
+
"January",
|
| 219 |
+
"February",
|
| 220 |
+
"March",
|
| 221 |
+
"April",
|
| 222 |
+
"May",
|
| 223 |
+
"June",
|
| 224 |
+
"July",
|
| 225 |
+
"August",
|
| 226 |
+
"September",
|
| 227 |
+
"October",
|
| 228 |
+
"November",
|
| 229 |
+
"December",
|
| 230 |
+
]
|
| 231 |
+
else:
|
| 232 |
+
with tm.set_locale(time_locale, locale.LC_TIME):
|
| 233 |
+
expected_days = calendar.day_name[:]
|
| 234 |
+
expected_months = calendar.month_name[1:]
|
| 235 |
+
|
| 236 |
+
# GH#11128
|
| 237 |
+
dti = date_range(freq="D", start=datetime(1998, 1, 1), periods=365)
|
| 238 |
+
english_days = [
|
| 239 |
+
"Monday",
|
| 240 |
+
"Tuesday",
|
| 241 |
+
"Wednesday",
|
| 242 |
+
"Thursday",
|
| 243 |
+
"Friday",
|
| 244 |
+
"Saturday",
|
| 245 |
+
"Sunday",
|
| 246 |
+
]
|
| 247 |
+
for day, name, eng_name in zip(range(4, 11), expected_days, english_days):
|
| 248 |
+
name = name.capitalize()
|
| 249 |
+
assert dti.day_name(locale=time_locale)[day] == name
|
| 250 |
+
assert dti.day_name(locale=None)[day] == eng_name
|
| 251 |
+
ts = Timestamp(datetime(2016, 4, day))
|
| 252 |
+
assert ts.day_name(locale=time_locale) == name
|
| 253 |
+
dti = dti.append(DatetimeIndex([pd.NaT]))
|
| 254 |
+
assert np.isnan(dti.day_name(locale=time_locale)[-1])
|
| 255 |
+
ts = Timestamp(pd.NaT)
|
| 256 |
+
assert np.isnan(ts.day_name(locale=time_locale))
|
| 257 |
+
|
| 258 |
+
# GH#12805
|
| 259 |
+
dti = date_range(freq="M", start="2012", end="2013")
|
| 260 |
+
result = dti.month_name(locale=time_locale)
|
| 261 |
+
expected = Index([month.capitalize() for month in expected_months])
|
| 262 |
+
|
| 263 |
+
# work around different normalization schemes
|
| 264 |
+
# https://github.com/pandas-dev/pandas/issues/22342
|
| 265 |
+
result = result.str.normalize("NFD")
|
| 266 |
+
expected = expected.str.normalize("NFD")
|
| 267 |
+
|
| 268 |
+
tm.assert_index_equal(result, expected)
|
| 269 |
+
|
| 270 |
+
for date, expected in zip(dti, expected_months):
|
| 271 |
+
result = date.month_name(locale=time_locale)
|
| 272 |
+
expected = expected.capitalize()
|
| 273 |
+
|
| 274 |
+
result = unicodedata.normalize("NFD", result)
|
| 275 |
+
expected = unicodedata.normalize("NFD", result)
|
| 276 |
+
|
| 277 |
+
assert result == expected
|
| 278 |
+
dti = dti.append(DatetimeIndex([pd.NaT]))
|
| 279 |
+
assert np.isnan(dti.month_name(locale=time_locale)[-1])
|
| 280 |
+
|
| 281 |
+
def test_nanosecond_field(self):
|
| 282 |
+
dti = DatetimeIndex(np.arange(10))
|
| 283 |
+
expected = Index(np.arange(10, dtype=np.int32))
|
| 284 |
+
|
| 285 |
+
tm.assert_index_equal(dti.nanosecond, expected)
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
def test_iter_readonly():
|
| 289 |
+
# GH#28055 ints_to_pydatetime with readonly array
|
| 290 |
+
arr = np.array([np.datetime64("2012-02-15T12:00:00.000000000")])
|
| 291 |
+
arr.setflags(write=False)
|
| 292 |
+
dti = pd.to_datetime(arr)
|
| 293 |
+
list(dti)
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def test_add_timedelta_preserves_freq():
|
| 297 |
+
# GH#37295 should hold for any DTI with freq=None or Tick freq
|
| 298 |
+
tz = "Canada/Eastern"
|
| 299 |
+
dti = date_range(
|
| 300 |
+
start=Timestamp("2019-03-26 00:00:00-0400", tz=tz),
|
| 301 |
+
end=Timestamp("2020-10-17 00:00:00-0400", tz=tz),
|
| 302 |
+
freq="D",
|
| 303 |
+
)
|
| 304 |
+
result = dti + Timedelta(days=1)
|
| 305 |
+
assert result.freq == dti.freq
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_npfuncs.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from pandas import date_range
|
| 4 |
+
import pandas._testing as tm
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestSplit:
|
| 8 |
+
def test_split_non_utc(self):
|
| 9 |
+
# GH#14042
|
| 10 |
+
indices = date_range("2016-01-01 00:00:00+0200", freq="S", periods=10)
|
| 11 |
+
result = np.split(indices, indices_or_sections=[])[0]
|
| 12 |
+
expected = indices._with_freq(None)
|
| 13 |
+
tm.assert_index_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_ops.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
|
| 3 |
+
from dateutil.tz import tzlocal
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from pandas.compat import IS64
|
| 7 |
+
|
| 8 |
+
from pandas import (
|
| 9 |
+
DatetimeIndex,
|
| 10 |
+
Index,
|
| 11 |
+
bdate_range,
|
| 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 TestDatetimeIndexOps:
|
| 20 |
+
@pytest.mark.parametrize(
|
| 21 |
+
"freq,expected",
|
| 22 |
+
[
|
| 23 |
+
("A", "day"),
|
| 24 |
+
("Q", "day"),
|
| 25 |
+
("M", "day"),
|
| 26 |
+
("D", "day"),
|
| 27 |
+
("H", "hour"),
|
| 28 |
+
("T", "minute"),
|
| 29 |
+
("S", "second"),
|
| 30 |
+
("L", "millisecond"),
|
| 31 |
+
("U", "microsecond"),
|
| 32 |
+
],
|
| 33 |
+
)
|
| 34 |
+
def test_resolution(self, request, tz_naive_fixture, freq, expected):
|
| 35 |
+
tz = tz_naive_fixture
|
| 36 |
+
if freq == "A" and not IS64 and isinstance(tz, tzlocal):
|
| 37 |
+
request.node.add_marker(
|
| 38 |
+
pytest.mark.xfail(reason="OverflowError inside tzlocal past 2038")
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
idx = date_range(start="2013-04-01", periods=30, freq=freq, tz=tz)
|
| 42 |
+
assert idx.resolution == expected
|
| 43 |
+
|
| 44 |
+
def test_infer_freq(self, freq_sample):
|
| 45 |
+
# GH 11018
|
| 46 |
+
idx = date_range("2011-01-01 09:00:00", freq=freq_sample, periods=10)
|
| 47 |
+
result = DatetimeIndex(idx.asi8, freq="infer")
|
| 48 |
+
tm.assert_index_equal(idx, result)
|
| 49 |
+
assert result.freq == freq_sample
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
@pytest.mark.parametrize("freq", ["B", "C"])
|
| 53 |
+
class TestBusinessDatetimeIndex:
|
| 54 |
+
@pytest.fixture
|
| 55 |
+
def rng(self, freq):
|
| 56 |
+
return bdate_range(START, END, freq=freq)
|
| 57 |
+
|
| 58 |
+
def test_comparison(self, rng):
|
| 59 |
+
d = rng[10]
|
| 60 |
+
|
| 61 |
+
comp = rng > d
|
| 62 |
+
assert comp[11]
|
| 63 |
+
assert not comp[9]
|
| 64 |
+
|
| 65 |
+
def test_copy(self, rng):
|
| 66 |
+
cp = rng.copy()
|
| 67 |
+
repr(cp)
|
| 68 |
+
tm.assert_index_equal(cp, rng)
|
| 69 |
+
|
| 70 |
+
def test_identical(self, rng):
|
| 71 |
+
t1 = rng.copy()
|
| 72 |
+
t2 = rng.copy()
|
| 73 |
+
assert t1.identical(t2)
|
| 74 |
+
|
| 75 |
+
# name
|
| 76 |
+
t1 = t1.rename("foo")
|
| 77 |
+
assert t1.equals(t2)
|
| 78 |
+
assert not t1.identical(t2)
|
| 79 |
+
t2 = t2.rename("foo")
|
| 80 |
+
assert t1.identical(t2)
|
| 81 |
+
|
| 82 |
+
# freq
|
| 83 |
+
t2v = Index(t2.values)
|
| 84 |
+
assert t1.equals(t2v)
|
| 85 |
+
assert not t1.identical(t2v)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_partial_slicing.py
ADDED
|
@@ -0,0 +1,457 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" test partial slicing on Series/Frame """
|
| 2 |
+
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pytest
|
| 7 |
+
|
| 8 |
+
from pandas import (
|
| 9 |
+
DataFrame,
|
| 10 |
+
DatetimeIndex,
|
| 11 |
+
Index,
|
| 12 |
+
Series,
|
| 13 |
+
Timedelta,
|
| 14 |
+
Timestamp,
|
| 15 |
+
date_range,
|
| 16 |
+
)
|
| 17 |
+
import pandas._testing as tm
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class TestSlicing:
|
| 21 |
+
def test_string_index_series_name_converted(self):
|
| 22 |
+
# GH#1644
|
| 23 |
+
df = DataFrame(np.random.randn(10, 4), index=date_range("1/1/2000", periods=10))
|
| 24 |
+
|
| 25 |
+
result = df.loc["1/3/2000"]
|
| 26 |
+
assert result.name == df.index[2]
|
| 27 |
+
|
| 28 |
+
result = df.T["1/3/2000"]
|
| 29 |
+
assert result.name == df.index[2]
|
| 30 |
+
|
| 31 |
+
def test_stringified_slice_with_tz(self):
|
| 32 |
+
# GH#2658
|
| 33 |
+
start = "2013-01-07"
|
| 34 |
+
idx = date_range(start=start, freq="1d", periods=10, tz="US/Eastern")
|
| 35 |
+
df = DataFrame(np.arange(10), index=idx)
|
| 36 |
+
df["2013-01-14 23:44:34.437768-05:00":] # no exception here
|
| 37 |
+
|
| 38 |
+
def test_return_type_doesnt_depend_on_monotonicity(self):
|
| 39 |
+
# GH#24892 we get Series back regardless of whether our DTI is monotonic
|
| 40 |
+
dti = date_range(start="2015-5-13 23:59:00", freq="min", periods=3)
|
| 41 |
+
ser = Series(range(3), index=dti)
|
| 42 |
+
|
| 43 |
+
# non-monotonic index
|
| 44 |
+
ser2 = Series(range(3), index=[dti[1], dti[0], dti[2]])
|
| 45 |
+
|
| 46 |
+
# key with resolution strictly lower than "min"
|
| 47 |
+
key = "2015-5-14 00"
|
| 48 |
+
|
| 49 |
+
# monotonic increasing index
|
| 50 |
+
result = ser.loc[key]
|
| 51 |
+
expected = ser.iloc[1:]
|
| 52 |
+
tm.assert_series_equal(result, expected)
|
| 53 |
+
|
| 54 |
+
# monotonic decreasing index
|
| 55 |
+
result = ser.iloc[::-1].loc[key]
|
| 56 |
+
expected = ser.iloc[::-1][:-1]
|
| 57 |
+
tm.assert_series_equal(result, expected)
|
| 58 |
+
|
| 59 |
+
# non-monotonic index
|
| 60 |
+
result2 = ser2.loc[key]
|
| 61 |
+
expected2 = ser2.iloc[::2]
|
| 62 |
+
tm.assert_series_equal(result2, expected2)
|
| 63 |
+
|
| 64 |
+
def test_return_type_doesnt_depend_on_monotonicity_higher_reso(self):
|
| 65 |
+
# GH#24892 we get Series back regardless of whether our DTI is monotonic
|
| 66 |
+
dti = date_range(start="2015-5-13 23:59:00", freq="min", periods=3)
|
| 67 |
+
ser = Series(range(3), index=dti)
|
| 68 |
+
|
| 69 |
+
# non-monotonic index
|
| 70 |
+
ser2 = Series(range(3), index=[dti[1], dti[0], dti[2]])
|
| 71 |
+
|
| 72 |
+
# key with resolution strictly *higher) than "min"
|
| 73 |
+
key = "2015-5-14 00:00:00"
|
| 74 |
+
|
| 75 |
+
# monotonic increasing index
|
| 76 |
+
result = ser.loc[key]
|
| 77 |
+
assert result == 1
|
| 78 |
+
|
| 79 |
+
# monotonic decreasing index
|
| 80 |
+
result = ser.iloc[::-1].loc[key]
|
| 81 |
+
assert result == 1
|
| 82 |
+
|
| 83 |
+
# non-monotonic index
|
| 84 |
+
result2 = ser2.loc[key]
|
| 85 |
+
assert result2 == 0
|
| 86 |
+
|
| 87 |
+
def test_monotone_DTI_indexing_bug(self):
|
| 88 |
+
# GH 19362
|
| 89 |
+
# Testing accessing the first element in a monotonic descending
|
| 90 |
+
# partial string indexing.
|
| 91 |
+
|
| 92 |
+
df = DataFrame(list(range(5)))
|
| 93 |
+
date_list = [
|
| 94 |
+
"2018-01-02",
|
| 95 |
+
"2017-02-10",
|
| 96 |
+
"2016-03-10",
|
| 97 |
+
"2015-03-15",
|
| 98 |
+
"2014-03-16",
|
| 99 |
+
]
|
| 100 |
+
date_index = DatetimeIndex(date_list)
|
| 101 |
+
df["date"] = date_index
|
| 102 |
+
expected = DataFrame({0: list(range(5)), "date": date_index})
|
| 103 |
+
tm.assert_frame_equal(df, expected)
|
| 104 |
+
|
| 105 |
+
# We get a slice because df.index's resolution is hourly and we
|
| 106 |
+
# are slicing with a daily-resolution string. If both were daily,
|
| 107 |
+
# we would get a single item back
|
| 108 |
+
dti = date_range("20170101 01:00:00", periods=3)
|
| 109 |
+
df = DataFrame({"A": [1, 2, 3]}, index=dti[::-1])
|
| 110 |
+
|
| 111 |
+
expected = DataFrame({"A": 1}, index=dti[-1:][::-1])
|
| 112 |
+
result = df.loc["2017-01-03"]
|
| 113 |
+
tm.assert_frame_equal(result, expected)
|
| 114 |
+
|
| 115 |
+
result2 = df.iloc[::-1].loc["2017-01-03"]
|
| 116 |
+
expected2 = expected.iloc[::-1]
|
| 117 |
+
tm.assert_frame_equal(result2, expected2)
|
| 118 |
+
|
| 119 |
+
def test_slice_year(self):
|
| 120 |
+
dti = date_range(freq="B", start=datetime(2005, 1, 1), periods=500)
|
| 121 |
+
|
| 122 |
+
s = Series(np.arange(len(dti)), index=dti)
|
| 123 |
+
result = s["2005"]
|
| 124 |
+
expected = s[s.index.year == 2005]
|
| 125 |
+
tm.assert_series_equal(result, expected)
|
| 126 |
+
|
| 127 |
+
df = DataFrame(np.random.rand(len(dti), 5), index=dti)
|
| 128 |
+
result = df.loc["2005"]
|
| 129 |
+
expected = df[df.index.year == 2005]
|
| 130 |
+
tm.assert_frame_equal(result, expected)
|
| 131 |
+
|
| 132 |
+
@pytest.mark.parametrize(
|
| 133 |
+
"partial_dtime",
|
| 134 |
+
[
|
| 135 |
+
"2019",
|
| 136 |
+
"2019Q4",
|
| 137 |
+
"Dec 2019",
|
| 138 |
+
"2019-12-31",
|
| 139 |
+
"2019-12-31 23",
|
| 140 |
+
"2019-12-31 23:59",
|
| 141 |
+
],
|
| 142 |
+
)
|
| 143 |
+
def test_slice_end_of_period_resolution(self, partial_dtime):
|
| 144 |
+
# GH#31064
|
| 145 |
+
dti = date_range("2019-12-31 23:59:55.999999999", periods=10, freq="s")
|
| 146 |
+
|
| 147 |
+
ser = Series(range(10), index=dti)
|
| 148 |
+
result = ser[partial_dtime]
|
| 149 |
+
expected = ser.iloc[:5]
|
| 150 |
+
tm.assert_series_equal(result, expected)
|
| 151 |
+
|
| 152 |
+
def test_slice_quarter(self):
|
| 153 |
+
dti = date_range(freq="D", start=datetime(2000, 6, 1), periods=500)
|
| 154 |
+
|
| 155 |
+
s = Series(np.arange(len(dti)), index=dti)
|
| 156 |
+
assert len(s["2001Q1"]) == 90
|
| 157 |
+
|
| 158 |
+
df = DataFrame(np.random.rand(len(dti), 5), index=dti)
|
| 159 |
+
assert len(df.loc["1Q01"]) == 90
|
| 160 |
+
|
| 161 |
+
def test_slice_month(self):
|
| 162 |
+
dti = date_range(freq="D", start=datetime(2005, 1, 1), periods=500)
|
| 163 |
+
s = Series(np.arange(len(dti)), index=dti)
|
| 164 |
+
assert len(s["2005-11"]) == 30
|
| 165 |
+
|
| 166 |
+
df = DataFrame(np.random.rand(len(dti), 5), index=dti)
|
| 167 |
+
assert len(df.loc["2005-11"]) == 30
|
| 168 |
+
|
| 169 |
+
tm.assert_series_equal(s["2005-11"], s["11-2005"])
|
| 170 |
+
|
| 171 |
+
def test_partial_slice(self):
|
| 172 |
+
rng = date_range(freq="D", start=datetime(2005, 1, 1), periods=500)
|
| 173 |
+
s = Series(np.arange(len(rng)), index=rng)
|
| 174 |
+
|
| 175 |
+
result = s["2005-05":"2006-02"]
|
| 176 |
+
expected = s["20050501":"20060228"]
|
| 177 |
+
tm.assert_series_equal(result, expected)
|
| 178 |
+
|
| 179 |
+
result = s["2005-05":]
|
| 180 |
+
expected = s["20050501":]
|
| 181 |
+
tm.assert_series_equal(result, expected)
|
| 182 |
+
|
| 183 |
+
result = s[:"2006-02"]
|
| 184 |
+
expected = s[:"20060228"]
|
| 185 |
+
tm.assert_series_equal(result, expected)
|
| 186 |
+
|
| 187 |
+
result = s["2005-1-1"]
|
| 188 |
+
assert result == s.iloc[0]
|
| 189 |
+
|
| 190 |
+
with pytest.raises(KeyError, match=r"^'2004-12-31'$"):
|
| 191 |
+
s["2004-12-31"]
|
| 192 |
+
|
| 193 |
+
def test_partial_slice_daily(self):
|
| 194 |
+
rng = date_range(freq="H", start=datetime(2005, 1, 31), periods=500)
|
| 195 |
+
s = Series(np.arange(len(rng)), index=rng)
|
| 196 |
+
|
| 197 |
+
result = s["2005-1-31"]
|
| 198 |
+
tm.assert_series_equal(result, s.iloc[:24])
|
| 199 |
+
|
| 200 |
+
with pytest.raises(KeyError, match=r"^'2004-12-31 00'$"):
|
| 201 |
+
s["2004-12-31 00"]
|
| 202 |
+
|
| 203 |
+
def test_partial_slice_hourly(self):
|
| 204 |
+
rng = date_range(freq="T", start=datetime(2005, 1, 1, 20, 0, 0), periods=500)
|
| 205 |
+
s = Series(np.arange(len(rng)), index=rng)
|
| 206 |
+
|
| 207 |
+
result = s["2005-1-1"]
|
| 208 |
+
tm.assert_series_equal(result, s.iloc[: 60 * 4])
|
| 209 |
+
|
| 210 |
+
result = s["2005-1-1 20"]
|
| 211 |
+
tm.assert_series_equal(result, s.iloc[:60])
|
| 212 |
+
|
| 213 |
+
assert s["2005-1-1 20:00"] == s.iloc[0]
|
| 214 |
+
with pytest.raises(KeyError, match=r"^'2004-12-31 00:15'$"):
|
| 215 |
+
s["2004-12-31 00:15"]
|
| 216 |
+
|
| 217 |
+
def test_partial_slice_minutely(self):
|
| 218 |
+
rng = date_range(freq="S", start=datetime(2005, 1, 1, 23, 59, 0), periods=500)
|
| 219 |
+
s = Series(np.arange(len(rng)), index=rng)
|
| 220 |
+
|
| 221 |
+
result = s["2005-1-1 23:59"]
|
| 222 |
+
tm.assert_series_equal(result, s.iloc[:60])
|
| 223 |
+
|
| 224 |
+
result = s["2005-1-1"]
|
| 225 |
+
tm.assert_series_equal(result, s.iloc[:60])
|
| 226 |
+
|
| 227 |
+
assert s[Timestamp("2005-1-1 23:59:00")] == s.iloc[0]
|
| 228 |
+
with pytest.raises(KeyError, match=r"^'2004-12-31 00:00:00'$"):
|
| 229 |
+
s["2004-12-31 00:00:00"]
|
| 230 |
+
|
| 231 |
+
def test_partial_slice_second_precision(self):
|
| 232 |
+
rng = date_range(
|
| 233 |
+
start=datetime(2005, 1, 1, 0, 0, 59, microsecond=999990),
|
| 234 |
+
periods=20,
|
| 235 |
+
freq="US",
|
| 236 |
+
)
|
| 237 |
+
s = Series(np.arange(20), rng)
|
| 238 |
+
|
| 239 |
+
tm.assert_series_equal(s["2005-1-1 00:00"], s.iloc[:10])
|
| 240 |
+
tm.assert_series_equal(s["2005-1-1 00:00:59"], s.iloc[:10])
|
| 241 |
+
|
| 242 |
+
tm.assert_series_equal(s["2005-1-1 00:01"], s.iloc[10:])
|
| 243 |
+
tm.assert_series_equal(s["2005-1-1 00:01:00"], s.iloc[10:])
|
| 244 |
+
|
| 245 |
+
assert s[Timestamp("2005-1-1 00:00:59.999990")] == s.iloc[0]
|
| 246 |
+
with pytest.raises(KeyError, match="2005-1-1 00:00:00"):
|
| 247 |
+
s["2005-1-1 00:00:00"]
|
| 248 |
+
|
| 249 |
+
def test_partial_slicing_dataframe(self):
|
| 250 |
+
# GH14856
|
| 251 |
+
# Test various combinations of string slicing resolution vs.
|
| 252 |
+
# index resolution
|
| 253 |
+
# - If string resolution is less precise than index resolution,
|
| 254 |
+
# string is considered a slice
|
| 255 |
+
# - If string resolution is equal to or more precise than index
|
| 256 |
+
# resolution, string is considered an exact match
|
| 257 |
+
formats = [
|
| 258 |
+
"%Y",
|
| 259 |
+
"%Y-%m",
|
| 260 |
+
"%Y-%m-%d",
|
| 261 |
+
"%Y-%m-%d %H",
|
| 262 |
+
"%Y-%m-%d %H:%M",
|
| 263 |
+
"%Y-%m-%d %H:%M:%S",
|
| 264 |
+
]
|
| 265 |
+
resolutions = ["year", "month", "day", "hour", "minute", "second"]
|
| 266 |
+
for rnum, resolution in enumerate(resolutions[2:], 2):
|
| 267 |
+
# we check only 'day', 'hour', 'minute' and 'second'
|
| 268 |
+
unit = Timedelta("1 " + resolution)
|
| 269 |
+
middate = datetime(2012, 1, 1, 0, 0, 0)
|
| 270 |
+
index = DatetimeIndex([middate - unit, middate, middate + unit])
|
| 271 |
+
values = [1, 2, 3]
|
| 272 |
+
df = DataFrame({"a": values}, index, dtype=np.int64)
|
| 273 |
+
assert df.index.resolution == resolution
|
| 274 |
+
|
| 275 |
+
# Timestamp with the same resolution as index
|
| 276 |
+
# Should be exact match for Series (return scalar)
|
| 277 |
+
# and raise KeyError for Frame
|
| 278 |
+
for timestamp, expected in zip(index, values):
|
| 279 |
+
ts_string = timestamp.strftime(formats[rnum])
|
| 280 |
+
# make ts_string as precise as index
|
| 281 |
+
result = df["a"][ts_string]
|
| 282 |
+
assert isinstance(result, np.int64)
|
| 283 |
+
assert result == expected
|
| 284 |
+
msg = rf"^'{ts_string}'$"
|
| 285 |
+
with pytest.raises(KeyError, match=msg):
|
| 286 |
+
df[ts_string]
|
| 287 |
+
|
| 288 |
+
# Timestamp with resolution less precise than index
|
| 289 |
+
for fmt in formats[:rnum]:
|
| 290 |
+
for element, theslice in [[0, slice(None, 1)], [1, slice(1, None)]]:
|
| 291 |
+
ts_string = index[element].strftime(fmt)
|
| 292 |
+
|
| 293 |
+
# Series should return slice
|
| 294 |
+
result = df["a"][ts_string]
|
| 295 |
+
expected = df["a"][theslice]
|
| 296 |
+
tm.assert_series_equal(result, expected)
|
| 297 |
+
|
| 298 |
+
# pre-2.0 df[ts_string] was overloaded to interpret this
|
| 299 |
+
# as slicing along index
|
| 300 |
+
with pytest.raises(KeyError, match=ts_string):
|
| 301 |
+
df[ts_string]
|
| 302 |
+
|
| 303 |
+
# Timestamp with resolution more precise than index
|
| 304 |
+
# Compatible with existing key
|
| 305 |
+
# Should return scalar for Series
|
| 306 |
+
# and raise KeyError for Frame
|
| 307 |
+
for fmt in formats[rnum + 1 :]:
|
| 308 |
+
ts_string = index[1].strftime(fmt)
|
| 309 |
+
result = df["a"][ts_string]
|
| 310 |
+
assert isinstance(result, np.int64)
|
| 311 |
+
assert result == 2
|
| 312 |
+
msg = rf"^'{ts_string}'$"
|
| 313 |
+
with pytest.raises(KeyError, match=msg):
|
| 314 |
+
df[ts_string]
|
| 315 |
+
|
| 316 |
+
# Not compatible with existing key
|
| 317 |
+
# Should raise KeyError
|
| 318 |
+
for fmt, res in list(zip(formats, resolutions))[rnum + 1 :]:
|
| 319 |
+
ts = index[1] + Timedelta("1 " + res)
|
| 320 |
+
ts_string = ts.strftime(fmt)
|
| 321 |
+
msg = rf"^'{ts_string}'$"
|
| 322 |
+
with pytest.raises(KeyError, match=msg):
|
| 323 |
+
df["a"][ts_string]
|
| 324 |
+
with pytest.raises(KeyError, match=msg):
|
| 325 |
+
df[ts_string]
|
| 326 |
+
|
| 327 |
+
def test_partial_slicing_with_multiindex(self):
|
| 328 |
+
# GH 4758
|
| 329 |
+
# partial string indexing with a multi-index buggy
|
| 330 |
+
df = DataFrame(
|
| 331 |
+
{
|
| 332 |
+
"ACCOUNT": ["ACCT1", "ACCT1", "ACCT1", "ACCT2"],
|
| 333 |
+
"TICKER": ["ABC", "MNP", "XYZ", "XYZ"],
|
| 334 |
+
"val": [1, 2, 3, 4],
|
| 335 |
+
},
|
| 336 |
+
index=date_range("2013-06-19 09:30:00", periods=4, freq="5T"),
|
| 337 |
+
)
|
| 338 |
+
df_multi = df.set_index(["ACCOUNT", "TICKER"], append=True)
|
| 339 |
+
|
| 340 |
+
expected = DataFrame(
|
| 341 |
+
[[1]], index=Index(["ABC"], name="TICKER"), columns=["val"]
|
| 342 |
+
)
|
| 343 |
+
result = df_multi.loc[("2013-06-19 09:30:00", "ACCT1")]
|
| 344 |
+
tm.assert_frame_equal(result, expected)
|
| 345 |
+
|
| 346 |
+
expected = df_multi.loc[
|
| 347 |
+
(Timestamp("2013-06-19 09:30:00", tz=None), "ACCT1", "ABC")
|
| 348 |
+
]
|
| 349 |
+
result = df_multi.loc[("2013-06-19 09:30:00", "ACCT1", "ABC")]
|
| 350 |
+
tm.assert_series_equal(result, expected)
|
| 351 |
+
|
| 352 |
+
# partial string indexing on first level, scalar indexing on the other two
|
| 353 |
+
result = df_multi.loc[("2013-06-19", "ACCT1", "ABC")]
|
| 354 |
+
expected = df_multi.iloc[:1].droplevel([1, 2])
|
| 355 |
+
tm.assert_frame_equal(result, expected)
|
| 356 |
+
|
| 357 |
+
def test_partial_slicing_with_multiindex_series(self):
|
| 358 |
+
# GH 4294
|
| 359 |
+
# partial slice on a series mi
|
| 360 |
+
ser = DataFrame(
|
| 361 |
+
np.random.rand(1000, 1000), index=date_range("2000-1-1", periods=1000)
|
| 362 |
+
).stack()
|
| 363 |
+
|
| 364 |
+
s2 = ser[:-1].copy()
|
| 365 |
+
expected = s2["2000-1-4"]
|
| 366 |
+
result = s2[Timestamp("2000-1-4")]
|
| 367 |
+
tm.assert_series_equal(result, expected)
|
| 368 |
+
|
| 369 |
+
result = ser[Timestamp("2000-1-4")]
|
| 370 |
+
expected = ser["2000-1-4"]
|
| 371 |
+
tm.assert_series_equal(result, expected)
|
| 372 |
+
|
| 373 |
+
df2 = DataFrame(ser)
|
| 374 |
+
expected = df2.xs("2000-1-4")
|
| 375 |
+
result = df2.loc[Timestamp("2000-1-4")]
|
| 376 |
+
tm.assert_frame_equal(result, expected)
|
| 377 |
+
|
| 378 |
+
def test_partial_slice_requires_monotonicity(self):
|
| 379 |
+
# Disallowed since 2.0 (GH 37819)
|
| 380 |
+
ser = Series(np.arange(10), date_range("2014-01-01", periods=10))
|
| 381 |
+
|
| 382 |
+
nonmonotonic = ser[[3, 5, 4]]
|
| 383 |
+
timestamp = Timestamp("2014-01-10")
|
| 384 |
+
with pytest.raises(
|
| 385 |
+
KeyError, match="Value based partial slicing on non-monotonic"
|
| 386 |
+
):
|
| 387 |
+
nonmonotonic["2014-01-10":]
|
| 388 |
+
|
| 389 |
+
with pytest.raises(KeyError, match=r"Timestamp\('2014-01-10 00:00:00'\)"):
|
| 390 |
+
nonmonotonic[timestamp:]
|
| 391 |
+
|
| 392 |
+
with pytest.raises(
|
| 393 |
+
KeyError, match="Value based partial slicing on non-monotonic"
|
| 394 |
+
):
|
| 395 |
+
nonmonotonic.loc["2014-01-10":]
|
| 396 |
+
|
| 397 |
+
with pytest.raises(KeyError, match=r"Timestamp\('2014-01-10 00:00:00'\)"):
|
| 398 |
+
nonmonotonic.loc[timestamp:]
|
| 399 |
+
|
| 400 |
+
def test_loc_datetime_length_one(self):
|
| 401 |
+
# GH16071
|
| 402 |
+
df = DataFrame(
|
| 403 |
+
columns=["1"],
|
| 404 |
+
index=date_range("2016-10-01T00:00:00", "2016-10-01T23:59:59"),
|
| 405 |
+
)
|
| 406 |
+
result = df.loc[datetime(2016, 10, 1) :]
|
| 407 |
+
tm.assert_frame_equal(result, df)
|
| 408 |
+
|
| 409 |
+
result = df.loc["2016-10-01T00:00:00":]
|
| 410 |
+
tm.assert_frame_equal(result, df)
|
| 411 |
+
|
| 412 |
+
@pytest.mark.parametrize(
|
| 413 |
+
"start",
|
| 414 |
+
[
|
| 415 |
+
"2018-12-02 21:50:00+00:00",
|
| 416 |
+
Timestamp("2018-12-02 21:50:00+00:00"),
|
| 417 |
+
Timestamp("2018-12-02 21:50:00+00:00").to_pydatetime(),
|
| 418 |
+
],
|
| 419 |
+
)
|
| 420 |
+
@pytest.mark.parametrize(
|
| 421 |
+
"end",
|
| 422 |
+
[
|
| 423 |
+
"2018-12-02 21:52:00+00:00",
|
| 424 |
+
Timestamp("2018-12-02 21:52:00+00:00"),
|
| 425 |
+
Timestamp("2018-12-02 21:52:00+00:00").to_pydatetime(),
|
| 426 |
+
],
|
| 427 |
+
)
|
| 428 |
+
def test_getitem_with_datestring_with_UTC_offset(self, start, end):
|
| 429 |
+
# GH 24076
|
| 430 |
+
idx = date_range(
|
| 431 |
+
start="2018-12-02 14:50:00-07:00",
|
| 432 |
+
end="2018-12-02 14:50:00-07:00",
|
| 433 |
+
freq="1min",
|
| 434 |
+
)
|
| 435 |
+
df = DataFrame(1, index=idx, columns=["A"])
|
| 436 |
+
result = df[start:end]
|
| 437 |
+
expected = df.iloc[0:3, :]
|
| 438 |
+
tm.assert_frame_equal(result, expected)
|
| 439 |
+
|
| 440 |
+
# GH 16785
|
| 441 |
+
start = str(start)
|
| 442 |
+
end = str(end)
|
| 443 |
+
with pytest.raises(ValueError, match="Both dates must"):
|
| 444 |
+
df[start : end[:-4] + "1:00"]
|
| 445 |
+
|
| 446 |
+
with pytest.raises(ValueError, match="The index must be timezone"):
|
| 447 |
+
df = df.tz_localize(None)
|
| 448 |
+
df[start:end]
|
| 449 |
+
|
| 450 |
+
def test_slice_reduce_to_series(self):
|
| 451 |
+
# GH 27516
|
| 452 |
+
df = DataFrame({"A": range(24)}, index=date_range("2000", periods=24, freq="M"))
|
| 453 |
+
expected = Series(
|
| 454 |
+
range(12), index=date_range("2000", periods=12, freq="M"), name="A"
|
| 455 |
+
)
|
| 456 |
+
result = df.loc["2000", "A"]
|
| 457 |
+
tm.assert_series_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_pickle.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from pandas import (
|
| 4 |
+
NaT,
|
| 5 |
+
date_range,
|
| 6 |
+
to_datetime,
|
| 7 |
+
)
|
| 8 |
+
import pandas._testing as tm
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TestPickle:
|
| 12 |
+
def test_pickle(self):
|
| 13 |
+
# GH#4606
|
| 14 |
+
idx = to_datetime(["2013-01-01", NaT, "2014-01-06"])
|
| 15 |
+
idx_p = tm.round_trip_pickle(idx)
|
| 16 |
+
assert idx_p[0] == idx[0]
|
| 17 |
+
assert idx_p[1] is NaT
|
| 18 |
+
assert idx_p[2] == idx[2]
|
| 19 |
+
|
| 20 |
+
def test_pickle_dont_infer_freq(self):
|
| 21 |
+
# GH#11002
|
| 22 |
+
# don't infer freq
|
| 23 |
+
idx = date_range("1750-1-1", "2050-1-1", freq="7D")
|
| 24 |
+
idx_p = tm.round_trip_pickle(idx)
|
| 25 |
+
tm.assert_index_equal(idx, idx_p)
|
| 26 |
+
|
| 27 |
+
def test_pickle_after_set_freq(self):
|
| 28 |
+
dti = date_range("20130101", periods=3, tz="US/Eastern", name="foo")
|
| 29 |
+
dti = dti._with_freq(None)
|
| 30 |
+
|
| 31 |
+
res = tm.round_trip_pickle(dti)
|
| 32 |
+
tm.assert_index_equal(res, dti)
|
| 33 |
+
|
| 34 |
+
def test_roundtrip_pickle_with_tz(self):
|
| 35 |
+
# GH#8367
|
| 36 |
+
# round-trip of timezone
|
| 37 |
+
index = date_range("20130101", periods=3, tz="US/Eastern", name="foo")
|
| 38 |
+
unpickled = tm.round_trip_pickle(index)
|
| 39 |
+
tm.assert_index_equal(index, unpickled)
|
| 40 |
+
|
| 41 |
+
@pytest.mark.parametrize("freq", ["B", "C"])
|
| 42 |
+
def test_pickle_unpickle(self, freq):
|
| 43 |
+
rng = date_range("2009-01-01", "2010-01-01", freq=freq)
|
| 44 |
+
unpickled = tm.round_trip_pickle(rng)
|
| 45 |
+
assert unpickled.freq == freq
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_reindex.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import timedelta
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
from pandas import (
|
| 6 |
+
DatetimeIndex,
|
| 7 |
+
date_range,
|
| 8 |
+
)
|
| 9 |
+
import pandas._testing as tm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestDatetimeIndexReindex:
|
| 13 |
+
def test_reindex_preserves_tz_if_target_is_empty_list_or_array(self):
|
| 14 |
+
# GH#7774
|
| 15 |
+
index = date_range("2013-01-01", periods=3, tz="US/Eastern")
|
| 16 |
+
assert str(index.reindex([])[0].tz) == "US/Eastern"
|
| 17 |
+
assert str(index.reindex(np.array([]))[0].tz) == "US/Eastern"
|
| 18 |
+
|
| 19 |
+
def test_reindex_with_same_tz_nearest(self):
|
| 20 |
+
# GH#32740
|
| 21 |
+
rng_a = date_range("2010-01-01", "2010-01-02", periods=24, tz="utc")
|
| 22 |
+
rng_b = date_range("2010-01-01", "2010-01-02", periods=23, tz="utc")
|
| 23 |
+
result1, result2 = rng_a.reindex(
|
| 24 |
+
rng_b, method="nearest", tolerance=timedelta(seconds=20)
|
| 25 |
+
)
|
| 26 |
+
expected_list1 = [
|
| 27 |
+
"2010-01-01 00:00:00",
|
| 28 |
+
"2010-01-01 01:05:27.272727272",
|
| 29 |
+
"2010-01-01 02:10:54.545454545",
|
| 30 |
+
"2010-01-01 03:16:21.818181818",
|
| 31 |
+
"2010-01-01 04:21:49.090909090",
|
| 32 |
+
"2010-01-01 05:27:16.363636363",
|
| 33 |
+
"2010-01-01 06:32:43.636363636",
|
| 34 |
+
"2010-01-01 07:38:10.909090909",
|
| 35 |
+
"2010-01-01 08:43:38.181818181",
|
| 36 |
+
"2010-01-01 09:49:05.454545454",
|
| 37 |
+
"2010-01-01 10:54:32.727272727",
|
| 38 |
+
"2010-01-01 12:00:00",
|
| 39 |
+
"2010-01-01 13:05:27.272727272",
|
| 40 |
+
"2010-01-01 14:10:54.545454545",
|
| 41 |
+
"2010-01-01 15:16:21.818181818",
|
| 42 |
+
"2010-01-01 16:21:49.090909090",
|
| 43 |
+
"2010-01-01 17:27:16.363636363",
|
| 44 |
+
"2010-01-01 18:32:43.636363636",
|
| 45 |
+
"2010-01-01 19:38:10.909090909",
|
| 46 |
+
"2010-01-01 20:43:38.181818181",
|
| 47 |
+
"2010-01-01 21:49:05.454545454",
|
| 48 |
+
"2010-01-01 22:54:32.727272727",
|
| 49 |
+
"2010-01-02 00:00:00",
|
| 50 |
+
]
|
| 51 |
+
expected1 = DatetimeIndex(
|
| 52 |
+
expected_list1, dtype="datetime64[ns, UTC]", freq=None
|
| 53 |
+
)
|
| 54 |
+
expected2 = np.array([0] + [-1] * 21 + [23], dtype=np.dtype("intp"))
|
| 55 |
+
tm.assert_index_equal(result1, expected1)
|
| 56 |
+
tm.assert_numpy_array_equal(result2, expected2)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_scalar_compat.py
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
+
from pandas._libs.tslibs import (
|
| 10 |
+
OutOfBoundsDatetime,
|
| 11 |
+
to_offset,
|
| 12 |
+
)
|
| 13 |
+
from pandas._libs.tslibs.offsets import INVALID_FREQ_ERR_MSG
|
| 14 |
+
|
| 15 |
+
import pandas as pd
|
| 16 |
+
from pandas import (
|
| 17 |
+
DatetimeIndex,
|
| 18 |
+
Timestamp,
|
| 19 |
+
date_range,
|
| 20 |
+
)
|
| 21 |
+
import pandas._testing as tm
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class TestDatetimeIndexOps:
|
| 25 |
+
def test_dti_time(self):
|
| 26 |
+
rng = date_range("1/1/2000", freq="12min", periods=10)
|
| 27 |
+
result = pd.Index(rng).time
|
| 28 |
+
expected = [t.time() for t in rng]
|
| 29 |
+
assert (result == expected).all()
|
| 30 |
+
|
| 31 |
+
def test_dti_date(self):
|
| 32 |
+
rng = date_range("1/1/2000", freq="12H", periods=10)
|
| 33 |
+
result = pd.Index(rng).date
|
| 34 |
+
expected = [t.date() for t in rng]
|
| 35 |
+
assert (result == expected).all()
|
| 36 |
+
|
| 37 |
+
@pytest.mark.parametrize("data", [["1400-01-01"], [datetime(1400, 1, 1)]])
|
| 38 |
+
def test_dti_date_out_of_range(self, data):
|
| 39 |
+
# GH#1475
|
| 40 |
+
msg = (
|
| 41 |
+
"^Out of bounds nanosecond timestamp: "
|
| 42 |
+
"1400-01-01( 00:00:00)?, at position 0$"
|
| 43 |
+
)
|
| 44 |
+
with pytest.raises(OutOfBoundsDatetime, match=msg):
|
| 45 |
+
DatetimeIndex(data)
|
| 46 |
+
|
| 47 |
+
@pytest.mark.parametrize(
|
| 48 |
+
"field",
|
| 49 |
+
[
|
| 50 |
+
"dayofweek",
|
| 51 |
+
"day_of_week",
|
| 52 |
+
"dayofyear",
|
| 53 |
+
"day_of_year",
|
| 54 |
+
"quarter",
|
| 55 |
+
"days_in_month",
|
| 56 |
+
"is_month_start",
|
| 57 |
+
"is_month_end",
|
| 58 |
+
"is_quarter_start",
|
| 59 |
+
"is_quarter_end",
|
| 60 |
+
"is_year_start",
|
| 61 |
+
"is_year_end",
|
| 62 |
+
],
|
| 63 |
+
)
|
| 64 |
+
def test_dti_timestamp_fields(self, field):
|
| 65 |
+
# extra fields from DatetimeIndex like quarter and week
|
| 66 |
+
idx = tm.makeDateIndex(100)
|
| 67 |
+
expected = getattr(idx, field)[-1]
|
| 68 |
+
|
| 69 |
+
result = getattr(Timestamp(idx[-1]), field)
|
| 70 |
+
assert result == expected
|
| 71 |
+
|
| 72 |
+
def test_dti_timestamp_isocalendar_fields(self):
|
| 73 |
+
idx = tm.makeDateIndex(100)
|
| 74 |
+
expected = tuple(idx.isocalendar().iloc[-1].to_list())
|
| 75 |
+
result = idx[-1].isocalendar()
|
| 76 |
+
assert result == expected
|
| 77 |
+
|
| 78 |
+
# ----------------------------------------------------------------
|
| 79 |
+
# DatetimeIndex.round
|
| 80 |
+
|
| 81 |
+
def test_round_daily(self):
|
| 82 |
+
dti = date_range("20130101 09:10:11", periods=5)
|
| 83 |
+
result = dti.round("D")
|
| 84 |
+
expected = date_range("20130101", periods=5)
|
| 85 |
+
tm.assert_index_equal(result, expected)
|
| 86 |
+
|
| 87 |
+
dti = dti.tz_localize("UTC").tz_convert("US/Eastern")
|
| 88 |
+
result = dti.round("D")
|
| 89 |
+
expected = date_range("20130101", periods=5).tz_localize("US/Eastern")
|
| 90 |
+
tm.assert_index_equal(result, expected)
|
| 91 |
+
|
| 92 |
+
result = dti.round("s")
|
| 93 |
+
tm.assert_index_equal(result, dti)
|
| 94 |
+
|
| 95 |
+
@pytest.mark.parametrize(
|
| 96 |
+
"freq, error_msg",
|
| 97 |
+
[
|
| 98 |
+
("Y", "<YearEnd: month=12> is a non-fixed frequency"),
|
| 99 |
+
("M", "<MonthEnd> is a non-fixed frequency"),
|
| 100 |
+
("foobar", "Invalid frequency: foobar"),
|
| 101 |
+
],
|
| 102 |
+
)
|
| 103 |
+
def test_round_invalid(self, freq, error_msg):
|
| 104 |
+
dti = date_range("20130101 09:10:11", periods=5)
|
| 105 |
+
dti = dti.tz_localize("UTC").tz_convert("US/Eastern")
|
| 106 |
+
with pytest.raises(ValueError, match=error_msg):
|
| 107 |
+
dti.round(freq)
|
| 108 |
+
|
| 109 |
+
def test_round(self, tz_naive_fixture):
|
| 110 |
+
tz = tz_naive_fixture
|
| 111 |
+
rng = date_range(start="2016-01-01", periods=5, freq="30Min", tz=tz)
|
| 112 |
+
elt = rng[1]
|
| 113 |
+
|
| 114 |
+
expected_rng = DatetimeIndex(
|
| 115 |
+
[
|
| 116 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
| 117 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
| 118 |
+
Timestamp("2016-01-01 01:00:00", tz=tz),
|
| 119 |
+
Timestamp("2016-01-01 02:00:00", tz=tz),
|
| 120 |
+
Timestamp("2016-01-01 02:00:00", tz=tz),
|
| 121 |
+
]
|
| 122 |
+
)
|
| 123 |
+
expected_elt = expected_rng[1]
|
| 124 |
+
|
| 125 |
+
tm.assert_index_equal(rng.round(freq="H"), expected_rng)
|
| 126 |
+
assert elt.round(freq="H") == expected_elt
|
| 127 |
+
|
| 128 |
+
msg = INVALID_FREQ_ERR_MSG
|
| 129 |
+
with pytest.raises(ValueError, match=msg):
|
| 130 |
+
rng.round(freq="foo")
|
| 131 |
+
with pytest.raises(ValueError, match=msg):
|
| 132 |
+
elt.round(freq="foo")
|
| 133 |
+
|
| 134 |
+
msg = "<MonthEnd> is a non-fixed frequency"
|
| 135 |
+
with pytest.raises(ValueError, match=msg):
|
| 136 |
+
rng.round(freq="M")
|
| 137 |
+
with pytest.raises(ValueError, match=msg):
|
| 138 |
+
elt.round(freq="M")
|
| 139 |
+
|
| 140 |
+
# GH#14440 & GH#15578
|
| 141 |
+
index = DatetimeIndex(["2016-10-17 12:00:00.0015"], tz=tz)
|
| 142 |
+
result = index.round("ms")
|
| 143 |
+
expected = DatetimeIndex(["2016-10-17 12:00:00.002000"], tz=tz)
|
| 144 |
+
tm.assert_index_equal(result, expected)
|
| 145 |
+
|
| 146 |
+
for freq in ["us", "ns"]:
|
| 147 |
+
tm.assert_index_equal(index, index.round(freq))
|
| 148 |
+
|
| 149 |
+
index = DatetimeIndex(["2016-10-17 12:00:00.00149"], tz=tz)
|
| 150 |
+
result = index.round("ms")
|
| 151 |
+
expected = DatetimeIndex(["2016-10-17 12:00:00.001000"], tz=tz)
|
| 152 |
+
tm.assert_index_equal(result, expected)
|
| 153 |
+
|
| 154 |
+
index = DatetimeIndex(["2016-10-17 12:00:00.001501031"])
|
| 155 |
+
result = index.round("10ns")
|
| 156 |
+
expected = DatetimeIndex(["2016-10-17 12:00:00.001501030"])
|
| 157 |
+
tm.assert_index_equal(result, expected)
|
| 158 |
+
|
| 159 |
+
with tm.assert_produces_warning(False):
|
| 160 |
+
ts = "2016-10-17 12:00:00.001501031"
|
| 161 |
+
DatetimeIndex([ts]).round("1010ns")
|
| 162 |
+
|
| 163 |
+
def test_no_rounding_occurs(self, tz_naive_fixture):
|
| 164 |
+
# GH 21262
|
| 165 |
+
tz = tz_naive_fixture
|
| 166 |
+
rng = date_range(start="2016-01-01", periods=5, freq="2Min", tz=tz)
|
| 167 |
+
|
| 168 |
+
expected_rng = DatetimeIndex(
|
| 169 |
+
[
|
| 170 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
| 171 |
+
Timestamp("2016-01-01 00:02:00", tz=tz),
|
| 172 |
+
Timestamp("2016-01-01 00:04:00", tz=tz),
|
| 173 |
+
Timestamp("2016-01-01 00:06:00", tz=tz),
|
| 174 |
+
Timestamp("2016-01-01 00:08:00", tz=tz),
|
| 175 |
+
]
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
tm.assert_index_equal(rng.round(freq="2T"), expected_rng)
|
| 179 |
+
|
| 180 |
+
@pytest.mark.parametrize(
|
| 181 |
+
"test_input, rounder, freq, expected",
|
| 182 |
+
[
|
| 183 |
+
(["2117-01-01 00:00:45"], "floor", "15s", ["2117-01-01 00:00:45"]),
|
| 184 |
+
(["2117-01-01 00:00:45"], "ceil", "15s", ["2117-01-01 00:00:45"]),
|
| 185 |
+
(
|
| 186 |
+
["2117-01-01 00:00:45.000000012"],
|
| 187 |
+
"floor",
|
| 188 |
+
"10ns",
|
| 189 |
+
["2117-01-01 00:00:45.000000010"],
|
| 190 |
+
),
|
| 191 |
+
(
|
| 192 |
+
["1823-01-01 00:00:01.000000012"],
|
| 193 |
+
"ceil",
|
| 194 |
+
"10ns",
|
| 195 |
+
["1823-01-01 00:00:01.000000020"],
|
| 196 |
+
),
|
| 197 |
+
(["1823-01-01 00:00:01"], "floor", "1s", ["1823-01-01 00:00:01"]),
|
| 198 |
+
(["1823-01-01 00:00:01"], "ceil", "1s", ["1823-01-01 00:00:01"]),
|
| 199 |
+
(["2018-01-01 00:15:00"], "ceil", "15T", ["2018-01-01 00:15:00"]),
|
| 200 |
+
(["2018-01-01 00:15:00"], "floor", "15T", ["2018-01-01 00:15:00"]),
|
| 201 |
+
(["1823-01-01 03:00:00"], "ceil", "3H", ["1823-01-01 03:00:00"]),
|
| 202 |
+
(["1823-01-01 03:00:00"], "floor", "3H", ["1823-01-01 03:00:00"]),
|
| 203 |
+
(
|
| 204 |
+
("NaT", "1823-01-01 00:00:01"),
|
| 205 |
+
"floor",
|
| 206 |
+
"1s",
|
| 207 |
+
("NaT", "1823-01-01 00:00:01"),
|
| 208 |
+
),
|
| 209 |
+
(
|
| 210 |
+
("NaT", "1823-01-01 00:00:01"),
|
| 211 |
+
"ceil",
|
| 212 |
+
"1s",
|
| 213 |
+
("NaT", "1823-01-01 00:00:01"),
|
| 214 |
+
),
|
| 215 |
+
],
|
| 216 |
+
)
|
| 217 |
+
def test_ceil_floor_edge(self, test_input, rounder, freq, expected):
|
| 218 |
+
dt = DatetimeIndex(list(test_input))
|
| 219 |
+
func = getattr(dt, rounder)
|
| 220 |
+
result = func(freq)
|
| 221 |
+
expected = DatetimeIndex(list(expected))
|
| 222 |
+
assert expected.equals(result)
|
| 223 |
+
|
| 224 |
+
@pytest.mark.parametrize(
|
| 225 |
+
"start, index_freq, periods",
|
| 226 |
+
[("2018-01-01", "12H", 25), ("2018-01-01 0:0:0.124999", "1ns", 1000)],
|
| 227 |
+
)
|
| 228 |
+
@pytest.mark.parametrize(
|
| 229 |
+
"round_freq",
|
| 230 |
+
[
|
| 231 |
+
"2ns",
|
| 232 |
+
"3ns",
|
| 233 |
+
"4ns",
|
| 234 |
+
"5ns",
|
| 235 |
+
"6ns",
|
| 236 |
+
"7ns",
|
| 237 |
+
"250ns",
|
| 238 |
+
"500ns",
|
| 239 |
+
"750ns",
|
| 240 |
+
"1us",
|
| 241 |
+
"19us",
|
| 242 |
+
"250us",
|
| 243 |
+
"500us",
|
| 244 |
+
"750us",
|
| 245 |
+
"1s",
|
| 246 |
+
"2s",
|
| 247 |
+
"3s",
|
| 248 |
+
"12H",
|
| 249 |
+
"1D",
|
| 250 |
+
],
|
| 251 |
+
)
|
| 252 |
+
def test_round_int64(self, start, index_freq, periods, round_freq):
|
| 253 |
+
dt = date_range(start=start, freq=index_freq, periods=periods)
|
| 254 |
+
unit = to_offset(round_freq).nanos
|
| 255 |
+
|
| 256 |
+
# test floor
|
| 257 |
+
result = dt.floor(round_freq)
|
| 258 |
+
diff = dt.asi8 - result.asi8
|
| 259 |
+
mod = result.asi8 % unit
|
| 260 |
+
assert (mod == 0).all(), f"floor not a {round_freq} multiple"
|
| 261 |
+
assert (0 <= diff).all() and (diff < unit).all(), "floor error"
|
| 262 |
+
|
| 263 |
+
# test ceil
|
| 264 |
+
result = dt.ceil(round_freq)
|
| 265 |
+
diff = result.asi8 - dt.asi8
|
| 266 |
+
mod = result.asi8 % unit
|
| 267 |
+
assert (mod == 0).all(), f"ceil not a {round_freq} multiple"
|
| 268 |
+
assert (0 <= diff).all() and (diff < unit).all(), "ceil error"
|
| 269 |
+
|
| 270 |
+
# test round
|
| 271 |
+
result = dt.round(round_freq)
|
| 272 |
+
diff = abs(result.asi8 - dt.asi8)
|
| 273 |
+
mod = result.asi8 % unit
|
| 274 |
+
assert (mod == 0).all(), f"round not a {round_freq} multiple"
|
| 275 |
+
assert (diff <= unit // 2).all(), "round error"
|
| 276 |
+
if unit % 2 == 0:
|
| 277 |
+
assert (
|
| 278 |
+
result.asi8[diff == unit // 2] % 2 == 0
|
| 279 |
+
).all(), "round half to even error"
|
| 280 |
+
|
| 281 |
+
# ----------------------------------------------------------------
|
| 282 |
+
# DatetimeIndex.normalize
|
| 283 |
+
|
| 284 |
+
def test_normalize(self):
|
| 285 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D")
|
| 286 |
+
|
| 287 |
+
result = rng.normalize()
|
| 288 |
+
expected = date_range("1/1/2000", periods=10, freq="D")
|
| 289 |
+
tm.assert_index_equal(result, expected)
|
| 290 |
+
|
| 291 |
+
arr_ns = np.array([1380585623454345752, 1380585612343234312]).astype(
|
| 292 |
+
"datetime64[ns]"
|
| 293 |
+
)
|
| 294 |
+
rng_ns = DatetimeIndex(arr_ns)
|
| 295 |
+
rng_ns_normalized = rng_ns.normalize()
|
| 296 |
+
|
| 297 |
+
arr_ns = np.array([1380585600000000000, 1380585600000000000]).astype(
|
| 298 |
+
"datetime64[ns]"
|
| 299 |
+
)
|
| 300 |
+
expected = DatetimeIndex(arr_ns)
|
| 301 |
+
tm.assert_index_equal(rng_ns_normalized, expected)
|
| 302 |
+
|
| 303 |
+
assert result.is_normalized
|
| 304 |
+
assert not rng.is_normalized
|
| 305 |
+
|
| 306 |
+
def test_normalize_nat(self):
|
| 307 |
+
dti = DatetimeIndex([pd.NaT, Timestamp("2018-01-01 01:00:00")])
|
| 308 |
+
result = dti.normalize()
|
| 309 |
+
expected = DatetimeIndex([pd.NaT, Timestamp("2018-01-01")])
|
| 310 |
+
tm.assert_index_equal(result, expected)
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
class TestDateTimeIndexToJulianDate:
|
| 314 |
+
def test_1700(self):
|
| 315 |
+
dr = date_range(start=Timestamp("1710-10-01"), periods=5, freq="D")
|
| 316 |
+
r1 = pd.Index([x.to_julian_date() for x in dr])
|
| 317 |
+
r2 = dr.to_julian_date()
|
| 318 |
+
assert isinstance(r2, pd.Index) and r2.dtype == np.float64
|
| 319 |
+
tm.assert_index_equal(r1, r2)
|
| 320 |
+
|
| 321 |
+
def test_2000(self):
|
| 322 |
+
dr = date_range(start=Timestamp("2000-02-27"), periods=5, freq="D")
|
| 323 |
+
r1 = pd.Index([x.to_julian_date() for x in dr])
|
| 324 |
+
r2 = dr.to_julian_date()
|
| 325 |
+
assert isinstance(r2, pd.Index) and r2.dtype == np.float64
|
| 326 |
+
tm.assert_index_equal(r1, r2)
|
| 327 |
+
|
| 328 |
+
def test_hour(self):
|
| 329 |
+
dr = date_range(start=Timestamp("2000-02-27"), periods=5, freq="H")
|
| 330 |
+
r1 = pd.Index([x.to_julian_date() for x in dr])
|
| 331 |
+
r2 = dr.to_julian_date()
|
| 332 |
+
assert isinstance(r2, pd.Index) and r2.dtype == np.float64
|
| 333 |
+
tm.assert_index_equal(r1, r2)
|
| 334 |
+
|
| 335 |
+
def test_minute(self):
|
| 336 |
+
dr = date_range(start=Timestamp("2000-02-27"), periods=5, freq="T")
|
| 337 |
+
r1 = pd.Index([x.to_julian_date() for x in dr])
|
| 338 |
+
r2 = dr.to_julian_date()
|
| 339 |
+
assert isinstance(r2, pd.Index) and r2.dtype == np.float64
|
| 340 |
+
tm.assert_index_equal(r1, r2)
|
| 341 |
+
|
| 342 |
+
def test_second(self):
|
| 343 |
+
dr = date_range(start=Timestamp("2000-02-27"), periods=5, freq="S")
|
| 344 |
+
r1 = pd.Index([x.to_julian_date() for x in dr])
|
| 345 |
+
r2 = dr.to_julian_date()
|
| 346 |
+
assert isinstance(r2, pd.Index) and r2.dtype == np.float64
|
| 347 |
+
tm.assert_index_equal(r1, r2)
|
vide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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
import pytz
|
| 6 |
+
|
| 7 |
+
import pandas.util._test_decorators as td
|
| 8 |
+
|
| 9 |
+
import pandas as pd
|
| 10 |
+
from pandas import (
|
| 11 |
+
DataFrame,
|
| 12 |
+
DatetimeIndex,
|
| 13 |
+
Index,
|
| 14 |
+
Series,
|
| 15 |
+
bdate_range,
|
| 16 |
+
date_range,
|
| 17 |
+
)
|
| 18 |
+
import pandas._testing as tm
|
| 19 |
+
|
| 20 |
+
from pandas.tseries.offsets import (
|
| 21 |
+
BMonthEnd,
|
| 22 |
+
Minute,
|
| 23 |
+
MonthEnd,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
START, END = datetime(2009, 1, 1), datetime(2010, 1, 1)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class TestDatetimeIndexSetOps:
|
| 30 |
+
tz = [
|
| 31 |
+
None,
|
| 32 |
+
"UTC",
|
| 33 |
+
"Asia/Tokyo",
|
| 34 |
+
"US/Eastern",
|
| 35 |
+
"dateutil/Asia/Singapore",
|
| 36 |
+
"dateutil/US/Pacific",
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
# TODO: moved from test_datetimelike; dedup with version below
|
| 40 |
+
def test_union2(self, sort):
|
| 41 |
+
everything = tm.makeDateIndex(10)
|
| 42 |
+
first = everything[:5]
|
| 43 |
+
second = everything[5:]
|
| 44 |
+
union = first.union(second, sort=sort)
|
| 45 |
+
tm.assert_index_equal(union, everything)
|
| 46 |
+
|
| 47 |
+
@pytest.mark.parametrize("box", [np.array, Series, list])
|
| 48 |
+
def test_union3(self, sort, box):
|
| 49 |
+
everything = tm.makeDateIndex(10)
|
| 50 |
+
first = everything[:5]
|
| 51 |
+
second = everything[5:]
|
| 52 |
+
|
| 53 |
+
# GH 10149 support listlike inputs other than Index objects
|
| 54 |
+
expected = first.union(second, sort=sort)
|
| 55 |
+
case = box(second.values)
|
| 56 |
+
result = first.union(case, sort=sort)
|
| 57 |
+
tm.assert_index_equal(result, expected)
|
| 58 |
+
|
| 59 |
+
@pytest.mark.parametrize("tz", tz)
|
| 60 |
+
def test_union(self, tz, sort):
|
| 61 |
+
rng1 = date_range("1/1/2000", freq="D", periods=5, tz=tz)
|
| 62 |
+
other1 = date_range("1/6/2000", freq="D", periods=5, tz=tz)
|
| 63 |
+
expected1 = date_range("1/1/2000", freq="D", periods=10, tz=tz)
|
| 64 |
+
expected1_notsorted = DatetimeIndex(list(other1) + list(rng1))
|
| 65 |
+
|
| 66 |
+
rng2 = date_range("1/1/2000", freq="D", periods=5, tz=tz)
|
| 67 |
+
other2 = date_range("1/4/2000", freq="D", periods=5, tz=tz)
|
| 68 |
+
expected2 = date_range("1/1/2000", freq="D", periods=8, tz=tz)
|
| 69 |
+
expected2_notsorted = DatetimeIndex(list(other2) + list(rng2[:3]))
|
| 70 |
+
|
| 71 |
+
rng3 = date_range("1/1/2000", freq="D", periods=5, tz=tz)
|
| 72 |
+
other3 = DatetimeIndex([], tz=tz)
|
| 73 |
+
expected3 = date_range("1/1/2000", freq="D", periods=5, tz=tz)
|
| 74 |
+
expected3_notsorted = rng3
|
| 75 |
+
|
| 76 |
+
for rng, other, exp, exp_notsorted in [
|
| 77 |
+
(rng1, other1, expected1, expected1_notsorted),
|
| 78 |
+
(rng2, other2, expected2, expected2_notsorted),
|
| 79 |
+
(rng3, other3, expected3, expected3_notsorted),
|
| 80 |
+
]:
|
| 81 |
+
result_union = rng.union(other, sort=sort)
|
| 82 |
+
tm.assert_index_equal(result_union, exp)
|
| 83 |
+
|
| 84 |
+
result_union = other.union(rng, sort=sort)
|
| 85 |
+
if sort is None:
|
| 86 |
+
tm.assert_index_equal(result_union, exp)
|
| 87 |
+
else:
|
| 88 |
+
tm.assert_index_equal(result_union, exp_notsorted)
|
| 89 |
+
|
| 90 |
+
def test_union_coverage(self, sort):
|
| 91 |
+
idx = DatetimeIndex(["2000-01-03", "2000-01-01", "2000-01-02"])
|
| 92 |
+
ordered = DatetimeIndex(idx.sort_values(), freq="infer")
|
| 93 |
+
result = ordered.union(idx, sort=sort)
|
| 94 |
+
tm.assert_index_equal(result, ordered)
|
| 95 |
+
|
| 96 |
+
result = ordered[:0].union(ordered, sort=sort)
|
| 97 |
+
tm.assert_index_equal(result, ordered)
|
| 98 |
+
assert result.freq == ordered.freq
|
| 99 |
+
|
| 100 |
+
def test_union_bug_1730(self, sort):
|
| 101 |
+
rng_a = date_range("1/1/2012", periods=4, freq="3H")
|
| 102 |
+
rng_b = date_range("1/1/2012", periods=4, freq="4H")
|
| 103 |
+
|
| 104 |
+
result = rng_a.union(rng_b, sort=sort)
|
| 105 |
+
exp = list(rng_a) + list(rng_b[1:])
|
| 106 |
+
if sort is None:
|
| 107 |
+
exp = DatetimeIndex(sorted(exp))
|
| 108 |
+
else:
|
| 109 |
+
exp = DatetimeIndex(exp)
|
| 110 |
+
tm.assert_index_equal(result, exp)
|
| 111 |
+
|
| 112 |
+
def test_union_bug_1745(self, sort):
|
| 113 |
+
left = DatetimeIndex(["2012-05-11 15:19:49.695000"])
|
| 114 |
+
right = DatetimeIndex(
|
| 115 |
+
[
|
| 116 |
+
"2012-05-29 13:04:21.322000",
|
| 117 |
+
"2012-05-11 15:27:24.873000",
|
| 118 |
+
"2012-05-11 15:31:05.350000",
|
| 119 |
+
]
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
result = left.union(right, sort=sort)
|
| 123 |
+
exp = DatetimeIndex(
|
| 124 |
+
[
|
| 125 |
+
"2012-05-11 15:19:49.695000",
|
| 126 |
+
"2012-05-29 13:04:21.322000",
|
| 127 |
+
"2012-05-11 15:27:24.873000",
|
| 128 |
+
"2012-05-11 15:31:05.350000",
|
| 129 |
+
]
|
| 130 |
+
)
|
| 131 |
+
if sort is None:
|
| 132 |
+
exp = exp.sort_values()
|
| 133 |
+
tm.assert_index_equal(result, exp)
|
| 134 |
+
|
| 135 |
+
def test_union_bug_4564(self, sort):
|
| 136 |
+
from pandas import DateOffset
|
| 137 |
+
|
| 138 |
+
left = date_range("2013-01-01", "2013-02-01")
|
| 139 |
+
right = left + DateOffset(minutes=15)
|
| 140 |
+
|
| 141 |
+
result = left.union(right, sort=sort)
|
| 142 |
+
exp = list(left) + list(right)
|
| 143 |
+
if sort is None:
|
| 144 |
+
exp = DatetimeIndex(sorted(exp))
|
| 145 |
+
else:
|
| 146 |
+
exp = DatetimeIndex(exp)
|
| 147 |
+
tm.assert_index_equal(result, exp)
|
| 148 |
+
|
| 149 |
+
def test_union_freq_both_none(self, sort):
|
| 150 |
+
# GH11086
|
| 151 |
+
expected = bdate_range("20150101", periods=10)
|
| 152 |
+
expected._data.freq = None
|
| 153 |
+
|
| 154 |
+
result = expected.union(expected, sort=sort)
|
| 155 |
+
tm.assert_index_equal(result, expected)
|
| 156 |
+
assert result.freq is None
|
| 157 |
+
|
| 158 |
+
def test_union_freq_infer(self):
|
| 159 |
+
# When taking the union of two DatetimeIndexes, we infer
|
| 160 |
+
# a freq even if the arguments don't have freq. This matches
|
| 161 |
+
# TimedeltaIndex behavior.
|
| 162 |
+
dti = date_range("2016-01-01", periods=5)
|
| 163 |
+
left = dti[[0, 1, 3, 4]]
|
| 164 |
+
right = dti[[2, 3, 1]]
|
| 165 |
+
|
| 166 |
+
assert left.freq is None
|
| 167 |
+
assert right.freq is None
|
| 168 |
+
|
| 169 |
+
result = left.union(right)
|
| 170 |
+
tm.assert_index_equal(result, dti)
|
| 171 |
+
assert result.freq == "D"
|
| 172 |
+
|
| 173 |
+
def test_union_dataframe_index(self):
|
| 174 |
+
rng1 = date_range("1/1/1999", "1/1/2012", freq="MS")
|
| 175 |
+
s1 = Series(np.random.randn(len(rng1)), rng1)
|
| 176 |
+
|
| 177 |
+
rng2 = date_range("1/1/1980", "12/1/2001", freq="MS")
|
| 178 |
+
s2 = Series(np.random.randn(len(rng2)), rng2)
|
| 179 |
+
df = DataFrame({"s1": s1, "s2": s2})
|
| 180 |
+
|
| 181 |
+
exp = date_range("1/1/1980", "1/1/2012", freq="MS")
|
| 182 |
+
tm.assert_index_equal(df.index, exp)
|
| 183 |
+
|
| 184 |
+
def test_union_with_DatetimeIndex(self, sort):
|
| 185 |
+
i1 = Index(np.arange(0, 20, 2, dtype=np.int64))
|
| 186 |
+
i2 = date_range(start="2012-01-03 00:00:00", periods=10, freq="D")
|
| 187 |
+
# Works
|
| 188 |
+
i1.union(i2, sort=sort)
|
| 189 |
+
# Fails with "AttributeError: can't set attribute"
|
| 190 |
+
i2.union(i1, sort=sort)
|
| 191 |
+
|
| 192 |
+
# TODO: moved from test_datetimelike; de-duplicate with version below
|
| 193 |
+
def test_intersection2(self):
|
| 194 |
+
first = tm.makeDateIndex(10)
|
| 195 |
+
second = first[5:]
|
| 196 |
+
intersect = first.intersection(second)
|
| 197 |
+
assert tm.equalContents(intersect, second)
|
| 198 |
+
|
| 199 |
+
# GH 10149
|
| 200 |
+
cases = [klass(second.values) for klass in [np.array, Series, list]]
|
| 201 |
+
for case in cases:
|
| 202 |
+
result = first.intersection(case)
|
| 203 |
+
assert tm.equalContents(result, second)
|
| 204 |
+
|
| 205 |
+
third = Index(["a", "b", "c"])
|
| 206 |
+
result = first.intersection(third)
|
| 207 |
+
expected = Index([], dtype=object)
|
| 208 |
+
tm.assert_index_equal(result, expected)
|
| 209 |
+
|
| 210 |
+
@pytest.mark.parametrize(
|
| 211 |
+
"tz", [None, "Asia/Tokyo", "US/Eastern", "dateutil/US/Pacific"]
|
| 212 |
+
)
|
| 213 |
+
def test_intersection(self, tz, sort):
|
| 214 |
+
# GH 4690 (with tz)
|
| 215 |
+
base = date_range("6/1/2000", "6/30/2000", freq="D", name="idx")
|
| 216 |
+
|
| 217 |
+
# if target has the same name, it is preserved
|
| 218 |
+
rng2 = date_range("5/15/2000", "6/20/2000", freq="D", name="idx")
|
| 219 |
+
expected2 = date_range("6/1/2000", "6/20/2000", freq="D", name="idx")
|
| 220 |
+
|
| 221 |
+
# if target name is different, it will be reset
|
| 222 |
+
rng3 = date_range("5/15/2000", "6/20/2000", freq="D", name="other")
|
| 223 |
+
expected3 = date_range("6/1/2000", "6/20/2000", freq="D", name=None)
|
| 224 |
+
|
| 225 |
+
rng4 = date_range("7/1/2000", "7/31/2000", freq="D", name="idx")
|
| 226 |
+
expected4 = DatetimeIndex([], freq="D", name="idx")
|
| 227 |
+
|
| 228 |
+
for rng, expected in [
|
| 229 |
+
(rng2, expected2),
|
| 230 |
+
(rng3, expected3),
|
| 231 |
+
(rng4, expected4),
|
| 232 |
+
]:
|
| 233 |
+
result = base.intersection(rng)
|
| 234 |
+
tm.assert_index_equal(result, expected)
|
| 235 |
+
assert result.freq == expected.freq
|
| 236 |
+
|
| 237 |
+
# non-monotonic
|
| 238 |
+
base = DatetimeIndex(
|
| 239 |
+
["2011-01-05", "2011-01-04", "2011-01-02", "2011-01-03"], tz=tz, name="idx"
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
rng2 = DatetimeIndex(
|
| 243 |
+
["2011-01-04", "2011-01-02", "2011-02-02", "2011-02-03"], tz=tz, name="idx"
|
| 244 |
+
)
|
| 245 |
+
expected2 = DatetimeIndex(["2011-01-04", "2011-01-02"], tz=tz, name="idx")
|
| 246 |
+
|
| 247 |
+
rng3 = DatetimeIndex(
|
| 248 |
+
["2011-01-04", "2011-01-02", "2011-02-02", "2011-02-03"],
|
| 249 |
+
tz=tz,
|
| 250 |
+
name="other",
|
| 251 |
+
)
|
| 252 |
+
expected3 = DatetimeIndex(["2011-01-04", "2011-01-02"], tz=tz, name=None)
|
| 253 |
+
|
| 254 |
+
# GH 7880
|
| 255 |
+
rng4 = date_range("7/1/2000", "7/31/2000", freq="D", tz=tz, name="idx")
|
| 256 |
+
expected4 = DatetimeIndex([], tz=tz, name="idx")
|
| 257 |
+
assert expected4.freq is None
|
| 258 |
+
|
| 259 |
+
for rng, expected in [
|
| 260 |
+
(rng2, expected2),
|
| 261 |
+
(rng3, expected3),
|
| 262 |
+
(rng4, expected4),
|
| 263 |
+
]:
|
| 264 |
+
result = base.intersection(rng, sort=sort)
|
| 265 |
+
if sort is None:
|
| 266 |
+
expected = expected.sort_values()
|
| 267 |
+
tm.assert_index_equal(result, expected)
|
| 268 |
+
assert result.freq == expected.freq
|
| 269 |
+
|
| 270 |
+
# parametrize over both anchored and non-anchored freqs, as they
|
| 271 |
+
# have different code paths
|
| 272 |
+
@pytest.mark.parametrize("freq", ["T", "B"])
|
| 273 |
+
def test_intersection_empty(self, tz_aware_fixture, freq):
|
| 274 |
+
# empty same freq GH2129
|
| 275 |
+
tz = tz_aware_fixture
|
| 276 |
+
rng = date_range("6/1/2000", "6/15/2000", freq=freq, tz=tz)
|
| 277 |
+
result = rng[0:0].intersection(rng)
|
| 278 |
+
assert len(result) == 0
|
| 279 |
+
assert result.freq == rng.freq
|
| 280 |
+
|
| 281 |
+
result = rng.intersection(rng[0:0])
|
| 282 |
+
assert len(result) == 0
|
| 283 |
+
assert result.freq == rng.freq
|
| 284 |
+
|
| 285 |
+
# no overlap GH#33604
|
| 286 |
+
check_freq = freq != "T" # We don't preserve freq on non-anchored offsets
|
| 287 |
+
result = rng[:3].intersection(rng[-3:])
|
| 288 |
+
tm.assert_index_equal(result, rng[:0])
|
| 289 |
+
if check_freq:
|
| 290 |
+
# We don't preserve freq on non-anchored offsets
|
| 291 |
+
assert result.freq == rng.freq
|
| 292 |
+
|
| 293 |
+
# swapped left and right
|
| 294 |
+
result = rng[-3:].intersection(rng[:3])
|
| 295 |
+
tm.assert_index_equal(result, rng[:0])
|
| 296 |
+
if check_freq:
|
| 297 |
+
# We don't preserve freq on non-anchored offsets
|
| 298 |
+
assert result.freq == rng.freq
|
| 299 |
+
|
| 300 |
+
def test_intersection_bug_1708(self):
|
| 301 |
+
from pandas import DateOffset
|
| 302 |
+
|
| 303 |
+
index_1 = date_range("1/1/2012", periods=4, freq="12H")
|
| 304 |
+
index_2 = index_1 + DateOffset(hours=1)
|
| 305 |
+
|
| 306 |
+
result = index_1.intersection(index_2)
|
| 307 |
+
assert len(result) == 0
|
| 308 |
+
|
| 309 |
+
@pytest.mark.parametrize("tz", tz)
|
| 310 |
+
def test_difference(self, tz, sort):
|
| 311 |
+
rng_dates = ["1/2/2000", "1/3/2000", "1/1/2000", "1/4/2000", "1/5/2000"]
|
| 312 |
+
|
| 313 |
+
rng1 = DatetimeIndex(rng_dates, tz=tz)
|
| 314 |
+
other1 = date_range("1/6/2000", freq="D", periods=5, tz=tz)
|
| 315 |
+
expected1 = DatetimeIndex(rng_dates, tz=tz)
|
| 316 |
+
|
| 317 |
+
rng2 = DatetimeIndex(rng_dates, tz=tz)
|
| 318 |
+
other2 = date_range("1/4/2000", freq="D", periods=5, tz=tz)
|
| 319 |
+
expected2 = DatetimeIndex(rng_dates[:3], tz=tz)
|
| 320 |
+
|
| 321 |
+
rng3 = DatetimeIndex(rng_dates, tz=tz)
|
| 322 |
+
other3 = DatetimeIndex([], tz=tz)
|
| 323 |
+
expected3 = DatetimeIndex(rng_dates, tz=tz)
|
| 324 |
+
|
| 325 |
+
for rng, other, expected in [
|
| 326 |
+
(rng1, other1, expected1),
|
| 327 |
+
(rng2, other2, expected2),
|
| 328 |
+
(rng3, other3, expected3),
|
| 329 |
+
]:
|
| 330 |
+
result_diff = rng.difference(other, sort)
|
| 331 |
+
if sort is None and len(other):
|
| 332 |
+
# We dont sort (yet?) when empty GH#24959
|
| 333 |
+
expected = expected.sort_values()
|
| 334 |
+
tm.assert_index_equal(result_diff, expected)
|
| 335 |
+
|
| 336 |
+
def test_difference_freq(self, sort):
|
| 337 |
+
# GH14323: difference of DatetimeIndex should not preserve frequency
|
| 338 |
+
|
| 339 |
+
index = date_range("20160920", "20160925", freq="D")
|
| 340 |
+
other = date_range("20160921", "20160924", freq="D")
|
| 341 |
+
expected = DatetimeIndex(["20160920", "20160925"], freq=None)
|
| 342 |
+
idx_diff = index.difference(other, sort)
|
| 343 |
+
tm.assert_index_equal(idx_diff, expected)
|
| 344 |
+
tm.assert_attr_equal("freq", idx_diff, expected)
|
| 345 |
+
|
| 346 |
+
other = date_range("20160922", "20160925", freq="D")
|
| 347 |
+
idx_diff = index.difference(other, sort)
|
| 348 |
+
expected = DatetimeIndex(["20160920", "20160921"], freq=None)
|
| 349 |
+
tm.assert_index_equal(idx_diff, expected)
|
| 350 |
+
tm.assert_attr_equal("freq", idx_diff, expected)
|
| 351 |
+
|
| 352 |
+
def test_datetimeindex_diff(self, sort):
|
| 353 |
+
dti1 = date_range(freq="Q-JAN", start=datetime(1997, 12, 31), periods=100)
|
| 354 |
+
dti2 = date_range(freq="Q-JAN", start=datetime(1997, 12, 31), periods=98)
|
| 355 |
+
assert len(dti1.difference(dti2, sort)) == 2
|
| 356 |
+
|
| 357 |
+
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo", "US/Eastern"])
|
| 358 |
+
def test_setops_preserve_freq(self, tz):
|
| 359 |
+
rng = date_range("1/1/2000", "1/1/2002", name="idx", tz=tz)
|
| 360 |
+
|
| 361 |
+
result = rng[:50].union(rng[50:100])
|
| 362 |
+
assert result.name == rng.name
|
| 363 |
+
assert result.freq == rng.freq
|
| 364 |
+
assert result.tz == rng.tz
|
| 365 |
+
|
| 366 |
+
result = rng[:50].union(rng[30:100])
|
| 367 |
+
assert result.name == rng.name
|
| 368 |
+
assert result.freq == rng.freq
|
| 369 |
+
assert result.tz == rng.tz
|
| 370 |
+
|
| 371 |
+
result = rng[:50].union(rng[60:100])
|
| 372 |
+
assert result.name == rng.name
|
| 373 |
+
assert result.freq is None
|
| 374 |
+
assert result.tz == rng.tz
|
| 375 |
+
|
| 376 |
+
result = rng[:50].intersection(rng[25:75])
|
| 377 |
+
assert result.name == rng.name
|
| 378 |
+
assert result.freqstr == "D"
|
| 379 |
+
assert result.tz == rng.tz
|
| 380 |
+
|
| 381 |
+
nofreq = DatetimeIndex(list(rng[25:75]), name="other")
|
| 382 |
+
result = rng[:50].union(nofreq)
|
| 383 |
+
assert result.name is None
|
| 384 |
+
assert result.freq == rng.freq
|
| 385 |
+
assert result.tz == rng.tz
|
| 386 |
+
|
| 387 |
+
result = rng[:50].intersection(nofreq)
|
| 388 |
+
assert result.name is None
|
| 389 |
+
assert result.freq == rng.freq
|
| 390 |
+
assert result.tz == rng.tz
|
| 391 |
+
|
| 392 |
+
def test_intersection_non_tick_no_fastpath(self):
|
| 393 |
+
# GH#42104
|
| 394 |
+
dti = DatetimeIndex(
|
| 395 |
+
[
|
| 396 |
+
"2018-12-31",
|
| 397 |
+
"2019-03-31",
|
| 398 |
+
"2019-06-30",
|
| 399 |
+
"2019-09-30",
|
| 400 |
+
"2019-12-31",
|
| 401 |
+
"2020-03-31",
|
| 402 |
+
],
|
| 403 |
+
freq="Q-DEC",
|
| 404 |
+
)
|
| 405 |
+
result = dti[::2].intersection(dti[1::2])
|
| 406 |
+
expected = dti[:0]
|
| 407 |
+
tm.assert_index_equal(result, expected)
|
| 408 |
+
|
| 409 |
+
|
| 410 |
+
class TestBusinessDatetimeIndex:
|
| 411 |
+
def test_union(self, sort):
|
| 412 |
+
rng = bdate_range(START, END)
|
| 413 |
+
# overlapping
|
| 414 |
+
left = rng[:10]
|
| 415 |
+
right = rng[5:10]
|
| 416 |
+
|
| 417 |
+
the_union = left.union(right, sort=sort)
|
| 418 |
+
assert isinstance(the_union, DatetimeIndex)
|
| 419 |
+
|
| 420 |
+
# non-overlapping, gap in middle
|
| 421 |
+
left = rng[:5]
|
| 422 |
+
right = rng[10:]
|
| 423 |
+
|
| 424 |
+
the_union = left.union(right, sort=sort)
|
| 425 |
+
assert isinstance(the_union, Index)
|
| 426 |
+
|
| 427 |
+
# non-overlapping, no gap
|
| 428 |
+
left = rng[:5]
|
| 429 |
+
right = rng[5:10]
|
| 430 |
+
|
| 431 |
+
the_union = left.union(right, sort=sort)
|
| 432 |
+
assert isinstance(the_union, DatetimeIndex)
|
| 433 |
+
|
| 434 |
+
# order does not matter
|
| 435 |
+
if sort is None:
|
| 436 |
+
tm.assert_index_equal(right.union(left, sort=sort), the_union)
|
| 437 |
+
else:
|
| 438 |
+
expected = DatetimeIndex(list(right) + list(left))
|
| 439 |
+
tm.assert_index_equal(right.union(left, sort=sort), expected)
|
| 440 |
+
|
| 441 |
+
# overlapping, but different offset
|
| 442 |
+
rng = date_range(START, END, freq=BMonthEnd())
|
| 443 |
+
|
| 444 |
+
the_union = rng.union(rng, sort=sort)
|
| 445 |
+
assert isinstance(the_union, DatetimeIndex)
|
| 446 |
+
|
| 447 |
+
def test_union_not_cacheable(self, sort):
|
| 448 |
+
rng = date_range("1/1/2000", periods=50, freq=Minute())
|
| 449 |
+
rng1 = rng[10:]
|
| 450 |
+
rng2 = rng[:25]
|
| 451 |
+
the_union = rng1.union(rng2, sort=sort)
|
| 452 |
+
if sort is None:
|
| 453 |
+
tm.assert_index_equal(the_union, rng)
|
| 454 |
+
else:
|
| 455 |
+
expected = DatetimeIndex(list(rng[10:]) + list(rng[:10]))
|
| 456 |
+
tm.assert_index_equal(the_union, expected)
|
| 457 |
+
|
| 458 |
+
rng1 = rng[10:]
|
| 459 |
+
rng2 = rng[15:35]
|
| 460 |
+
the_union = rng1.union(rng2, sort=sort)
|
| 461 |
+
expected = rng[10:]
|
| 462 |
+
tm.assert_index_equal(the_union, expected)
|
| 463 |
+
|
| 464 |
+
def test_intersection(self):
|
| 465 |
+
rng = date_range("1/1/2000", periods=50, freq=Minute())
|
| 466 |
+
rng1 = rng[10:]
|
| 467 |
+
rng2 = rng[:25]
|
| 468 |
+
the_int = rng1.intersection(rng2)
|
| 469 |
+
expected = rng[10:25]
|
| 470 |
+
tm.assert_index_equal(the_int, expected)
|
| 471 |
+
assert isinstance(the_int, DatetimeIndex)
|
| 472 |
+
assert the_int.freq == rng.freq
|
| 473 |
+
|
| 474 |
+
the_int = rng1.intersection(rng2.view(DatetimeIndex))
|
| 475 |
+
tm.assert_index_equal(the_int, expected)
|
| 476 |
+
|
| 477 |
+
# non-overlapping
|
| 478 |
+
the_int = rng[:10].intersection(rng[10:])
|
| 479 |
+
expected = DatetimeIndex([])
|
| 480 |
+
tm.assert_index_equal(the_int, expected)
|
| 481 |
+
|
| 482 |
+
def test_intersection_bug(self):
|
| 483 |
+
# GH #771
|
| 484 |
+
a = bdate_range("11/30/2011", "12/31/2011")
|
| 485 |
+
b = bdate_range("12/10/2011", "12/20/2011")
|
| 486 |
+
result = a.intersection(b)
|
| 487 |
+
tm.assert_index_equal(result, b)
|
| 488 |
+
assert result.freq == b.freq
|
| 489 |
+
|
| 490 |
+
def test_intersection_list(self):
|
| 491 |
+
# GH#35876
|
| 492 |
+
# values is not an Index -> no name -> retain "a"
|
| 493 |
+
values = [pd.Timestamp("2020-01-01"), pd.Timestamp("2020-02-01")]
|
| 494 |
+
idx = DatetimeIndex(values, name="a")
|
| 495 |
+
res = idx.intersection(values)
|
| 496 |
+
tm.assert_index_equal(res, idx)
|
| 497 |
+
|
| 498 |
+
def test_month_range_union_tz_pytz(self, sort):
|
| 499 |
+
from pytz import timezone
|
| 500 |
+
|
| 501 |
+
tz = timezone("US/Eastern")
|
| 502 |
+
|
| 503 |
+
early_start = datetime(2011, 1, 1)
|
| 504 |
+
early_end = datetime(2011, 3, 1)
|
| 505 |
+
|
| 506 |
+
late_start = datetime(2011, 3, 1)
|
| 507 |
+
late_end = datetime(2011, 5, 1)
|
| 508 |
+
|
| 509 |
+
early_dr = date_range(start=early_start, end=early_end, tz=tz, freq=MonthEnd())
|
| 510 |
+
late_dr = date_range(start=late_start, end=late_end, tz=tz, freq=MonthEnd())
|
| 511 |
+
|
| 512 |
+
early_dr.union(late_dr, sort=sort)
|
| 513 |
+
|
| 514 |
+
@td.skip_if_windows
|
| 515 |
+
def test_month_range_union_tz_dateutil(self, sort):
|
| 516 |
+
from pandas._libs.tslibs.timezones import dateutil_gettz
|
| 517 |
+
|
| 518 |
+
tz = dateutil_gettz("US/Eastern")
|
| 519 |
+
|
| 520 |
+
early_start = datetime(2011, 1, 1)
|
| 521 |
+
early_end = datetime(2011, 3, 1)
|
| 522 |
+
|
| 523 |
+
late_start = datetime(2011, 3, 1)
|
| 524 |
+
late_end = datetime(2011, 5, 1)
|
| 525 |
+
|
| 526 |
+
early_dr = date_range(start=early_start, end=early_end, tz=tz, freq=MonthEnd())
|
| 527 |
+
late_dr = date_range(start=late_start, end=late_end, tz=tz, freq=MonthEnd())
|
| 528 |
+
|
| 529 |
+
early_dr.union(late_dr, sort=sort)
|
| 530 |
+
|
| 531 |
+
@pytest.mark.parametrize("sort", [False, None])
|
| 532 |
+
def test_intersection_duplicates(self, sort):
|
| 533 |
+
# GH#38196
|
| 534 |
+
idx1 = Index(
|
| 535 |
+
[
|
| 536 |
+
pd.Timestamp("2019-12-13"),
|
| 537 |
+
pd.Timestamp("2019-12-12"),
|
| 538 |
+
pd.Timestamp("2019-12-12"),
|
| 539 |
+
]
|
| 540 |
+
)
|
| 541 |
+
result = idx1.intersection(idx1, sort=sort)
|
| 542 |
+
expected = Index([pd.Timestamp("2019-12-13"), pd.Timestamp("2019-12-12")])
|
| 543 |
+
tm.assert_index_equal(result, expected)
|
| 544 |
+
|
| 545 |
+
|
| 546 |
+
class TestCustomDatetimeIndex:
|
| 547 |
+
def test_union(self, sort):
|
| 548 |
+
# overlapping
|
| 549 |
+
rng = bdate_range(START, END, freq="C")
|
| 550 |
+
left = rng[:10]
|
| 551 |
+
right = rng[5:10]
|
| 552 |
+
|
| 553 |
+
the_union = left.union(right, sort=sort)
|
| 554 |
+
assert isinstance(the_union, DatetimeIndex)
|
| 555 |
+
|
| 556 |
+
# non-overlapping, gap in middle
|
| 557 |
+
left = rng[:5]
|
| 558 |
+
right = rng[10:]
|
| 559 |
+
|
| 560 |
+
the_union = left.union(right, sort)
|
| 561 |
+
assert isinstance(the_union, Index)
|
| 562 |
+
|
| 563 |
+
# non-overlapping, no gap
|
| 564 |
+
left = rng[:5]
|
| 565 |
+
right = rng[5:10]
|
| 566 |
+
|
| 567 |
+
the_union = left.union(right, sort=sort)
|
| 568 |
+
assert isinstance(the_union, DatetimeIndex)
|
| 569 |
+
|
| 570 |
+
# order does not matter
|
| 571 |
+
if sort is None:
|
| 572 |
+
tm.assert_index_equal(right.union(left, sort=sort), the_union)
|
| 573 |
+
|
| 574 |
+
# overlapping, but different offset
|
| 575 |
+
rng = date_range(START, END, freq=BMonthEnd())
|
| 576 |
+
|
| 577 |
+
the_union = rng.union(rng, sort=sort)
|
| 578 |
+
assert isinstance(the_union, DatetimeIndex)
|
| 579 |
+
|
| 580 |
+
def test_intersection_bug(self):
|
| 581 |
+
# GH #771
|
| 582 |
+
a = bdate_range("11/30/2011", "12/31/2011", freq="C")
|
| 583 |
+
b = bdate_range("12/10/2011", "12/20/2011", freq="C")
|
| 584 |
+
result = a.intersection(b)
|
| 585 |
+
tm.assert_index_equal(result, b)
|
| 586 |
+
assert result.freq == b.freq
|
| 587 |
+
|
| 588 |
+
@pytest.mark.parametrize(
|
| 589 |
+
"tz", [None, "UTC", "Europe/Berlin", pytz.FixedOffset(-60)]
|
| 590 |
+
)
|
| 591 |
+
def test_intersection_dst_transition(self, tz):
|
| 592 |
+
# GH 46702: Europe/Berlin has DST transition
|
| 593 |
+
idx1 = date_range("2020-03-27", periods=5, freq="D", tz=tz)
|
| 594 |
+
idx2 = date_range("2020-03-30", periods=5, freq="D", tz=tz)
|
| 595 |
+
result = idx1.intersection(idx2)
|
| 596 |
+
expected = date_range("2020-03-30", periods=2, freq="D", tz=tz)
|
| 597 |
+
tm.assert_index_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_timezones.py
ADDED
|
@@ -0,0 +1,1213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tests for DatetimeIndex timezone-related methods
|
| 3 |
+
"""
|
| 4 |
+
from datetime import (
|
| 5 |
+
date,
|
| 6 |
+
datetime,
|
| 7 |
+
time,
|
| 8 |
+
timedelta,
|
| 9 |
+
timezone,
|
| 10 |
+
tzinfo,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
import dateutil
|
| 14 |
+
from dateutil.tz import (
|
| 15 |
+
gettz,
|
| 16 |
+
tzlocal,
|
| 17 |
+
)
|
| 18 |
+
import numpy as np
|
| 19 |
+
import pytest
|
| 20 |
+
import pytz
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
from zoneinfo import ZoneInfo
|
| 24 |
+
except ImportError:
|
| 25 |
+
ZoneInfo = None
|
| 26 |
+
|
| 27 |
+
from pandas._libs.tslibs import (
|
| 28 |
+
conversion,
|
| 29 |
+
timezones,
|
| 30 |
+
)
|
| 31 |
+
import pandas.util._test_decorators as td
|
| 32 |
+
|
| 33 |
+
import pandas as pd
|
| 34 |
+
from pandas import (
|
| 35 |
+
DatetimeIndex,
|
| 36 |
+
Index,
|
| 37 |
+
Timestamp,
|
| 38 |
+
bdate_range,
|
| 39 |
+
date_range,
|
| 40 |
+
isna,
|
| 41 |
+
to_datetime,
|
| 42 |
+
)
|
| 43 |
+
import pandas._testing as tm
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class FixedOffset(tzinfo):
|
| 47 |
+
"""Fixed offset in minutes east from UTC."""
|
| 48 |
+
|
| 49 |
+
def __init__(self, offset, name) -> None:
|
| 50 |
+
self.__offset = timedelta(minutes=offset)
|
| 51 |
+
self.__name = name
|
| 52 |
+
|
| 53 |
+
def utcoffset(self, dt):
|
| 54 |
+
return self.__offset
|
| 55 |
+
|
| 56 |
+
def tzname(self, dt):
|
| 57 |
+
return self.__name
|
| 58 |
+
|
| 59 |
+
def dst(self, dt):
|
| 60 |
+
return timedelta(0)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
fixed_off = FixedOffset(-420, "-07:00")
|
| 64 |
+
fixed_off_no_name = FixedOffset(-330, None)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class TestDatetimeIndexTimezones:
|
| 68 |
+
# -------------------------------------------------------------
|
| 69 |
+
# DatetimeIndex.tz_convert
|
| 70 |
+
def test_tz_convert_nat(self):
|
| 71 |
+
# GH#5546
|
| 72 |
+
dates = [pd.NaT]
|
| 73 |
+
idx = DatetimeIndex(dates)
|
| 74 |
+
idx = idx.tz_localize("US/Pacific")
|
| 75 |
+
tm.assert_index_equal(idx, DatetimeIndex(dates, tz="US/Pacific"))
|
| 76 |
+
idx = idx.tz_convert("US/Eastern")
|
| 77 |
+
tm.assert_index_equal(idx, DatetimeIndex(dates, tz="US/Eastern"))
|
| 78 |
+
idx = idx.tz_convert("UTC")
|
| 79 |
+
tm.assert_index_equal(idx, DatetimeIndex(dates, tz="UTC"))
|
| 80 |
+
|
| 81 |
+
dates = ["2010-12-01 00:00", "2010-12-02 00:00", pd.NaT]
|
| 82 |
+
idx = DatetimeIndex(dates)
|
| 83 |
+
idx = idx.tz_localize("US/Pacific")
|
| 84 |
+
tm.assert_index_equal(idx, DatetimeIndex(dates, tz="US/Pacific"))
|
| 85 |
+
idx = idx.tz_convert("US/Eastern")
|
| 86 |
+
expected = ["2010-12-01 03:00", "2010-12-02 03:00", pd.NaT]
|
| 87 |
+
tm.assert_index_equal(idx, DatetimeIndex(expected, tz="US/Eastern"))
|
| 88 |
+
|
| 89 |
+
idx = idx + pd.offsets.Hour(5)
|
| 90 |
+
expected = ["2010-12-01 08:00", "2010-12-02 08:00", pd.NaT]
|
| 91 |
+
tm.assert_index_equal(idx, DatetimeIndex(expected, tz="US/Eastern"))
|
| 92 |
+
idx = idx.tz_convert("US/Pacific")
|
| 93 |
+
expected = ["2010-12-01 05:00", "2010-12-02 05:00", pd.NaT]
|
| 94 |
+
tm.assert_index_equal(idx, DatetimeIndex(expected, tz="US/Pacific"))
|
| 95 |
+
|
| 96 |
+
idx = idx + np.timedelta64(3, "h")
|
| 97 |
+
expected = ["2010-12-01 08:00", "2010-12-02 08:00", pd.NaT]
|
| 98 |
+
tm.assert_index_equal(idx, DatetimeIndex(expected, tz="US/Pacific"))
|
| 99 |
+
|
| 100 |
+
idx = idx.tz_convert("US/Eastern")
|
| 101 |
+
expected = ["2010-12-01 11:00", "2010-12-02 11:00", pd.NaT]
|
| 102 |
+
tm.assert_index_equal(idx, DatetimeIndex(expected, tz="US/Eastern"))
|
| 103 |
+
|
| 104 |
+
@pytest.mark.parametrize("prefix", ["", "dateutil/"])
|
| 105 |
+
def test_dti_tz_convert_compat_timestamp(self, prefix):
|
| 106 |
+
strdates = ["1/1/2012", "3/1/2012", "4/1/2012"]
|
| 107 |
+
idx = DatetimeIndex(strdates, tz=prefix + "US/Eastern")
|
| 108 |
+
|
| 109 |
+
conv = idx[0].tz_convert(prefix + "US/Pacific")
|
| 110 |
+
expected = idx.tz_convert(prefix + "US/Pacific")[0]
|
| 111 |
+
|
| 112 |
+
assert conv == expected
|
| 113 |
+
|
| 114 |
+
def test_dti_tz_convert_hour_overflow_dst(self):
|
| 115 |
+
# Regression test for:
|
| 116 |
+
# https://github.com/pandas-dev/pandas/issues/13306
|
| 117 |
+
|
| 118 |
+
# sorted case US/Eastern -> UTC
|
| 119 |
+
ts = ["2008-05-12 09:50:00", "2008-12-12 09:50:35", "2009-05-12 09:50:32"]
|
| 120 |
+
tt = DatetimeIndex(ts).tz_localize("US/Eastern")
|
| 121 |
+
ut = tt.tz_convert("UTC")
|
| 122 |
+
expected = Index([13, 14, 13], dtype=np.int32)
|
| 123 |
+
tm.assert_index_equal(ut.hour, expected)
|
| 124 |
+
|
| 125 |
+
# sorted case UTC -> US/Eastern
|
| 126 |
+
ts = ["2008-05-12 13:50:00", "2008-12-12 14:50:35", "2009-05-12 13:50:32"]
|
| 127 |
+
tt = DatetimeIndex(ts).tz_localize("UTC")
|
| 128 |
+
ut = tt.tz_convert("US/Eastern")
|
| 129 |
+
expected = Index([9, 9, 9], dtype=np.int32)
|
| 130 |
+
tm.assert_index_equal(ut.hour, expected)
|
| 131 |
+
|
| 132 |
+
# unsorted case US/Eastern -> UTC
|
| 133 |
+
ts = ["2008-05-12 09:50:00", "2008-12-12 09:50:35", "2008-05-12 09:50:32"]
|
| 134 |
+
tt = DatetimeIndex(ts).tz_localize("US/Eastern")
|
| 135 |
+
ut = tt.tz_convert("UTC")
|
| 136 |
+
expected = Index([13, 14, 13], dtype=np.int32)
|
| 137 |
+
tm.assert_index_equal(ut.hour, expected)
|
| 138 |
+
|
| 139 |
+
# unsorted case UTC -> US/Eastern
|
| 140 |
+
ts = ["2008-05-12 13:50:00", "2008-12-12 14:50:35", "2008-05-12 13:50:32"]
|
| 141 |
+
tt = DatetimeIndex(ts).tz_localize("UTC")
|
| 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("tz", ["US/Eastern", "dateutil/US/Eastern"])
|
| 147 |
+
def test_dti_tz_convert_hour_overflow_dst_timestamps(self, tz):
|
| 148 |
+
# Regression test for GH#13306
|
| 149 |
+
|
| 150 |
+
# sorted case US/Eastern -> UTC
|
| 151 |
+
ts = [
|
| 152 |
+
Timestamp("2008-05-12 09:50:00", tz=tz),
|
| 153 |
+
Timestamp("2008-12-12 09:50:35", tz=tz),
|
| 154 |
+
Timestamp("2009-05-12 09:50:32", tz=tz),
|
| 155 |
+
]
|
| 156 |
+
tt = DatetimeIndex(ts)
|
| 157 |
+
ut = tt.tz_convert("UTC")
|
| 158 |
+
expected = Index([13, 14, 13], dtype=np.int32)
|
| 159 |
+
tm.assert_index_equal(ut.hour, expected)
|
| 160 |
+
|
| 161 |
+
# sorted case UTC -> US/Eastern
|
| 162 |
+
ts = [
|
| 163 |
+
Timestamp("2008-05-12 13:50:00", tz="UTC"),
|
| 164 |
+
Timestamp("2008-12-12 14:50:35", tz="UTC"),
|
| 165 |
+
Timestamp("2009-05-12 13:50:32", tz="UTC"),
|
| 166 |
+
]
|
| 167 |
+
tt = DatetimeIndex(ts)
|
| 168 |
+
ut = tt.tz_convert("US/Eastern")
|
| 169 |
+
expected = Index([9, 9, 9], dtype=np.int32)
|
| 170 |
+
tm.assert_index_equal(ut.hour, expected)
|
| 171 |
+
|
| 172 |
+
# unsorted case US/Eastern -> UTC
|
| 173 |
+
ts = [
|
| 174 |
+
Timestamp("2008-05-12 09:50:00", tz=tz),
|
| 175 |
+
Timestamp("2008-12-12 09:50:35", tz=tz),
|
| 176 |
+
Timestamp("2008-05-12 09:50:32", tz=tz),
|
| 177 |
+
]
|
| 178 |
+
tt = DatetimeIndex(ts)
|
| 179 |
+
ut = tt.tz_convert("UTC")
|
| 180 |
+
expected = Index([13, 14, 13], dtype=np.int32)
|
| 181 |
+
tm.assert_index_equal(ut.hour, expected)
|
| 182 |
+
|
| 183 |
+
# unsorted case UTC -> US/Eastern
|
| 184 |
+
ts = [
|
| 185 |
+
Timestamp("2008-05-12 13:50:00", tz="UTC"),
|
| 186 |
+
Timestamp("2008-12-12 14:50:35", tz="UTC"),
|
| 187 |
+
Timestamp("2008-05-12 13:50:32", tz="UTC"),
|
| 188 |
+
]
|
| 189 |
+
tt = DatetimeIndex(ts)
|
| 190 |
+
ut = tt.tz_convert("US/Eastern")
|
| 191 |
+
expected = Index([9, 9, 9], dtype=np.int32)
|
| 192 |
+
tm.assert_index_equal(ut.hour, expected)
|
| 193 |
+
|
| 194 |
+
@pytest.mark.parametrize("freq, n", [("H", 1), ("T", 60), ("S", 3600)])
|
| 195 |
+
def test_dti_tz_convert_trans_pos_plus_1__bug(self, freq, n):
|
| 196 |
+
# Regression test for tslib.tz_convert(vals, tz1, tz2).
|
| 197 |
+
# See https://github.com/pandas-dev/pandas/issues/4496 for details.
|
| 198 |
+
idx = date_range(datetime(2011, 3, 26, 23), datetime(2011, 3, 27, 1), freq=freq)
|
| 199 |
+
idx = idx.tz_localize("UTC")
|
| 200 |
+
idx = idx.tz_convert("Europe/Moscow")
|
| 201 |
+
|
| 202 |
+
expected = np.repeat(np.array([3, 4, 5]), np.array([n, n, 1]))
|
| 203 |
+
tm.assert_index_equal(idx.hour, Index(expected, dtype=np.int32))
|
| 204 |
+
|
| 205 |
+
def test_dti_tz_convert_dst(self):
|
| 206 |
+
for freq, n in [("H", 1), ("T", 60), ("S", 3600)]:
|
| 207 |
+
# Start DST
|
| 208 |
+
idx = date_range(
|
| 209 |
+
"2014-03-08 23:00", "2014-03-09 09:00", freq=freq, tz="UTC"
|
| 210 |
+
)
|
| 211 |
+
idx = idx.tz_convert("US/Eastern")
|
| 212 |
+
expected = np.repeat(
|
| 213 |
+
np.array([18, 19, 20, 21, 22, 23, 0, 1, 3, 4, 5]),
|
| 214 |
+
np.array([n, n, n, n, n, n, n, n, n, n, 1]),
|
| 215 |
+
)
|
| 216 |
+
tm.assert_index_equal(idx.hour, Index(expected, dtype=np.int32))
|
| 217 |
+
|
| 218 |
+
idx = date_range(
|
| 219 |
+
"2014-03-08 18:00", "2014-03-09 05:00", freq=freq, tz="US/Eastern"
|
| 220 |
+
)
|
| 221 |
+
idx = idx.tz_convert("UTC")
|
| 222 |
+
expected = np.repeat(
|
| 223 |
+
np.array([23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
|
| 224 |
+
np.array([n, n, n, n, n, n, n, n, n, n, 1]),
|
| 225 |
+
)
|
| 226 |
+
tm.assert_index_equal(idx.hour, Index(expected, dtype=np.int32))
|
| 227 |
+
|
| 228 |
+
# End DST
|
| 229 |
+
idx = date_range(
|
| 230 |
+
"2014-11-01 23:00", "2014-11-02 09:00", freq=freq, tz="UTC"
|
| 231 |
+
)
|
| 232 |
+
idx = idx.tz_convert("US/Eastern")
|
| 233 |
+
expected = np.repeat(
|
| 234 |
+
np.array([19, 20, 21, 22, 23, 0, 1, 1, 2, 3, 4]),
|
| 235 |
+
np.array([n, n, n, n, n, n, n, n, n, n, 1]),
|
| 236 |
+
)
|
| 237 |
+
tm.assert_index_equal(idx.hour, Index(expected, dtype=np.int32))
|
| 238 |
+
|
| 239 |
+
idx = date_range(
|
| 240 |
+
"2014-11-01 18:00", "2014-11-02 05:00", freq=freq, tz="US/Eastern"
|
| 241 |
+
)
|
| 242 |
+
idx = idx.tz_convert("UTC")
|
| 243 |
+
expected = np.repeat(
|
| 244 |
+
np.array([22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
|
| 245 |
+
np.array([n, n, n, n, n, n, n, n, n, n, n, n, 1]),
|
| 246 |
+
)
|
| 247 |
+
tm.assert_index_equal(idx.hour, Index(expected, dtype=np.int32))
|
| 248 |
+
|
| 249 |
+
# daily
|
| 250 |
+
# Start DST
|
| 251 |
+
idx = date_range("2014-03-08 00:00", "2014-03-09 00:00", freq="D", tz="UTC")
|
| 252 |
+
idx = idx.tz_convert("US/Eastern")
|
| 253 |
+
tm.assert_index_equal(idx.hour, Index([19, 19], dtype=np.int32))
|
| 254 |
+
|
| 255 |
+
idx = date_range(
|
| 256 |
+
"2014-03-08 00:00", "2014-03-09 00:00", freq="D", tz="US/Eastern"
|
| 257 |
+
)
|
| 258 |
+
idx = idx.tz_convert("UTC")
|
| 259 |
+
tm.assert_index_equal(idx.hour, Index([5, 5], dtype=np.int32))
|
| 260 |
+
|
| 261 |
+
# End DST
|
| 262 |
+
idx = date_range("2014-11-01 00:00", "2014-11-02 00:00", freq="D", tz="UTC")
|
| 263 |
+
idx = idx.tz_convert("US/Eastern")
|
| 264 |
+
tm.assert_index_equal(idx.hour, Index([20, 20], dtype=np.int32))
|
| 265 |
+
|
| 266 |
+
idx = date_range(
|
| 267 |
+
"2014-11-01 00:00", "2014-11-02 000:00", freq="D", tz="US/Eastern"
|
| 268 |
+
)
|
| 269 |
+
idx = idx.tz_convert("UTC")
|
| 270 |
+
tm.assert_index_equal(idx.hour, Index([4, 4], dtype=np.int32))
|
| 271 |
+
|
| 272 |
+
def test_tz_convert_roundtrip(self, tz_aware_fixture):
|
| 273 |
+
tz = tz_aware_fixture
|
| 274 |
+
idx1 = date_range(start="2014-01-01", end="2014-12-31", freq="M", tz="UTC")
|
| 275 |
+
exp1 = date_range(start="2014-01-01", end="2014-12-31", freq="M")
|
| 276 |
+
|
| 277 |
+
idx2 = date_range(start="2014-01-01", end="2014-12-31", freq="D", tz="UTC")
|
| 278 |
+
exp2 = date_range(start="2014-01-01", end="2014-12-31", freq="D")
|
| 279 |
+
|
| 280 |
+
idx3 = date_range(start="2014-01-01", end="2014-03-01", freq="H", tz="UTC")
|
| 281 |
+
exp3 = date_range(start="2014-01-01", end="2014-03-01", freq="H")
|
| 282 |
+
|
| 283 |
+
idx4 = date_range(start="2014-08-01", end="2014-10-31", freq="T", tz="UTC")
|
| 284 |
+
exp4 = date_range(start="2014-08-01", end="2014-10-31", freq="T")
|
| 285 |
+
|
| 286 |
+
for idx, expected in [(idx1, exp1), (idx2, exp2), (idx3, exp3), (idx4, exp4)]:
|
| 287 |
+
converted = idx.tz_convert(tz)
|
| 288 |
+
reset = converted.tz_convert(None)
|
| 289 |
+
tm.assert_index_equal(reset, expected)
|
| 290 |
+
assert reset.tzinfo is None
|
| 291 |
+
expected = converted.tz_convert("UTC").tz_localize(None)
|
| 292 |
+
expected = expected._with_freq("infer")
|
| 293 |
+
tm.assert_index_equal(reset, expected)
|
| 294 |
+
|
| 295 |
+
def test_dti_tz_convert_tzlocal(self):
|
| 296 |
+
# GH#13583
|
| 297 |
+
# tz_convert doesn't affect to internal
|
| 298 |
+
dti = date_range(start="2001-01-01", end="2001-03-01", tz="UTC")
|
| 299 |
+
dti2 = dti.tz_convert(dateutil.tz.tzlocal())
|
| 300 |
+
tm.assert_numpy_array_equal(dti2.asi8, dti.asi8)
|
| 301 |
+
|
| 302 |
+
dti = date_range(start="2001-01-01", end="2001-03-01", tz=dateutil.tz.tzlocal())
|
| 303 |
+
dti2 = dti.tz_convert(None)
|
| 304 |
+
tm.assert_numpy_array_equal(dti2.asi8, dti.asi8)
|
| 305 |
+
|
| 306 |
+
@pytest.mark.parametrize(
|
| 307 |
+
"tz",
|
| 308 |
+
[
|
| 309 |
+
"US/Eastern",
|
| 310 |
+
"dateutil/US/Eastern",
|
| 311 |
+
pytz.timezone("US/Eastern"),
|
| 312 |
+
gettz("US/Eastern"),
|
| 313 |
+
],
|
| 314 |
+
)
|
| 315 |
+
def test_dti_tz_convert_utc_to_local_no_modify(self, tz):
|
| 316 |
+
rng = date_range("3/11/2012", "3/12/2012", freq="H", tz="utc")
|
| 317 |
+
rng_eastern = rng.tz_convert(tz)
|
| 318 |
+
|
| 319 |
+
# Values are unmodified
|
| 320 |
+
tm.assert_numpy_array_equal(rng.asi8, rng_eastern.asi8)
|
| 321 |
+
|
| 322 |
+
assert timezones.tz_compare(rng_eastern.tz, timezones.maybe_get_tz(tz))
|
| 323 |
+
|
| 324 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 325 |
+
def test_tz_convert_unsorted(self, tzstr):
|
| 326 |
+
dr = date_range("2012-03-09", freq="H", periods=100, tz="utc")
|
| 327 |
+
dr = dr.tz_convert(tzstr)
|
| 328 |
+
|
| 329 |
+
result = dr[::-1].hour
|
| 330 |
+
exp = dr.hour[::-1]
|
| 331 |
+
tm.assert_almost_equal(result, exp)
|
| 332 |
+
|
| 333 |
+
# -------------------------------------------------------------
|
| 334 |
+
# DatetimeIndex.tz_localize
|
| 335 |
+
|
| 336 |
+
def test_tz_localize_utc_copies(self, utc_fixture):
|
| 337 |
+
# GH#46460
|
| 338 |
+
times = ["2015-03-08 01:00", "2015-03-08 02:00", "2015-03-08 03:00"]
|
| 339 |
+
index = DatetimeIndex(times)
|
| 340 |
+
|
| 341 |
+
res = index.tz_localize(utc_fixture)
|
| 342 |
+
assert not tm.shares_memory(res, index)
|
| 343 |
+
|
| 344 |
+
res2 = index._data.tz_localize(utc_fixture)
|
| 345 |
+
assert not tm.shares_memory(index._data, res2)
|
| 346 |
+
|
| 347 |
+
def test_dti_tz_localize_nonexistent_raise_coerce(self):
|
| 348 |
+
# GH#13057
|
| 349 |
+
times = ["2015-03-08 01:00", "2015-03-08 02:00", "2015-03-08 03:00"]
|
| 350 |
+
index = DatetimeIndex(times)
|
| 351 |
+
tz = "US/Eastern"
|
| 352 |
+
with pytest.raises(pytz.NonExistentTimeError, match="|".join(times)):
|
| 353 |
+
index.tz_localize(tz=tz)
|
| 354 |
+
|
| 355 |
+
with pytest.raises(pytz.NonExistentTimeError, match="|".join(times)):
|
| 356 |
+
index.tz_localize(tz=tz, nonexistent="raise")
|
| 357 |
+
|
| 358 |
+
result = index.tz_localize(tz=tz, nonexistent="NaT")
|
| 359 |
+
test_times = ["2015-03-08 01:00-05:00", "NaT", "2015-03-08 03:00-04:00"]
|
| 360 |
+
dti = to_datetime(test_times, utc=True)
|
| 361 |
+
expected = dti.tz_convert("US/Eastern")
|
| 362 |
+
tm.assert_index_equal(result, expected)
|
| 363 |
+
|
| 364 |
+
easts = [pytz.timezone("US/Eastern"), gettz("US/Eastern")]
|
| 365 |
+
if ZoneInfo is not None:
|
| 366 |
+
try:
|
| 367 |
+
tz = ZoneInfo("US/Eastern")
|
| 368 |
+
except KeyError:
|
| 369 |
+
# no tzdata
|
| 370 |
+
pass
|
| 371 |
+
else:
|
| 372 |
+
easts.append(tz)
|
| 373 |
+
|
| 374 |
+
@pytest.mark.parametrize("tz", easts)
|
| 375 |
+
def test_dti_tz_localize_ambiguous_infer(self, tz):
|
| 376 |
+
# November 6, 2011, fall back, repeat 2 AM hour
|
| 377 |
+
# With no repeated hours, we cannot infer the transition
|
| 378 |
+
dr = date_range(datetime(2011, 11, 6, 0), periods=5, freq=pd.offsets.Hour())
|
| 379 |
+
with pytest.raises(pytz.AmbiguousTimeError, match="Cannot infer dst time"):
|
| 380 |
+
dr.tz_localize(tz)
|
| 381 |
+
|
| 382 |
+
# With repeated hours, we can infer the transition
|
| 383 |
+
dr = date_range(
|
| 384 |
+
datetime(2011, 11, 6, 0), periods=5, freq=pd.offsets.Hour(), tz=tz
|
| 385 |
+
)
|
| 386 |
+
times = [
|
| 387 |
+
"11/06/2011 00:00",
|
| 388 |
+
"11/06/2011 01:00",
|
| 389 |
+
"11/06/2011 01:00",
|
| 390 |
+
"11/06/2011 02:00",
|
| 391 |
+
"11/06/2011 03:00",
|
| 392 |
+
]
|
| 393 |
+
di = DatetimeIndex(times)
|
| 394 |
+
localized = di.tz_localize(tz, ambiguous="infer")
|
| 395 |
+
expected = dr._with_freq(None)
|
| 396 |
+
tm.assert_index_equal(expected, localized)
|
| 397 |
+
tm.assert_index_equal(expected, DatetimeIndex(times, tz=tz, ambiguous="infer"))
|
| 398 |
+
|
| 399 |
+
# When there is no dst transition, nothing special happens
|
| 400 |
+
dr = date_range(datetime(2011, 6, 1, 0), periods=10, freq=pd.offsets.Hour())
|
| 401 |
+
localized = dr.tz_localize(tz)
|
| 402 |
+
localized_infer = dr.tz_localize(tz, ambiguous="infer")
|
| 403 |
+
tm.assert_index_equal(localized, localized_infer)
|
| 404 |
+
|
| 405 |
+
@pytest.mark.parametrize("tz", [pytz.timezone("US/Eastern"), gettz("US/Eastern")])
|
| 406 |
+
def test_dti_tz_localize_ambiguous_times(self, tz):
|
| 407 |
+
# March 13, 2011, spring forward, skip from 2 AM to 3 AM
|
| 408 |
+
dr = date_range(datetime(2011, 3, 13, 1, 30), periods=3, freq=pd.offsets.Hour())
|
| 409 |
+
with pytest.raises(pytz.NonExistentTimeError, match="2011-03-13 02:30:00"):
|
| 410 |
+
dr.tz_localize(tz)
|
| 411 |
+
|
| 412 |
+
# after dst transition, it works
|
| 413 |
+
dr = date_range(
|
| 414 |
+
datetime(2011, 3, 13, 3, 30), periods=3, freq=pd.offsets.Hour(), tz=tz
|
| 415 |
+
)
|
| 416 |
+
|
| 417 |
+
# November 6, 2011, fall back, repeat 2 AM hour
|
| 418 |
+
dr = date_range(datetime(2011, 11, 6, 1, 30), periods=3, freq=pd.offsets.Hour())
|
| 419 |
+
with pytest.raises(pytz.AmbiguousTimeError, match="Cannot infer dst time"):
|
| 420 |
+
dr.tz_localize(tz)
|
| 421 |
+
|
| 422 |
+
# UTC is OK
|
| 423 |
+
dr = date_range(
|
| 424 |
+
datetime(2011, 3, 13), periods=48, freq=pd.offsets.Minute(30), tz=pytz.utc
|
| 425 |
+
)
|
| 426 |
+
|
| 427 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 428 |
+
def test_dti_tz_localize_pass_dates_to_utc(self, tzstr):
|
| 429 |
+
strdates = ["1/1/2012", "3/1/2012", "4/1/2012"]
|
| 430 |
+
|
| 431 |
+
idx = DatetimeIndex(strdates)
|
| 432 |
+
conv = idx.tz_localize(tzstr)
|
| 433 |
+
|
| 434 |
+
fromdates = DatetimeIndex(strdates, tz=tzstr)
|
| 435 |
+
|
| 436 |
+
assert conv.tz == fromdates.tz
|
| 437 |
+
tm.assert_numpy_array_equal(conv.values, fromdates.values)
|
| 438 |
+
|
| 439 |
+
@pytest.mark.parametrize("prefix", ["", "dateutil/"])
|
| 440 |
+
def test_dti_tz_localize(self, prefix):
|
| 441 |
+
tzstr = prefix + "US/Eastern"
|
| 442 |
+
dti = date_range(start="1/1/2005", end="1/1/2005 0:00:30.256", freq="L")
|
| 443 |
+
dti2 = dti.tz_localize(tzstr)
|
| 444 |
+
|
| 445 |
+
dti_utc = date_range(
|
| 446 |
+
start="1/1/2005 05:00", end="1/1/2005 5:00:30.256", freq="L", tz="utc"
|
| 447 |
+
)
|
| 448 |
+
|
| 449 |
+
tm.assert_numpy_array_equal(dti2.values, dti_utc.values)
|
| 450 |
+
|
| 451 |
+
dti3 = dti2.tz_convert(prefix + "US/Pacific")
|
| 452 |
+
tm.assert_numpy_array_equal(dti3.values, dti_utc.values)
|
| 453 |
+
|
| 454 |
+
dti = date_range(start="11/6/2011 1:59", end="11/6/2011 2:00", freq="L")
|
| 455 |
+
with pytest.raises(pytz.AmbiguousTimeError, match="Cannot infer dst time"):
|
| 456 |
+
dti.tz_localize(tzstr)
|
| 457 |
+
|
| 458 |
+
dti = date_range(start="3/13/2011 1:59", end="3/13/2011 2:00", freq="L")
|
| 459 |
+
with pytest.raises(pytz.NonExistentTimeError, match="2011-03-13 02:00:00"):
|
| 460 |
+
dti.tz_localize(tzstr)
|
| 461 |
+
|
| 462 |
+
@pytest.mark.parametrize(
|
| 463 |
+
"tz",
|
| 464 |
+
[
|
| 465 |
+
"US/Eastern",
|
| 466 |
+
"dateutil/US/Eastern",
|
| 467 |
+
pytz.timezone("US/Eastern"),
|
| 468 |
+
gettz("US/Eastern"),
|
| 469 |
+
],
|
| 470 |
+
)
|
| 471 |
+
def test_dti_tz_localize_utc_conversion(self, tz):
|
| 472 |
+
# Localizing to time zone should:
|
| 473 |
+
# 1) check for DST ambiguities
|
| 474 |
+
# 2) convert to UTC
|
| 475 |
+
|
| 476 |
+
rng = date_range("3/10/2012", "3/11/2012", freq="30T")
|
| 477 |
+
|
| 478 |
+
converted = rng.tz_localize(tz)
|
| 479 |
+
expected_naive = rng + pd.offsets.Hour(5)
|
| 480 |
+
tm.assert_numpy_array_equal(converted.asi8, expected_naive.asi8)
|
| 481 |
+
|
| 482 |
+
# DST ambiguity, this should fail
|
| 483 |
+
rng = date_range("3/11/2012", "3/12/2012", freq="30T")
|
| 484 |
+
# Is this really how it should fail??
|
| 485 |
+
with pytest.raises(pytz.NonExistentTimeError, match="2012-03-11 02:00:00"):
|
| 486 |
+
rng.tz_localize(tz)
|
| 487 |
+
|
| 488 |
+
def test_dti_tz_localize_roundtrip(self, tz_aware_fixture):
|
| 489 |
+
# note: this tz tests that a tz-naive index can be localized
|
| 490 |
+
# and de-localized successfully, when there are no DST transitions
|
| 491 |
+
# in the range.
|
| 492 |
+
idx = date_range(start="2014-06-01", end="2014-08-30", freq="15T")
|
| 493 |
+
tz = tz_aware_fixture
|
| 494 |
+
localized = idx.tz_localize(tz)
|
| 495 |
+
# can't localize a tz-aware object
|
| 496 |
+
with pytest.raises(
|
| 497 |
+
TypeError, match="Already tz-aware, use tz_convert to convert"
|
| 498 |
+
):
|
| 499 |
+
localized.tz_localize(tz)
|
| 500 |
+
reset = localized.tz_localize(None)
|
| 501 |
+
assert reset.tzinfo is None
|
| 502 |
+
expected = idx._with_freq(None)
|
| 503 |
+
tm.assert_index_equal(reset, expected)
|
| 504 |
+
|
| 505 |
+
def test_dti_tz_localize_naive(self):
|
| 506 |
+
rng = date_range("1/1/2011", periods=100, freq="H")
|
| 507 |
+
|
| 508 |
+
conv = rng.tz_localize("US/Pacific")
|
| 509 |
+
exp = date_range("1/1/2011", periods=100, freq="H", tz="US/Pacific")
|
| 510 |
+
|
| 511 |
+
tm.assert_index_equal(conv, exp._with_freq(None))
|
| 512 |
+
|
| 513 |
+
def test_dti_tz_localize_tzlocal(self):
|
| 514 |
+
# GH#13583
|
| 515 |
+
offset = dateutil.tz.tzlocal().utcoffset(datetime(2011, 1, 1))
|
| 516 |
+
offset = int(offset.total_seconds() * 1000000000)
|
| 517 |
+
|
| 518 |
+
dti = date_range(start="2001-01-01", end="2001-03-01")
|
| 519 |
+
dti2 = dti.tz_localize(dateutil.tz.tzlocal())
|
| 520 |
+
tm.assert_numpy_array_equal(dti2.asi8 + offset, dti.asi8)
|
| 521 |
+
|
| 522 |
+
dti = date_range(start="2001-01-01", end="2001-03-01", tz=dateutil.tz.tzlocal())
|
| 523 |
+
dti2 = dti.tz_localize(None)
|
| 524 |
+
tm.assert_numpy_array_equal(dti2.asi8 - offset, dti.asi8)
|
| 525 |
+
|
| 526 |
+
@pytest.mark.parametrize("tz", [pytz.timezone("US/Eastern"), gettz("US/Eastern")])
|
| 527 |
+
def test_dti_tz_localize_ambiguous_nat(self, tz):
|
| 528 |
+
times = [
|
| 529 |
+
"11/06/2011 00:00",
|
| 530 |
+
"11/06/2011 01:00",
|
| 531 |
+
"11/06/2011 01:00",
|
| 532 |
+
"11/06/2011 02:00",
|
| 533 |
+
"11/06/2011 03:00",
|
| 534 |
+
]
|
| 535 |
+
di = DatetimeIndex(times)
|
| 536 |
+
localized = di.tz_localize(tz, ambiguous="NaT")
|
| 537 |
+
|
| 538 |
+
times = [
|
| 539 |
+
"11/06/2011 00:00",
|
| 540 |
+
np.NaN,
|
| 541 |
+
np.NaN,
|
| 542 |
+
"11/06/2011 02:00",
|
| 543 |
+
"11/06/2011 03:00",
|
| 544 |
+
]
|
| 545 |
+
di_test = DatetimeIndex(times, tz="US/Eastern")
|
| 546 |
+
|
| 547 |
+
# left dtype is datetime64[ns, US/Eastern]
|
| 548 |
+
# right is datetime64[ns, tzfile('/usr/share/zoneinfo/US/Eastern')]
|
| 549 |
+
tm.assert_numpy_array_equal(di_test.values, localized.values)
|
| 550 |
+
|
| 551 |
+
@pytest.mark.parametrize("tz", [pytz.timezone("US/Eastern"), gettz("US/Eastern")])
|
| 552 |
+
def test_dti_tz_localize_ambiguous_flags(self, tz):
|
| 553 |
+
# November 6, 2011, fall back, repeat 2 AM hour
|
| 554 |
+
|
| 555 |
+
# Pass in flags to determine right dst transition
|
| 556 |
+
dr = date_range(
|
| 557 |
+
datetime(2011, 11, 6, 0), periods=5, freq=pd.offsets.Hour(), tz=tz
|
| 558 |
+
)
|
| 559 |
+
times = [
|
| 560 |
+
"11/06/2011 00:00",
|
| 561 |
+
"11/06/2011 01:00",
|
| 562 |
+
"11/06/2011 01:00",
|
| 563 |
+
"11/06/2011 02:00",
|
| 564 |
+
"11/06/2011 03:00",
|
| 565 |
+
]
|
| 566 |
+
|
| 567 |
+
# Test tz_localize
|
| 568 |
+
di = DatetimeIndex(times)
|
| 569 |
+
is_dst = [1, 1, 0, 0, 0]
|
| 570 |
+
localized = di.tz_localize(tz, ambiguous=is_dst)
|
| 571 |
+
expected = dr._with_freq(None)
|
| 572 |
+
tm.assert_index_equal(expected, localized)
|
| 573 |
+
tm.assert_index_equal(expected, DatetimeIndex(times, tz=tz, ambiguous=is_dst))
|
| 574 |
+
|
| 575 |
+
localized = di.tz_localize(tz, ambiguous=np.array(is_dst))
|
| 576 |
+
tm.assert_index_equal(dr, localized)
|
| 577 |
+
|
| 578 |
+
localized = di.tz_localize(tz, ambiguous=np.array(is_dst).astype("bool"))
|
| 579 |
+
tm.assert_index_equal(dr, localized)
|
| 580 |
+
|
| 581 |
+
# Test constructor
|
| 582 |
+
localized = DatetimeIndex(times, tz=tz, ambiguous=is_dst)
|
| 583 |
+
tm.assert_index_equal(dr, localized)
|
| 584 |
+
|
| 585 |
+
# Test duplicate times where inferring the dst fails
|
| 586 |
+
times += times
|
| 587 |
+
di = DatetimeIndex(times)
|
| 588 |
+
|
| 589 |
+
# When the sizes are incompatible, make sure error is raised
|
| 590 |
+
msg = "Length of ambiguous bool-array must be the same size as vals"
|
| 591 |
+
with pytest.raises(Exception, match=msg):
|
| 592 |
+
di.tz_localize(tz, ambiguous=is_dst)
|
| 593 |
+
|
| 594 |
+
# When sizes are compatible and there are repeats ('infer' won't work)
|
| 595 |
+
is_dst = np.hstack((is_dst, is_dst))
|
| 596 |
+
localized = di.tz_localize(tz, ambiguous=is_dst)
|
| 597 |
+
dr = dr.append(dr)
|
| 598 |
+
tm.assert_index_equal(dr, localized)
|
| 599 |
+
|
| 600 |
+
# When there is no dst transition, nothing special happens
|
| 601 |
+
dr = date_range(datetime(2011, 6, 1, 0), periods=10, freq=pd.offsets.Hour())
|
| 602 |
+
is_dst = np.array([1] * 10)
|
| 603 |
+
localized = dr.tz_localize(tz)
|
| 604 |
+
localized_is_dst = dr.tz_localize(tz, ambiguous=is_dst)
|
| 605 |
+
tm.assert_index_equal(localized, localized_is_dst)
|
| 606 |
+
|
| 607 |
+
# TODO: belongs outside tz_localize tests?
|
| 608 |
+
@pytest.mark.parametrize("tz", ["Europe/London", "dateutil/Europe/London"])
|
| 609 |
+
def test_dti_construction_ambiguous_endpoint(self, tz):
|
| 610 |
+
# construction with an ambiguous end-point
|
| 611 |
+
# GH#11626
|
| 612 |
+
|
| 613 |
+
with pytest.raises(pytz.AmbiguousTimeError, match="Cannot infer dst time"):
|
| 614 |
+
date_range(
|
| 615 |
+
"2013-10-26 23:00", "2013-10-27 01:00", tz="Europe/London", freq="H"
|
| 616 |
+
)
|
| 617 |
+
|
| 618 |
+
times = date_range(
|
| 619 |
+
"2013-10-26 23:00", "2013-10-27 01:00", freq="H", tz=tz, ambiguous="infer"
|
| 620 |
+
)
|
| 621 |
+
assert times[0] == Timestamp("2013-10-26 23:00", tz=tz)
|
| 622 |
+
assert times[-1] == Timestamp("2013-10-27 01:00:00+0000", tz=tz)
|
| 623 |
+
|
| 624 |
+
@pytest.mark.parametrize(
|
| 625 |
+
"tz, option, expected",
|
| 626 |
+
[
|
| 627 |
+
["US/Pacific", "shift_forward", "2019-03-10 03:00"],
|
| 628 |
+
["dateutil/US/Pacific", "shift_forward", "2019-03-10 03:00"],
|
| 629 |
+
["US/Pacific", "shift_backward", "2019-03-10 01:00"],
|
| 630 |
+
["dateutil/US/Pacific", "shift_backward", "2019-03-10 01:00"],
|
| 631 |
+
["US/Pacific", timedelta(hours=1), "2019-03-10 03:00"],
|
| 632 |
+
],
|
| 633 |
+
)
|
| 634 |
+
def test_dti_construction_nonexistent_endpoint(self, tz, option, expected):
|
| 635 |
+
# construction with an nonexistent end-point
|
| 636 |
+
|
| 637 |
+
with pytest.raises(pytz.NonExistentTimeError, match="2019-03-10 02:00:00"):
|
| 638 |
+
date_range(
|
| 639 |
+
"2019-03-10 00:00", "2019-03-10 02:00", tz="US/Pacific", freq="H"
|
| 640 |
+
)
|
| 641 |
+
|
| 642 |
+
times = date_range(
|
| 643 |
+
"2019-03-10 00:00", "2019-03-10 02:00", freq="H", tz=tz, nonexistent=option
|
| 644 |
+
)
|
| 645 |
+
assert times[-1] == Timestamp(expected, tz=tz)
|
| 646 |
+
|
| 647 |
+
def test_dti_tz_localize_bdate_range(self):
|
| 648 |
+
dr = bdate_range("1/1/2009", "1/1/2010")
|
| 649 |
+
dr_utc = bdate_range("1/1/2009", "1/1/2010", tz=pytz.utc)
|
| 650 |
+
localized = dr.tz_localize(pytz.utc)
|
| 651 |
+
tm.assert_index_equal(dr_utc, localized)
|
| 652 |
+
|
| 653 |
+
@pytest.mark.parametrize(
|
| 654 |
+
"start_ts, tz, end_ts, shift",
|
| 655 |
+
[
|
| 656 |
+
["2015-03-29 02:20:00", "Europe/Warsaw", "2015-03-29 03:00:00", "forward"],
|
| 657 |
+
[
|
| 658 |
+
"2015-03-29 02:20:00",
|
| 659 |
+
"Europe/Warsaw",
|
| 660 |
+
"2015-03-29 01:59:59.999999999",
|
| 661 |
+
"backward",
|
| 662 |
+
],
|
| 663 |
+
[
|
| 664 |
+
"2015-03-29 02:20:00",
|
| 665 |
+
"Europe/Warsaw",
|
| 666 |
+
"2015-03-29 03:20:00",
|
| 667 |
+
timedelta(hours=1),
|
| 668 |
+
],
|
| 669 |
+
[
|
| 670 |
+
"2015-03-29 02:20:00",
|
| 671 |
+
"Europe/Warsaw",
|
| 672 |
+
"2015-03-29 01:20:00",
|
| 673 |
+
timedelta(hours=-1),
|
| 674 |
+
],
|
| 675 |
+
["2018-03-11 02:33:00", "US/Pacific", "2018-03-11 03:00:00", "forward"],
|
| 676 |
+
[
|
| 677 |
+
"2018-03-11 02:33:00",
|
| 678 |
+
"US/Pacific",
|
| 679 |
+
"2018-03-11 01:59:59.999999999",
|
| 680 |
+
"backward",
|
| 681 |
+
],
|
| 682 |
+
[
|
| 683 |
+
"2018-03-11 02:33:00",
|
| 684 |
+
"US/Pacific",
|
| 685 |
+
"2018-03-11 03:33:00",
|
| 686 |
+
timedelta(hours=1),
|
| 687 |
+
],
|
| 688 |
+
[
|
| 689 |
+
"2018-03-11 02:33:00",
|
| 690 |
+
"US/Pacific",
|
| 691 |
+
"2018-03-11 01:33:00",
|
| 692 |
+
timedelta(hours=-1),
|
| 693 |
+
],
|
| 694 |
+
],
|
| 695 |
+
)
|
| 696 |
+
@pytest.mark.parametrize("tz_type", ["", "dateutil/"])
|
| 697 |
+
def test_dti_tz_localize_nonexistent_shift(
|
| 698 |
+
self, start_ts, tz, end_ts, shift, tz_type
|
| 699 |
+
):
|
| 700 |
+
# GH 8917
|
| 701 |
+
tz = tz_type + tz
|
| 702 |
+
if isinstance(shift, str):
|
| 703 |
+
shift = "shift_" + shift
|
| 704 |
+
dti = DatetimeIndex([Timestamp(start_ts)])
|
| 705 |
+
result = dti.tz_localize(tz, nonexistent=shift)
|
| 706 |
+
expected = DatetimeIndex([Timestamp(end_ts)]).tz_localize(tz)
|
| 707 |
+
tm.assert_index_equal(result, expected)
|
| 708 |
+
|
| 709 |
+
@pytest.mark.parametrize("offset", [-1, 1])
|
| 710 |
+
def test_dti_tz_localize_nonexistent_shift_invalid(self, offset, warsaw):
|
| 711 |
+
# GH 8917
|
| 712 |
+
tz = warsaw
|
| 713 |
+
dti = DatetimeIndex([Timestamp("2015-03-29 02:20:00")])
|
| 714 |
+
msg = "The provided timedelta will relocalize on a nonexistent time"
|
| 715 |
+
with pytest.raises(ValueError, match=msg):
|
| 716 |
+
dti.tz_localize(tz, nonexistent=timedelta(seconds=offset))
|
| 717 |
+
|
| 718 |
+
# -------------------------------------------------------------
|
| 719 |
+
# DatetimeIndex.normalize
|
| 720 |
+
|
| 721 |
+
def test_normalize_tz(self):
|
| 722 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz="US/Eastern")
|
| 723 |
+
|
| 724 |
+
result = rng.normalize() # does not preserve freq
|
| 725 |
+
expected = date_range("1/1/2000", periods=10, freq="D", tz="US/Eastern")
|
| 726 |
+
tm.assert_index_equal(result, expected._with_freq(None))
|
| 727 |
+
|
| 728 |
+
assert result.is_normalized
|
| 729 |
+
assert not rng.is_normalized
|
| 730 |
+
|
| 731 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz="UTC")
|
| 732 |
+
|
| 733 |
+
result = rng.normalize()
|
| 734 |
+
expected = date_range("1/1/2000", periods=10, freq="D", tz="UTC")
|
| 735 |
+
tm.assert_index_equal(result, expected)
|
| 736 |
+
|
| 737 |
+
assert result.is_normalized
|
| 738 |
+
assert not rng.is_normalized
|
| 739 |
+
|
| 740 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz=tzlocal())
|
| 741 |
+
result = rng.normalize() # does not preserve freq
|
| 742 |
+
expected = date_range("1/1/2000", periods=10, freq="D", tz=tzlocal())
|
| 743 |
+
tm.assert_index_equal(result, expected._with_freq(None))
|
| 744 |
+
|
| 745 |
+
assert result.is_normalized
|
| 746 |
+
assert not rng.is_normalized
|
| 747 |
+
|
| 748 |
+
@td.skip_if_windows
|
| 749 |
+
@pytest.mark.parametrize(
|
| 750 |
+
"timezone",
|
| 751 |
+
[
|
| 752 |
+
"US/Pacific",
|
| 753 |
+
"US/Eastern",
|
| 754 |
+
"UTC",
|
| 755 |
+
"Asia/Kolkata",
|
| 756 |
+
"Asia/Shanghai",
|
| 757 |
+
"Australia/Canberra",
|
| 758 |
+
],
|
| 759 |
+
)
|
| 760 |
+
def test_normalize_tz_local(self, timezone):
|
| 761 |
+
# GH#13459
|
| 762 |
+
with tm.set_timezone(timezone):
|
| 763 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz=tzlocal())
|
| 764 |
+
|
| 765 |
+
result = rng.normalize()
|
| 766 |
+
expected = date_range("1/1/2000", periods=10, freq="D", tz=tzlocal())
|
| 767 |
+
expected = expected._with_freq(None)
|
| 768 |
+
tm.assert_index_equal(result, expected)
|
| 769 |
+
|
| 770 |
+
assert result.is_normalized
|
| 771 |
+
assert not rng.is_normalized
|
| 772 |
+
|
| 773 |
+
# ------------------------------------------------------------
|
| 774 |
+
# DatetimeIndex.__new__
|
| 775 |
+
|
| 776 |
+
@pytest.mark.parametrize("prefix", ["", "dateutil/"])
|
| 777 |
+
def test_dti_constructor_static_tzinfo(self, prefix):
|
| 778 |
+
# it works!
|
| 779 |
+
index = DatetimeIndex([datetime(2012, 1, 1)], tz=prefix + "EST")
|
| 780 |
+
index.hour
|
| 781 |
+
index[0]
|
| 782 |
+
|
| 783 |
+
def test_dti_constructor_with_fixed_tz(self):
|
| 784 |
+
off = FixedOffset(420, "+07:00")
|
| 785 |
+
start = datetime(2012, 3, 11, 5, 0, 0, tzinfo=off)
|
| 786 |
+
end = datetime(2012, 6, 11, 5, 0, 0, tzinfo=off)
|
| 787 |
+
rng = date_range(start=start, end=end)
|
| 788 |
+
assert off == rng.tz
|
| 789 |
+
|
| 790 |
+
rng2 = date_range(start, periods=len(rng), tz=off)
|
| 791 |
+
tm.assert_index_equal(rng, rng2)
|
| 792 |
+
|
| 793 |
+
rng3 = date_range("3/11/2012 05:00:00+07:00", "6/11/2012 05:00:00+07:00")
|
| 794 |
+
assert (rng.values == rng3.values).all()
|
| 795 |
+
|
| 796 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 797 |
+
def test_dti_convert_datetime_list(self, tzstr):
|
| 798 |
+
dr = date_range("2012-06-02", periods=10, tz=tzstr, name="foo")
|
| 799 |
+
dr2 = DatetimeIndex(list(dr), name="foo", freq="D")
|
| 800 |
+
tm.assert_index_equal(dr, dr2)
|
| 801 |
+
|
| 802 |
+
def test_dti_construction_univalent(self):
|
| 803 |
+
rng = date_range("03/12/2012 00:00", periods=10, freq="W-FRI", tz="US/Eastern")
|
| 804 |
+
rng2 = DatetimeIndex(data=rng, tz="US/Eastern")
|
| 805 |
+
tm.assert_index_equal(rng, rng2)
|
| 806 |
+
|
| 807 |
+
@pytest.mark.parametrize("tz", [pytz.timezone("US/Eastern"), gettz("US/Eastern")])
|
| 808 |
+
def test_dti_from_tzaware_datetime(self, tz):
|
| 809 |
+
d = [datetime(2012, 8, 19, tzinfo=tz)]
|
| 810 |
+
|
| 811 |
+
index = DatetimeIndex(d)
|
| 812 |
+
assert timezones.tz_compare(index.tz, tz)
|
| 813 |
+
|
| 814 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 815 |
+
def test_dti_tz_constructors(self, tzstr):
|
| 816 |
+
"""Test different DatetimeIndex constructions with timezone
|
| 817 |
+
Follow-up of GH#4229
|
| 818 |
+
"""
|
| 819 |
+
arr = ["11/10/2005 08:00:00", "11/10/2005 09:00:00"]
|
| 820 |
+
|
| 821 |
+
idx1 = to_datetime(arr).tz_localize(tzstr)
|
| 822 |
+
idx2 = date_range(start="2005-11-10 08:00:00", freq="H", periods=2, tz=tzstr)
|
| 823 |
+
idx2 = idx2._with_freq(None) # the others all have freq=None
|
| 824 |
+
idx3 = DatetimeIndex(arr, tz=tzstr)
|
| 825 |
+
idx4 = DatetimeIndex(np.array(arr), tz=tzstr)
|
| 826 |
+
|
| 827 |
+
for other in [idx2, idx3, idx4]:
|
| 828 |
+
tm.assert_index_equal(idx1, other)
|
| 829 |
+
|
| 830 |
+
# -------------------------------------------------------------
|
| 831 |
+
# Unsorted
|
| 832 |
+
|
| 833 |
+
@pytest.mark.parametrize(
|
| 834 |
+
"dtype",
|
| 835 |
+
[None, "datetime64[ns, CET]", "datetime64[ns, EST]", "datetime64[ns, UTC]"],
|
| 836 |
+
)
|
| 837 |
+
def test_date_accessor(self, dtype):
|
| 838 |
+
# Regression test for GH#21230
|
| 839 |
+
expected = np.array([date(2018, 6, 4), pd.NaT])
|
| 840 |
+
|
| 841 |
+
index = DatetimeIndex(["2018-06-04 10:00:00", pd.NaT], dtype=dtype)
|
| 842 |
+
result = index.date
|
| 843 |
+
|
| 844 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 845 |
+
|
| 846 |
+
@pytest.mark.parametrize(
|
| 847 |
+
"dtype",
|
| 848 |
+
[None, "datetime64[ns, CET]", "datetime64[ns, EST]", "datetime64[ns, UTC]"],
|
| 849 |
+
)
|
| 850 |
+
def test_time_accessor(self, dtype):
|
| 851 |
+
# Regression test for GH#21267
|
| 852 |
+
expected = np.array([time(10, 20, 30), pd.NaT])
|
| 853 |
+
|
| 854 |
+
index = DatetimeIndex(["2018-06-04 10:20:30", pd.NaT], dtype=dtype)
|
| 855 |
+
result = index.time
|
| 856 |
+
|
| 857 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 858 |
+
|
| 859 |
+
def test_timetz_accessor(self, tz_naive_fixture):
|
| 860 |
+
# GH21358
|
| 861 |
+
tz = timezones.maybe_get_tz(tz_naive_fixture)
|
| 862 |
+
|
| 863 |
+
expected = np.array([time(10, 20, 30, tzinfo=tz), pd.NaT])
|
| 864 |
+
|
| 865 |
+
index = DatetimeIndex(["2018-06-04 10:20:30", pd.NaT], tz=tz)
|
| 866 |
+
result = index.timetz
|
| 867 |
+
|
| 868 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 869 |
+
|
| 870 |
+
def test_dti_drop_dont_lose_tz(self):
|
| 871 |
+
# GH#2621
|
| 872 |
+
ind = date_range("2012-12-01", periods=10, tz="utc")
|
| 873 |
+
ind = ind.drop(ind[-1])
|
| 874 |
+
|
| 875 |
+
assert ind.tz is not None
|
| 876 |
+
|
| 877 |
+
def test_dti_tz_conversion_freq(self, tz_naive_fixture):
|
| 878 |
+
# GH25241
|
| 879 |
+
t3 = DatetimeIndex(["2019-01-01 10:00"], freq="H")
|
| 880 |
+
assert t3.tz_localize(tz=tz_naive_fixture).freq == t3.freq
|
| 881 |
+
t4 = DatetimeIndex(["2019-01-02 12:00"], tz="UTC", freq="T")
|
| 882 |
+
assert t4.tz_convert(tz="UTC").freq == t4.freq
|
| 883 |
+
|
| 884 |
+
def test_drop_dst_boundary(self):
|
| 885 |
+
# see gh-18031
|
| 886 |
+
tz = "Europe/Brussels"
|
| 887 |
+
freq = "15min"
|
| 888 |
+
|
| 889 |
+
start = Timestamp("201710290100", tz=tz)
|
| 890 |
+
end = Timestamp("201710290300", tz=tz)
|
| 891 |
+
index = date_range(start=start, end=end, freq=freq)
|
| 892 |
+
|
| 893 |
+
expected = DatetimeIndex(
|
| 894 |
+
[
|
| 895 |
+
"201710290115",
|
| 896 |
+
"201710290130",
|
| 897 |
+
"201710290145",
|
| 898 |
+
"201710290200",
|
| 899 |
+
"201710290215",
|
| 900 |
+
"201710290230",
|
| 901 |
+
"201710290245",
|
| 902 |
+
"201710290200",
|
| 903 |
+
"201710290215",
|
| 904 |
+
"201710290230",
|
| 905 |
+
"201710290245",
|
| 906 |
+
"201710290300",
|
| 907 |
+
],
|
| 908 |
+
tz=tz,
|
| 909 |
+
freq=freq,
|
| 910 |
+
ambiguous=[
|
| 911 |
+
True,
|
| 912 |
+
True,
|
| 913 |
+
True,
|
| 914 |
+
True,
|
| 915 |
+
True,
|
| 916 |
+
True,
|
| 917 |
+
True,
|
| 918 |
+
False,
|
| 919 |
+
False,
|
| 920 |
+
False,
|
| 921 |
+
False,
|
| 922 |
+
False,
|
| 923 |
+
],
|
| 924 |
+
)
|
| 925 |
+
result = index.drop(index[0])
|
| 926 |
+
tm.assert_index_equal(result, expected)
|
| 927 |
+
|
| 928 |
+
def test_date_range_localize(self):
|
| 929 |
+
rng = date_range("3/11/2012 03:00", periods=15, freq="H", tz="US/Eastern")
|
| 930 |
+
rng2 = DatetimeIndex(["3/11/2012 03:00", "3/11/2012 04:00"], tz="US/Eastern")
|
| 931 |
+
rng3 = date_range("3/11/2012 03:00", periods=15, freq="H")
|
| 932 |
+
rng3 = rng3.tz_localize("US/Eastern")
|
| 933 |
+
|
| 934 |
+
tm.assert_index_equal(rng._with_freq(None), rng3)
|
| 935 |
+
|
| 936 |
+
# DST transition time
|
| 937 |
+
val = rng[0]
|
| 938 |
+
exp = Timestamp("3/11/2012 03:00", tz="US/Eastern")
|
| 939 |
+
|
| 940 |
+
assert val.hour == 3
|
| 941 |
+
assert exp.hour == 3
|
| 942 |
+
assert val == exp # same UTC value
|
| 943 |
+
tm.assert_index_equal(rng[:2], rng2)
|
| 944 |
+
|
| 945 |
+
# Right before the DST transition
|
| 946 |
+
rng = date_range("3/11/2012 00:00", periods=2, freq="H", tz="US/Eastern")
|
| 947 |
+
rng2 = DatetimeIndex(
|
| 948 |
+
["3/11/2012 00:00", "3/11/2012 01:00"], tz="US/Eastern", freq="H"
|
| 949 |
+
)
|
| 950 |
+
tm.assert_index_equal(rng, rng2)
|
| 951 |
+
exp = Timestamp("3/11/2012 00:00", tz="US/Eastern")
|
| 952 |
+
assert exp.hour == 0
|
| 953 |
+
assert rng[0] == exp
|
| 954 |
+
exp = Timestamp("3/11/2012 01:00", tz="US/Eastern")
|
| 955 |
+
assert exp.hour == 1
|
| 956 |
+
assert rng[1] == exp
|
| 957 |
+
|
| 958 |
+
rng = date_range("3/11/2012 00:00", periods=10, freq="H", tz="US/Eastern")
|
| 959 |
+
assert rng[2].hour == 3
|
| 960 |
+
|
| 961 |
+
def test_timestamp_equality_different_timezones(self):
|
| 962 |
+
utc_range = date_range("1/1/2000", periods=20, tz="UTC")
|
| 963 |
+
eastern_range = utc_range.tz_convert("US/Eastern")
|
| 964 |
+
berlin_range = utc_range.tz_convert("Europe/Berlin")
|
| 965 |
+
|
| 966 |
+
for a, b, c in zip(utc_range, eastern_range, berlin_range):
|
| 967 |
+
assert a == b
|
| 968 |
+
assert b == c
|
| 969 |
+
assert a == c
|
| 970 |
+
|
| 971 |
+
assert (utc_range == eastern_range).all()
|
| 972 |
+
assert (utc_range == berlin_range).all()
|
| 973 |
+
assert (berlin_range == eastern_range).all()
|
| 974 |
+
|
| 975 |
+
def test_dti_intersection(self):
|
| 976 |
+
rng = date_range("1/1/2011", periods=100, freq="H", tz="utc")
|
| 977 |
+
|
| 978 |
+
left = rng[10:90][::-1]
|
| 979 |
+
right = rng[20:80][::-1]
|
| 980 |
+
|
| 981 |
+
assert left.tz == rng.tz
|
| 982 |
+
result = left.intersection(right)
|
| 983 |
+
assert result.tz == left.tz
|
| 984 |
+
|
| 985 |
+
def test_dti_equals_with_tz(self):
|
| 986 |
+
left = date_range("1/1/2011", periods=100, freq="H", tz="utc")
|
| 987 |
+
right = date_range("1/1/2011", periods=100, freq="H", tz="US/Eastern")
|
| 988 |
+
|
| 989 |
+
assert not left.equals(right)
|
| 990 |
+
|
| 991 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 992 |
+
def test_dti_tz_nat(self, tzstr):
|
| 993 |
+
idx = DatetimeIndex([Timestamp("2013-1-1", tz=tzstr), pd.NaT])
|
| 994 |
+
|
| 995 |
+
assert isna(idx[1])
|
| 996 |
+
assert idx[0].tzinfo is not None
|
| 997 |
+
|
| 998 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 999 |
+
def test_dti_astype_asobject_tzinfos(self, tzstr):
|
| 1000 |
+
# GH#1345
|
| 1001 |
+
|
| 1002 |
+
# dates around a dst transition
|
| 1003 |
+
rng = date_range("2/13/2010", "5/6/2010", tz=tzstr)
|
| 1004 |
+
|
| 1005 |
+
objs = rng.astype(object)
|
| 1006 |
+
for i, x in enumerate(objs):
|
| 1007 |
+
exval = rng[i]
|
| 1008 |
+
assert x == exval
|
| 1009 |
+
assert x.tzinfo == exval.tzinfo
|
| 1010 |
+
|
| 1011 |
+
objs = rng.astype(object)
|
| 1012 |
+
for i, x in enumerate(objs):
|
| 1013 |
+
exval = rng[i]
|
| 1014 |
+
assert x == exval
|
| 1015 |
+
assert x.tzinfo == exval.tzinfo
|
| 1016 |
+
|
| 1017 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 1018 |
+
def test_dti_with_timezone_repr(self, tzstr):
|
| 1019 |
+
rng = date_range("4/13/2010", "5/6/2010")
|
| 1020 |
+
|
| 1021 |
+
rng_eastern = rng.tz_localize(tzstr)
|
| 1022 |
+
|
| 1023 |
+
rng_repr = repr(rng_eastern)
|
| 1024 |
+
assert "2010-04-13 00:00:00" in rng_repr
|
| 1025 |
+
|
| 1026 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 1027 |
+
def test_dti_take_dont_lose_meta(self, tzstr):
|
| 1028 |
+
rng = date_range("1/1/2000", periods=20, tz=tzstr)
|
| 1029 |
+
|
| 1030 |
+
result = rng.take(range(5))
|
| 1031 |
+
assert result.tz == rng.tz
|
| 1032 |
+
assert result.freq == rng.freq
|
| 1033 |
+
|
| 1034 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 1035 |
+
def test_utc_box_timestamp_and_localize(self, tzstr):
|
| 1036 |
+
tz = timezones.maybe_get_tz(tzstr)
|
| 1037 |
+
|
| 1038 |
+
rng = date_range("3/11/2012", "3/12/2012", freq="H", tz="utc")
|
| 1039 |
+
rng_eastern = rng.tz_convert(tzstr)
|
| 1040 |
+
|
| 1041 |
+
expected = rng[-1].astimezone(tz)
|
| 1042 |
+
|
| 1043 |
+
stamp = rng_eastern[-1]
|
| 1044 |
+
assert stamp == expected
|
| 1045 |
+
assert stamp.tzinfo == expected.tzinfo
|
| 1046 |
+
|
| 1047 |
+
# right tzinfo
|
| 1048 |
+
rng = date_range("3/13/2012", "3/14/2012", freq="H", tz="utc")
|
| 1049 |
+
rng_eastern = rng.tz_convert(tzstr)
|
| 1050 |
+
# test not valid for dateutil timezones.
|
| 1051 |
+
# assert 'EDT' in repr(rng_eastern[0].tzinfo)
|
| 1052 |
+
assert "EDT" in repr(rng_eastern[0].tzinfo) or "tzfile" in repr(
|
| 1053 |
+
rng_eastern[0].tzinfo
|
| 1054 |
+
)
|
| 1055 |
+
|
| 1056 |
+
def test_dti_to_pydatetime(self):
|
| 1057 |
+
dt = dateutil.parser.parse("2012-06-13T01:39:00Z")
|
| 1058 |
+
dt = dt.replace(tzinfo=tzlocal())
|
| 1059 |
+
|
| 1060 |
+
arr = np.array([dt], dtype=object)
|
| 1061 |
+
|
| 1062 |
+
result = to_datetime(arr, utc=True)
|
| 1063 |
+
assert result.tz is timezone.utc
|
| 1064 |
+
|
| 1065 |
+
rng = date_range("2012-11-03 03:00", "2012-11-05 03:00", tz=tzlocal())
|
| 1066 |
+
arr = rng.to_pydatetime()
|
| 1067 |
+
result = to_datetime(arr, utc=True)
|
| 1068 |
+
assert result.tz is timezone.utc
|
| 1069 |
+
|
| 1070 |
+
def test_dti_to_pydatetime_fizedtz(self):
|
| 1071 |
+
dates = np.array(
|
| 1072 |
+
[
|
| 1073 |
+
datetime(2000, 1, 1, tzinfo=fixed_off),
|
| 1074 |
+
datetime(2000, 1, 2, tzinfo=fixed_off),
|
| 1075 |
+
datetime(2000, 1, 3, tzinfo=fixed_off),
|
| 1076 |
+
]
|
| 1077 |
+
)
|
| 1078 |
+
dti = DatetimeIndex(dates)
|
| 1079 |
+
|
| 1080 |
+
result = dti.to_pydatetime()
|
| 1081 |
+
tm.assert_numpy_array_equal(dates, result)
|
| 1082 |
+
|
| 1083 |
+
result = dti._mpl_repr()
|
| 1084 |
+
tm.assert_numpy_array_equal(dates, result)
|
| 1085 |
+
|
| 1086 |
+
@pytest.mark.parametrize("tz", [pytz.timezone("US/Central"), gettz("US/Central")])
|
| 1087 |
+
def test_with_tz(self, tz):
|
| 1088 |
+
# just want it to work
|
| 1089 |
+
start = datetime(2011, 3, 12, tzinfo=pytz.utc)
|
| 1090 |
+
dr = bdate_range(start, periods=50, freq=pd.offsets.Hour())
|
| 1091 |
+
assert dr.tz is pytz.utc
|
| 1092 |
+
|
| 1093 |
+
# DateRange with naive datetimes
|
| 1094 |
+
dr = bdate_range("1/1/2005", "1/1/2009", tz=pytz.utc)
|
| 1095 |
+
dr = bdate_range("1/1/2005", "1/1/2009", tz=tz)
|
| 1096 |
+
|
| 1097 |
+
# normalized
|
| 1098 |
+
central = dr.tz_convert(tz)
|
| 1099 |
+
assert central.tz is tz
|
| 1100 |
+
naive = central[0].to_pydatetime().replace(tzinfo=None)
|
| 1101 |
+
comp = conversion.localize_pydatetime(naive, tz).tzinfo
|
| 1102 |
+
assert central[0].tz is comp
|
| 1103 |
+
|
| 1104 |
+
# compare vs a localized tz
|
| 1105 |
+
naive = dr[0].to_pydatetime().replace(tzinfo=None)
|
| 1106 |
+
comp = conversion.localize_pydatetime(naive, tz).tzinfo
|
| 1107 |
+
assert central[0].tz is comp
|
| 1108 |
+
|
| 1109 |
+
# datetimes with tzinfo set
|
| 1110 |
+
dr = bdate_range(
|
| 1111 |
+
datetime(2005, 1, 1, tzinfo=pytz.utc), datetime(2009, 1, 1, tzinfo=pytz.utc)
|
| 1112 |
+
)
|
| 1113 |
+
msg = "Start and end cannot both be tz-aware with different timezones"
|
| 1114 |
+
with pytest.raises(Exception, match=msg):
|
| 1115 |
+
bdate_range(datetime(2005, 1, 1, tzinfo=pytz.utc), "1/1/2009", tz=tz)
|
| 1116 |
+
|
| 1117 |
+
@pytest.mark.parametrize("prefix", ["", "dateutil/"])
|
| 1118 |
+
def test_field_access_localize(self, prefix):
|
| 1119 |
+
strdates = ["1/1/2012", "3/1/2012", "4/1/2012"]
|
| 1120 |
+
rng = DatetimeIndex(strdates, tz=prefix + "US/Eastern")
|
| 1121 |
+
assert (rng.hour == 0).all()
|
| 1122 |
+
|
| 1123 |
+
# a more unusual time zone, #1946
|
| 1124 |
+
dr = date_range(
|
| 1125 |
+
"2011-10-02 00:00", freq="h", periods=10, tz=prefix + "America/Atikokan"
|
| 1126 |
+
)
|
| 1127 |
+
|
| 1128 |
+
expected = Index(np.arange(10, dtype=np.int32))
|
| 1129 |
+
tm.assert_index_equal(dr.hour, expected)
|
| 1130 |
+
|
| 1131 |
+
@pytest.mark.parametrize("tz", [pytz.timezone("US/Eastern"), gettz("US/Eastern")])
|
| 1132 |
+
def test_dti_convert_tz_aware_datetime_datetime(self, tz):
|
| 1133 |
+
# GH#1581
|
| 1134 |
+
dates = [datetime(2000, 1, 1), datetime(2000, 1, 2), datetime(2000, 1, 3)]
|
| 1135 |
+
|
| 1136 |
+
dates_aware = [conversion.localize_pydatetime(x, tz) for x in dates]
|
| 1137 |
+
result = DatetimeIndex(dates_aware)
|
| 1138 |
+
assert timezones.tz_compare(result.tz, tz)
|
| 1139 |
+
|
| 1140 |
+
converted = to_datetime(dates_aware, utc=True)
|
| 1141 |
+
ex_vals = np.array([Timestamp(x).as_unit("ns")._value for x in dates_aware])
|
| 1142 |
+
tm.assert_numpy_array_equal(converted.asi8, ex_vals)
|
| 1143 |
+
assert converted.tz is timezone.utc
|
| 1144 |
+
|
| 1145 |
+
# Note: not difference, as there is no symmetry requirement there
|
| 1146 |
+
@pytest.mark.parametrize("setop", ["union", "intersection", "symmetric_difference"])
|
| 1147 |
+
def test_dti_setop_aware(self, setop):
|
| 1148 |
+
# non-overlapping
|
| 1149 |
+
# GH#39328 as of 2.0 we cast these to UTC instead of object
|
| 1150 |
+
rng = date_range("2012-11-15 00:00:00", periods=6, freq="H", tz="US/Central")
|
| 1151 |
+
|
| 1152 |
+
rng2 = date_range("2012-11-15 12:00:00", periods=6, freq="H", tz="US/Eastern")
|
| 1153 |
+
|
| 1154 |
+
result = getattr(rng, setop)(rng2)
|
| 1155 |
+
|
| 1156 |
+
left = rng.tz_convert("UTC")
|
| 1157 |
+
right = rng2.tz_convert("UTC")
|
| 1158 |
+
expected = getattr(left, setop)(right)
|
| 1159 |
+
tm.assert_index_equal(result, expected)
|
| 1160 |
+
assert result.tz == left.tz
|
| 1161 |
+
if len(result):
|
| 1162 |
+
assert result[0].tz is timezone.utc
|
| 1163 |
+
assert result[-1].tz is timezone.utc
|
| 1164 |
+
|
| 1165 |
+
def test_dti_union_mixed(self):
|
| 1166 |
+
# GH 21671
|
| 1167 |
+
rng = DatetimeIndex([Timestamp("2011-01-01"), pd.NaT])
|
| 1168 |
+
rng2 = DatetimeIndex(["2012-01-01", "2012-01-02"], tz="Asia/Tokyo")
|
| 1169 |
+
result = rng.union(rng2)
|
| 1170 |
+
expected = Index(
|
| 1171 |
+
[
|
| 1172 |
+
Timestamp("2011-01-01"),
|
| 1173 |
+
pd.NaT,
|
| 1174 |
+
Timestamp("2012-01-01", tz="Asia/Tokyo"),
|
| 1175 |
+
Timestamp("2012-01-02", tz="Asia/Tokyo"),
|
| 1176 |
+
],
|
| 1177 |
+
dtype=object,
|
| 1178 |
+
)
|
| 1179 |
+
tm.assert_index_equal(result, expected)
|
| 1180 |
+
|
| 1181 |
+
@pytest.mark.parametrize(
|
| 1182 |
+
"tz", [None, "UTC", "US/Central", dateutil.tz.tzoffset(None, -28800)]
|
| 1183 |
+
)
|
| 1184 |
+
def test_iteration_preserves_nanoseconds(self, tz):
|
| 1185 |
+
# GH 19603
|
| 1186 |
+
index = DatetimeIndex(
|
| 1187 |
+
["2018-02-08 15:00:00.168456358", "2018-02-08 15:00:00.168456359"], tz=tz
|
| 1188 |
+
)
|
| 1189 |
+
for i, ts in enumerate(index):
|
| 1190 |
+
assert ts == index[i] # pylint: disable=unnecessary-list-index-lookup
|
| 1191 |
+
|
| 1192 |
+
|
| 1193 |
+
def test_tz_localize_invalidates_freq():
|
| 1194 |
+
# we only preserve freq in unambiguous cases
|
| 1195 |
+
|
| 1196 |
+
# if localized to US/Eastern, this crosses a DST transition
|
| 1197 |
+
dti = date_range("2014-03-08 23:00", "2014-03-09 09:00", freq="H")
|
| 1198 |
+
assert dti.freq == "H"
|
| 1199 |
+
|
| 1200 |
+
result = dti.tz_localize(None) # no-op
|
| 1201 |
+
assert result.freq == "H"
|
| 1202 |
+
|
| 1203 |
+
result = dti.tz_localize("UTC") # unambiguous freq preservation
|
| 1204 |
+
assert result.freq == "H"
|
| 1205 |
+
|
| 1206 |
+
result = dti.tz_localize("US/Eastern", nonexistent="shift_forward")
|
| 1207 |
+
assert result.freq is None
|
| 1208 |
+
assert result.inferred_freq is None # i.e. we are not _too_ strict here
|
| 1209 |
+
|
| 1210 |
+
# Case where we _can_ keep freq because we're length==1
|
| 1211 |
+
dti2 = dti[:1]
|
| 1212 |
+
result = dti2.tz_localize("US/Eastern")
|
| 1213 |
+
assert result.freq == "H"
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/datetimes/test_unique.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
)
|
| 38 |
+
assert uniques.dtype == "M8[ns]" # sanity
|
| 39 |
+
tm.assert_index_equal(uniques, expected)
|
| 40 |
+
assert index.nunique() == 4
|
| 41 |
+
|
| 42 |
+
# GH#2563
|
| 43 |
+
assert isinstance(uniques, DatetimeIndex)
|
| 44 |
+
|
| 45 |
+
dups_local = index.tz_localize("US/Eastern")
|
| 46 |
+
dups_local.name = "foo"
|
| 47 |
+
result = dups_local.unique()
|
| 48 |
+
expected = DatetimeIndex(expected, name="foo")
|
| 49 |
+
expected = expected.tz_localize("US/Eastern")
|
| 50 |
+
assert result.tz is not None
|
| 51 |
+
assert result.name == "foo"
|
| 52 |
+
tm.assert_index_equal(result, expected)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def test_index_unique2():
|
| 56 |
+
# NaT, note this is excluded
|
| 57 |
+
arr = [1370745748 + t for t in range(20)] + [NaT._value]
|
| 58 |
+
idx = DatetimeIndex(arr * 3)
|
| 59 |
+
tm.assert_index_equal(idx.unique(), DatetimeIndex(arr))
|
| 60 |
+
assert idx.nunique() == 20
|
| 61 |
+
assert idx.nunique(dropna=False) == 21
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def test_index_unique3():
|
| 65 |
+
arr = [
|
| 66 |
+
Timestamp("2013-06-09 02:42:28") + timedelta(seconds=t) for t in range(20)
|
| 67 |
+
] + [NaT]
|
| 68 |
+
idx = DatetimeIndex(arr * 3)
|
| 69 |
+
tm.assert_index_equal(idx.unique(), DatetimeIndex(arr))
|
| 70 |
+
assert idx.nunique() == 20
|
| 71 |
+
assert idx.nunique(dropna=False) == 21
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def test_is_unique_monotonic(rand_series_with_duplicate_datetimeindex):
|
| 75 |
+
index = rand_series_with_duplicate_datetimeindex.index
|
| 76 |
+
assert not index.is_unique
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (183 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_constructors.cpython-310.pyc
ADDED
|
Binary file (18.5 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_formats.cpython-310.pyc
ADDED
|
Binary file (5.22 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_freq_attr.cpython-310.pyc
ADDED
|
Binary file (1.03 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_indexing.cpython-310.pyc
ADDED
|
Binary file (22.3 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_join.cpython-310.pyc
ADDED
|
Binary file (2.56 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_monotonic.cpython-310.pyc
ADDED
|
Binary file (1.02 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_partial_slicing.cpython-310.pyc
ADDED
|
Binary file (6.5 kB). View file
|
|
|