Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__init__.py +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__pycache__/test_astype.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__pycache__/test_indexing.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__pycache__/test_join.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__pycache__/test_numeric.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__pycache__/test_setops.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/test_astype.py +95 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/test_indexing.py +611 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/test_join.py +380 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/test_setops.py +168 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__init__.py +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_constructors.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_formats.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_freq_attr.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_indexing.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_join.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_monotonic.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_partial_slicing.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_period.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_period_range.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_pickle.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_resolution.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_scalar_compat.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_searchsorted.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_setops.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_tools.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__init__.py +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/__init__.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_asfreq.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_astype.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_factorize.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_fillna.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_insert.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_is_full.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_repeat.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_shift.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_to_timestamp.cpython-310.pyc +0 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_asfreq.py +189 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_astype.py +151 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_factorize.py +41 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_fillna.py +41 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_insert.py +18 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_is_full.py +23 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_repeat.py +26 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_shift.py +122 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_to_timestamp.py +142 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/test_constructors.py +691 -0
- venv/lib/python3.10/site-packages/pandas/tests/indexes/period/test_formats.py +350 -0
venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__init__.py
ADDED
File without changes
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (196 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__pycache__/test_astype.cpython-310.pyc
ADDED
Binary file (3.86 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__pycache__/test_indexing.cpython-310.pyc
ADDED
Binary file (22.5 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__pycache__/test_join.cpython-310.pyc
ADDED
Binary file (10 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__pycache__/test_numeric.cpython-310.pyc
ADDED
Binary file (16.9 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/__pycache__/test_setops.cpython-310.pyc
ADDED
Binary file (5.63 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/test_astype.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
Index,
|
6 |
+
to_datetime,
|
7 |
+
to_timedelta,
|
8 |
+
)
|
9 |
+
import pandas._testing as tm
|
10 |
+
|
11 |
+
|
12 |
+
class TestAstype:
|
13 |
+
def test_astype_float64_to_uint64(self):
|
14 |
+
# GH#45309 used to incorrectly return Index with int64 dtype
|
15 |
+
idx = Index([0.0, 5.0, 10.0, 15.0, 20.0], dtype=np.float64)
|
16 |
+
result = idx.astype("u8")
|
17 |
+
expected = Index([0, 5, 10, 15, 20], dtype=np.uint64)
|
18 |
+
tm.assert_index_equal(result, expected, exact=True)
|
19 |
+
|
20 |
+
idx_with_negatives = idx - 10
|
21 |
+
with pytest.raises(ValueError, match="losslessly"):
|
22 |
+
idx_with_negatives.astype(np.uint64)
|
23 |
+
|
24 |
+
def test_astype_float64_to_object(self):
|
25 |
+
float_index = Index([0.0, 2.5, 5.0, 7.5, 10.0], dtype=np.float64)
|
26 |
+
result = float_index.astype(object)
|
27 |
+
assert result.equals(float_index)
|
28 |
+
assert float_index.equals(result)
|
29 |
+
assert isinstance(result, Index) and result.dtype == object
|
30 |
+
|
31 |
+
def test_astype_float64_mixed_to_object(self):
|
32 |
+
# mixed int-float
|
33 |
+
idx = Index([1.5, 2, 3, 4, 5], dtype=np.float64)
|
34 |
+
idx.name = "foo"
|
35 |
+
result = idx.astype(object)
|
36 |
+
assert result.equals(idx)
|
37 |
+
assert idx.equals(result)
|
38 |
+
assert isinstance(result, Index) and result.dtype == object
|
39 |
+
|
40 |
+
@pytest.mark.parametrize("dtype", ["int16", "int32", "int64"])
|
41 |
+
def test_astype_float64_to_int_dtype(self, dtype):
|
42 |
+
# GH#12881
|
43 |
+
# a float astype int
|
44 |
+
idx = Index([0, 1, 2], dtype=np.float64)
|
45 |
+
result = idx.astype(dtype)
|
46 |
+
expected = Index([0, 1, 2], dtype=dtype)
|
47 |
+
tm.assert_index_equal(result, expected, exact=True)
|
48 |
+
|
49 |
+
idx = Index([0, 1.1, 2], dtype=np.float64)
|
50 |
+
result = idx.astype(dtype)
|
51 |
+
expected = Index([0, 1, 2], dtype=dtype)
|
52 |
+
tm.assert_index_equal(result, expected, exact=True)
|
53 |
+
|
54 |
+
@pytest.mark.parametrize("dtype", ["float32", "float64"])
|
55 |
+
def test_astype_float64_to_float_dtype(self, dtype):
|
56 |
+
# GH#12881
|
57 |
+
# a float astype int
|
58 |
+
idx = Index([0, 1, 2], dtype=np.float64)
|
59 |
+
result = idx.astype(dtype)
|
60 |
+
assert isinstance(result, Index) and result.dtype == dtype
|
61 |
+
|
62 |
+
@pytest.mark.parametrize("dtype", ["M8[ns]", "m8[ns]"])
|
63 |
+
def test_astype_float_to_datetimelike(self, dtype):
|
64 |
+
# GH#49660 pre-2.0 Index.astype from floating to M8/m8/Period raised,
|
65 |
+
# inconsistent with Series.astype
|
66 |
+
idx = Index([0, 1.1, 2], dtype=np.float64)
|
67 |
+
|
68 |
+
result = idx.astype(dtype)
|
69 |
+
if dtype[0] == "M":
|
70 |
+
expected = to_datetime(idx.values)
|
71 |
+
else:
|
72 |
+
expected = to_timedelta(idx.values)
|
73 |
+
tm.assert_index_equal(result, expected)
|
74 |
+
|
75 |
+
# check that we match Series behavior
|
76 |
+
result = idx.to_series().set_axis(range(3)).astype(dtype)
|
77 |
+
expected = expected.to_series().set_axis(range(3))
|
78 |
+
tm.assert_series_equal(result, expected)
|
79 |
+
|
80 |
+
@pytest.mark.parametrize("dtype", [int, "int16", "int32", "int64"])
|
81 |
+
@pytest.mark.parametrize("non_finite", [np.inf, np.nan])
|
82 |
+
def test_cannot_cast_inf_to_int(self, non_finite, dtype):
|
83 |
+
# GH#13149
|
84 |
+
idx = Index([1, 2, non_finite], dtype=np.float64)
|
85 |
+
|
86 |
+
msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
|
87 |
+
with pytest.raises(ValueError, match=msg):
|
88 |
+
idx.astype(dtype)
|
89 |
+
|
90 |
+
def test_astype_from_object(self):
|
91 |
+
index = Index([1.0, np.nan, 0.2], dtype="object")
|
92 |
+
result = index.astype(float)
|
93 |
+
expected = Index([1.0, np.nan, 0.2], dtype=np.float64)
|
94 |
+
assert result.dtype == expected.dtype
|
95 |
+
tm.assert_index_equal(result, expected)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/test_indexing.py
ADDED
@@ -0,0 +1,611 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas.errors import InvalidIndexError
|
5 |
+
|
6 |
+
from pandas import (
|
7 |
+
NA,
|
8 |
+
Index,
|
9 |
+
RangeIndex,
|
10 |
+
Series,
|
11 |
+
Timestamp,
|
12 |
+
)
|
13 |
+
import pandas._testing as tm
|
14 |
+
from pandas.core.arrays import (
|
15 |
+
ArrowExtensionArray,
|
16 |
+
FloatingArray,
|
17 |
+
)
|
18 |
+
|
19 |
+
|
20 |
+
@pytest.fixture
|
21 |
+
def index_large():
|
22 |
+
# large values used in Index[uint64] tests where no compat needed with Int64/Float64
|
23 |
+
large = [2**63, 2**63 + 10, 2**63 + 15, 2**63 + 20, 2**63 + 25]
|
24 |
+
return Index(large, dtype=np.uint64)
|
25 |
+
|
26 |
+
|
27 |
+
class TestGetLoc:
|
28 |
+
def test_get_loc(self):
|
29 |
+
index = Index([0, 1, 2])
|
30 |
+
assert index.get_loc(1) == 1
|
31 |
+
|
32 |
+
def test_get_loc_raises_bad_label(self):
|
33 |
+
index = Index([0, 1, 2])
|
34 |
+
with pytest.raises(InvalidIndexError, match=r"\[1, 2\]"):
|
35 |
+
index.get_loc([1, 2])
|
36 |
+
|
37 |
+
def test_get_loc_float64(self):
|
38 |
+
idx = Index([0.0, 1.0, 2.0], dtype=np.float64)
|
39 |
+
|
40 |
+
with pytest.raises(KeyError, match="^'foo'$"):
|
41 |
+
idx.get_loc("foo")
|
42 |
+
with pytest.raises(KeyError, match=r"^1\.5$"):
|
43 |
+
idx.get_loc(1.5)
|
44 |
+
with pytest.raises(KeyError, match="^True$"):
|
45 |
+
idx.get_loc(True)
|
46 |
+
with pytest.raises(KeyError, match="^False$"):
|
47 |
+
idx.get_loc(False)
|
48 |
+
|
49 |
+
def test_get_loc_na(self):
|
50 |
+
idx = Index([np.nan, 1, 2], dtype=np.float64)
|
51 |
+
assert idx.get_loc(1) == 1
|
52 |
+
assert idx.get_loc(np.nan) == 0
|
53 |
+
|
54 |
+
idx = Index([np.nan, 1, np.nan], dtype=np.float64)
|
55 |
+
assert idx.get_loc(1) == 1
|
56 |
+
|
57 |
+
# representable by slice [0:2:2]
|
58 |
+
msg = "'Cannot get left slice bound for non-unique label: nan'"
|
59 |
+
with pytest.raises(KeyError, match=msg):
|
60 |
+
idx.slice_locs(np.nan)
|
61 |
+
# not representable by slice
|
62 |
+
idx = Index([np.nan, 1, np.nan, np.nan], dtype=np.float64)
|
63 |
+
assert idx.get_loc(1) == 1
|
64 |
+
msg = "'Cannot get left slice bound for non-unique label: nan"
|
65 |
+
with pytest.raises(KeyError, match=msg):
|
66 |
+
idx.slice_locs(np.nan)
|
67 |
+
|
68 |
+
def test_get_loc_missing_nan(self):
|
69 |
+
# GH#8569
|
70 |
+
idx = Index([1, 2], dtype=np.float64)
|
71 |
+
assert idx.get_loc(1) == 0
|
72 |
+
with pytest.raises(KeyError, match=r"^3$"):
|
73 |
+
idx.get_loc(3)
|
74 |
+
with pytest.raises(KeyError, match="^nan$"):
|
75 |
+
idx.get_loc(np.nan)
|
76 |
+
with pytest.raises(InvalidIndexError, match=r"\[nan\]"):
|
77 |
+
# listlike/non-hashable raises TypeError
|
78 |
+
idx.get_loc([np.nan])
|
79 |
+
|
80 |
+
@pytest.mark.parametrize("vals", [[1], [1.0], [Timestamp("2019-12-31")], ["test"]])
|
81 |
+
def test_get_loc_float_index_nan_with_method(self, vals):
|
82 |
+
# GH#39382
|
83 |
+
idx = Index(vals)
|
84 |
+
with pytest.raises(KeyError, match="nan"):
|
85 |
+
idx.get_loc(np.nan)
|
86 |
+
|
87 |
+
@pytest.mark.parametrize("dtype", ["f8", "i8", "u8"])
|
88 |
+
def test_get_loc_numericindex_none_raises(self, dtype):
|
89 |
+
# case that goes through searchsorted and key is non-comparable to values
|
90 |
+
arr = np.arange(10**7, dtype=dtype)
|
91 |
+
idx = Index(arr)
|
92 |
+
with pytest.raises(KeyError, match="None"):
|
93 |
+
idx.get_loc(None)
|
94 |
+
|
95 |
+
def test_get_loc_overflows(self):
|
96 |
+
# unique but non-monotonic goes through IndexEngine.mapping.get_item
|
97 |
+
idx = Index([0, 2, 1])
|
98 |
+
|
99 |
+
val = np.iinfo(np.int64).max + 1
|
100 |
+
|
101 |
+
with pytest.raises(KeyError, match=str(val)):
|
102 |
+
idx.get_loc(val)
|
103 |
+
with pytest.raises(KeyError, match=str(val)):
|
104 |
+
idx._engine.get_loc(val)
|
105 |
+
|
106 |
+
|
107 |
+
class TestGetIndexer:
|
108 |
+
def test_get_indexer(self):
|
109 |
+
index1 = Index([1, 2, 3, 4, 5])
|
110 |
+
index2 = Index([2, 4, 6])
|
111 |
+
|
112 |
+
r1 = index1.get_indexer(index2)
|
113 |
+
e1 = np.array([1, 3, -1], dtype=np.intp)
|
114 |
+
tm.assert_almost_equal(r1, e1)
|
115 |
+
|
116 |
+
@pytest.mark.parametrize("reverse", [True, False])
|
117 |
+
@pytest.mark.parametrize(
|
118 |
+
"expected,method",
|
119 |
+
[
|
120 |
+
(np.array([-1, 0, 0, 1, 1], dtype=np.intp), "pad"),
|
121 |
+
(np.array([-1, 0, 0, 1, 1], dtype=np.intp), "ffill"),
|
122 |
+
(np.array([0, 0, 1, 1, 2], dtype=np.intp), "backfill"),
|
123 |
+
(np.array([0, 0, 1, 1, 2], dtype=np.intp), "bfill"),
|
124 |
+
],
|
125 |
+
)
|
126 |
+
def test_get_indexer_methods(self, reverse, expected, method):
|
127 |
+
index1 = Index([1, 2, 3, 4, 5])
|
128 |
+
index2 = Index([2, 4, 6])
|
129 |
+
|
130 |
+
if reverse:
|
131 |
+
index1 = index1[::-1]
|
132 |
+
expected = expected[::-1]
|
133 |
+
|
134 |
+
result = index2.get_indexer(index1, method=method)
|
135 |
+
tm.assert_almost_equal(result, expected)
|
136 |
+
|
137 |
+
def test_get_indexer_invalid(self):
|
138 |
+
# GH10411
|
139 |
+
index = Index(np.arange(10))
|
140 |
+
|
141 |
+
with pytest.raises(ValueError, match="tolerance argument"):
|
142 |
+
index.get_indexer([1, 0], tolerance=1)
|
143 |
+
|
144 |
+
with pytest.raises(ValueError, match="limit argument"):
|
145 |
+
index.get_indexer([1, 0], limit=1)
|
146 |
+
|
147 |
+
@pytest.mark.parametrize(
|
148 |
+
"method, tolerance, indexer, expected",
|
149 |
+
[
|
150 |
+
("pad", None, [0, 5, 9], [0, 5, 9]),
|
151 |
+
("backfill", None, [0, 5, 9], [0, 5, 9]),
|
152 |
+
("nearest", None, [0, 5, 9], [0, 5, 9]),
|
153 |
+
("pad", 0, [0, 5, 9], [0, 5, 9]),
|
154 |
+
("backfill", 0, [0, 5, 9], [0, 5, 9]),
|
155 |
+
("nearest", 0, [0, 5, 9], [0, 5, 9]),
|
156 |
+
("pad", None, [0.2, 1.8, 8.5], [0, 1, 8]),
|
157 |
+
("backfill", None, [0.2, 1.8, 8.5], [1, 2, 9]),
|
158 |
+
("nearest", None, [0.2, 1.8, 8.5], [0, 2, 9]),
|
159 |
+
("pad", 1, [0.2, 1.8, 8.5], [0, 1, 8]),
|
160 |
+
("backfill", 1, [0.2, 1.8, 8.5], [1, 2, 9]),
|
161 |
+
("nearest", 1, [0.2, 1.8, 8.5], [0, 2, 9]),
|
162 |
+
("pad", 0.2, [0.2, 1.8, 8.5], [0, -1, -1]),
|
163 |
+
("backfill", 0.2, [0.2, 1.8, 8.5], [-1, 2, -1]),
|
164 |
+
("nearest", 0.2, [0.2, 1.8, 8.5], [0, 2, -1]),
|
165 |
+
],
|
166 |
+
)
|
167 |
+
def test_get_indexer_nearest(self, method, tolerance, indexer, expected):
|
168 |
+
index = Index(np.arange(10))
|
169 |
+
|
170 |
+
actual = index.get_indexer(indexer, method=method, tolerance=tolerance)
|
171 |
+
tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp))
|
172 |
+
|
173 |
+
@pytest.mark.parametrize("listtype", [list, tuple, Series, np.array])
|
174 |
+
@pytest.mark.parametrize(
|
175 |
+
"tolerance, expected",
|
176 |
+
list(
|
177 |
+
zip(
|
178 |
+
[[0.3, 0.3, 0.1], [0.2, 0.1, 0.1], [0.1, 0.5, 0.5]],
|
179 |
+
[[0, 2, -1], [0, -1, -1], [-1, 2, 9]],
|
180 |
+
)
|
181 |
+
),
|
182 |
+
)
|
183 |
+
def test_get_indexer_nearest_listlike_tolerance(
|
184 |
+
self, tolerance, expected, listtype
|
185 |
+
):
|
186 |
+
index = Index(np.arange(10))
|
187 |
+
|
188 |
+
actual = index.get_indexer(
|
189 |
+
[0.2, 1.8, 8.5], method="nearest", tolerance=listtype(tolerance)
|
190 |
+
)
|
191 |
+
tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp))
|
192 |
+
|
193 |
+
def test_get_indexer_nearest_error(self):
|
194 |
+
index = Index(np.arange(10))
|
195 |
+
with pytest.raises(ValueError, match="limit argument"):
|
196 |
+
index.get_indexer([1, 0], method="nearest", limit=1)
|
197 |
+
|
198 |
+
with pytest.raises(ValueError, match="tolerance size must match"):
|
199 |
+
index.get_indexer([1, 0], method="nearest", tolerance=[1, 2, 3])
|
200 |
+
|
201 |
+
@pytest.mark.parametrize(
|
202 |
+
"method,expected",
|
203 |
+
[("pad", [8, 7, 0]), ("backfill", [9, 8, 1]), ("nearest", [9, 7, 0])],
|
204 |
+
)
|
205 |
+
def test_get_indexer_nearest_decreasing(self, method, expected):
|
206 |
+
index = Index(np.arange(10))[::-1]
|
207 |
+
|
208 |
+
actual = index.get_indexer([0, 5, 9], method=method)
|
209 |
+
tm.assert_numpy_array_equal(actual, np.array([9, 4, 0], dtype=np.intp))
|
210 |
+
|
211 |
+
actual = index.get_indexer([0.2, 1.8, 8.5], method=method)
|
212 |
+
tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp))
|
213 |
+
|
214 |
+
@pytest.mark.parametrize("idx_dtype", ["int64", "float64", "uint64", "range"])
|
215 |
+
@pytest.mark.parametrize("method", ["get_indexer", "get_indexer_non_unique"])
|
216 |
+
def test_get_indexer_numeric_index_boolean_target(self, method, idx_dtype):
|
217 |
+
# GH 16877
|
218 |
+
|
219 |
+
if idx_dtype == "range":
|
220 |
+
numeric_index = RangeIndex(4)
|
221 |
+
else:
|
222 |
+
numeric_index = Index(np.arange(4, dtype=idx_dtype))
|
223 |
+
|
224 |
+
other = Index([True, False, True])
|
225 |
+
|
226 |
+
result = getattr(numeric_index, method)(other)
|
227 |
+
expected = np.array([-1, -1, -1], dtype=np.intp)
|
228 |
+
if method == "get_indexer":
|
229 |
+
tm.assert_numpy_array_equal(result, expected)
|
230 |
+
else:
|
231 |
+
missing = np.arange(3, dtype=np.intp)
|
232 |
+
tm.assert_numpy_array_equal(result[0], expected)
|
233 |
+
tm.assert_numpy_array_equal(result[1], missing)
|
234 |
+
|
235 |
+
@pytest.mark.parametrize("method", ["pad", "backfill", "nearest"])
|
236 |
+
def test_get_indexer_with_method_numeric_vs_bool(self, method):
|
237 |
+
left = Index([1, 2, 3])
|
238 |
+
right = Index([True, False])
|
239 |
+
|
240 |
+
with pytest.raises(TypeError, match="Cannot compare"):
|
241 |
+
left.get_indexer(right, method=method)
|
242 |
+
|
243 |
+
with pytest.raises(TypeError, match="Cannot compare"):
|
244 |
+
right.get_indexer(left, method=method)
|
245 |
+
|
246 |
+
def test_get_indexer_numeric_vs_bool(self):
|
247 |
+
left = Index([1, 2, 3])
|
248 |
+
right = Index([True, False])
|
249 |
+
|
250 |
+
res = left.get_indexer(right)
|
251 |
+
expected = -1 * np.ones(len(right), dtype=np.intp)
|
252 |
+
tm.assert_numpy_array_equal(res, expected)
|
253 |
+
|
254 |
+
res = right.get_indexer(left)
|
255 |
+
expected = -1 * np.ones(len(left), dtype=np.intp)
|
256 |
+
tm.assert_numpy_array_equal(res, expected)
|
257 |
+
|
258 |
+
res = left.get_indexer_non_unique(right)[0]
|
259 |
+
expected = -1 * np.ones(len(right), dtype=np.intp)
|
260 |
+
tm.assert_numpy_array_equal(res, expected)
|
261 |
+
|
262 |
+
res = right.get_indexer_non_unique(left)[0]
|
263 |
+
expected = -1 * np.ones(len(left), dtype=np.intp)
|
264 |
+
tm.assert_numpy_array_equal(res, expected)
|
265 |
+
|
266 |
+
def test_get_indexer_float64(self):
|
267 |
+
idx = Index([0.0, 1.0, 2.0], dtype=np.float64)
|
268 |
+
tm.assert_numpy_array_equal(
|
269 |
+
idx.get_indexer(idx), np.array([0, 1, 2], dtype=np.intp)
|
270 |
+
)
|
271 |
+
|
272 |
+
target = [-0.1, 0.5, 1.1]
|
273 |
+
tm.assert_numpy_array_equal(
|
274 |
+
idx.get_indexer(target, "pad"), np.array([-1, 0, 1], dtype=np.intp)
|
275 |
+
)
|
276 |
+
tm.assert_numpy_array_equal(
|
277 |
+
idx.get_indexer(target, "backfill"), np.array([0, 1, 2], dtype=np.intp)
|
278 |
+
)
|
279 |
+
tm.assert_numpy_array_equal(
|
280 |
+
idx.get_indexer(target, "nearest"), np.array([0, 1, 1], dtype=np.intp)
|
281 |
+
)
|
282 |
+
|
283 |
+
def test_get_indexer_nan(self):
|
284 |
+
# GH#7820
|
285 |
+
result = Index([1, 2, np.nan], dtype=np.float64).get_indexer([np.nan])
|
286 |
+
expected = np.array([2], dtype=np.intp)
|
287 |
+
tm.assert_numpy_array_equal(result, expected)
|
288 |
+
|
289 |
+
def test_get_indexer_int64(self):
|
290 |
+
index = Index(range(0, 20, 2), dtype=np.int64)
|
291 |
+
target = Index(np.arange(10), dtype=np.int64)
|
292 |
+
indexer = index.get_indexer(target)
|
293 |
+
expected = np.array([0, -1, 1, -1, 2, -1, 3, -1, 4, -1], dtype=np.intp)
|
294 |
+
tm.assert_numpy_array_equal(indexer, expected)
|
295 |
+
|
296 |
+
target = Index(np.arange(10), dtype=np.int64)
|
297 |
+
indexer = index.get_indexer(target, method="pad")
|
298 |
+
expected = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4], dtype=np.intp)
|
299 |
+
tm.assert_numpy_array_equal(indexer, expected)
|
300 |
+
|
301 |
+
target = Index(np.arange(10), dtype=np.int64)
|
302 |
+
indexer = index.get_indexer(target, method="backfill")
|
303 |
+
expected = np.array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5], dtype=np.intp)
|
304 |
+
tm.assert_numpy_array_equal(indexer, expected)
|
305 |
+
|
306 |
+
def test_get_indexer_uint64(self, index_large):
|
307 |
+
target = Index(np.arange(10).astype("uint64") * 5 + 2**63)
|
308 |
+
indexer = index_large.get_indexer(target)
|
309 |
+
expected = np.array([0, -1, 1, 2, 3, 4, -1, -1, -1, -1], dtype=np.intp)
|
310 |
+
tm.assert_numpy_array_equal(indexer, expected)
|
311 |
+
|
312 |
+
target = Index(np.arange(10).astype("uint64") * 5 + 2**63)
|
313 |
+
indexer = index_large.get_indexer(target, method="pad")
|
314 |
+
expected = np.array([0, 0, 1, 2, 3, 4, 4, 4, 4, 4], dtype=np.intp)
|
315 |
+
tm.assert_numpy_array_equal(indexer, expected)
|
316 |
+
|
317 |
+
target = Index(np.arange(10).astype("uint64") * 5 + 2**63)
|
318 |
+
indexer = index_large.get_indexer(target, method="backfill")
|
319 |
+
expected = np.array([0, 1, 1, 2, 3, 4, -1, -1, -1, -1], dtype=np.intp)
|
320 |
+
tm.assert_numpy_array_equal(indexer, expected)
|
321 |
+
|
322 |
+
@pytest.mark.parametrize("val, val2", [(4, 5), (4, 4), (4, NA), (NA, NA)])
|
323 |
+
def test_get_loc_masked(self, val, val2, any_numeric_ea_and_arrow_dtype):
|
324 |
+
# GH#39133
|
325 |
+
idx = Index([1, 2, 3, val, val2], dtype=any_numeric_ea_and_arrow_dtype)
|
326 |
+
result = idx.get_loc(2)
|
327 |
+
assert result == 1
|
328 |
+
|
329 |
+
with pytest.raises(KeyError, match="9"):
|
330 |
+
idx.get_loc(9)
|
331 |
+
|
332 |
+
def test_get_loc_masked_na(self, any_numeric_ea_and_arrow_dtype):
|
333 |
+
# GH#39133
|
334 |
+
idx = Index([1, 2, NA], dtype=any_numeric_ea_and_arrow_dtype)
|
335 |
+
result = idx.get_loc(NA)
|
336 |
+
assert result == 2
|
337 |
+
|
338 |
+
idx = Index([1, 2, NA, NA], dtype=any_numeric_ea_and_arrow_dtype)
|
339 |
+
result = idx.get_loc(NA)
|
340 |
+
tm.assert_numpy_array_equal(result, np.array([False, False, True, True]))
|
341 |
+
|
342 |
+
idx = Index([1, 2, 3], dtype=any_numeric_ea_and_arrow_dtype)
|
343 |
+
with pytest.raises(KeyError, match="NA"):
|
344 |
+
idx.get_loc(NA)
|
345 |
+
|
346 |
+
def test_get_loc_masked_na_and_nan(self):
|
347 |
+
# GH#39133
|
348 |
+
idx = Index(
|
349 |
+
FloatingArray(
|
350 |
+
np.array([1, 2, 1, np.nan]), mask=np.array([False, False, True, False])
|
351 |
+
)
|
352 |
+
)
|
353 |
+
result = idx.get_loc(NA)
|
354 |
+
assert result == 2
|
355 |
+
result = idx.get_loc(np.nan)
|
356 |
+
assert result == 3
|
357 |
+
|
358 |
+
idx = Index(
|
359 |
+
FloatingArray(np.array([1, 2, 1.0]), mask=np.array([False, False, True]))
|
360 |
+
)
|
361 |
+
result = idx.get_loc(NA)
|
362 |
+
assert result == 2
|
363 |
+
with pytest.raises(KeyError, match="nan"):
|
364 |
+
idx.get_loc(np.nan)
|
365 |
+
|
366 |
+
idx = Index(
|
367 |
+
FloatingArray(
|
368 |
+
np.array([1, 2, np.nan]), mask=np.array([False, False, False])
|
369 |
+
)
|
370 |
+
)
|
371 |
+
result = idx.get_loc(np.nan)
|
372 |
+
assert result == 2
|
373 |
+
with pytest.raises(KeyError, match="NA"):
|
374 |
+
idx.get_loc(NA)
|
375 |
+
|
376 |
+
@pytest.mark.parametrize("val", [4, 2])
|
377 |
+
def test_get_indexer_masked_na(self, any_numeric_ea_and_arrow_dtype, val):
|
378 |
+
# GH#39133
|
379 |
+
idx = Index([1, 2, NA, 3, val], dtype=any_numeric_ea_and_arrow_dtype)
|
380 |
+
result = idx.get_indexer_for([1, NA, 5])
|
381 |
+
expected = np.array([0, 2, -1])
|
382 |
+
tm.assert_numpy_array_equal(result, expected, check_dtype=False)
|
383 |
+
|
384 |
+
@pytest.mark.parametrize("dtype", ["boolean", "bool[pyarrow]"])
|
385 |
+
def test_get_indexer_masked_na_boolean(self, dtype):
|
386 |
+
# GH#39133
|
387 |
+
if dtype == "bool[pyarrow]":
|
388 |
+
pytest.importorskip("pyarrow")
|
389 |
+
idx = Index([True, False, NA], dtype=dtype)
|
390 |
+
result = idx.get_loc(False)
|
391 |
+
assert result == 1
|
392 |
+
result = idx.get_loc(NA)
|
393 |
+
assert result == 2
|
394 |
+
|
395 |
+
def test_get_indexer_arrow_dictionary_target(self):
|
396 |
+
pa = pytest.importorskip("pyarrow")
|
397 |
+
target = Index(
|
398 |
+
ArrowExtensionArray(
|
399 |
+
pa.array([1, 2], type=pa.dictionary(pa.int8(), pa.int8()))
|
400 |
+
)
|
401 |
+
)
|
402 |
+
idx = Index([1])
|
403 |
+
|
404 |
+
result = idx.get_indexer(target)
|
405 |
+
expected = np.array([0, -1], dtype=np.int64)
|
406 |
+
tm.assert_numpy_array_equal(result, expected)
|
407 |
+
|
408 |
+
result_1, result_2 = idx.get_indexer_non_unique(target)
|
409 |
+
expected_1, expected_2 = np.array([0, -1], dtype=np.int64), np.array(
|
410 |
+
[1], dtype=np.int64
|
411 |
+
)
|
412 |
+
tm.assert_numpy_array_equal(result_1, expected_1)
|
413 |
+
tm.assert_numpy_array_equal(result_2, expected_2)
|
414 |
+
|
415 |
+
|
416 |
+
class TestWhere:
|
417 |
+
@pytest.mark.parametrize(
|
418 |
+
"index",
|
419 |
+
[
|
420 |
+
Index(np.arange(5, dtype="float64")),
|
421 |
+
Index(range(0, 20, 2), dtype=np.int64),
|
422 |
+
Index(np.arange(5, dtype="uint64")),
|
423 |
+
],
|
424 |
+
)
|
425 |
+
def test_where(self, listlike_box, index):
|
426 |
+
cond = [True] * len(index)
|
427 |
+
expected = index
|
428 |
+
result = index.where(listlike_box(cond))
|
429 |
+
|
430 |
+
cond = [False] + [True] * (len(index) - 1)
|
431 |
+
expected = Index([index._na_value] + index[1:].tolist(), dtype=np.float64)
|
432 |
+
result = index.where(listlike_box(cond))
|
433 |
+
tm.assert_index_equal(result, expected)
|
434 |
+
|
435 |
+
def test_where_uint64(self):
|
436 |
+
idx = Index([0, 6, 2], dtype=np.uint64)
|
437 |
+
mask = np.array([False, True, False])
|
438 |
+
other = np.array([1], dtype=np.int64)
|
439 |
+
|
440 |
+
expected = Index([1, 6, 1], dtype=np.uint64)
|
441 |
+
|
442 |
+
result = idx.where(mask, other)
|
443 |
+
tm.assert_index_equal(result, expected)
|
444 |
+
|
445 |
+
result = idx.putmask(~mask, other)
|
446 |
+
tm.assert_index_equal(result, expected)
|
447 |
+
|
448 |
+
def test_where_infers_type_instead_of_trying_to_convert_string_to_float(self):
|
449 |
+
# GH 32413
|
450 |
+
index = Index([1, np.nan])
|
451 |
+
cond = index.notna()
|
452 |
+
other = Index(["a", "b"], dtype="string")
|
453 |
+
|
454 |
+
expected = Index([1.0, "b"])
|
455 |
+
result = index.where(cond, other)
|
456 |
+
|
457 |
+
tm.assert_index_equal(result, expected)
|
458 |
+
|
459 |
+
|
460 |
+
class TestTake:
|
461 |
+
@pytest.mark.parametrize("idx_dtype", [np.float64, np.int64, np.uint64])
|
462 |
+
def test_take_preserve_name(self, idx_dtype):
|
463 |
+
index = Index([1, 2, 3, 4], dtype=idx_dtype, name="foo")
|
464 |
+
taken = index.take([3, 0, 1])
|
465 |
+
assert index.name == taken.name
|
466 |
+
|
467 |
+
def test_take_fill_value_float64(self):
|
468 |
+
# GH 12631
|
469 |
+
idx = Index([1.0, 2.0, 3.0], name="xxx", dtype=np.float64)
|
470 |
+
result = idx.take(np.array([1, 0, -1]))
|
471 |
+
expected = Index([2.0, 1.0, 3.0], dtype=np.float64, name="xxx")
|
472 |
+
tm.assert_index_equal(result, expected)
|
473 |
+
|
474 |
+
# fill_value
|
475 |
+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
476 |
+
expected = Index([2.0, 1.0, np.nan], dtype=np.float64, name="xxx")
|
477 |
+
tm.assert_index_equal(result, expected)
|
478 |
+
|
479 |
+
# allow_fill=False
|
480 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
481 |
+
expected = Index([2.0, 1.0, 3.0], dtype=np.float64, name="xxx")
|
482 |
+
tm.assert_index_equal(result, expected)
|
483 |
+
|
484 |
+
msg = (
|
485 |
+
"When allow_fill=True and fill_value is not None, "
|
486 |
+
"all indices must be >= -1"
|
487 |
+
)
|
488 |
+
with pytest.raises(ValueError, match=msg):
|
489 |
+
idx.take(np.array([1, 0, -2]), fill_value=True)
|
490 |
+
with pytest.raises(ValueError, match=msg):
|
491 |
+
idx.take(np.array([1, 0, -5]), fill_value=True)
|
492 |
+
|
493 |
+
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
|
494 |
+
with pytest.raises(IndexError, match=msg):
|
495 |
+
idx.take(np.array([1, -5]))
|
496 |
+
|
497 |
+
@pytest.mark.parametrize("dtype", [np.int64, np.uint64])
|
498 |
+
def test_take_fill_value_ints(self, dtype):
|
499 |
+
# see gh-12631
|
500 |
+
idx = Index([1, 2, 3], dtype=dtype, name="xxx")
|
501 |
+
result = idx.take(np.array([1, 0, -1]))
|
502 |
+
expected = Index([2, 1, 3], dtype=dtype, name="xxx")
|
503 |
+
tm.assert_index_equal(result, expected)
|
504 |
+
|
505 |
+
name = type(idx).__name__
|
506 |
+
msg = f"Unable to fill values because {name} cannot contain NA"
|
507 |
+
|
508 |
+
# fill_value=True
|
509 |
+
with pytest.raises(ValueError, match=msg):
|
510 |
+
idx.take(np.array([1, 0, -1]), fill_value=True)
|
511 |
+
|
512 |
+
# allow_fill=False
|
513 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
514 |
+
expected = Index([2, 1, 3], dtype=dtype, name="xxx")
|
515 |
+
tm.assert_index_equal(result, expected)
|
516 |
+
|
517 |
+
with pytest.raises(ValueError, match=msg):
|
518 |
+
idx.take(np.array([1, 0, -2]), fill_value=True)
|
519 |
+
with pytest.raises(ValueError, match=msg):
|
520 |
+
idx.take(np.array([1, 0, -5]), fill_value=True)
|
521 |
+
|
522 |
+
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
|
523 |
+
with pytest.raises(IndexError, match=msg):
|
524 |
+
idx.take(np.array([1, -5]))
|
525 |
+
|
526 |
+
|
527 |
+
class TestContains:
|
528 |
+
@pytest.mark.parametrize("dtype", [np.float64, np.int64, np.uint64])
|
529 |
+
def test_contains_none(self, dtype):
|
530 |
+
# GH#35788 should return False, not raise TypeError
|
531 |
+
index = Index([0, 1, 2, 3, 4], dtype=dtype)
|
532 |
+
assert None not in index
|
533 |
+
|
534 |
+
def test_contains_float64_nans(self):
|
535 |
+
index = Index([1.0, 2.0, np.nan], dtype=np.float64)
|
536 |
+
assert np.nan in index
|
537 |
+
|
538 |
+
def test_contains_float64_not_nans(self):
|
539 |
+
index = Index([1.0, 2.0, np.nan], dtype=np.float64)
|
540 |
+
assert 1.0 in index
|
541 |
+
|
542 |
+
|
543 |
+
class TestSliceLocs:
|
544 |
+
@pytest.mark.parametrize("dtype", [int, float])
|
545 |
+
def test_slice_locs(self, dtype):
|
546 |
+
index = Index(np.array([0, 1, 2, 5, 6, 7, 9, 10], dtype=dtype))
|
547 |
+
n = len(index)
|
548 |
+
|
549 |
+
assert index.slice_locs(start=2) == (2, n)
|
550 |
+
assert index.slice_locs(start=3) == (3, n)
|
551 |
+
assert index.slice_locs(3, 8) == (3, 6)
|
552 |
+
assert index.slice_locs(5, 10) == (3, n)
|
553 |
+
assert index.slice_locs(end=8) == (0, 6)
|
554 |
+
assert index.slice_locs(end=9) == (0, 7)
|
555 |
+
|
556 |
+
# reversed
|
557 |
+
index2 = index[::-1]
|
558 |
+
assert index2.slice_locs(8, 2) == (2, 6)
|
559 |
+
assert index2.slice_locs(7, 3) == (2, 5)
|
560 |
+
|
561 |
+
@pytest.mark.parametrize("dtype", [int, float])
|
562 |
+
def test_slice_locs_float_locs(self, dtype):
|
563 |
+
index = Index(np.array([0, 1, 2, 5, 6, 7, 9, 10], dtype=dtype))
|
564 |
+
n = len(index)
|
565 |
+
assert index.slice_locs(5.0, 10.0) == (3, n)
|
566 |
+
assert index.slice_locs(4.5, 10.5) == (3, 8)
|
567 |
+
|
568 |
+
index2 = index[::-1]
|
569 |
+
assert index2.slice_locs(8.5, 1.5) == (2, 6)
|
570 |
+
assert index2.slice_locs(10.5, -1) == (0, n)
|
571 |
+
|
572 |
+
@pytest.mark.parametrize("dtype", [int, float])
|
573 |
+
def test_slice_locs_dup_numeric(self, dtype):
|
574 |
+
index = Index(np.array([10, 12, 12, 14], dtype=dtype))
|
575 |
+
assert index.slice_locs(12, 12) == (1, 3)
|
576 |
+
assert index.slice_locs(11, 13) == (1, 3)
|
577 |
+
|
578 |
+
index2 = index[::-1]
|
579 |
+
assert index2.slice_locs(12, 12) == (1, 3)
|
580 |
+
assert index2.slice_locs(13, 11) == (1, 3)
|
581 |
+
|
582 |
+
def test_slice_locs_na(self):
|
583 |
+
index = Index([np.nan, 1, 2])
|
584 |
+
assert index.slice_locs(1) == (1, 3)
|
585 |
+
assert index.slice_locs(np.nan) == (0, 3)
|
586 |
+
|
587 |
+
index = Index([0, np.nan, np.nan, 1, 2])
|
588 |
+
assert index.slice_locs(np.nan) == (1, 5)
|
589 |
+
|
590 |
+
def test_slice_locs_na_raises(self):
|
591 |
+
index = Index([np.nan, 1, 2])
|
592 |
+
with pytest.raises(KeyError, match=""):
|
593 |
+
index.slice_locs(start=1.5)
|
594 |
+
|
595 |
+
with pytest.raises(KeyError, match=""):
|
596 |
+
index.slice_locs(end=1.5)
|
597 |
+
|
598 |
+
|
599 |
+
class TestGetSliceBounds:
|
600 |
+
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
|
601 |
+
def test_get_slice_bounds_within(self, side, expected):
|
602 |
+
index = Index(range(6))
|
603 |
+
result = index.get_slice_bound(4, side=side)
|
604 |
+
assert result == expected
|
605 |
+
|
606 |
+
@pytest.mark.parametrize("side", ["left", "right"])
|
607 |
+
@pytest.mark.parametrize("bound, expected", [(-1, 0), (10, 6)])
|
608 |
+
def test_get_slice_bounds_outside(self, side, expected, bound):
|
609 |
+
index = Index(range(6))
|
610 |
+
result = index.get_slice_bound(bound, side=side)
|
611 |
+
assert result == expected
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/test_join.py
ADDED
@@ -0,0 +1,380 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas._testing as tm
|
5 |
+
from pandas.core.indexes.api import Index
|
6 |
+
|
7 |
+
|
8 |
+
class TestJoinInt64Index:
|
9 |
+
def test_join_non_unique(self):
|
10 |
+
left = Index([4, 4, 3, 3])
|
11 |
+
|
12 |
+
joined, lidx, ridx = left.join(left, return_indexers=True)
|
13 |
+
|
14 |
+
exp_joined = Index([4, 4, 4, 4, 3, 3, 3, 3])
|
15 |
+
tm.assert_index_equal(joined, exp_joined)
|
16 |
+
|
17 |
+
exp_lidx = np.array([0, 0, 1, 1, 2, 2, 3, 3], dtype=np.intp)
|
18 |
+
tm.assert_numpy_array_equal(lidx, exp_lidx)
|
19 |
+
|
20 |
+
exp_ridx = np.array([0, 1, 0, 1, 2, 3, 2, 3], dtype=np.intp)
|
21 |
+
tm.assert_numpy_array_equal(ridx, exp_ridx)
|
22 |
+
|
23 |
+
def test_join_inner(self):
|
24 |
+
index = Index(range(0, 20, 2), dtype=np.int64)
|
25 |
+
other = Index([7, 12, 25, 1, 2, 5], dtype=np.int64)
|
26 |
+
other_mono = Index([1, 2, 5, 7, 12, 25], dtype=np.int64)
|
27 |
+
|
28 |
+
# not monotonic
|
29 |
+
res, lidx, ridx = index.join(other, how="inner", return_indexers=True)
|
30 |
+
|
31 |
+
# no guarantee of sortedness, so sort for comparison purposes
|
32 |
+
ind = res.argsort()
|
33 |
+
res = res.take(ind)
|
34 |
+
lidx = lidx.take(ind)
|
35 |
+
ridx = ridx.take(ind)
|
36 |
+
|
37 |
+
eres = Index([2, 12], dtype=np.int64)
|
38 |
+
elidx = np.array([1, 6], dtype=np.intp)
|
39 |
+
eridx = np.array([4, 1], dtype=np.intp)
|
40 |
+
|
41 |
+
assert isinstance(res, Index) and res.dtype == np.int64
|
42 |
+
tm.assert_index_equal(res, eres)
|
43 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
44 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
45 |
+
|
46 |
+
# monotonic
|
47 |
+
res, lidx, ridx = index.join(other_mono, how="inner", return_indexers=True)
|
48 |
+
|
49 |
+
res2 = index.intersection(other_mono)
|
50 |
+
tm.assert_index_equal(res, res2)
|
51 |
+
|
52 |
+
elidx = np.array([1, 6], dtype=np.intp)
|
53 |
+
eridx = np.array([1, 4], dtype=np.intp)
|
54 |
+
assert isinstance(res, Index) and res.dtype == np.int64
|
55 |
+
tm.assert_index_equal(res, eres)
|
56 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
57 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
58 |
+
|
59 |
+
def test_join_left(self):
|
60 |
+
index = Index(range(0, 20, 2), dtype=np.int64)
|
61 |
+
other = Index([7, 12, 25, 1, 2, 5], dtype=np.int64)
|
62 |
+
other_mono = Index([1, 2, 5, 7, 12, 25], dtype=np.int64)
|
63 |
+
|
64 |
+
# not monotonic
|
65 |
+
res, lidx, ridx = index.join(other, how="left", return_indexers=True)
|
66 |
+
eres = index
|
67 |
+
eridx = np.array([-1, 4, -1, -1, -1, -1, 1, -1, -1, -1], dtype=np.intp)
|
68 |
+
|
69 |
+
assert isinstance(res, Index) and res.dtype == np.int64
|
70 |
+
tm.assert_index_equal(res, eres)
|
71 |
+
assert lidx is None
|
72 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
73 |
+
|
74 |
+
# monotonic
|
75 |
+
res, lidx, ridx = index.join(other_mono, how="left", return_indexers=True)
|
76 |
+
eridx = np.array([-1, 1, -1, -1, -1, -1, 4, -1, -1, -1], dtype=np.intp)
|
77 |
+
assert isinstance(res, Index) and res.dtype == np.int64
|
78 |
+
tm.assert_index_equal(res, eres)
|
79 |
+
assert lidx is None
|
80 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
81 |
+
|
82 |
+
# non-unique
|
83 |
+
idx = Index([1, 1, 2, 5])
|
84 |
+
idx2 = Index([1, 2, 5, 7, 9])
|
85 |
+
res, lidx, ridx = idx2.join(idx, how="left", return_indexers=True)
|
86 |
+
eres = Index([1, 1, 2, 5, 7, 9]) # 1 is in idx2, so it should be x2
|
87 |
+
eridx = np.array([0, 1, 2, 3, -1, -1], dtype=np.intp)
|
88 |
+
elidx = np.array([0, 0, 1, 2, 3, 4], dtype=np.intp)
|
89 |
+
tm.assert_index_equal(res, eres)
|
90 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
91 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
92 |
+
|
93 |
+
def test_join_right(self):
|
94 |
+
index = Index(range(0, 20, 2), dtype=np.int64)
|
95 |
+
other = Index([7, 12, 25, 1, 2, 5], dtype=np.int64)
|
96 |
+
other_mono = Index([1, 2, 5, 7, 12, 25], dtype=np.int64)
|
97 |
+
|
98 |
+
# not monotonic
|
99 |
+
res, lidx, ridx = index.join(other, how="right", return_indexers=True)
|
100 |
+
eres = other
|
101 |
+
elidx = np.array([-1, 6, -1, -1, 1, -1], dtype=np.intp)
|
102 |
+
|
103 |
+
assert isinstance(other, Index) and other.dtype == np.int64
|
104 |
+
tm.assert_index_equal(res, eres)
|
105 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
106 |
+
assert ridx is None
|
107 |
+
|
108 |
+
# monotonic
|
109 |
+
res, lidx, ridx = index.join(other_mono, how="right", return_indexers=True)
|
110 |
+
eres = other_mono
|
111 |
+
elidx = np.array([-1, 1, -1, -1, 6, -1], dtype=np.intp)
|
112 |
+
assert isinstance(other, Index) and other.dtype == np.int64
|
113 |
+
tm.assert_index_equal(res, eres)
|
114 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
115 |
+
assert ridx is None
|
116 |
+
|
117 |
+
# non-unique
|
118 |
+
idx = Index([1, 1, 2, 5])
|
119 |
+
idx2 = Index([1, 2, 5, 7, 9])
|
120 |
+
res, lidx, ridx = idx.join(idx2, how="right", return_indexers=True)
|
121 |
+
eres = Index([1, 1, 2, 5, 7, 9]) # 1 is in idx2, so it should be x2
|
122 |
+
elidx = np.array([0, 1, 2, 3, -1, -1], dtype=np.intp)
|
123 |
+
eridx = np.array([0, 0, 1, 2, 3, 4], dtype=np.intp)
|
124 |
+
tm.assert_index_equal(res, eres)
|
125 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
126 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
127 |
+
|
128 |
+
def test_join_non_int_index(self):
|
129 |
+
index = Index(range(0, 20, 2), dtype=np.int64)
|
130 |
+
other = Index([3, 6, 7, 8, 10], dtype=object)
|
131 |
+
|
132 |
+
outer = index.join(other, how="outer")
|
133 |
+
outer2 = other.join(index, how="outer")
|
134 |
+
expected = Index([0, 2, 3, 4, 6, 7, 8, 10, 12, 14, 16, 18])
|
135 |
+
tm.assert_index_equal(outer, outer2)
|
136 |
+
tm.assert_index_equal(outer, expected)
|
137 |
+
|
138 |
+
inner = index.join(other, how="inner")
|
139 |
+
inner2 = other.join(index, how="inner")
|
140 |
+
expected = Index([6, 8, 10])
|
141 |
+
tm.assert_index_equal(inner, inner2)
|
142 |
+
tm.assert_index_equal(inner, expected)
|
143 |
+
|
144 |
+
left = index.join(other, how="left")
|
145 |
+
tm.assert_index_equal(left, index.astype(object))
|
146 |
+
|
147 |
+
left2 = other.join(index, how="left")
|
148 |
+
tm.assert_index_equal(left2, other)
|
149 |
+
|
150 |
+
right = index.join(other, how="right")
|
151 |
+
tm.assert_index_equal(right, other)
|
152 |
+
|
153 |
+
right2 = other.join(index, how="right")
|
154 |
+
tm.assert_index_equal(right2, index.astype(object))
|
155 |
+
|
156 |
+
def test_join_outer(self):
|
157 |
+
index = Index(range(0, 20, 2), dtype=np.int64)
|
158 |
+
other = Index([7, 12, 25, 1, 2, 5], dtype=np.int64)
|
159 |
+
other_mono = Index([1, 2, 5, 7, 12, 25], dtype=np.int64)
|
160 |
+
|
161 |
+
# not monotonic
|
162 |
+
# guarantee of sortedness
|
163 |
+
res, lidx, ridx = index.join(other, how="outer", return_indexers=True)
|
164 |
+
noidx_res = index.join(other, how="outer")
|
165 |
+
tm.assert_index_equal(res, noidx_res)
|
166 |
+
|
167 |
+
eres = Index([0, 1, 2, 4, 5, 6, 7, 8, 10, 12, 14, 16, 18, 25], dtype=np.int64)
|
168 |
+
elidx = np.array([0, -1, 1, 2, -1, 3, -1, 4, 5, 6, 7, 8, 9, -1], dtype=np.intp)
|
169 |
+
eridx = np.array(
|
170 |
+
[-1, 3, 4, -1, 5, -1, 0, -1, -1, 1, -1, -1, -1, 2], dtype=np.intp
|
171 |
+
)
|
172 |
+
|
173 |
+
assert isinstance(res, Index) and res.dtype == np.int64
|
174 |
+
tm.assert_index_equal(res, eres)
|
175 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
176 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
177 |
+
|
178 |
+
# monotonic
|
179 |
+
res, lidx, ridx = index.join(other_mono, how="outer", return_indexers=True)
|
180 |
+
noidx_res = index.join(other_mono, how="outer")
|
181 |
+
tm.assert_index_equal(res, noidx_res)
|
182 |
+
|
183 |
+
elidx = np.array([0, -1, 1, 2, -1, 3, -1, 4, 5, 6, 7, 8, 9, -1], dtype=np.intp)
|
184 |
+
eridx = np.array(
|
185 |
+
[-1, 0, 1, -1, 2, -1, 3, -1, -1, 4, -1, -1, -1, 5], dtype=np.intp
|
186 |
+
)
|
187 |
+
assert isinstance(res, Index) and res.dtype == np.int64
|
188 |
+
tm.assert_index_equal(res, eres)
|
189 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
190 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
191 |
+
|
192 |
+
|
193 |
+
class TestJoinUInt64Index:
|
194 |
+
@pytest.fixture
|
195 |
+
def index_large(self):
|
196 |
+
# large values used in TestUInt64Index where no compat needed with int64/float64
|
197 |
+
large = [2**63, 2**63 + 10, 2**63 + 15, 2**63 + 20, 2**63 + 25]
|
198 |
+
return Index(large, dtype=np.uint64)
|
199 |
+
|
200 |
+
def test_join_inner(self, index_large):
|
201 |
+
other = Index(2**63 + np.array([7, 12, 25, 1, 2, 10], dtype="uint64"))
|
202 |
+
other_mono = Index(2**63 + np.array([1, 2, 7, 10, 12, 25], dtype="uint64"))
|
203 |
+
|
204 |
+
# not monotonic
|
205 |
+
res, lidx, ridx = index_large.join(other, how="inner", return_indexers=True)
|
206 |
+
|
207 |
+
# no guarantee of sortedness, so sort for comparison purposes
|
208 |
+
ind = res.argsort()
|
209 |
+
res = res.take(ind)
|
210 |
+
lidx = lidx.take(ind)
|
211 |
+
ridx = ridx.take(ind)
|
212 |
+
|
213 |
+
eres = Index(2**63 + np.array([10, 25], dtype="uint64"))
|
214 |
+
elidx = np.array([1, 4], dtype=np.intp)
|
215 |
+
eridx = np.array([5, 2], dtype=np.intp)
|
216 |
+
|
217 |
+
assert isinstance(res, Index) and res.dtype == np.uint64
|
218 |
+
tm.assert_index_equal(res, eres)
|
219 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
220 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
221 |
+
|
222 |
+
# monotonic
|
223 |
+
res, lidx, ridx = index_large.join(
|
224 |
+
other_mono, how="inner", return_indexers=True
|
225 |
+
)
|
226 |
+
|
227 |
+
res2 = index_large.intersection(other_mono)
|
228 |
+
tm.assert_index_equal(res, res2)
|
229 |
+
|
230 |
+
elidx = np.array([1, 4], dtype=np.intp)
|
231 |
+
eridx = np.array([3, 5], dtype=np.intp)
|
232 |
+
|
233 |
+
assert isinstance(res, Index) and res.dtype == np.uint64
|
234 |
+
tm.assert_index_equal(res, eres)
|
235 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
236 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
237 |
+
|
238 |
+
def test_join_left(self, index_large):
|
239 |
+
other = Index(2**63 + np.array([7, 12, 25, 1, 2, 10], dtype="uint64"))
|
240 |
+
other_mono = Index(2**63 + np.array([1, 2, 7, 10, 12, 25], dtype="uint64"))
|
241 |
+
|
242 |
+
# not monotonic
|
243 |
+
res, lidx, ridx = index_large.join(other, how="left", return_indexers=True)
|
244 |
+
eres = index_large
|
245 |
+
eridx = np.array([-1, 5, -1, -1, 2], dtype=np.intp)
|
246 |
+
|
247 |
+
assert isinstance(res, Index) and res.dtype == np.uint64
|
248 |
+
tm.assert_index_equal(res, eres)
|
249 |
+
assert lidx is None
|
250 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
251 |
+
|
252 |
+
# monotonic
|
253 |
+
res, lidx, ridx = index_large.join(other_mono, how="left", return_indexers=True)
|
254 |
+
eridx = np.array([-1, 3, -1, -1, 5], dtype=np.intp)
|
255 |
+
|
256 |
+
assert isinstance(res, Index) and res.dtype == np.uint64
|
257 |
+
tm.assert_index_equal(res, eres)
|
258 |
+
assert lidx is None
|
259 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
260 |
+
|
261 |
+
# non-unique
|
262 |
+
idx = Index(2**63 + np.array([1, 1, 2, 5], dtype="uint64"))
|
263 |
+
idx2 = Index(2**63 + np.array([1, 2, 5, 7, 9], dtype="uint64"))
|
264 |
+
res, lidx, ridx = idx2.join(idx, how="left", return_indexers=True)
|
265 |
+
|
266 |
+
# 1 is in idx2, so it should be x2
|
267 |
+
eres = Index(2**63 + np.array([1, 1, 2, 5, 7, 9], dtype="uint64"))
|
268 |
+
eridx = np.array([0, 1, 2, 3, -1, -1], dtype=np.intp)
|
269 |
+
elidx = np.array([0, 0, 1, 2, 3, 4], dtype=np.intp)
|
270 |
+
|
271 |
+
tm.assert_index_equal(res, eres)
|
272 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
273 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
274 |
+
|
275 |
+
def test_join_right(self, index_large):
|
276 |
+
other = Index(2**63 + np.array([7, 12, 25, 1, 2, 10], dtype="uint64"))
|
277 |
+
other_mono = Index(2**63 + np.array([1, 2, 7, 10, 12, 25], dtype="uint64"))
|
278 |
+
|
279 |
+
# not monotonic
|
280 |
+
res, lidx, ridx = index_large.join(other, how="right", return_indexers=True)
|
281 |
+
eres = other
|
282 |
+
elidx = np.array([-1, -1, 4, -1, -1, 1], dtype=np.intp)
|
283 |
+
|
284 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
285 |
+
assert isinstance(other, Index) and other.dtype == np.uint64
|
286 |
+
tm.assert_index_equal(res, eres)
|
287 |
+
assert ridx is None
|
288 |
+
|
289 |
+
# monotonic
|
290 |
+
res, lidx, ridx = index_large.join(
|
291 |
+
other_mono, how="right", return_indexers=True
|
292 |
+
)
|
293 |
+
eres = other_mono
|
294 |
+
elidx = np.array([-1, -1, -1, 1, -1, 4], dtype=np.intp)
|
295 |
+
|
296 |
+
assert isinstance(other, Index) and other.dtype == np.uint64
|
297 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
298 |
+
tm.assert_index_equal(res, eres)
|
299 |
+
assert ridx is None
|
300 |
+
|
301 |
+
# non-unique
|
302 |
+
idx = Index(2**63 + np.array([1, 1, 2, 5], dtype="uint64"))
|
303 |
+
idx2 = Index(2**63 + np.array([1, 2, 5, 7, 9], dtype="uint64"))
|
304 |
+
res, lidx, ridx = idx.join(idx2, how="right", return_indexers=True)
|
305 |
+
|
306 |
+
# 1 is in idx2, so it should be x2
|
307 |
+
eres = Index(2**63 + np.array([1, 1, 2, 5, 7, 9], dtype="uint64"))
|
308 |
+
elidx = np.array([0, 1, 2, 3, -1, -1], dtype=np.intp)
|
309 |
+
eridx = np.array([0, 0, 1, 2, 3, 4], dtype=np.intp)
|
310 |
+
|
311 |
+
tm.assert_index_equal(res, eres)
|
312 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
313 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
314 |
+
|
315 |
+
def test_join_non_int_index(self, index_large):
|
316 |
+
other = Index(
|
317 |
+
2**63 + np.array([1, 5, 7, 10, 20], dtype="uint64"), dtype=object
|
318 |
+
)
|
319 |
+
|
320 |
+
outer = index_large.join(other, how="outer")
|
321 |
+
outer2 = other.join(index_large, how="outer")
|
322 |
+
expected = Index(
|
323 |
+
2**63 + np.array([0, 1, 5, 7, 10, 15, 20, 25], dtype="uint64")
|
324 |
+
)
|
325 |
+
tm.assert_index_equal(outer, outer2)
|
326 |
+
tm.assert_index_equal(outer, expected)
|
327 |
+
|
328 |
+
inner = index_large.join(other, how="inner")
|
329 |
+
inner2 = other.join(index_large, how="inner")
|
330 |
+
expected = Index(2**63 + np.array([10, 20], dtype="uint64"))
|
331 |
+
tm.assert_index_equal(inner, inner2)
|
332 |
+
tm.assert_index_equal(inner, expected)
|
333 |
+
|
334 |
+
left = index_large.join(other, how="left")
|
335 |
+
tm.assert_index_equal(left, index_large.astype(object))
|
336 |
+
|
337 |
+
left2 = other.join(index_large, how="left")
|
338 |
+
tm.assert_index_equal(left2, other)
|
339 |
+
|
340 |
+
right = index_large.join(other, how="right")
|
341 |
+
tm.assert_index_equal(right, other)
|
342 |
+
|
343 |
+
right2 = other.join(index_large, how="right")
|
344 |
+
tm.assert_index_equal(right2, index_large.astype(object))
|
345 |
+
|
346 |
+
def test_join_outer(self, index_large):
|
347 |
+
other = Index(2**63 + np.array([7, 12, 25, 1, 2, 10], dtype="uint64"))
|
348 |
+
other_mono = Index(2**63 + np.array([1, 2, 7, 10, 12, 25], dtype="uint64"))
|
349 |
+
|
350 |
+
# not monotonic
|
351 |
+
# guarantee of sortedness
|
352 |
+
res, lidx, ridx = index_large.join(other, how="outer", return_indexers=True)
|
353 |
+
noidx_res = index_large.join(other, how="outer")
|
354 |
+
tm.assert_index_equal(res, noidx_res)
|
355 |
+
|
356 |
+
eres = Index(
|
357 |
+
2**63 + np.array([0, 1, 2, 7, 10, 12, 15, 20, 25], dtype="uint64")
|
358 |
+
)
|
359 |
+
elidx = np.array([0, -1, -1, -1, 1, -1, 2, 3, 4], dtype=np.intp)
|
360 |
+
eridx = np.array([-1, 3, 4, 0, 5, 1, -1, -1, 2], dtype=np.intp)
|
361 |
+
|
362 |
+
assert isinstance(res, Index) and res.dtype == np.uint64
|
363 |
+
tm.assert_index_equal(res, eres)
|
364 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
365 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
366 |
+
|
367 |
+
# monotonic
|
368 |
+
res, lidx, ridx = index_large.join(
|
369 |
+
other_mono, how="outer", return_indexers=True
|
370 |
+
)
|
371 |
+
noidx_res = index_large.join(other_mono, how="outer")
|
372 |
+
tm.assert_index_equal(res, noidx_res)
|
373 |
+
|
374 |
+
elidx = np.array([0, -1, -1, -1, 1, -1, 2, 3, 4], dtype=np.intp)
|
375 |
+
eridx = np.array([-1, 0, 1, 2, 3, 4, -1, -1, 5], dtype=np.intp)
|
376 |
+
|
377 |
+
assert isinstance(res, Index) and res.dtype == np.uint64
|
378 |
+
tm.assert_index_equal(res, eres)
|
379 |
+
tm.assert_numpy_array_equal(lidx, elidx)
|
380 |
+
tm.assert_numpy_array_equal(ridx, eridx)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/numeric/test_setops.py
ADDED
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import (
|
2 |
+
datetime,
|
3 |
+
timedelta,
|
4 |
+
)
|
5 |
+
|
6 |
+
import numpy as np
|
7 |
+
import pytest
|
8 |
+
|
9 |
+
import pandas._testing as tm
|
10 |
+
from pandas.core.indexes.api import (
|
11 |
+
Index,
|
12 |
+
RangeIndex,
|
13 |
+
)
|
14 |
+
|
15 |
+
|
16 |
+
@pytest.fixture
|
17 |
+
def index_large():
|
18 |
+
# large values used in TestUInt64Index where no compat needed with int64/float64
|
19 |
+
large = [2**63, 2**63 + 10, 2**63 + 15, 2**63 + 20, 2**63 + 25]
|
20 |
+
return Index(large, dtype=np.uint64)
|
21 |
+
|
22 |
+
|
23 |
+
class TestSetOps:
|
24 |
+
@pytest.mark.parametrize("dtype", ["f8", "u8", "i8"])
|
25 |
+
def test_union_non_numeric(self, dtype):
|
26 |
+
# corner case, non-numeric
|
27 |
+
index = Index(np.arange(5, dtype=dtype), dtype=dtype)
|
28 |
+
assert index.dtype == dtype
|
29 |
+
|
30 |
+
other = Index([datetime.now() + timedelta(i) for i in range(4)], dtype=object)
|
31 |
+
result = index.union(other)
|
32 |
+
expected = Index(np.concatenate((index, other)))
|
33 |
+
tm.assert_index_equal(result, expected)
|
34 |
+
|
35 |
+
result = other.union(index)
|
36 |
+
expected = Index(np.concatenate((other, index)))
|
37 |
+
tm.assert_index_equal(result, expected)
|
38 |
+
|
39 |
+
def test_intersection(self):
|
40 |
+
index = Index(range(5), dtype=np.int64)
|
41 |
+
|
42 |
+
other = Index([1, 2, 3, 4, 5])
|
43 |
+
result = index.intersection(other)
|
44 |
+
expected = Index(np.sort(np.intersect1d(index.values, other.values)))
|
45 |
+
tm.assert_index_equal(result, expected)
|
46 |
+
|
47 |
+
result = other.intersection(index)
|
48 |
+
expected = Index(
|
49 |
+
np.sort(np.asarray(np.intersect1d(index.values, other.values)))
|
50 |
+
)
|
51 |
+
tm.assert_index_equal(result, expected)
|
52 |
+
|
53 |
+
@pytest.mark.parametrize("dtype", ["int64", "uint64"])
|
54 |
+
def test_int_float_union_dtype(self, dtype):
|
55 |
+
# https://github.com/pandas-dev/pandas/issues/26778
|
56 |
+
# [u]int | float -> float
|
57 |
+
index = Index([0, 2, 3], dtype=dtype)
|
58 |
+
other = Index([0.5, 1.5], dtype=np.float64)
|
59 |
+
expected = Index([0.0, 0.5, 1.5, 2.0, 3.0], dtype=np.float64)
|
60 |
+
result = index.union(other)
|
61 |
+
tm.assert_index_equal(result, expected)
|
62 |
+
|
63 |
+
result = other.union(index)
|
64 |
+
tm.assert_index_equal(result, expected)
|
65 |
+
|
66 |
+
def test_range_float_union_dtype(self):
|
67 |
+
# https://github.com/pandas-dev/pandas/issues/26778
|
68 |
+
index = RangeIndex(start=0, stop=3)
|
69 |
+
other = Index([0.5, 1.5], dtype=np.float64)
|
70 |
+
result = index.union(other)
|
71 |
+
expected = Index([0.0, 0.5, 1, 1.5, 2.0], dtype=np.float64)
|
72 |
+
tm.assert_index_equal(result, expected)
|
73 |
+
|
74 |
+
result = other.union(index)
|
75 |
+
tm.assert_index_equal(result, expected)
|
76 |
+
|
77 |
+
def test_range_uint64_union_dtype(self):
|
78 |
+
# https://github.com/pandas-dev/pandas/issues/26778
|
79 |
+
index = RangeIndex(start=0, stop=3)
|
80 |
+
other = Index([0, 10], dtype=np.uint64)
|
81 |
+
result = index.union(other)
|
82 |
+
expected = Index([0, 1, 2, 10], dtype=object)
|
83 |
+
tm.assert_index_equal(result, expected)
|
84 |
+
|
85 |
+
result = other.union(index)
|
86 |
+
tm.assert_index_equal(result, expected)
|
87 |
+
|
88 |
+
def test_float64_index_difference(self):
|
89 |
+
# https://github.com/pandas-dev/pandas/issues/35217
|
90 |
+
float_index = Index([1.0, 2, 3])
|
91 |
+
string_index = Index(["1", "2", "3"])
|
92 |
+
|
93 |
+
result = float_index.difference(string_index)
|
94 |
+
tm.assert_index_equal(result, float_index)
|
95 |
+
|
96 |
+
result = string_index.difference(float_index)
|
97 |
+
tm.assert_index_equal(result, string_index)
|
98 |
+
|
99 |
+
def test_intersection_uint64_outside_int64_range(self, index_large):
|
100 |
+
other = Index([2**63, 2**63 + 5, 2**63 + 10, 2**63 + 15, 2**63 + 20])
|
101 |
+
result = index_large.intersection(other)
|
102 |
+
expected = Index(np.sort(np.intersect1d(index_large.values, other.values)))
|
103 |
+
tm.assert_index_equal(result, expected)
|
104 |
+
|
105 |
+
result = other.intersection(index_large)
|
106 |
+
expected = Index(
|
107 |
+
np.sort(np.asarray(np.intersect1d(index_large.values, other.values)))
|
108 |
+
)
|
109 |
+
tm.assert_index_equal(result, expected)
|
110 |
+
|
111 |
+
@pytest.mark.parametrize(
|
112 |
+
"index2,keeps_name",
|
113 |
+
[
|
114 |
+
(Index([4, 7, 6, 5, 3], name="index"), True),
|
115 |
+
(Index([4, 7, 6, 5, 3], name="other"), False),
|
116 |
+
],
|
117 |
+
)
|
118 |
+
def test_intersection_monotonic(self, index2, keeps_name, sort):
|
119 |
+
index1 = Index([5, 3, 2, 4, 1], name="index")
|
120 |
+
expected = Index([5, 3, 4])
|
121 |
+
|
122 |
+
if keeps_name:
|
123 |
+
expected.name = "index"
|
124 |
+
|
125 |
+
result = index1.intersection(index2, sort=sort)
|
126 |
+
if sort is None:
|
127 |
+
expected = expected.sort_values()
|
128 |
+
tm.assert_index_equal(result, expected)
|
129 |
+
|
130 |
+
def test_symmetric_difference(self, sort):
|
131 |
+
# smoke
|
132 |
+
index1 = Index([5, 2, 3, 4], name="index1")
|
133 |
+
index2 = Index([2, 3, 4, 1])
|
134 |
+
result = index1.symmetric_difference(index2, sort=sort)
|
135 |
+
expected = Index([5, 1])
|
136 |
+
if sort is not None:
|
137 |
+
tm.assert_index_equal(result, expected)
|
138 |
+
else:
|
139 |
+
tm.assert_index_equal(result, expected.sort_values())
|
140 |
+
assert result.name is None
|
141 |
+
if sort is None:
|
142 |
+
expected = expected.sort_values()
|
143 |
+
tm.assert_index_equal(result, expected)
|
144 |
+
|
145 |
+
|
146 |
+
class TestSetOpsSort:
|
147 |
+
@pytest.mark.parametrize("slice_", [slice(None), slice(0)])
|
148 |
+
def test_union_sort_other_special(self, slice_):
|
149 |
+
# https://github.com/pandas-dev/pandas/issues/24959
|
150 |
+
|
151 |
+
idx = Index([1, 0, 2])
|
152 |
+
# default, sort=None
|
153 |
+
other = idx[slice_]
|
154 |
+
tm.assert_index_equal(idx.union(other), idx)
|
155 |
+
tm.assert_index_equal(other.union(idx), idx)
|
156 |
+
|
157 |
+
# sort=False
|
158 |
+
tm.assert_index_equal(idx.union(other, sort=False), idx)
|
159 |
+
|
160 |
+
@pytest.mark.parametrize("slice_", [slice(None), slice(0)])
|
161 |
+
def test_union_sort_special_true(self, slice_):
|
162 |
+
idx = Index([1, 0, 2])
|
163 |
+
# default, sort=None
|
164 |
+
other = idx[slice_]
|
165 |
+
|
166 |
+
result = idx.union(other, sort=True)
|
167 |
+
expected = Index([0, 1, 2])
|
168 |
+
tm.assert_index_equal(result, expected)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__init__.py
ADDED
File without changes
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (195 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_constructors.cpython-310.pyc
ADDED
Binary file (24.8 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_formats.cpython-310.pyc
ADDED
Binary file (9.85 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_freq_attr.cpython-310.pyc
ADDED
Binary file (1.04 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_indexing.cpython-310.pyc
ADDED
Binary file (22.4 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_join.cpython-310.pyc
ADDED
Binary file (2.45 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_monotonic.cpython-310.pyc
ADDED
Binary file (1.03 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_partial_slicing.cpython-310.pyc
ADDED
Binary file (6.69 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_period.cpython-310.pyc
ADDED
Binary file (7.12 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_period_range.cpython-310.pyc
ADDED
Binary file (7.83 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_pickle.cpython-310.pyc
ADDED
Binary file (1.21 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_resolution.cpython-310.pyc
ADDED
Binary file (855 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_scalar_compat.cpython-310.pyc
ADDED
Binary file (1.75 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_searchsorted.cpython-310.pyc
ADDED
Binary file (2.68 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_setops.cpython-310.pyc
ADDED
Binary file (7.01 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/__pycache__/test_tools.cpython-310.pyc
ADDED
Binary file (1.74 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__init__.py
ADDED
File without changes
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (203 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_asfreq.cpython-310.pyc
ADDED
Binary file (5.83 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_astype.cpython-310.pyc
ADDED
Binary file (5.02 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_factorize.cpython-310.pyc
ADDED
Binary file (1.55 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_fillna.cpython-310.pyc
ADDED
Binary file (1.06 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_insert.cpython-310.pyc
ADDED
Binary file (918 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_is_full.cpython-310.pyc
ADDED
Binary file (762 Bytes). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_repeat.cpython-310.pyc
ADDED
Binary file (1.22 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_shift.cpython-310.pyc
ADDED
Binary file (3.56 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/__pycache__/test_to_timestamp.cpython-310.pyc
ADDED
Binary file (4.72 kB). View file
|
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_asfreq.py
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
|
3 |
+
import pytest
|
4 |
+
|
5 |
+
from pandas import (
|
6 |
+
PeriodIndex,
|
7 |
+
Series,
|
8 |
+
period_range,
|
9 |
+
)
|
10 |
+
import pandas._testing as tm
|
11 |
+
|
12 |
+
from pandas.tseries import offsets
|
13 |
+
|
14 |
+
|
15 |
+
class TestPeriodIndex:
|
16 |
+
def test_asfreq(self):
|
17 |
+
pi1 = period_range(freq="Y", start="1/1/2001", end="1/1/2001")
|
18 |
+
pi2 = period_range(freq="Q", start="1/1/2001", end="1/1/2001")
|
19 |
+
pi3 = period_range(freq="M", start="1/1/2001", end="1/1/2001")
|
20 |
+
pi4 = period_range(freq="D", start="1/1/2001", end="1/1/2001")
|
21 |
+
pi5 = period_range(freq="h", start="1/1/2001", end="1/1/2001 00:00")
|
22 |
+
pi6 = period_range(freq="Min", start="1/1/2001", end="1/1/2001 00:00")
|
23 |
+
pi7 = period_range(freq="s", start="1/1/2001", end="1/1/2001 00:00:00")
|
24 |
+
|
25 |
+
assert pi1.asfreq("Q", "s") == pi2
|
26 |
+
assert pi1.asfreq("Q", "s") == pi2
|
27 |
+
assert pi1.asfreq("M", "start") == pi3
|
28 |
+
assert pi1.asfreq("D", "StarT") == pi4
|
29 |
+
assert pi1.asfreq("h", "beGIN") == pi5
|
30 |
+
assert pi1.asfreq("Min", "s") == pi6
|
31 |
+
assert pi1.asfreq("s", "s") == pi7
|
32 |
+
|
33 |
+
assert pi2.asfreq("Y", "s") == pi1
|
34 |
+
assert pi2.asfreq("M", "s") == pi3
|
35 |
+
assert pi2.asfreq("D", "s") == pi4
|
36 |
+
assert pi2.asfreq("h", "s") == pi5
|
37 |
+
assert pi2.asfreq("Min", "s") == pi6
|
38 |
+
assert pi2.asfreq("s", "s") == pi7
|
39 |
+
|
40 |
+
assert pi3.asfreq("Y", "s") == pi1
|
41 |
+
assert pi3.asfreq("Q", "s") == pi2
|
42 |
+
assert pi3.asfreq("D", "s") == pi4
|
43 |
+
assert pi3.asfreq("h", "s") == pi5
|
44 |
+
assert pi3.asfreq("Min", "s") == pi6
|
45 |
+
assert pi3.asfreq("s", "s") == pi7
|
46 |
+
|
47 |
+
assert pi4.asfreq("Y", "s") == pi1
|
48 |
+
assert pi4.asfreq("Q", "s") == pi2
|
49 |
+
assert pi4.asfreq("M", "s") == pi3
|
50 |
+
assert pi4.asfreq("h", "s") == pi5
|
51 |
+
assert pi4.asfreq("Min", "s") == pi6
|
52 |
+
assert pi4.asfreq("s", "s") == pi7
|
53 |
+
|
54 |
+
assert pi5.asfreq("Y", "s") == pi1
|
55 |
+
assert pi5.asfreq("Q", "s") == pi2
|
56 |
+
assert pi5.asfreq("M", "s") == pi3
|
57 |
+
assert pi5.asfreq("D", "s") == pi4
|
58 |
+
assert pi5.asfreq("Min", "s") == pi6
|
59 |
+
assert pi5.asfreq("s", "s") == pi7
|
60 |
+
|
61 |
+
assert pi6.asfreq("Y", "s") == pi1
|
62 |
+
assert pi6.asfreq("Q", "s") == pi2
|
63 |
+
assert pi6.asfreq("M", "s") == pi3
|
64 |
+
assert pi6.asfreq("D", "s") == pi4
|
65 |
+
assert pi6.asfreq("h", "s") == pi5
|
66 |
+
assert pi6.asfreq("s", "s") == pi7
|
67 |
+
|
68 |
+
assert pi7.asfreq("Y", "s") == pi1
|
69 |
+
assert pi7.asfreq("Q", "s") == pi2
|
70 |
+
assert pi7.asfreq("M", "s") == pi3
|
71 |
+
assert pi7.asfreq("D", "s") == pi4
|
72 |
+
assert pi7.asfreq("h", "s") == pi5
|
73 |
+
assert pi7.asfreq("Min", "s") == pi6
|
74 |
+
|
75 |
+
msg = "How must be one of S or E"
|
76 |
+
with pytest.raises(ValueError, match=msg):
|
77 |
+
pi7.asfreq("T", "foo")
|
78 |
+
result1 = pi1.asfreq("3M")
|
79 |
+
result2 = pi1.asfreq("M")
|
80 |
+
expected = period_range(freq="M", start="2001-12", end="2001-12")
|
81 |
+
tm.assert_numpy_array_equal(result1.asi8, expected.asi8)
|
82 |
+
assert result1.freqstr == "3M"
|
83 |
+
tm.assert_numpy_array_equal(result2.asi8, expected.asi8)
|
84 |
+
assert result2.freqstr == "M"
|
85 |
+
|
86 |
+
def test_asfreq_nat(self):
|
87 |
+
idx = PeriodIndex(["2011-01", "2011-02", "NaT", "2011-04"], freq="M")
|
88 |
+
result = idx.asfreq(freq="Q")
|
89 |
+
expected = PeriodIndex(["2011Q1", "2011Q1", "NaT", "2011Q2"], freq="Q")
|
90 |
+
tm.assert_index_equal(result, expected)
|
91 |
+
|
92 |
+
@pytest.mark.parametrize("freq", ["D", "3D"])
|
93 |
+
def test_asfreq_mult_pi(self, freq):
|
94 |
+
pi = PeriodIndex(["2001-01", "2001-02", "NaT", "2001-03"], freq="2M")
|
95 |
+
|
96 |
+
result = pi.asfreq(freq)
|
97 |
+
exp = PeriodIndex(["2001-02-28", "2001-03-31", "NaT", "2001-04-30"], freq=freq)
|
98 |
+
tm.assert_index_equal(result, exp)
|
99 |
+
assert result.freq == exp.freq
|
100 |
+
|
101 |
+
result = pi.asfreq(freq, how="S")
|
102 |
+
exp = PeriodIndex(["2001-01-01", "2001-02-01", "NaT", "2001-03-01"], freq=freq)
|
103 |
+
tm.assert_index_equal(result, exp)
|
104 |
+
assert result.freq == exp.freq
|
105 |
+
|
106 |
+
def test_asfreq_combined_pi(self):
|
107 |
+
pi = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq="h")
|
108 |
+
exp = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq="25h")
|
109 |
+
for freq, how in zip(["1D1h", "1h1D"], ["S", "E"]):
|
110 |
+
result = pi.asfreq(freq, how=how)
|
111 |
+
tm.assert_index_equal(result, exp)
|
112 |
+
assert result.freq == exp.freq
|
113 |
+
|
114 |
+
for freq in ["1D1h", "1h1D"]:
|
115 |
+
pi = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq=freq)
|
116 |
+
result = pi.asfreq("h")
|
117 |
+
exp = PeriodIndex(["2001-01-02 00:00", "2001-01-03 02:00", "NaT"], freq="h")
|
118 |
+
tm.assert_index_equal(result, exp)
|
119 |
+
assert result.freq == exp.freq
|
120 |
+
|
121 |
+
pi = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq=freq)
|
122 |
+
result = pi.asfreq("h", how="S")
|
123 |
+
exp = PeriodIndex(["2001-01-01 00:00", "2001-01-02 02:00", "NaT"], freq="h")
|
124 |
+
tm.assert_index_equal(result, exp)
|
125 |
+
assert result.freq == exp.freq
|
126 |
+
|
127 |
+
def test_astype_asfreq(self):
|
128 |
+
pi1 = PeriodIndex(["2011-01-01", "2011-02-01", "2011-03-01"], freq="D")
|
129 |
+
exp = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M")
|
130 |
+
tm.assert_index_equal(pi1.asfreq("M"), exp)
|
131 |
+
tm.assert_index_equal(pi1.astype("period[M]"), exp)
|
132 |
+
|
133 |
+
exp = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="3M")
|
134 |
+
tm.assert_index_equal(pi1.asfreq("3M"), exp)
|
135 |
+
tm.assert_index_equal(pi1.astype("period[3M]"), exp)
|
136 |
+
|
137 |
+
def test_asfreq_with_different_n(self):
|
138 |
+
ser = Series([1, 2], index=PeriodIndex(["2020-01", "2020-03"], freq="2M"))
|
139 |
+
result = ser.asfreq("M")
|
140 |
+
|
141 |
+
excepted = Series([1, 2], index=PeriodIndex(["2020-02", "2020-04"], freq="M"))
|
142 |
+
tm.assert_series_equal(result, excepted)
|
143 |
+
|
144 |
+
@pytest.mark.parametrize(
|
145 |
+
"freq",
|
146 |
+
[
|
147 |
+
"2BMS",
|
148 |
+
"2YS-MAR",
|
149 |
+
"2bh",
|
150 |
+
],
|
151 |
+
)
|
152 |
+
def test_pi_asfreq_not_supported_frequency(self, freq):
|
153 |
+
# GH#55785
|
154 |
+
msg = f"{freq[1:]} is not supported as period frequency"
|
155 |
+
|
156 |
+
pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
|
157 |
+
with pytest.raises(ValueError, match=msg):
|
158 |
+
pi.asfreq(freq=freq)
|
159 |
+
|
160 |
+
@pytest.mark.parametrize(
|
161 |
+
"freq",
|
162 |
+
[
|
163 |
+
"2BME",
|
164 |
+
"2YE-MAR",
|
165 |
+
"2QE",
|
166 |
+
],
|
167 |
+
)
|
168 |
+
def test_pi_asfreq_invalid_frequency(self, freq):
|
169 |
+
# GH#55785
|
170 |
+
msg = f"Invalid frequency: {freq}"
|
171 |
+
|
172 |
+
pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
|
173 |
+
with pytest.raises(ValueError, match=msg):
|
174 |
+
pi.asfreq(freq=freq)
|
175 |
+
|
176 |
+
@pytest.mark.parametrize(
|
177 |
+
"freq",
|
178 |
+
[
|
179 |
+
offsets.MonthBegin(2),
|
180 |
+
offsets.BusinessMonthEnd(2),
|
181 |
+
],
|
182 |
+
)
|
183 |
+
def test_pi_asfreq_invalid_baseoffset(self, freq):
|
184 |
+
# GH#56945
|
185 |
+
msg = re.escape(f"{freq} is not supported as period frequency")
|
186 |
+
|
187 |
+
pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
|
188 |
+
with pytest.raises(ValueError, match=msg):
|
189 |
+
pi.asfreq(freq=freq)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_astype.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
CategoricalIndex,
|
6 |
+
DatetimeIndex,
|
7 |
+
Index,
|
8 |
+
NaT,
|
9 |
+
Period,
|
10 |
+
PeriodIndex,
|
11 |
+
period_range,
|
12 |
+
)
|
13 |
+
import pandas._testing as tm
|
14 |
+
|
15 |
+
|
16 |
+
class TestPeriodIndexAsType:
|
17 |
+
@pytest.mark.parametrize("dtype", [float, "timedelta64", "timedelta64[ns]"])
|
18 |
+
def test_astype_raises(self, dtype):
|
19 |
+
# GH#13149, GH#13209
|
20 |
+
idx = PeriodIndex(["2016-05-16", "NaT", NaT, np.nan], freq="D")
|
21 |
+
msg = "Cannot cast PeriodIndex to dtype"
|
22 |
+
with pytest.raises(TypeError, match=msg):
|
23 |
+
idx.astype(dtype)
|
24 |
+
|
25 |
+
def test_astype_conversion(self):
|
26 |
+
# GH#13149, GH#13209
|
27 |
+
idx = PeriodIndex(["2016-05-16", "NaT", NaT, np.nan], freq="D", name="idx")
|
28 |
+
|
29 |
+
result = idx.astype(object)
|
30 |
+
expected = Index(
|
31 |
+
[Period("2016-05-16", freq="D")] + [Period(NaT, freq="D")] * 3,
|
32 |
+
dtype="object",
|
33 |
+
name="idx",
|
34 |
+
)
|
35 |
+
tm.assert_index_equal(result, expected)
|
36 |
+
|
37 |
+
result = idx.astype(np.int64)
|
38 |
+
expected = Index(
|
39 |
+
[16937] + [-9223372036854775808] * 3, dtype=np.int64, name="idx"
|
40 |
+
)
|
41 |
+
tm.assert_index_equal(result, expected)
|
42 |
+
|
43 |
+
result = idx.astype(str)
|
44 |
+
expected = Index([str(x) for x in idx], name="idx", dtype=object)
|
45 |
+
tm.assert_index_equal(result, expected)
|
46 |
+
|
47 |
+
idx = period_range("1990", "2009", freq="Y", name="idx")
|
48 |
+
result = idx.astype("i8")
|
49 |
+
tm.assert_index_equal(result, Index(idx.asi8, name="idx"))
|
50 |
+
tm.assert_numpy_array_equal(result.values, idx.asi8)
|
51 |
+
|
52 |
+
def test_astype_uint(self):
|
53 |
+
arr = period_range("2000", periods=2, name="idx")
|
54 |
+
|
55 |
+
with pytest.raises(TypeError, match=r"Do obj.astype\('int64'\)"):
|
56 |
+
arr.astype("uint64")
|
57 |
+
with pytest.raises(TypeError, match=r"Do obj.astype\('int64'\)"):
|
58 |
+
arr.astype("uint32")
|
59 |
+
|
60 |
+
def test_astype_object(self):
|
61 |
+
idx = PeriodIndex([], freq="M")
|
62 |
+
|
63 |
+
exp = np.array([], dtype=object)
|
64 |
+
tm.assert_numpy_array_equal(idx.astype(object).values, exp)
|
65 |
+
tm.assert_numpy_array_equal(idx._mpl_repr(), exp)
|
66 |
+
|
67 |
+
idx = PeriodIndex(["2011-01", NaT], freq="M")
|
68 |
+
|
69 |
+
exp = np.array([Period("2011-01", freq="M"), NaT], dtype=object)
|
70 |
+
tm.assert_numpy_array_equal(idx.astype(object).values, exp)
|
71 |
+
tm.assert_numpy_array_equal(idx._mpl_repr(), exp)
|
72 |
+
|
73 |
+
exp = np.array([Period("2011-01-01", freq="D"), NaT], dtype=object)
|
74 |
+
idx = PeriodIndex(["2011-01-01", NaT], freq="D")
|
75 |
+
tm.assert_numpy_array_equal(idx.astype(object).values, exp)
|
76 |
+
tm.assert_numpy_array_equal(idx._mpl_repr(), exp)
|
77 |
+
|
78 |
+
# TODO: de-duplicate this version (from test_ops) with the one above
|
79 |
+
# (from test_period)
|
80 |
+
def test_astype_object2(self):
|
81 |
+
idx = period_range(start="2013-01-01", periods=4, freq="M", name="idx")
|
82 |
+
expected_list = [
|
83 |
+
Period("2013-01-31", freq="M"),
|
84 |
+
Period("2013-02-28", freq="M"),
|
85 |
+
Period("2013-03-31", freq="M"),
|
86 |
+
Period("2013-04-30", freq="M"),
|
87 |
+
]
|
88 |
+
expected = Index(expected_list, dtype=object, name="idx")
|
89 |
+
result = idx.astype(object)
|
90 |
+
assert isinstance(result, Index)
|
91 |
+
assert result.dtype == object
|
92 |
+
tm.assert_index_equal(result, expected)
|
93 |
+
assert result.name == expected.name
|
94 |
+
assert idx.tolist() == expected_list
|
95 |
+
|
96 |
+
idx = PeriodIndex(
|
97 |
+
["2013-01-01", "2013-01-02", "NaT", "2013-01-04"], freq="D", name="idx"
|
98 |
+
)
|
99 |
+
expected_list = [
|
100 |
+
Period("2013-01-01", freq="D"),
|
101 |
+
Period("2013-01-02", freq="D"),
|
102 |
+
Period("NaT", freq="D"),
|
103 |
+
Period("2013-01-04", freq="D"),
|
104 |
+
]
|
105 |
+
expected = Index(expected_list, dtype=object, name="idx")
|
106 |
+
result = idx.astype(object)
|
107 |
+
assert isinstance(result, Index)
|
108 |
+
assert result.dtype == object
|
109 |
+
tm.assert_index_equal(result, expected)
|
110 |
+
for i in [0, 1, 3]:
|
111 |
+
assert result[i] == expected[i]
|
112 |
+
assert result[2] is NaT
|
113 |
+
assert result.name == expected.name
|
114 |
+
|
115 |
+
result_list = idx.tolist()
|
116 |
+
for i in [0, 1, 3]:
|
117 |
+
assert result_list[i] == expected_list[i]
|
118 |
+
assert result_list[2] is NaT
|
119 |
+
|
120 |
+
def test_astype_category(self):
|
121 |
+
obj = period_range("2000", periods=2, name="idx")
|
122 |
+
result = obj.astype("category")
|
123 |
+
expected = CategoricalIndex(
|
124 |
+
[Period("2000-01-01", freq="D"), Period("2000-01-02", freq="D")], name="idx"
|
125 |
+
)
|
126 |
+
tm.assert_index_equal(result, expected)
|
127 |
+
|
128 |
+
result = obj._data.astype("category")
|
129 |
+
expected = expected.values
|
130 |
+
tm.assert_categorical_equal(result, expected)
|
131 |
+
|
132 |
+
def test_astype_array_fallback(self):
|
133 |
+
obj = period_range("2000", periods=2, name="idx")
|
134 |
+
result = obj.astype(bool)
|
135 |
+
expected = Index(np.array([True, True]), name="idx")
|
136 |
+
tm.assert_index_equal(result, expected)
|
137 |
+
|
138 |
+
result = obj._data.astype(bool)
|
139 |
+
expected = np.array([True, True])
|
140 |
+
tm.assert_numpy_array_equal(result, expected)
|
141 |
+
|
142 |
+
def test_period_astype_to_timestamp(self, unit):
|
143 |
+
# GH#55958
|
144 |
+
pi = PeriodIndex(["2011-01", "2011-02", "2011-03"], freq="M")
|
145 |
+
|
146 |
+
exp = DatetimeIndex(
|
147 |
+
["2011-01-01", "2011-02-01", "2011-03-01"], tz="US/Eastern"
|
148 |
+
).as_unit(unit)
|
149 |
+
res = pi.astype(f"datetime64[{unit}, US/Eastern]")
|
150 |
+
tm.assert_index_equal(res, exp)
|
151 |
+
assert res.freq == exp.freq
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_factorize.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
from pandas import PeriodIndex
|
4 |
+
import pandas._testing as tm
|
5 |
+
|
6 |
+
|
7 |
+
class TestFactorize:
|
8 |
+
def test_factorize_period(self):
|
9 |
+
idx1 = PeriodIndex(
|
10 |
+
["2014-01", "2014-01", "2014-02", "2014-02", "2014-03", "2014-03"],
|
11 |
+
freq="M",
|
12 |
+
)
|
13 |
+
|
14 |
+
exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp)
|
15 |
+
exp_idx = PeriodIndex(["2014-01", "2014-02", "2014-03"], freq="M")
|
16 |
+
|
17 |
+
arr, idx = idx1.factorize()
|
18 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
19 |
+
tm.assert_index_equal(idx, exp_idx)
|
20 |
+
|
21 |
+
arr, idx = idx1.factorize(sort=True)
|
22 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
23 |
+
tm.assert_index_equal(idx, exp_idx)
|
24 |
+
|
25 |
+
def test_factorize_period_nonmonotonic(self):
|
26 |
+
idx2 = PeriodIndex(
|
27 |
+
["2014-03", "2014-03", "2014-02", "2014-01", "2014-03", "2014-01"],
|
28 |
+
freq="M",
|
29 |
+
)
|
30 |
+
exp_idx = PeriodIndex(["2014-01", "2014-02", "2014-03"], freq="M")
|
31 |
+
|
32 |
+
exp_arr = np.array([2, 2, 1, 0, 2, 0], dtype=np.intp)
|
33 |
+
arr, idx = idx2.factorize(sort=True)
|
34 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
35 |
+
tm.assert_index_equal(idx, exp_idx)
|
36 |
+
|
37 |
+
exp_arr = np.array([0, 0, 1, 2, 0, 2], dtype=np.intp)
|
38 |
+
exp_idx = PeriodIndex(["2014-03", "2014-02", "2014-01"], freq="M")
|
39 |
+
arr, idx = idx2.factorize()
|
40 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
41 |
+
tm.assert_index_equal(idx, exp_idx)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_fillna.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pandas import (
|
2 |
+
Index,
|
3 |
+
NaT,
|
4 |
+
Period,
|
5 |
+
PeriodIndex,
|
6 |
+
)
|
7 |
+
import pandas._testing as tm
|
8 |
+
|
9 |
+
|
10 |
+
class TestFillNA:
|
11 |
+
def test_fillna_period(self):
|
12 |
+
# GH#11343
|
13 |
+
idx = PeriodIndex(["2011-01-01 09:00", NaT, "2011-01-01 11:00"], freq="h")
|
14 |
+
|
15 |
+
exp = PeriodIndex(
|
16 |
+
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"], freq="h"
|
17 |
+
)
|
18 |
+
result = idx.fillna(Period("2011-01-01 10:00", freq="h"))
|
19 |
+
tm.assert_index_equal(result, exp)
|
20 |
+
|
21 |
+
exp = Index(
|
22 |
+
[
|
23 |
+
Period("2011-01-01 09:00", freq="h"),
|
24 |
+
"x",
|
25 |
+
Period("2011-01-01 11:00", freq="h"),
|
26 |
+
],
|
27 |
+
dtype=object,
|
28 |
+
)
|
29 |
+
result = idx.fillna("x")
|
30 |
+
tm.assert_index_equal(result, exp)
|
31 |
+
|
32 |
+
exp = Index(
|
33 |
+
[
|
34 |
+
Period("2011-01-01 09:00", freq="h"),
|
35 |
+
Period("2011-01-01", freq="D"),
|
36 |
+
Period("2011-01-01 11:00", freq="h"),
|
37 |
+
],
|
38 |
+
dtype=object,
|
39 |
+
)
|
40 |
+
result = idx.fillna(Period("2011-01-01", freq="D"))
|
41 |
+
tm.assert_index_equal(result, exp)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_insert.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
NaT,
|
6 |
+
PeriodIndex,
|
7 |
+
period_range,
|
8 |
+
)
|
9 |
+
import pandas._testing as tm
|
10 |
+
|
11 |
+
|
12 |
+
class TestInsert:
|
13 |
+
@pytest.mark.parametrize("na", [np.nan, NaT, None])
|
14 |
+
def test_insert(self, na):
|
15 |
+
# GH#18295 (test missing)
|
16 |
+
expected = PeriodIndex(["2017Q1", NaT, "2017Q2", "2017Q3", "2017Q4"], freq="Q")
|
17 |
+
result = period_range("2017Q1", periods=4, freq="Q").insert(1, na)
|
18 |
+
tm.assert_index_equal(result, expected)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_is_full.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas import PeriodIndex
|
4 |
+
|
5 |
+
|
6 |
+
def test_is_full():
|
7 |
+
index = PeriodIndex([2005, 2007, 2009], freq="Y")
|
8 |
+
assert not index.is_full
|
9 |
+
|
10 |
+
index = PeriodIndex([2005, 2006, 2007], freq="Y")
|
11 |
+
assert index.is_full
|
12 |
+
|
13 |
+
index = PeriodIndex([2005, 2005, 2007], freq="Y")
|
14 |
+
assert not index.is_full
|
15 |
+
|
16 |
+
index = PeriodIndex([2005, 2005, 2006], freq="Y")
|
17 |
+
assert index.is_full
|
18 |
+
|
19 |
+
index = PeriodIndex([2006, 2005, 2005], freq="Y")
|
20 |
+
with pytest.raises(ValueError, match="Index is not monotonic"):
|
21 |
+
index.is_full
|
22 |
+
|
23 |
+
assert index[:0].is_full
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_repeat.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
PeriodIndex,
|
6 |
+
period_range,
|
7 |
+
)
|
8 |
+
import pandas._testing as tm
|
9 |
+
|
10 |
+
|
11 |
+
class TestRepeat:
|
12 |
+
@pytest.mark.parametrize("use_numpy", [True, False])
|
13 |
+
@pytest.mark.parametrize(
|
14 |
+
"index",
|
15 |
+
[
|
16 |
+
period_range("2000-01-01", periods=3, freq="D"),
|
17 |
+
period_range("2001-01-01", periods=3, freq="2D"),
|
18 |
+
PeriodIndex(["2001-01", "NaT", "2003-01"], freq="M"),
|
19 |
+
],
|
20 |
+
)
|
21 |
+
def test_repeat_freqstr(self, index, use_numpy):
|
22 |
+
# GH#10183
|
23 |
+
expected = PeriodIndex([per for per in index for _ in range(3)])
|
24 |
+
result = np.repeat(index, 3) if use_numpy else index.repeat(3)
|
25 |
+
tm.assert_index_equal(result, expected)
|
26 |
+
assert result.freqstr == index.freqstr
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_shift.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
PeriodIndex,
|
6 |
+
period_range,
|
7 |
+
)
|
8 |
+
import pandas._testing as tm
|
9 |
+
|
10 |
+
|
11 |
+
class TestPeriodIndexShift:
|
12 |
+
# ---------------------------------------------------------------
|
13 |
+
# PeriodIndex.shift is used by __add__ and __sub__
|
14 |
+
|
15 |
+
def test_pi_shift_ndarray(self):
|
16 |
+
idx = PeriodIndex(
|
17 |
+
["2011-01", "2011-02", "NaT", "2011-04"], freq="M", name="idx"
|
18 |
+
)
|
19 |
+
result = idx.shift(np.array([1, 2, 3, 4]))
|
20 |
+
expected = PeriodIndex(
|
21 |
+
["2011-02", "2011-04", "NaT", "2011-08"], freq="M", name="idx"
|
22 |
+
)
|
23 |
+
tm.assert_index_equal(result, expected)
|
24 |
+
|
25 |
+
result = idx.shift(np.array([1, -2, 3, -4]))
|
26 |
+
expected = PeriodIndex(
|
27 |
+
["2011-02", "2010-12", "NaT", "2010-12"], freq="M", name="idx"
|
28 |
+
)
|
29 |
+
tm.assert_index_equal(result, expected)
|
30 |
+
|
31 |
+
def test_shift(self):
|
32 |
+
pi1 = period_range(freq="Y", start="1/1/2001", end="12/1/2009")
|
33 |
+
pi2 = period_range(freq="Y", start="1/1/2002", end="12/1/2010")
|
34 |
+
|
35 |
+
tm.assert_index_equal(pi1.shift(0), pi1)
|
36 |
+
|
37 |
+
assert len(pi1) == len(pi2)
|
38 |
+
tm.assert_index_equal(pi1.shift(1), pi2)
|
39 |
+
|
40 |
+
pi1 = period_range(freq="Y", start="1/1/2001", end="12/1/2009")
|
41 |
+
pi2 = period_range(freq="Y", start="1/1/2000", end="12/1/2008")
|
42 |
+
assert len(pi1) == len(pi2)
|
43 |
+
tm.assert_index_equal(pi1.shift(-1), pi2)
|
44 |
+
|
45 |
+
pi1 = period_range(freq="M", start="1/1/2001", end="12/1/2009")
|
46 |
+
pi2 = period_range(freq="M", start="2/1/2001", end="1/1/2010")
|
47 |
+
assert len(pi1) == len(pi2)
|
48 |
+
tm.assert_index_equal(pi1.shift(1), pi2)
|
49 |
+
|
50 |
+
pi1 = period_range(freq="M", start="1/1/2001", end="12/1/2009")
|
51 |
+
pi2 = period_range(freq="M", start="12/1/2000", end="11/1/2009")
|
52 |
+
assert len(pi1) == len(pi2)
|
53 |
+
tm.assert_index_equal(pi1.shift(-1), pi2)
|
54 |
+
|
55 |
+
pi1 = period_range(freq="D", start="1/1/2001", end="12/1/2009")
|
56 |
+
pi2 = period_range(freq="D", start="1/2/2001", end="12/2/2009")
|
57 |
+
assert len(pi1) == len(pi2)
|
58 |
+
tm.assert_index_equal(pi1.shift(1), pi2)
|
59 |
+
|
60 |
+
pi1 = period_range(freq="D", start="1/1/2001", end="12/1/2009")
|
61 |
+
pi2 = period_range(freq="D", start="12/31/2000", end="11/30/2009")
|
62 |
+
assert len(pi1) == len(pi2)
|
63 |
+
tm.assert_index_equal(pi1.shift(-1), pi2)
|
64 |
+
|
65 |
+
def test_shift_corner_cases(self):
|
66 |
+
# GH#9903
|
67 |
+
idx = PeriodIndex([], name="xxx", freq="h")
|
68 |
+
|
69 |
+
msg = "`freq` argument is not supported for PeriodIndex.shift"
|
70 |
+
with pytest.raises(TypeError, match=msg):
|
71 |
+
# period shift doesn't accept freq
|
72 |
+
idx.shift(1, freq="h")
|
73 |
+
|
74 |
+
tm.assert_index_equal(idx.shift(0), idx)
|
75 |
+
tm.assert_index_equal(idx.shift(3), idx)
|
76 |
+
|
77 |
+
idx = PeriodIndex(
|
78 |
+
["2011-01-01 10:00", "2011-01-01 11:00", "2011-01-01 12:00"],
|
79 |
+
name="xxx",
|
80 |
+
freq="h",
|
81 |
+
)
|
82 |
+
tm.assert_index_equal(idx.shift(0), idx)
|
83 |
+
exp = PeriodIndex(
|
84 |
+
["2011-01-01 13:00", "2011-01-01 14:00", "2011-01-01 15:00"],
|
85 |
+
name="xxx",
|
86 |
+
freq="h",
|
87 |
+
)
|
88 |
+
tm.assert_index_equal(idx.shift(3), exp)
|
89 |
+
exp = PeriodIndex(
|
90 |
+
["2011-01-01 07:00", "2011-01-01 08:00", "2011-01-01 09:00"],
|
91 |
+
name="xxx",
|
92 |
+
freq="h",
|
93 |
+
)
|
94 |
+
tm.assert_index_equal(idx.shift(-3), exp)
|
95 |
+
|
96 |
+
def test_shift_nat(self):
|
97 |
+
idx = PeriodIndex(
|
98 |
+
["2011-01", "2011-02", "NaT", "2011-04"], freq="M", name="idx"
|
99 |
+
)
|
100 |
+
result = idx.shift(1)
|
101 |
+
expected = PeriodIndex(
|
102 |
+
["2011-02", "2011-03", "NaT", "2011-05"], freq="M", name="idx"
|
103 |
+
)
|
104 |
+
tm.assert_index_equal(result, expected)
|
105 |
+
assert result.name == expected.name
|
106 |
+
|
107 |
+
def test_shift_gh8083(self):
|
108 |
+
# test shift for PeriodIndex
|
109 |
+
# GH#8083
|
110 |
+
drange = period_range("20130101", periods=5, freq="D")
|
111 |
+
result = drange.shift(1)
|
112 |
+
expected = PeriodIndex(
|
113 |
+
["2013-01-02", "2013-01-03", "2013-01-04", "2013-01-05", "2013-01-06"],
|
114 |
+
freq="D",
|
115 |
+
)
|
116 |
+
tm.assert_index_equal(result, expected)
|
117 |
+
|
118 |
+
def test_shift_periods(self):
|
119 |
+
# GH #22458 : argument 'n' was deprecated in favor of 'periods'
|
120 |
+
idx = period_range(freq="Y", start="1/1/2001", end="12/1/2009")
|
121 |
+
tm.assert_index_equal(idx.shift(periods=0), idx)
|
122 |
+
tm.assert_index_equal(idx.shift(0), idx)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/methods/test_to_timestamp.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
from pandas import (
|
7 |
+
DatetimeIndex,
|
8 |
+
NaT,
|
9 |
+
PeriodIndex,
|
10 |
+
Timedelta,
|
11 |
+
Timestamp,
|
12 |
+
date_range,
|
13 |
+
period_range,
|
14 |
+
)
|
15 |
+
import pandas._testing as tm
|
16 |
+
|
17 |
+
|
18 |
+
class TestToTimestamp:
|
19 |
+
def test_to_timestamp_non_contiguous(self):
|
20 |
+
# GH#44100
|
21 |
+
dti = date_range("2021-10-18", periods=9, freq="D")
|
22 |
+
pi = dti.to_period()
|
23 |
+
|
24 |
+
result = pi[::2].to_timestamp()
|
25 |
+
expected = dti[::2]
|
26 |
+
tm.assert_index_equal(result, expected)
|
27 |
+
|
28 |
+
result = pi._data[::2].to_timestamp()
|
29 |
+
expected = dti._data[::2]
|
30 |
+
# TODO: can we get the freq to round-trip?
|
31 |
+
tm.assert_datetime_array_equal(result, expected, check_freq=False)
|
32 |
+
|
33 |
+
result = pi[::-1].to_timestamp()
|
34 |
+
expected = dti[::-1]
|
35 |
+
tm.assert_index_equal(result, expected)
|
36 |
+
|
37 |
+
result = pi._data[::-1].to_timestamp()
|
38 |
+
expected = dti._data[::-1]
|
39 |
+
tm.assert_datetime_array_equal(result, expected, check_freq=False)
|
40 |
+
|
41 |
+
result = pi[::2][::-1].to_timestamp()
|
42 |
+
expected = dti[::2][::-1]
|
43 |
+
tm.assert_index_equal(result, expected)
|
44 |
+
|
45 |
+
result = pi._data[::2][::-1].to_timestamp()
|
46 |
+
expected = dti._data[::2][::-1]
|
47 |
+
tm.assert_datetime_array_equal(result, expected, check_freq=False)
|
48 |
+
|
49 |
+
def test_to_timestamp_freq(self):
|
50 |
+
idx = period_range("2017", periods=12, freq="Y-DEC")
|
51 |
+
result = idx.to_timestamp()
|
52 |
+
expected = date_range("2017", periods=12, freq="YS-JAN")
|
53 |
+
tm.assert_index_equal(result, expected)
|
54 |
+
|
55 |
+
def test_to_timestamp_pi_nat(self):
|
56 |
+
# GH#7228
|
57 |
+
index = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx")
|
58 |
+
|
59 |
+
result = index.to_timestamp("D")
|
60 |
+
expected = DatetimeIndex(
|
61 |
+
[NaT, datetime(2011, 1, 1), datetime(2011, 2, 1)],
|
62 |
+
dtype="M8[ns]",
|
63 |
+
name="idx",
|
64 |
+
)
|
65 |
+
tm.assert_index_equal(result, expected)
|
66 |
+
assert result.name == "idx"
|
67 |
+
|
68 |
+
result2 = result.to_period(freq="M")
|
69 |
+
tm.assert_index_equal(result2, index)
|
70 |
+
assert result2.name == "idx"
|
71 |
+
|
72 |
+
result3 = result.to_period(freq="3M")
|
73 |
+
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx")
|
74 |
+
tm.assert_index_equal(result3, exp)
|
75 |
+
assert result3.freqstr == "3M"
|
76 |
+
|
77 |
+
msg = "Frequency must be positive, because it represents span: -2Y"
|
78 |
+
with pytest.raises(ValueError, match=msg):
|
79 |
+
result.to_period(freq="-2Y")
|
80 |
+
|
81 |
+
def test_to_timestamp_preserve_name(self):
|
82 |
+
index = period_range(freq="Y", start="1/1/2001", end="12/1/2009", name="foo")
|
83 |
+
assert index.name == "foo"
|
84 |
+
|
85 |
+
conv = index.to_timestamp("D")
|
86 |
+
assert conv.name == "foo"
|
87 |
+
|
88 |
+
def test_to_timestamp_quarterly_bug(self):
|
89 |
+
years = np.arange(1960, 2000).repeat(4)
|
90 |
+
quarters = np.tile(list(range(1, 5)), 40)
|
91 |
+
|
92 |
+
pindex = PeriodIndex.from_fields(year=years, quarter=quarters)
|
93 |
+
|
94 |
+
stamps = pindex.to_timestamp("D", "end")
|
95 |
+
expected = DatetimeIndex([x.to_timestamp("D", "end") for x in pindex])
|
96 |
+
tm.assert_index_equal(stamps, expected)
|
97 |
+
assert stamps.freq == expected.freq
|
98 |
+
|
99 |
+
def test_to_timestamp_pi_mult(self):
|
100 |
+
idx = PeriodIndex(["2011-01", "NaT", "2011-02"], freq="2M", name="idx")
|
101 |
+
|
102 |
+
result = idx.to_timestamp()
|
103 |
+
expected = DatetimeIndex(
|
104 |
+
["2011-01-01", "NaT", "2011-02-01"], dtype="M8[ns]", name="idx"
|
105 |
+
)
|
106 |
+
tm.assert_index_equal(result, expected)
|
107 |
+
|
108 |
+
result = idx.to_timestamp(how="E")
|
109 |
+
expected = DatetimeIndex(
|
110 |
+
["2011-02-28", "NaT", "2011-03-31"], dtype="M8[ns]", name="idx"
|
111 |
+
)
|
112 |
+
expected = expected + Timedelta(1, "D") - Timedelta(1, "ns")
|
113 |
+
tm.assert_index_equal(result, expected)
|
114 |
+
|
115 |
+
def test_to_timestamp_pi_combined(self):
|
116 |
+
idx = period_range(start="2011", periods=2, freq="1D1h", name="idx")
|
117 |
+
|
118 |
+
result = idx.to_timestamp()
|
119 |
+
expected = DatetimeIndex(
|
120 |
+
["2011-01-01 00:00", "2011-01-02 01:00"], dtype="M8[ns]", name="idx"
|
121 |
+
)
|
122 |
+
tm.assert_index_equal(result, expected)
|
123 |
+
|
124 |
+
result = idx.to_timestamp(how="E")
|
125 |
+
expected = DatetimeIndex(
|
126 |
+
["2011-01-02 00:59:59", "2011-01-03 01:59:59"], name="idx", dtype="M8[ns]"
|
127 |
+
)
|
128 |
+
expected = expected + Timedelta(1, "s") - Timedelta(1, "ns")
|
129 |
+
tm.assert_index_equal(result, expected)
|
130 |
+
|
131 |
+
result = idx.to_timestamp(how="E", freq="h")
|
132 |
+
expected = DatetimeIndex(
|
133 |
+
["2011-01-02 00:00", "2011-01-03 01:00"], dtype="M8[ns]", name="idx"
|
134 |
+
)
|
135 |
+
expected = expected + Timedelta(1, "h") - Timedelta(1, "ns")
|
136 |
+
tm.assert_index_equal(result, expected)
|
137 |
+
|
138 |
+
def test_to_timestamp_1703(self):
|
139 |
+
index = period_range("1/1/2012", periods=4, freq="D")
|
140 |
+
|
141 |
+
result = index.to_timestamp()
|
142 |
+
assert result[0] == Timestamp("1/1/2012")
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/test_constructors.py
ADDED
@@ -0,0 +1,691 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas._libs.tslibs.period import IncompatibleFrequency
|
5 |
+
|
6 |
+
from pandas.core.dtypes.dtypes import PeriodDtype
|
7 |
+
|
8 |
+
from pandas import (
|
9 |
+
Index,
|
10 |
+
NaT,
|
11 |
+
Period,
|
12 |
+
PeriodIndex,
|
13 |
+
Series,
|
14 |
+
date_range,
|
15 |
+
offsets,
|
16 |
+
period_range,
|
17 |
+
)
|
18 |
+
import pandas._testing as tm
|
19 |
+
from pandas.core.arrays import PeriodArray
|
20 |
+
|
21 |
+
|
22 |
+
class TestPeriodIndexDisallowedFreqs:
|
23 |
+
@pytest.mark.parametrize(
|
24 |
+
"freq,freq_depr",
|
25 |
+
[
|
26 |
+
("2M", "2ME"),
|
27 |
+
("2Q-MAR", "2QE-MAR"),
|
28 |
+
("2Y-FEB", "2YE-FEB"),
|
29 |
+
("2M", "2me"),
|
30 |
+
("2Q-MAR", "2qe-MAR"),
|
31 |
+
("2Y-FEB", "2yE-feb"),
|
32 |
+
],
|
33 |
+
)
|
34 |
+
def test_period_index_offsets_frequency_error_message(self, freq, freq_depr):
|
35 |
+
# GH#52064
|
36 |
+
msg = f"for Period, please use '{freq[1:]}' instead of '{freq_depr[1:]}'"
|
37 |
+
|
38 |
+
with pytest.raises(ValueError, match=msg):
|
39 |
+
PeriodIndex(["2020-01-01", "2020-01-02"], freq=freq_depr)
|
40 |
+
|
41 |
+
with pytest.raises(ValueError, match=msg):
|
42 |
+
period_range(start="2020-01-01", end="2020-01-02", freq=freq_depr)
|
43 |
+
|
44 |
+
@pytest.mark.parametrize("freq_depr", ["2SME", "2sme", "2CBME", "2BYE", "2Bye"])
|
45 |
+
def test_period_index_frequency_invalid_freq(self, freq_depr):
|
46 |
+
# GH#9586
|
47 |
+
msg = f"Invalid frequency: {freq_depr[1:]}"
|
48 |
+
|
49 |
+
with pytest.raises(ValueError, match=msg):
|
50 |
+
period_range("2020-01", "2020-05", freq=freq_depr)
|
51 |
+
with pytest.raises(ValueError, match=msg):
|
52 |
+
PeriodIndex(["2020-01", "2020-05"], freq=freq_depr)
|
53 |
+
|
54 |
+
@pytest.mark.parametrize("freq", ["2BQE-SEP", "2BYE-MAR", "2BME"])
|
55 |
+
def test_period_index_from_datetime_index_invalid_freq(self, freq):
|
56 |
+
# GH#56899
|
57 |
+
msg = f"Invalid frequency: {freq[1:]}"
|
58 |
+
|
59 |
+
rng = date_range("01-Jan-2012", periods=8, freq=freq)
|
60 |
+
with pytest.raises(ValueError, match=msg):
|
61 |
+
rng.to_period()
|
62 |
+
|
63 |
+
|
64 |
+
class TestPeriodIndex:
|
65 |
+
def test_from_ordinals(self):
|
66 |
+
Period(ordinal=-1000, freq="Y")
|
67 |
+
Period(ordinal=0, freq="Y")
|
68 |
+
|
69 |
+
msg = "The 'ordinal' keyword in PeriodIndex is deprecated"
|
70 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
71 |
+
idx1 = PeriodIndex(ordinal=[-1, 0, 1], freq="Y")
|
72 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
73 |
+
idx2 = PeriodIndex(ordinal=np.array([-1, 0, 1]), freq="Y")
|
74 |
+
tm.assert_index_equal(idx1, idx2)
|
75 |
+
|
76 |
+
alt1 = PeriodIndex.from_ordinals([-1, 0, 1], freq="Y")
|
77 |
+
tm.assert_index_equal(alt1, idx1)
|
78 |
+
|
79 |
+
alt2 = PeriodIndex.from_ordinals(np.array([-1, 0, 1]), freq="Y")
|
80 |
+
tm.assert_index_equal(alt2, idx2)
|
81 |
+
|
82 |
+
def test_keyword_mismatch(self):
|
83 |
+
# GH#55961 we should get exactly one of data/ordinals/**fields
|
84 |
+
per = Period("2016-01-01", "D")
|
85 |
+
depr_msg1 = "The 'ordinal' keyword in PeriodIndex is deprecated"
|
86 |
+
depr_msg2 = "Constructing PeriodIndex from fields is deprecated"
|
87 |
+
|
88 |
+
err_msg1 = "Cannot pass both data and ordinal"
|
89 |
+
with pytest.raises(ValueError, match=err_msg1):
|
90 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg1):
|
91 |
+
PeriodIndex(data=[per], ordinal=[per.ordinal], freq=per.freq)
|
92 |
+
|
93 |
+
err_msg2 = "Cannot pass both data and fields"
|
94 |
+
with pytest.raises(ValueError, match=err_msg2):
|
95 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg2):
|
96 |
+
PeriodIndex(data=[per], year=[per.year], freq=per.freq)
|
97 |
+
|
98 |
+
err_msg3 = "Cannot pass both ordinal and fields"
|
99 |
+
with pytest.raises(ValueError, match=err_msg3):
|
100 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg2):
|
101 |
+
PeriodIndex(ordinal=[per.ordinal], year=[per.year], freq=per.freq)
|
102 |
+
|
103 |
+
def test_construction_base_constructor(self):
|
104 |
+
# GH 13664
|
105 |
+
arr = [Period("2011-01", freq="M"), NaT, Period("2011-03", freq="M")]
|
106 |
+
tm.assert_index_equal(Index(arr), PeriodIndex(arr))
|
107 |
+
tm.assert_index_equal(Index(np.array(arr)), PeriodIndex(np.array(arr)))
|
108 |
+
|
109 |
+
arr = [np.nan, NaT, Period("2011-03", freq="M")]
|
110 |
+
tm.assert_index_equal(Index(arr), PeriodIndex(arr))
|
111 |
+
tm.assert_index_equal(Index(np.array(arr)), PeriodIndex(np.array(arr)))
|
112 |
+
|
113 |
+
arr = [Period("2011-01", freq="M"), NaT, Period("2011-03", freq="D")]
|
114 |
+
tm.assert_index_equal(Index(arr), Index(arr, dtype=object))
|
115 |
+
|
116 |
+
tm.assert_index_equal(Index(np.array(arr)), Index(np.array(arr), dtype=object))
|
117 |
+
|
118 |
+
def test_base_constructor_with_period_dtype(self):
|
119 |
+
dtype = PeriodDtype("D")
|
120 |
+
values = ["2011-01-01", "2012-03-04", "2014-05-01"]
|
121 |
+
result = Index(values, dtype=dtype)
|
122 |
+
|
123 |
+
expected = PeriodIndex(values, dtype=dtype)
|
124 |
+
tm.assert_index_equal(result, expected)
|
125 |
+
|
126 |
+
@pytest.mark.parametrize(
|
127 |
+
"values_constructor", [list, np.array, PeriodIndex, PeriodArray._from_sequence]
|
128 |
+
)
|
129 |
+
def test_index_object_dtype(self, values_constructor):
|
130 |
+
# Index(periods, dtype=object) is an Index (not an PeriodIndex)
|
131 |
+
periods = [
|
132 |
+
Period("2011-01", freq="M"),
|
133 |
+
NaT,
|
134 |
+
Period("2011-03", freq="M"),
|
135 |
+
]
|
136 |
+
values = values_constructor(periods)
|
137 |
+
result = Index(values, dtype=object)
|
138 |
+
|
139 |
+
assert type(result) is Index
|
140 |
+
tm.assert_numpy_array_equal(result.values, np.array(values))
|
141 |
+
|
142 |
+
def test_constructor_use_start_freq(self):
|
143 |
+
# GH #1118
|
144 |
+
msg1 = "Period with BDay freq is deprecated"
|
145 |
+
with tm.assert_produces_warning(FutureWarning, match=msg1):
|
146 |
+
p = Period("4/2/2012", freq="B")
|
147 |
+
msg2 = r"PeriodDtype\[B\] is deprecated"
|
148 |
+
with tm.assert_produces_warning(FutureWarning, match=msg2):
|
149 |
+
expected = period_range(start="4/2/2012", periods=10, freq="B")
|
150 |
+
|
151 |
+
with tm.assert_produces_warning(FutureWarning, match=msg2):
|
152 |
+
index = period_range(start=p, periods=10)
|
153 |
+
tm.assert_index_equal(index, expected)
|
154 |
+
|
155 |
+
def test_constructor_field_arrays(self):
|
156 |
+
# GH #1264
|
157 |
+
|
158 |
+
years = np.arange(1990, 2010).repeat(4)[2:-2]
|
159 |
+
quarters = np.tile(np.arange(1, 5), 20)[2:-2]
|
160 |
+
|
161 |
+
depr_msg = "Constructing PeriodIndex from fields is deprecated"
|
162 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
163 |
+
index = PeriodIndex(year=years, quarter=quarters, freq="Q-DEC")
|
164 |
+
expected = period_range("1990Q3", "2009Q2", freq="Q-DEC")
|
165 |
+
tm.assert_index_equal(index, expected)
|
166 |
+
|
167 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
168 |
+
index2 = PeriodIndex(year=years, quarter=quarters, freq="2Q-DEC")
|
169 |
+
tm.assert_numpy_array_equal(index.asi8, index2.asi8)
|
170 |
+
|
171 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
172 |
+
index = PeriodIndex(year=years, quarter=quarters)
|
173 |
+
tm.assert_index_equal(index, expected)
|
174 |
+
|
175 |
+
years = [2007, 2007, 2007]
|
176 |
+
months = [1, 2]
|
177 |
+
|
178 |
+
msg = "Mismatched Period array lengths"
|
179 |
+
with pytest.raises(ValueError, match=msg):
|
180 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
181 |
+
PeriodIndex(year=years, month=months, freq="M")
|
182 |
+
with pytest.raises(ValueError, match=msg):
|
183 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
184 |
+
PeriodIndex(year=years, month=months, freq="2M")
|
185 |
+
|
186 |
+
years = [2007, 2007, 2007]
|
187 |
+
months = [1, 2, 3]
|
188 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
189 |
+
idx = PeriodIndex(year=years, month=months, freq="M")
|
190 |
+
exp = period_range("2007-01", periods=3, freq="M")
|
191 |
+
tm.assert_index_equal(idx, exp)
|
192 |
+
|
193 |
+
def test_constructor_nano(self):
|
194 |
+
idx = period_range(
|
195 |
+
start=Period(ordinal=1, freq="ns"),
|
196 |
+
end=Period(ordinal=4, freq="ns"),
|
197 |
+
freq="ns",
|
198 |
+
)
|
199 |
+
exp = PeriodIndex(
|
200 |
+
[
|
201 |
+
Period(ordinal=1, freq="ns"),
|
202 |
+
Period(ordinal=2, freq="ns"),
|
203 |
+
Period(ordinal=3, freq="ns"),
|
204 |
+
Period(ordinal=4, freq="ns"),
|
205 |
+
],
|
206 |
+
freq="ns",
|
207 |
+
)
|
208 |
+
tm.assert_index_equal(idx, exp)
|
209 |
+
|
210 |
+
def test_constructor_arrays_negative_year(self):
|
211 |
+
years = np.arange(1960, 2000, dtype=np.int64).repeat(4)
|
212 |
+
quarters = np.tile(np.array([1, 2, 3, 4], dtype=np.int64), 40)
|
213 |
+
|
214 |
+
msg = "Constructing PeriodIndex from fields is deprecated"
|
215 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
216 |
+
pindex = PeriodIndex(year=years, quarter=quarters)
|
217 |
+
|
218 |
+
tm.assert_index_equal(pindex.year, Index(years))
|
219 |
+
tm.assert_index_equal(pindex.quarter, Index(quarters))
|
220 |
+
|
221 |
+
alt = PeriodIndex.from_fields(year=years, quarter=quarters)
|
222 |
+
tm.assert_index_equal(alt, pindex)
|
223 |
+
|
224 |
+
def test_constructor_invalid_quarters(self):
|
225 |
+
depr_msg = "Constructing PeriodIndex from fields is deprecated"
|
226 |
+
msg = "Quarter must be 1 <= q <= 4"
|
227 |
+
with pytest.raises(ValueError, match=msg):
|
228 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
229 |
+
PeriodIndex(
|
230 |
+
year=range(2000, 2004), quarter=list(range(4)), freq="Q-DEC"
|
231 |
+
)
|
232 |
+
|
233 |
+
def test_period_range_fractional_period(self):
|
234 |
+
msg = "Non-integer 'periods' in pd.date_range, pd.timedelta_range"
|
235 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
236 |
+
result = period_range("2007-01", periods=10.5, freq="M")
|
237 |
+
exp = period_range("2007-01", periods=10, freq="M")
|
238 |
+
tm.assert_index_equal(result, exp)
|
239 |
+
|
240 |
+
def test_constructor_with_without_freq(self):
|
241 |
+
# GH53687
|
242 |
+
start = Period("2002-01-01 00:00", freq="30min")
|
243 |
+
exp = period_range(start=start, periods=5, freq=start.freq)
|
244 |
+
result = period_range(start=start, periods=5)
|
245 |
+
tm.assert_index_equal(exp, result)
|
246 |
+
|
247 |
+
def test_constructor_fromarraylike(self):
|
248 |
+
idx = period_range("2007-01", periods=20, freq="M")
|
249 |
+
|
250 |
+
# values is an array of Period, thus can retrieve freq
|
251 |
+
tm.assert_index_equal(PeriodIndex(idx.values), idx)
|
252 |
+
tm.assert_index_equal(PeriodIndex(list(idx.values)), idx)
|
253 |
+
|
254 |
+
msg = "freq not specified and cannot be inferred"
|
255 |
+
with pytest.raises(ValueError, match=msg):
|
256 |
+
PeriodIndex(idx.asi8)
|
257 |
+
with pytest.raises(ValueError, match=msg):
|
258 |
+
PeriodIndex(list(idx.asi8))
|
259 |
+
|
260 |
+
msg = "'Period' object is not iterable"
|
261 |
+
with pytest.raises(TypeError, match=msg):
|
262 |
+
PeriodIndex(data=Period("2007", freq="Y"))
|
263 |
+
|
264 |
+
result = PeriodIndex(iter(idx))
|
265 |
+
tm.assert_index_equal(result, idx)
|
266 |
+
|
267 |
+
result = PeriodIndex(idx)
|
268 |
+
tm.assert_index_equal(result, idx)
|
269 |
+
|
270 |
+
result = PeriodIndex(idx, freq="M")
|
271 |
+
tm.assert_index_equal(result, idx)
|
272 |
+
|
273 |
+
result = PeriodIndex(idx, freq=offsets.MonthEnd())
|
274 |
+
tm.assert_index_equal(result, idx)
|
275 |
+
assert result.freq == "ME"
|
276 |
+
|
277 |
+
result = PeriodIndex(idx, freq="2M")
|
278 |
+
tm.assert_index_equal(result, idx.asfreq("2M"))
|
279 |
+
assert result.freq == "2ME"
|
280 |
+
|
281 |
+
result = PeriodIndex(idx, freq=offsets.MonthEnd(2))
|
282 |
+
tm.assert_index_equal(result, idx.asfreq("2M"))
|
283 |
+
assert result.freq == "2ME"
|
284 |
+
|
285 |
+
result = PeriodIndex(idx, freq="D")
|
286 |
+
exp = idx.asfreq("D", "e")
|
287 |
+
tm.assert_index_equal(result, exp)
|
288 |
+
|
289 |
+
def test_constructor_datetime64arr(self):
|
290 |
+
vals = np.arange(100000, 100000 + 10000, 100, dtype=np.int64)
|
291 |
+
vals = vals.view(np.dtype("M8[us]"))
|
292 |
+
|
293 |
+
pi = PeriodIndex(vals, freq="D")
|
294 |
+
|
295 |
+
expected = PeriodIndex(vals.astype("M8[ns]"), freq="D")
|
296 |
+
tm.assert_index_equal(pi, expected)
|
297 |
+
|
298 |
+
@pytest.mark.parametrize("box", [None, "series", "index"])
|
299 |
+
def test_constructor_datetime64arr_ok(self, box):
|
300 |
+
# https://github.com/pandas-dev/pandas/issues/23438
|
301 |
+
data = date_range("2017", periods=4, freq="ME")
|
302 |
+
if box is None:
|
303 |
+
data = data._values
|
304 |
+
elif box == "series":
|
305 |
+
data = Series(data)
|
306 |
+
|
307 |
+
result = PeriodIndex(data, freq="D")
|
308 |
+
expected = PeriodIndex(
|
309 |
+
["2017-01-31", "2017-02-28", "2017-03-31", "2017-04-30"], freq="D"
|
310 |
+
)
|
311 |
+
tm.assert_index_equal(result, expected)
|
312 |
+
|
313 |
+
def test_constructor_dtype(self):
|
314 |
+
# passing a dtype with a tz should localize
|
315 |
+
idx = PeriodIndex(["2013-01", "2013-03"], dtype="period[M]")
|
316 |
+
exp = PeriodIndex(["2013-01", "2013-03"], freq="M")
|
317 |
+
tm.assert_index_equal(idx, exp)
|
318 |
+
assert idx.dtype == "period[M]"
|
319 |
+
|
320 |
+
idx = PeriodIndex(["2013-01-05", "2013-03-05"], dtype="period[3D]")
|
321 |
+
exp = PeriodIndex(["2013-01-05", "2013-03-05"], freq="3D")
|
322 |
+
tm.assert_index_equal(idx, exp)
|
323 |
+
assert idx.dtype == "period[3D]"
|
324 |
+
|
325 |
+
# if we already have a freq and its not the same, then asfreq
|
326 |
+
# (not changed)
|
327 |
+
idx = PeriodIndex(["2013-01-01", "2013-01-02"], freq="D")
|
328 |
+
|
329 |
+
res = PeriodIndex(idx, dtype="period[M]")
|
330 |
+
exp = PeriodIndex(["2013-01", "2013-01"], freq="M")
|
331 |
+
tm.assert_index_equal(res, exp)
|
332 |
+
assert res.dtype == "period[M]"
|
333 |
+
|
334 |
+
res = PeriodIndex(idx, freq="M")
|
335 |
+
tm.assert_index_equal(res, exp)
|
336 |
+
assert res.dtype == "period[M]"
|
337 |
+
|
338 |
+
msg = "specified freq and dtype are different"
|
339 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
340 |
+
PeriodIndex(["2011-01"], freq="M", dtype="period[D]")
|
341 |
+
|
342 |
+
def test_constructor_empty(self):
|
343 |
+
idx = PeriodIndex([], freq="M")
|
344 |
+
assert isinstance(idx, PeriodIndex)
|
345 |
+
assert len(idx) == 0
|
346 |
+
assert idx.freq == "ME"
|
347 |
+
|
348 |
+
with pytest.raises(ValueError, match="freq not specified"):
|
349 |
+
PeriodIndex([])
|
350 |
+
|
351 |
+
def test_constructor_pi_nat(self):
|
352 |
+
idx = PeriodIndex(
|
353 |
+
[Period("2011-01", freq="M"), NaT, Period("2011-01", freq="M")]
|
354 |
+
)
|
355 |
+
exp = PeriodIndex(["2011-01", "NaT", "2011-01"], freq="M")
|
356 |
+
tm.assert_index_equal(idx, exp)
|
357 |
+
|
358 |
+
idx = PeriodIndex(
|
359 |
+
np.array([Period("2011-01", freq="M"), NaT, Period("2011-01", freq="M")])
|
360 |
+
)
|
361 |
+
tm.assert_index_equal(idx, exp)
|
362 |
+
|
363 |
+
idx = PeriodIndex(
|
364 |
+
[NaT, NaT, Period("2011-01", freq="M"), Period("2011-01", freq="M")]
|
365 |
+
)
|
366 |
+
exp = PeriodIndex(["NaT", "NaT", "2011-01", "2011-01"], freq="M")
|
367 |
+
tm.assert_index_equal(idx, exp)
|
368 |
+
|
369 |
+
idx = PeriodIndex(
|
370 |
+
np.array(
|
371 |
+
[NaT, NaT, Period("2011-01", freq="M"), Period("2011-01", freq="M")]
|
372 |
+
)
|
373 |
+
)
|
374 |
+
tm.assert_index_equal(idx, exp)
|
375 |
+
|
376 |
+
idx = PeriodIndex([NaT, NaT, "2011-01", "2011-01"], freq="M")
|
377 |
+
tm.assert_index_equal(idx, exp)
|
378 |
+
|
379 |
+
with pytest.raises(ValueError, match="freq not specified"):
|
380 |
+
PeriodIndex([NaT, NaT])
|
381 |
+
|
382 |
+
with pytest.raises(ValueError, match="freq not specified"):
|
383 |
+
PeriodIndex(np.array([NaT, NaT]))
|
384 |
+
|
385 |
+
with pytest.raises(ValueError, match="freq not specified"):
|
386 |
+
PeriodIndex(["NaT", "NaT"])
|
387 |
+
|
388 |
+
with pytest.raises(ValueError, match="freq not specified"):
|
389 |
+
PeriodIndex(np.array(["NaT", "NaT"]))
|
390 |
+
|
391 |
+
def test_constructor_incompat_freq(self):
|
392 |
+
msg = "Input has different freq=D from PeriodIndex\\(freq=M\\)"
|
393 |
+
|
394 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
395 |
+
PeriodIndex([Period("2011-01", freq="M"), NaT, Period("2011-01", freq="D")])
|
396 |
+
|
397 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
398 |
+
PeriodIndex(
|
399 |
+
np.array(
|
400 |
+
[Period("2011-01", freq="M"), NaT, Period("2011-01", freq="D")]
|
401 |
+
)
|
402 |
+
)
|
403 |
+
|
404 |
+
# first element is NaT
|
405 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
406 |
+
PeriodIndex([NaT, Period("2011-01", freq="M"), Period("2011-01", freq="D")])
|
407 |
+
|
408 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
409 |
+
PeriodIndex(
|
410 |
+
np.array(
|
411 |
+
[NaT, Period("2011-01", freq="M"), Period("2011-01", freq="D")]
|
412 |
+
)
|
413 |
+
)
|
414 |
+
|
415 |
+
def test_constructor_mixed(self):
|
416 |
+
idx = PeriodIndex(["2011-01", NaT, Period("2011-01", freq="M")])
|
417 |
+
exp = PeriodIndex(["2011-01", "NaT", "2011-01"], freq="M")
|
418 |
+
tm.assert_index_equal(idx, exp)
|
419 |
+
|
420 |
+
idx = PeriodIndex(["NaT", NaT, Period("2011-01", freq="M")])
|
421 |
+
exp = PeriodIndex(["NaT", "NaT", "2011-01"], freq="M")
|
422 |
+
tm.assert_index_equal(idx, exp)
|
423 |
+
|
424 |
+
idx = PeriodIndex([Period("2011-01-01", freq="D"), NaT, "2012-01-01"])
|
425 |
+
exp = PeriodIndex(["2011-01-01", "NaT", "2012-01-01"], freq="D")
|
426 |
+
tm.assert_index_equal(idx, exp)
|
427 |
+
|
428 |
+
@pytest.mark.parametrize("floats", [[1.1, 2.1], np.array([1.1, 2.1])])
|
429 |
+
def test_constructor_floats(self, floats):
|
430 |
+
msg = "PeriodIndex does not allow floating point in construction"
|
431 |
+
with pytest.raises(TypeError, match=msg):
|
432 |
+
PeriodIndex(floats)
|
433 |
+
|
434 |
+
def test_constructor_year_and_quarter(self):
|
435 |
+
year = Series([2001, 2002, 2003])
|
436 |
+
quarter = year - 2000
|
437 |
+
msg = "Constructing PeriodIndex from fields is deprecated"
|
438 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
439 |
+
idx = PeriodIndex(year=year, quarter=quarter)
|
440 |
+
strs = [f"{t[0]:d}Q{t[1]:d}" for t in zip(quarter, year)]
|
441 |
+
lops = list(map(Period, strs))
|
442 |
+
p = PeriodIndex(lops)
|
443 |
+
tm.assert_index_equal(p, idx)
|
444 |
+
|
445 |
+
def test_constructor_freq_mult(self):
|
446 |
+
# GH #7811
|
447 |
+
pidx = period_range(start="2014-01", freq="2M", periods=4)
|
448 |
+
expected = PeriodIndex(["2014-01", "2014-03", "2014-05", "2014-07"], freq="2M")
|
449 |
+
tm.assert_index_equal(pidx, expected)
|
450 |
+
|
451 |
+
pidx = period_range(start="2014-01-02", end="2014-01-15", freq="3D")
|
452 |
+
expected = PeriodIndex(
|
453 |
+
["2014-01-02", "2014-01-05", "2014-01-08", "2014-01-11", "2014-01-14"],
|
454 |
+
freq="3D",
|
455 |
+
)
|
456 |
+
tm.assert_index_equal(pidx, expected)
|
457 |
+
|
458 |
+
pidx = period_range(end="2014-01-01 17:00", freq="4h", periods=3)
|
459 |
+
expected = PeriodIndex(
|
460 |
+
["2014-01-01 09:00", "2014-01-01 13:00", "2014-01-01 17:00"], freq="4h"
|
461 |
+
)
|
462 |
+
tm.assert_index_equal(pidx, expected)
|
463 |
+
|
464 |
+
msg = "Frequency must be positive, because it represents span: -1M"
|
465 |
+
with pytest.raises(ValueError, match=msg):
|
466 |
+
PeriodIndex(["2011-01"], freq="-1M")
|
467 |
+
|
468 |
+
msg = "Frequency must be positive, because it represents span: 0M"
|
469 |
+
with pytest.raises(ValueError, match=msg):
|
470 |
+
PeriodIndex(["2011-01"], freq="0M")
|
471 |
+
|
472 |
+
msg = "Frequency must be positive, because it represents span: 0M"
|
473 |
+
with pytest.raises(ValueError, match=msg):
|
474 |
+
period_range("2011-01", periods=3, freq="0M")
|
475 |
+
|
476 |
+
@pytest.mark.parametrize(
|
477 |
+
"freq_offset, freq_period",
|
478 |
+
[
|
479 |
+
("YE", "Y"),
|
480 |
+
("ME", "M"),
|
481 |
+
("D", "D"),
|
482 |
+
("min", "min"),
|
483 |
+
("s", "s"),
|
484 |
+
],
|
485 |
+
)
|
486 |
+
@pytest.mark.parametrize("mult", [1, 2, 3, 4, 5])
|
487 |
+
def test_constructor_freq_mult_dti_compat(self, mult, freq_offset, freq_period):
|
488 |
+
freqstr_offset = str(mult) + freq_offset
|
489 |
+
freqstr_period = str(mult) + freq_period
|
490 |
+
pidx = period_range(start="2014-04-01", freq=freqstr_period, periods=10)
|
491 |
+
expected = date_range(
|
492 |
+
start="2014-04-01", freq=freqstr_offset, periods=10
|
493 |
+
).to_period(freqstr_period)
|
494 |
+
tm.assert_index_equal(pidx, expected)
|
495 |
+
|
496 |
+
@pytest.mark.parametrize("mult", [1, 2, 3, 4, 5])
|
497 |
+
def test_constructor_freq_mult_dti_compat_month(self, mult):
|
498 |
+
pidx = period_range(start="2014-04-01", freq=f"{mult}M", periods=10)
|
499 |
+
expected = date_range(
|
500 |
+
start="2014-04-01", freq=f"{mult}ME", periods=10
|
501 |
+
).to_period(f"{mult}M")
|
502 |
+
tm.assert_index_equal(pidx, expected)
|
503 |
+
|
504 |
+
def test_constructor_freq_combined(self):
|
505 |
+
for freq in ["1D1h", "1h1D"]:
|
506 |
+
pidx = PeriodIndex(["2016-01-01", "2016-01-02"], freq=freq)
|
507 |
+
expected = PeriodIndex(["2016-01-01 00:00", "2016-01-02 00:00"], freq="25h")
|
508 |
+
for freq in ["1D1h", "1h1D"]:
|
509 |
+
pidx = period_range(start="2016-01-01", periods=2, freq=freq)
|
510 |
+
expected = PeriodIndex(["2016-01-01 00:00", "2016-01-02 01:00"], freq="25h")
|
511 |
+
tm.assert_index_equal(pidx, expected)
|
512 |
+
|
513 |
+
def test_period_range_length(self):
|
514 |
+
pi = period_range(freq="Y", start="1/1/2001", end="12/1/2009")
|
515 |
+
assert len(pi) == 9
|
516 |
+
|
517 |
+
pi = period_range(freq="Q", start="1/1/2001", end="12/1/2009")
|
518 |
+
assert len(pi) == 4 * 9
|
519 |
+
|
520 |
+
pi = period_range(freq="M", start="1/1/2001", end="12/1/2009")
|
521 |
+
assert len(pi) == 12 * 9
|
522 |
+
|
523 |
+
pi = period_range(freq="D", start="1/1/2001", end="12/31/2009")
|
524 |
+
assert len(pi) == 365 * 9 + 2
|
525 |
+
|
526 |
+
msg = "Period with BDay freq is deprecated"
|
527 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
528 |
+
pi = period_range(freq="B", start="1/1/2001", end="12/31/2009")
|
529 |
+
assert len(pi) == 261 * 9
|
530 |
+
|
531 |
+
pi = period_range(freq="h", start="1/1/2001", end="12/31/2001 23:00")
|
532 |
+
assert len(pi) == 365 * 24
|
533 |
+
|
534 |
+
pi = period_range(freq="Min", start="1/1/2001", end="1/1/2001 23:59")
|
535 |
+
assert len(pi) == 24 * 60
|
536 |
+
|
537 |
+
pi = period_range(freq="s", start="1/1/2001", end="1/1/2001 23:59:59")
|
538 |
+
assert len(pi) == 24 * 60 * 60
|
539 |
+
|
540 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
541 |
+
start = Period("02-Apr-2005", "B")
|
542 |
+
i1 = period_range(start=start, periods=20)
|
543 |
+
assert len(i1) == 20
|
544 |
+
assert i1.freq == start.freq
|
545 |
+
assert i1[0] == start
|
546 |
+
|
547 |
+
end_intv = Period("2006-12-31", "W")
|
548 |
+
i1 = period_range(end=end_intv, periods=10)
|
549 |
+
assert len(i1) == 10
|
550 |
+
assert i1.freq == end_intv.freq
|
551 |
+
assert i1[-1] == end_intv
|
552 |
+
|
553 |
+
msg = "'w' is deprecated and will be removed in a future version."
|
554 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
555 |
+
end_intv = Period("2006-12-31", "1w")
|
556 |
+
i2 = period_range(end=end_intv, periods=10)
|
557 |
+
assert len(i1) == len(i2)
|
558 |
+
assert (i1 == i2).all()
|
559 |
+
assert i1.freq == i2.freq
|
560 |
+
|
561 |
+
def test_infer_freq_from_first_element(self):
|
562 |
+
msg = "Period with BDay freq is deprecated"
|
563 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
564 |
+
start = Period("02-Apr-2005", "B")
|
565 |
+
end_intv = Period("2005-05-01", "B")
|
566 |
+
period_range(start=start, end=end_intv)
|
567 |
+
|
568 |
+
# infer freq from first element
|
569 |
+
i2 = PeriodIndex([end_intv, Period("2005-05-05", "B")])
|
570 |
+
assert len(i2) == 2
|
571 |
+
assert i2[0] == end_intv
|
572 |
+
|
573 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
574 |
+
i2 = PeriodIndex(np.array([end_intv, Period("2005-05-05", "B")]))
|
575 |
+
assert len(i2) == 2
|
576 |
+
assert i2[0] == end_intv
|
577 |
+
|
578 |
+
def test_mixed_freq_raises(self):
|
579 |
+
# Mixed freq should fail
|
580 |
+
msg = "Period with BDay freq is deprecated"
|
581 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
582 |
+
end_intv = Period("2005-05-01", "B")
|
583 |
+
|
584 |
+
msg = "'w' is deprecated and will be removed in a future version."
|
585 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
586 |
+
vals = [end_intv, Period("2006-12-31", "w")]
|
587 |
+
msg = r"Input has different freq=W-SUN from PeriodIndex\(freq=B\)"
|
588 |
+
depr_msg = r"PeriodDtype\[B\] is deprecated"
|
589 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
590 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
591 |
+
PeriodIndex(vals)
|
592 |
+
vals = np.array(vals)
|
593 |
+
with pytest.raises(IncompatibleFrequency, match=msg):
|
594 |
+
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
|
595 |
+
PeriodIndex(vals)
|
596 |
+
|
597 |
+
@pytest.mark.parametrize(
|
598 |
+
"freq", ["M", "Q", "Y", "D", "B", "min", "s", "ms", "us", "ns", "h"]
|
599 |
+
)
|
600 |
+
@pytest.mark.filterwarnings(
|
601 |
+
r"ignore:Period with BDay freq is deprecated:FutureWarning"
|
602 |
+
)
|
603 |
+
@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning")
|
604 |
+
def test_recreate_from_data(self, freq):
|
605 |
+
org = period_range(start="2001/04/01", freq=freq, periods=1)
|
606 |
+
idx = PeriodIndex(org.values, freq=freq)
|
607 |
+
tm.assert_index_equal(idx, org)
|
608 |
+
|
609 |
+
def test_map_with_string_constructor(self):
|
610 |
+
raw = [2005, 2007, 2009]
|
611 |
+
index = PeriodIndex(raw, freq="Y")
|
612 |
+
|
613 |
+
expected = Index([str(num) for num in raw])
|
614 |
+
res = index.map(str)
|
615 |
+
|
616 |
+
# should return an Index
|
617 |
+
assert isinstance(res, Index)
|
618 |
+
|
619 |
+
# preserve element types
|
620 |
+
assert all(isinstance(resi, str) for resi in res)
|
621 |
+
|
622 |
+
# lastly, values should compare equal
|
623 |
+
tm.assert_index_equal(res, expected)
|
624 |
+
|
625 |
+
|
626 |
+
class TestSimpleNew:
|
627 |
+
def test_constructor_simple_new(self):
|
628 |
+
idx = period_range("2007-01", name="p", periods=2, freq="M")
|
629 |
+
|
630 |
+
with pytest.raises(AssertionError, match="<class .*PeriodIndex'>"):
|
631 |
+
idx._simple_new(idx, name="p")
|
632 |
+
|
633 |
+
result = idx._simple_new(idx._data, name="p")
|
634 |
+
tm.assert_index_equal(result, idx)
|
635 |
+
|
636 |
+
msg = "Should be numpy array of type i8"
|
637 |
+
with pytest.raises(AssertionError, match=msg):
|
638 |
+
# Need ndarray, not int64 Index
|
639 |
+
type(idx._data)._simple_new(Index(idx.asi8), dtype=idx.dtype)
|
640 |
+
|
641 |
+
arr = type(idx._data)._simple_new(idx.asi8, dtype=idx.dtype)
|
642 |
+
result = idx._simple_new(arr, name="p")
|
643 |
+
tm.assert_index_equal(result, idx)
|
644 |
+
|
645 |
+
def test_constructor_simple_new_empty(self):
|
646 |
+
# GH13079
|
647 |
+
idx = PeriodIndex([], freq="M", name="p")
|
648 |
+
with pytest.raises(AssertionError, match="<class .*PeriodIndex'>"):
|
649 |
+
idx._simple_new(idx, name="p")
|
650 |
+
|
651 |
+
result = idx._simple_new(idx._data, name="p")
|
652 |
+
tm.assert_index_equal(result, idx)
|
653 |
+
|
654 |
+
@pytest.mark.parametrize("floats", [[1.1, 2.1], np.array([1.1, 2.1])])
|
655 |
+
def test_period_index_simple_new_disallows_floats(self, floats):
|
656 |
+
with pytest.raises(AssertionError, match="<class "):
|
657 |
+
PeriodIndex._simple_new(floats)
|
658 |
+
|
659 |
+
|
660 |
+
class TestShallowCopy:
|
661 |
+
def test_shallow_copy_empty(self):
|
662 |
+
# GH#13067
|
663 |
+
idx = PeriodIndex([], freq="M")
|
664 |
+
result = idx._view()
|
665 |
+
expected = idx
|
666 |
+
|
667 |
+
tm.assert_index_equal(result, expected)
|
668 |
+
|
669 |
+
def test_shallow_copy_disallow_i8(self):
|
670 |
+
# GH#24391
|
671 |
+
pi = period_range("2018-01-01", periods=3, freq="2D")
|
672 |
+
with pytest.raises(AssertionError, match="ndarray"):
|
673 |
+
pi._shallow_copy(pi.asi8)
|
674 |
+
|
675 |
+
def test_shallow_copy_requires_disallow_period_index(self):
|
676 |
+
pi = period_range("2018-01-01", periods=3, freq="2D")
|
677 |
+
with pytest.raises(AssertionError, match="PeriodIndex"):
|
678 |
+
pi._shallow_copy(pi)
|
679 |
+
|
680 |
+
|
681 |
+
class TestSeriesPeriod:
|
682 |
+
def test_constructor_cant_cast_period(self):
|
683 |
+
msg = "Cannot cast PeriodIndex to dtype float64"
|
684 |
+
with pytest.raises(TypeError, match=msg):
|
685 |
+
Series(period_range("2000-01-01", periods=10, freq="D"), dtype=float)
|
686 |
+
|
687 |
+
def test_constructor_cast_object(self):
|
688 |
+
pi = period_range("1/1/2000", periods=10)
|
689 |
+
ser = Series(pi, dtype=PeriodDtype("D"))
|
690 |
+
exp = Series(pi)
|
691 |
+
tm.assert_series_equal(ser, exp)
|
venv/lib/python3.10/site-packages/pandas/tests/indexes/period/test_formats.py
ADDED
@@ -0,0 +1,350 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from contextlib import nullcontext
|
2 |
+
from datetime import (
|
3 |
+
datetime,
|
4 |
+
time,
|
5 |
+
)
|
6 |
+
import locale
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import pytest
|
10 |
+
|
11 |
+
import pandas as pd
|
12 |
+
from pandas import (
|
13 |
+
PeriodIndex,
|
14 |
+
Series,
|
15 |
+
)
|
16 |
+
import pandas._testing as tm
|
17 |
+
|
18 |
+
|
19 |
+
def get_local_am_pm():
|
20 |
+
"""Return the AM and PM strings returned by strftime in current locale."""
|
21 |
+
am_local = time(1).strftime("%p")
|
22 |
+
pm_local = time(13).strftime("%p")
|
23 |
+
return am_local, pm_local
|
24 |
+
|
25 |
+
|
26 |
+
def test_get_values_for_csv():
|
27 |
+
index = PeriodIndex(["2017-01-01", "2017-01-02", "2017-01-03"], freq="D")
|
28 |
+
|
29 |
+
# First, with no arguments.
|
30 |
+
expected = np.array(["2017-01-01", "2017-01-02", "2017-01-03"], dtype=object)
|
31 |
+
|
32 |
+
result = index._get_values_for_csv()
|
33 |
+
tm.assert_numpy_array_equal(result, expected)
|
34 |
+
|
35 |
+
# No NaN values, so na_rep has no effect
|
36 |
+
result = index._get_values_for_csv(na_rep="pandas")
|
37 |
+
tm.assert_numpy_array_equal(result, expected)
|
38 |
+
|
39 |
+
# Make sure date formatting works
|
40 |
+
expected = np.array(["01-2017-01", "01-2017-02", "01-2017-03"], dtype=object)
|
41 |
+
|
42 |
+
result = index._get_values_for_csv(date_format="%m-%Y-%d")
|
43 |
+
tm.assert_numpy_array_equal(result, expected)
|
44 |
+
|
45 |
+
# NULL object handling should work
|
46 |
+
index = PeriodIndex(["2017-01-01", pd.NaT, "2017-01-03"], freq="D")
|
47 |
+
expected = np.array(["2017-01-01", "NaT", "2017-01-03"], dtype=object)
|
48 |
+
|
49 |
+
result = index._get_values_for_csv(na_rep="NaT")
|
50 |
+
tm.assert_numpy_array_equal(result, expected)
|
51 |
+
|
52 |
+
expected = np.array(["2017-01-01", "pandas", "2017-01-03"], dtype=object)
|
53 |
+
|
54 |
+
result = index._get_values_for_csv(na_rep="pandas")
|
55 |
+
tm.assert_numpy_array_equal(result, expected)
|
56 |
+
|
57 |
+
|
58 |
+
class TestPeriodIndexRendering:
|
59 |
+
def test_format_empty(self):
|
60 |
+
# GH#35712
|
61 |
+
empty_idx = PeriodIndex([], freq="Y")
|
62 |
+
msg = r"PeriodIndex\.format is deprecated"
|
63 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
64 |
+
assert empty_idx.format() == []
|
65 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
66 |
+
assert empty_idx.format(name=True) == [""]
|
67 |
+
|
68 |
+
@pytest.mark.parametrize("method", ["__repr__", "__str__"])
|
69 |
+
def test_representation(self, method):
|
70 |
+
# GH#7601
|
71 |
+
idx1 = PeriodIndex([], freq="D")
|
72 |
+
idx2 = PeriodIndex(["2011-01-01"], freq="D")
|
73 |
+
idx3 = PeriodIndex(["2011-01-01", "2011-01-02"], freq="D")
|
74 |
+
idx4 = PeriodIndex(["2011-01-01", "2011-01-02", "2011-01-03"], freq="D")
|
75 |
+
idx5 = PeriodIndex(["2011", "2012", "2013"], freq="Y")
|
76 |
+
idx6 = PeriodIndex(["2011-01-01 09:00", "2012-02-01 10:00", "NaT"], freq="h")
|
77 |
+
idx7 = pd.period_range("2013Q1", periods=1, freq="Q")
|
78 |
+
idx8 = pd.period_range("2013Q1", periods=2, freq="Q")
|
79 |
+
idx9 = pd.period_range("2013Q1", periods=3, freq="Q")
|
80 |
+
idx10 = PeriodIndex(["2011-01-01", "2011-02-01"], freq="3D")
|
81 |
+
|
82 |
+
exp1 = "PeriodIndex([], dtype='period[D]')"
|
83 |
+
|
84 |
+
exp2 = "PeriodIndex(['2011-01-01'], dtype='period[D]')"
|
85 |
+
|
86 |
+
exp3 = "PeriodIndex(['2011-01-01', '2011-01-02'], dtype='period[D]')"
|
87 |
+
|
88 |
+
exp4 = (
|
89 |
+
"PeriodIndex(['2011-01-01', '2011-01-02', '2011-01-03'], "
|
90 |
+
"dtype='period[D]')"
|
91 |
+
)
|
92 |
+
|
93 |
+
exp5 = "PeriodIndex(['2011', '2012', '2013'], dtype='period[Y-DEC]')"
|
94 |
+
|
95 |
+
exp6 = (
|
96 |
+
"PeriodIndex(['2011-01-01 09:00', '2012-02-01 10:00', 'NaT'], "
|
97 |
+
"dtype='period[h]')"
|
98 |
+
)
|
99 |
+
|
100 |
+
exp7 = "PeriodIndex(['2013Q1'], dtype='period[Q-DEC]')"
|
101 |
+
|
102 |
+
exp8 = "PeriodIndex(['2013Q1', '2013Q2'], dtype='period[Q-DEC]')"
|
103 |
+
|
104 |
+
exp9 = "PeriodIndex(['2013Q1', '2013Q2', '2013Q3'], dtype='period[Q-DEC]')"
|
105 |
+
|
106 |
+
exp10 = "PeriodIndex(['2011-01-01', '2011-02-01'], dtype='period[3D]')"
|
107 |
+
|
108 |
+
for idx, expected in zip(
|
109 |
+
[idx1, idx2, idx3, idx4, idx5, idx6, idx7, idx8, idx9, idx10],
|
110 |
+
[exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9, exp10],
|
111 |
+
):
|
112 |
+
result = getattr(idx, method)()
|
113 |
+
assert result == expected
|
114 |
+
|
115 |
+
# TODO: These are Series.__repr__ tests
|
116 |
+
def test_representation_to_series(self):
|
117 |
+
# GH#10971
|
118 |
+
idx1 = PeriodIndex([], freq="D")
|
119 |
+
idx2 = PeriodIndex(["2011-01-01"], freq="D")
|
120 |
+
idx3 = PeriodIndex(["2011-01-01", "2011-01-02"], freq="D")
|
121 |
+
idx4 = PeriodIndex(["2011-01-01", "2011-01-02", "2011-01-03"], freq="D")
|
122 |
+
idx5 = PeriodIndex(["2011", "2012", "2013"], freq="Y")
|
123 |
+
idx6 = PeriodIndex(["2011-01-01 09:00", "2012-02-01 10:00", "NaT"], freq="h")
|
124 |
+
|
125 |
+
idx7 = pd.period_range("2013Q1", periods=1, freq="Q")
|
126 |
+
idx8 = pd.period_range("2013Q1", periods=2, freq="Q")
|
127 |
+
idx9 = pd.period_range("2013Q1", periods=3, freq="Q")
|
128 |
+
|
129 |
+
exp1 = """Series([], dtype: period[D])"""
|
130 |
+
|
131 |
+
exp2 = """0 2011-01-01
|
132 |
+
dtype: period[D]"""
|
133 |
+
|
134 |
+
exp3 = """0 2011-01-01
|
135 |
+
1 2011-01-02
|
136 |
+
dtype: period[D]"""
|
137 |
+
|
138 |
+
exp4 = """0 2011-01-01
|
139 |
+
1 2011-01-02
|
140 |
+
2 2011-01-03
|
141 |
+
dtype: period[D]"""
|
142 |
+
|
143 |
+
exp5 = """0 2011
|
144 |
+
1 2012
|
145 |
+
2 2013
|
146 |
+
dtype: period[Y-DEC]"""
|
147 |
+
|
148 |
+
exp6 = """0 2011-01-01 09:00
|
149 |
+
1 2012-02-01 10:00
|
150 |
+
2 NaT
|
151 |
+
dtype: period[h]"""
|
152 |
+
|
153 |
+
exp7 = """0 2013Q1
|
154 |
+
dtype: period[Q-DEC]"""
|
155 |
+
|
156 |
+
exp8 = """0 2013Q1
|
157 |
+
1 2013Q2
|
158 |
+
dtype: period[Q-DEC]"""
|
159 |
+
|
160 |
+
exp9 = """0 2013Q1
|
161 |
+
1 2013Q2
|
162 |
+
2 2013Q3
|
163 |
+
dtype: period[Q-DEC]"""
|
164 |
+
|
165 |
+
for idx, expected in zip(
|
166 |
+
[idx1, idx2, idx3, idx4, idx5, idx6, idx7, idx8, idx9],
|
167 |
+
[exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9],
|
168 |
+
):
|
169 |
+
result = repr(Series(idx))
|
170 |
+
assert result == expected
|
171 |
+
|
172 |
+
def test_summary(self):
|
173 |
+
# GH#9116
|
174 |
+
idx1 = PeriodIndex([], freq="D")
|
175 |
+
idx2 = PeriodIndex(["2011-01-01"], freq="D")
|
176 |
+
idx3 = PeriodIndex(["2011-01-01", "2011-01-02"], freq="D")
|
177 |
+
idx4 = PeriodIndex(["2011-01-01", "2011-01-02", "2011-01-03"], freq="D")
|
178 |
+
idx5 = PeriodIndex(["2011", "2012", "2013"], freq="Y")
|
179 |
+
idx6 = PeriodIndex(["2011-01-01 09:00", "2012-02-01 10:00", "NaT"], freq="h")
|
180 |
+
|
181 |
+
idx7 = pd.period_range("2013Q1", periods=1, freq="Q")
|
182 |
+
idx8 = pd.period_range("2013Q1", periods=2, freq="Q")
|
183 |
+
idx9 = pd.period_range("2013Q1", periods=3, freq="Q")
|
184 |
+
|
185 |
+
exp1 = """PeriodIndex: 0 entries
|
186 |
+
Freq: D"""
|
187 |
+
|
188 |
+
exp2 = """PeriodIndex: 1 entries, 2011-01-01 to 2011-01-01
|
189 |
+
Freq: D"""
|
190 |
+
|
191 |
+
exp3 = """PeriodIndex: 2 entries, 2011-01-01 to 2011-01-02
|
192 |
+
Freq: D"""
|
193 |
+
|
194 |
+
exp4 = """PeriodIndex: 3 entries, 2011-01-01 to 2011-01-03
|
195 |
+
Freq: D"""
|
196 |
+
|
197 |
+
exp5 = """PeriodIndex: 3 entries, 2011 to 2013
|
198 |
+
Freq: Y-DEC"""
|
199 |
+
|
200 |
+
exp6 = """PeriodIndex: 3 entries, 2011-01-01 09:00 to NaT
|
201 |
+
Freq: h"""
|
202 |
+
|
203 |
+
exp7 = """PeriodIndex: 1 entries, 2013Q1 to 2013Q1
|
204 |
+
Freq: Q-DEC"""
|
205 |
+
|
206 |
+
exp8 = """PeriodIndex: 2 entries, 2013Q1 to 2013Q2
|
207 |
+
Freq: Q-DEC"""
|
208 |
+
|
209 |
+
exp9 = """PeriodIndex: 3 entries, 2013Q1 to 2013Q3
|
210 |
+
Freq: Q-DEC"""
|
211 |
+
|
212 |
+
for idx, expected in zip(
|
213 |
+
[idx1, idx2, idx3, idx4, idx5, idx6, idx7, idx8, idx9],
|
214 |
+
[exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9],
|
215 |
+
):
|
216 |
+
result = idx._summary()
|
217 |
+
assert result == expected
|
218 |
+
|
219 |
+
|
220 |
+
class TestPeriodIndexFormat:
|
221 |
+
def test_period_format_and_strftime_default(self):
|
222 |
+
per = PeriodIndex([datetime(2003, 1, 1, 12), None], freq="h")
|
223 |
+
|
224 |
+
# Default formatting
|
225 |
+
msg = "PeriodIndex.format is deprecated"
|
226 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
227 |
+
formatted = per.format()
|
228 |
+
assert formatted[0] == "2003-01-01 12:00" # default: minutes not shown
|
229 |
+
assert formatted[1] == "NaT"
|
230 |
+
# format is equivalent to strftime(None)...
|
231 |
+
assert formatted[0] == per.strftime(None)[0]
|
232 |
+
assert per.strftime(None)[1] is np.nan # ...except for NaTs
|
233 |
+
|
234 |
+
# Same test with nanoseconds freq
|
235 |
+
per = pd.period_range("2003-01-01 12:01:01.123456789", periods=2, freq="ns")
|
236 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
237 |
+
formatted = per.format()
|
238 |
+
assert (formatted == per.strftime(None)).all()
|
239 |
+
assert formatted[0] == "2003-01-01 12:01:01.123456789"
|
240 |
+
assert formatted[1] == "2003-01-01 12:01:01.123456790"
|
241 |
+
|
242 |
+
def test_period_custom(self):
|
243 |
+
# GH#46252 custom formatting directives %l (ms) and %u (us)
|
244 |
+
msg = "PeriodIndex.format is deprecated"
|
245 |
+
|
246 |
+
# 3 digits
|
247 |
+
per = pd.period_range("2003-01-01 12:01:01.123", periods=2, freq="ms")
|
248 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
249 |
+
formatted = per.format(date_format="%y %I:%M:%S (ms=%l us=%u ns=%n)")
|
250 |
+
assert formatted[0] == "03 12:01:01 (ms=123 us=123000 ns=123000000)"
|
251 |
+
assert formatted[1] == "03 12:01:01 (ms=124 us=124000 ns=124000000)"
|
252 |
+
|
253 |
+
# 6 digits
|
254 |
+
per = pd.period_range("2003-01-01 12:01:01.123456", periods=2, freq="us")
|
255 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
256 |
+
formatted = per.format(date_format="%y %I:%M:%S (ms=%l us=%u ns=%n)")
|
257 |
+
assert formatted[0] == "03 12:01:01 (ms=123 us=123456 ns=123456000)"
|
258 |
+
assert formatted[1] == "03 12:01:01 (ms=123 us=123457 ns=123457000)"
|
259 |
+
|
260 |
+
# 9 digits
|
261 |
+
per = pd.period_range("2003-01-01 12:01:01.123456789", periods=2, freq="ns")
|
262 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
263 |
+
formatted = per.format(date_format="%y %I:%M:%S (ms=%l us=%u ns=%n)")
|
264 |
+
assert formatted[0] == "03 12:01:01 (ms=123 us=123456 ns=123456789)"
|
265 |
+
assert formatted[1] == "03 12:01:01 (ms=123 us=123456 ns=123456790)"
|
266 |
+
|
267 |
+
def test_period_tz(self):
|
268 |
+
# Formatting periods created from a datetime with timezone.
|
269 |
+
msg = r"PeriodIndex\.format is deprecated"
|
270 |
+
# This timestamp is in 2013 in Europe/Paris but is 2012 in UTC
|
271 |
+
dt = pd.to_datetime(["2013-01-01 00:00:00+01:00"], utc=True)
|
272 |
+
|
273 |
+
# Converting to a period looses the timezone information
|
274 |
+
# Since tz is currently set as utc, we'll see 2012
|
275 |
+
with tm.assert_produces_warning(UserWarning, match="will drop timezone"):
|
276 |
+
per = dt.to_period(freq="h")
|
277 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
278 |
+
assert per.format()[0] == "2012-12-31 23:00"
|
279 |
+
|
280 |
+
# If tz is currently set as paris before conversion, we'll see 2013
|
281 |
+
dt = dt.tz_convert("Europe/Paris")
|
282 |
+
with tm.assert_produces_warning(UserWarning, match="will drop timezone"):
|
283 |
+
per = dt.to_period(freq="h")
|
284 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
285 |
+
assert per.format()[0] == "2013-01-01 00:00"
|
286 |
+
|
287 |
+
@pytest.mark.parametrize(
|
288 |
+
"locale_str",
|
289 |
+
[
|
290 |
+
pytest.param(None, id=str(locale.getlocale())),
|
291 |
+
"it_IT.utf8",
|
292 |
+
"it_IT", # Note: encoding will be 'ISO8859-1'
|
293 |
+
"zh_CN.utf8",
|
294 |
+
"zh_CN", # Note: encoding will be 'gb2312'
|
295 |
+
],
|
296 |
+
)
|
297 |
+
def test_period_non_ascii_fmt(self, locale_str):
|
298 |
+
# GH#46468 non-ascii char in input format string leads to wrong output
|
299 |
+
|
300 |
+
# Skip if locale cannot be set
|
301 |
+
if locale_str is not None and not tm.can_set_locale(locale_str, locale.LC_ALL):
|
302 |
+
pytest.skip(f"Skipping as locale '{locale_str}' cannot be set on host.")
|
303 |
+
|
304 |
+
# Change locale temporarily for this test.
|
305 |
+
with tm.set_locale(locale_str, locale.LC_ALL) if locale_str else nullcontext():
|
306 |
+
# Scalar
|
307 |
+
per = pd.Period("2018-03-11 13:00", freq="h")
|
308 |
+
assert per.strftime("%y é") == "18 é"
|
309 |
+
|
310 |
+
# Index
|
311 |
+
per = pd.period_range("2003-01-01 01:00:00", periods=2, freq="12h")
|
312 |
+
msg = "PeriodIndex.format is deprecated"
|
313 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
314 |
+
formatted = per.format(date_format="%y é")
|
315 |
+
assert formatted[0] == "03 é"
|
316 |
+
assert formatted[1] == "03 é"
|
317 |
+
|
318 |
+
@pytest.mark.parametrize(
|
319 |
+
"locale_str",
|
320 |
+
[
|
321 |
+
pytest.param(None, id=str(locale.getlocale())),
|
322 |
+
"it_IT.utf8",
|
323 |
+
"it_IT", # Note: encoding will be 'ISO8859-1'
|
324 |
+
"zh_CN.utf8",
|
325 |
+
"zh_CN", # Note: encoding will be 'gb2312'
|
326 |
+
],
|
327 |
+
)
|
328 |
+
def test_period_custom_locale_directive(self, locale_str):
|
329 |
+
# GH#46319 locale-specific directive leads to non-utf8 c strftime char* result
|
330 |
+
|
331 |
+
# Skip if locale cannot be set
|
332 |
+
if locale_str is not None and not tm.can_set_locale(locale_str, locale.LC_ALL):
|
333 |
+
pytest.skip(f"Skipping as locale '{locale_str}' cannot be set on host.")
|
334 |
+
|
335 |
+
# Change locale temporarily for this test.
|
336 |
+
with tm.set_locale(locale_str, locale.LC_ALL) if locale_str else nullcontext():
|
337 |
+
# Get locale-specific reference
|
338 |
+
am_local, pm_local = get_local_am_pm()
|
339 |
+
|
340 |
+
# Scalar
|
341 |
+
per = pd.Period("2018-03-11 13:00", freq="h")
|
342 |
+
assert per.strftime("%p") == pm_local
|
343 |
+
|
344 |
+
# Index
|
345 |
+
per = pd.period_range("2003-01-01 01:00:00", periods=2, freq="12h")
|
346 |
+
msg = "PeriodIndex.format is deprecated"
|
347 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
348 |
+
formatted = per.format(date_format="%y %I:%M:%S%p")
|
349 |
+
assert formatted[0] == f"03 01:00:00{am_local}"
|
350 |
+
assert formatted[1] == f"03 01:00:00{pm_local}"
|