forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_constructors.py
165 lines (135 loc) · 5.25 KB
/
test_constructors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from datetime import datetime
import numpy as np
import pytest
from pandas import Index, RangeIndex, Series
import pandas._testing as tm
class TestRangeIndexConstructors:
@pytest.mark.parametrize("name", [None, "foo"])
@pytest.mark.parametrize(
"args, kwargs, start, stop, step",
[
((5,), dict(), 0, 5, 1),
((1, 5), dict(), 1, 5, 1),
((1, 5, 2), dict(), 1, 5, 2),
((0,), dict(), 0, 0, 1),
((0, 0), dict(), 0, 0, 1),
(tuple(), dict(start=0), 0, 0, 1),
(tuple(), dict(stop=0), 0, 0, 1),
],
)
def test_constructor(self, args, kwargs, start, stop, step, name):
result = RangeIndex(*args, name=name, **kwargs)
expected = Index(np.arange(start, stop, step, dtype=np.int64), name=name)
assert isinstance(result, RangeIndex)
assert result.name is name
assert result._range == range(start, stop, step)
tm.assert_index_equal(result, expected)
def test_constructor_invalid_args(self):
msg = "RangeIndex\\(\\.\\.\\.\\) must be called with integers"
with pytest.raises(TypeError, match=msg):
RangeIndex()
with pytest.raises(TypeError, match=msg):
RangeIndex(name="Foo")
# we don't allow on a bare Index
msg = (
r"Index\(\.\.\.\) must be called with a collection of some "
r"kind, 0 was passed"
)
with pytest.raises(TypeError, match=msg):
Index(0, 1000)
@pytest.mark.parametrize(
"args",
[
Index(["a", "b"]),
Series(["a", "b"]),
np.array(["a", "b"]),
[],
np.arange(0, 10),
np.array([1]),
[1],
],
)
def test_constructor_additional_invalid_args(self, args):
msg = (
"Value needs to be a scalar value, "
f"was type {type(args).__name__}"
)
with pytest.raises(TypeError, match=msg):
RangeIndex(args)
@pytest.mark.parametrize("args", ["foo", datetime(2000, 1, 1, 0, 0)])
def test_constructor_invalid_args_wrong_type(self, args):
msg = f"Wrong type {type(args)} for value {args}"
with pytest.raises(TypeError, match=msg):
RangeIndex(args)
def test_constructor_same(self):
# pass thru w and w/o copy
index = RangeIndex(1, 5, 2)
result = RangeIndex(index, copy=False)
assert result.identical(index)
result = RangeIndex(index, copy=True)
tm.assert_index_equal(result, index, exact=True)
result = RangeIndex(index)
tm.assert_index_equal(result, index, exact=True)
with pytest.raises(
ValueError,
match="Incorrect `dtype` passed: expected signed integer, received float64",
):
RangeIndex(index, dtype="float64")
def test_constructor_range(self):
msg = "Value needs to be a scalar value, was type range"
with pytest.raises(TypeError, match=msg):
result = RangeIndex(range(1, 5, 2))
result = RangeIndex.from_range(range(1, 5, 2))
expected = RangeIndex(1, 5, 2)
tm.assert_index_equal(result, expected, exact=True)
result = RangeIndex.from_range(range(5, 6))
expected = RangeIndex(5, 6, 1)
tm.assert_index_equal(result, expected, exact=True)
# an invalid range
result = RangeIndex.from_range(range(5, 1))
expected = RangeIndex(0, 0, 1)
tm.assert_index_equal(result, expected, exact=True)
result = RangeIndex.from_range(range(5))
expected = RangeIndex(0, 5, 1)
tm.assert_index_equal(result, expected, exact=True)
result = Index(range(1, 5, 2))
expected = RangeIndex(1, 5, 2)
tm.assert_index_equal(result, expected, exact=True)
with pytest.raises(
ValueError,
match="Incorrect `dtype` passed: expected signed integer, received float64",
):
Index(range(1, 5, 2), dtype="float64")
msg = r"^from_range\(\) got an unexpected keyword argument"
with pytest.raises(TypeError, match=msg):
RangeIndex.from_range(range(10), copy=True)
def test_constructor_name(self):
# GH#12288
orig = RangeIndex(10)
orig.name = "original"
copy = RangeIndex(orig)
copy.name = "copy"
assert orig.name == "original"
assert copy.name == "copy"
new = Index(copy)
assert new.name == "copy"
new.name = "new"
assert orig.name == "original"
assert copy.name == "copy"
assert new.name == "new"
def test_constructor_corner(self):
arr = np.array([1, 2, 3, 4], dtype=object)
index = RangeIndex(1, 5)
assert index.values.dtype == np.int64
tm.assert_index_equal(index, Index(arr))
# non-int raise Exception
with pytest.raises(TypeError):
RangeIndex("1", "10", "1")
with pytest.raises(TypeError):
RangeIndex(1.1, 10.2, 1.3)
# invalid passed type
with pytest.raises(
ValueError,
match="Incorrect `dtype` passed: expected signed integer, received float64",
):
RangeIndex(1, 5, dtype="float64")