forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatetimelike.py
131 lines (101 loc) · 3.94 KB
/
datetimelike.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
""" generic datetimelike tests """
import numpy as np
import pytest
import pandas as pd
import pandas._testing as tm
from .common import Base
class DatetimeLike(Base):
def test_argmax_axis_invalid(self):
# GH#23081
msg = r"`axis` must be fewer than the number of dimensions \(1\)"
rng = self.create_index()
with pytest.raises(ValueError, match=msg):
rng.argmax(axis=1)
with pytest.raises(ValueError, match=msg):
rng.argmin(axis=2)
with pytest.raises(ValueError, match=msg):
rng.min(axis=-2)
with pytest.raises(ValueError, match=msg):
rng.max(axis=-3)
def test_can_hold_identifiers(self):
idx = self.create_index()
key = idx[0]
assert idx._can_hold_identifiers_and_holds_name(key) is False
def test_shift_identity(self):
idx = self.create_index()
tm.assert_index_equal(idx, idx.shift(0))
def test_shift_empty(self):
# GH#14811
idx = self.create_index()[:0]
tm.assert_index_equal(idx, idx.shift(1))
def test_str(self):
# test the string repr
idx = self.create_index()
idx.name = "foo"
assert not (f"length={len(idx)}" in str(idx))
assert "'foo'" in str(idx)
assert type(idx).__name__ in str(idx)
if hasattr(idx, "tz"):
if idx.tz is not None:
assert idx.tz in str(idx)
if hasattr(idx, "freq"):
assert f"freq='{idx.freqstr}'" in str(idx)
def test_view(self):
i = self.create_index()
i_view = i.view("i8")
result = self._holder(i)
tm.assert_index_equal(result, i)
i_view = i.view(self._holder)
result = self._holder(i)
tm.assert_index_equal(result, i_view)
def test_map_callable(self):
index = self.create_index()
expected = index + index.freq
result = index.map(lambda x: x + x.freq)
tm.assert_index_equal(result, expected)
# map to NaT
result = index.map(lambda x: pd.NaT if x == index[0] else x)
expected = pd.Index([pd.NaT] + index[1:].tolist())
tm.assert_index_equal(result, expected)
@pytest.mark.parametrize(
"mapper",
[
lambda values, index: {i: e for e, i in zip(values, index)},
lambda values, index: pd.Series(values, index, dtype=object),
],
)
def test_map_dictlike(self, mapper):
index = self.create_index()
expected = index + index.freq
# don't compare the freqs
if isinstance(expected, (pd.DatetimeIndex, pd.TimedeltaIndex)):
expected = expected._with_freq(None)
result = index.map(mapper(expected, index))
tm.assert_index_equal(result, expected)
expected = pd.Index([pd.NaT] + index[1:].tolist())
result = index.map(mapper(expected, index))
tm.assert_index_equal(result, expected)
# empty map; these map to np.nan because we cannot know
# to re-infer things
expected = pd.Index([np.nan] * len(index))
result = index.map(mapper([], []))
tm.assert_index_equal(result, expected)
def test_getitem_preserves_freq(self):
index = self.create_index()
assert index.freq is not None
result = index[:]
assert result.freq == index.freq
def test_where_cast_str(self):
index = self.create_index()
mask = np.ones(len(index), dtype=bool)
mask[-1] = False
result = index.where(mask, str(index[0]))
expected = index.where(mask, index[0])
tm.assert_index_equal(result, expected)
result = index.where(mask, [str(index[0])])
tm.assert_index_equal(result, expected)
msg = "value should be a '.*', 'NaT', or array of those"
with pytest.raises(TypeError, match=msg):
index.where(mask, "foo")
with pytest.raises(TypeError, match=msg):
index.where(mask, ["foo"])