-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
/
Copy pathtest_series.py
263 lines (216 loc) · 8.21 KB
/
test_series.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
from distutils.version import LooseVersion
from operator import methodcaller
import numpy as np
import pytest
import pandas.util._test_decorators as td
import pandas as pd
from pandas import MultiIndex, Series, date_range
import pandas.util.testing as tm
from .test_generic import Generic
try:
import xarray
_XARRAY_INSTALLED = True
except ImportError:
_XARRAY_INSTALLED = False
class TestSeries(Generic):
_typ = Series
_comparator = lambda self, x, y: tm.assert_series_equal(x, y)
def setup_method(self):
self.ts = tm.makeTimeSeries() # Was at top level in test_series
self.ts.name = "ts"
self.series = tm.makeStringSeries()
self.series.name = "series"
def test_rename_mi(self):
s = Series(
[11, 21, 31],
index=MultiIndex.from_tuples([("A", x) for x in ["a", "B", "c"]]),
)
s.rename(str.lower)
def test_set_axis_name(self):
s = Series([1, 2, 3], index=["a", "b", "c"])
funcs = ["rename_axis", "_set_axis_name"]
name = "foo"
for func in funcs:
result = methodcaller(func, name)(s)
assert s.index.name is None
assert result.index.name == name
def test_set_axis_name_mi(self):
s = Series(
[11, 21, 31],
index=MultiIndex.from_tuples(
[("A", x) for x in ["a", "B", "c"]], names=["l1", "l2"]
),
)
funcs = ["rename_axis", "_set_axis_name"]
for func in funcs:
result = methodcaller(func, ["L1", "L2"])(s)
assert s.index.name is None
assert s.index.names == ["l1", "l2"]
assert result.index.name is None
assert result.index.names, ["L1", "L2"]
def test_set_axis_name_raises(self):
s = pd.Series([1])
with pytest.raises(ValueError):
s._set_axis_name(name="a", axis=1)
def test_get_numeric_data_preserve_dtype(self):
# get the numeric data
o = Series([1, 2, 3])
result = o._get_numeric_data()
self._compare(result, o)
o = Series([1, "2", 3.0])
result = o._get_numeric_data()
expected = Series([], dtype=object, index=pd.Index([], dtype=object))
self._compare(result, expected)
o = Series([True, False, True])
result = o._get_numeric_data()
self._compare(result, o)
o = Series([True, False, True])
result = o._get_bool_data()
self._compare(result, o)
o = Series(date_range("20130101", periods=3))
result = o._get_numeric_data()
expected = Series([], dtype="M8[ns]", index=pd.Index([], dtype=object))
self._compare(result, expected)
def test_nonzero_single_element(self):
# allow single item via bool method
s = Series([True])
assert s.bool()
s = Series([False])
assert not s.bool()
msg = "The truth value of a Series is ambiguous"
# single item nan to raise
for s in [Series([np.nan]), Series([pd.NaT]), Series([True]), Series([False])]:
with pytest.raises(ValueError, match=msg):
bool(s)
msg = "bool cannot act on a non-boolean single element Series"
for s in [Series([np.nan]), Series([pd.NaT])]:
with pytest.raises(ValueError, match=msg):
s.bool()
# multiple bool are still an error
msg = "The truth value of a Series is ambiguous"
for s in [Series([True, True]), Series([False, False])]:
with pytest.raises(ValueError, match=msg):
bool(s)
with pytest.raises(ValueError, match=msg):
s.bool()
# single non-bool are an error
for s in [Series([1]), Series([0]), Series(["a"]), Series([0.0])]:
msg = "The truth value of a Series is ambiguous"
with pytest.raises(ValueError, match=msg):
bool(s)
msg = "bool cannot act on a non-boolean single element Series"
with pytest.raises(ValueError, match=msg):
s.bool()
def test_metadata_propagation_indiv(self):
# check that the metadata matches up on the resulting ops
o = Series(range(3), range(3))
o.name = "foo"
o2 = Series(range(3), range(3))
o2.name = "bar"
result = o.T
self.check_metadata(o, result)
# resample
ts = Series(
np.random.rand(1000),
index=date_range("20130101", periods=1000, freq="s"),
name="foo",
)
result = ts.resample("1T").mean()
self.check_metadata(ts, result)
result = ts.resample("1T").min()
self.check_metadata(ts, result)
result = ts.resample("1T").apply(lambda x: x.sum())
self.check_metadata(ts, result)
_metadata = Series._metadata
_finalize = Series.__finalize__
Series._metadata = ["name", "filename"]
o.filename = "foo"
o2.filename = "bar"
def finalize(self, other, method=None, **kwargs):
for name in self._metadata:
if method == "concat" and name == "filename":
value = "+".join(
[getattr(o, name) for o in other.objs if getattr(o, name, None)]
)
object.__setattr__(self, name, value)
else:
object.__setattr__(self, name, getattr(other, name, None))
return self
Series.__finalize__ = finalize
result = pd.concat([o, o2])
assert result.filename == "foo+bar"
assert result.name is None
# reset
Series._metadata = _metadata
Series.__finalize__ = _finalize
@pytest.mark.skipif(
not _XARRAY_INSTALLED
or _XARRAY_INSTALLED
and LooseVersion(xarray.__version__) < LooseVersion("0.10.0"),
reason="xarray >= 0.10.0 required",
)
@pytest.mark.parametrize(
"index",
[
"FloatIndex",
"IntIndex",
"StringIndex",
"UnicodeIndex",
"DateIndex",
"PeriodIndex",
"TimedeltaIndex",
"CategoricalIndex",
],
)
def test_to_xarray_index_types(self, index):
from xarray import DataArray
index = getattr(tm, f"make{index}")
s = Series(range(6), index=index(6))
s.index.name = "foo"
result = s.to_xarray()
repr(result)
assert len(result) == 6
assert len(result.coords) == 1
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
assert isinstance(result, DataArray)
# idempotency
tm.assert_series_equal(
result.to_series(), s, check_index_type=False, check_categorical=True
)
@td.skip_if_no("xarray", min_version="0.7.0")
def test_to_xarray(self):
from xarray import DataArray
s = Series([], dtype=object)
s.index.name = "foo"
result = s.to_xarray()
assert len(result) == 0
assert len(result.coords) == 1
tm.assert_almost_equal(list(result.coords.keys()), ["foo"])
assert isinstance(result, DataArray)
s = Series(range(6))
s.index.name = "foo"
s.index = pd.MultiIndex.from_product(
[["a", "b"], range(3)], names=["one", "two"]
)
result = s.to_xarray()
assert len(result) == 2
tm.assert_almost_equal(list(result.coords.keys()), ["one", "two"])
assert isinstance(result, DataArray)
tm.assert_series_equal(result.to_series(), s)
@pytest.mark.parametrize(
"s",
[
Series([np.arange(5)]),
pd.date_range("1/1/2011", periods=24, freq="H"),
pd.Series(range(5), index=pd.date_range("2017", periods=5)),
],
)
@pytest.mark.parametrize("shift_size", [0, 1, 2])
def test_shift_always_copy(self, s, shift_size):
# GH22397
assert s.shift(shift_size) is not s
@pytest.mark.parametrize("move_by_freq", [pd.Timedelta("1D"), pd.Timedelta("1M")])
def test_datetime_shift_always_copy(self, move_by_freq):
# GH22397
s = pd.Series(range(5), index=pd.date_range("2017", periods=5))
assert s.shift(freq=move_by_freq) is not s