Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +2 -0
- llmeval-env/lib/python3.10/site-packages/pandas/_libs/interval.cpython-310-x86_64-linux-gnu.so +3 -0
- llmeval-env/lib/python3.10/site-packages/pandas/_libs/join.cpython-310-x86_64-linux-gnu.so +3 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_argsort.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_between.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_case_when.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_compare.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_count.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_diff.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_drop_duplicates.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_dropna.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_duplicated.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_equals.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_explode.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_infer_objects.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_is_unique.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_isin.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_map.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_pop.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_rename.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_repeat.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_set_name.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_sort_values.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_to_csv.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_to_dict.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_truncate.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_tz_localize.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_unique.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_update.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_value_counts.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_values.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_view.cpython-310.pyc +0 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_add_prefix_suffix.py +41 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_align.py +249 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_autocorr.py +30 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_between.py +75 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_case_when.py +148 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_combine.py +17 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_combine_first.py +149 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_compare.py +141 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_convert_dtypes.py +306 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_count.py +34 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_cov_corr.py +185 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_diff.py +88 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_drop_duplicates.py +267 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_dropna.py +117 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_duplicated.py +77 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_head_tail.py +8 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_info.py +181 -0
- llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_interpolate.py +868 -0
.gitattributes
CHANGED
@@ -191,3 +191,5 @@ llmeval-env/lib/python3.10/site-packages/nvidia/nccl/lib/libnccl.so.2 filter=lfs
|
|
191 |
llmeval-env/lib/python3.10/site-packages/nvidia/cusparse/lib/libcusparse.so.12 filter=lfs diff=lfs merge=lfs -text
|
192 |
llmeval-env/lib/python3.10/site-packages/numexpr/interpreter.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
193 |
llmeval-env/lib/python3.10/site-packages/nvidia/cufft/lib/libcufft.so.11 filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
191 |
llmeval-env/lib/python3.10/site-packages/nvidia/cusparse/lib/libcusparse.so.12 filter=lfs diff=lfs merge=lfs -text
|
192 |
llmeval-env/lib/python3.10/site-packages/numexpr/interpreter.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
193 |
llmeval-env/lib/python3.10/site-packages/nvidia/cufft/lib/libcufft.so.11 filter=lfs diff=lfs merge=lfs -text
|
194 |
+
llmeval-env/lib/python3.10/site-packages/pandas/_libs/interval.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
195 |
+
llmeval-env/lib/python3.10/site-packages/pandas/_libs/join.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
llmeval-env/lib/python3.10/site-packages/pandas/_libs/interval.cpython-310-x86_64-linux-gnu.so
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7e13e757cc911f2818ebea4f5cfaf5197639154cf160d8be4d0e0c27451997f4
|
3 |
+
size 1532904
|
llmeval-env/lib/python3.10/site-packages/pandas/_libs/join.cpython-310-x86_64-linux-gnu.so
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:97df0241d7470fafc2785aaef2c2c4216803f45190f2c617a9042fecfc04678b
|
3 |
+
size 1409928
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_argsort.cpython-310.pyc
ADDED
Binary file (3.38 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_between.cpython-310.pyc
ADDED
Binary file (2.65 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_case_when.cpython-310.pyc
ADDED
Binary file (5.47 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_compare.cpython-310.pyc
ADDED
Binary file (3.91 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_count.cpython-310.pyc
ADDED
Binary file (1.55 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_diff.cpython-310.pyc
ADDED
Binary file (2.7 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_drop_duplicates.cpython-310.pyc
ADDED
Binary file (6.76 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_dropna.cpython-310.pyc
ADDED
Binary file (3.4 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_duplicated.cpython-310.pyc
ADDED
Binary file (1.96 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_equals.cpython-310.pyc
ADDED
Binary file (3.8 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_explode.cpython-310.pyc
ADDED
Binary file (5.67 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_infer_objects.cpython-310.pyc
ADDED
Binary file (1.96 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_is_unique.cpython-310.pyc
ADDED
Binary file (1.73 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_isin.cpython-310.pyc
ADDED
Binary file (7.51 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_map.cpython-310.pyc
ADDED
Binary file (21.5 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_pop.cpython-310.pyc
ADDED
Binary file (592 Bytes). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_rename.cpython-310.pyc
ADDED
Binary file (7.38 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_repeat.cpython-310.pyc
ADDED
Binary file (1.79 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_set_name.cpython-310.pyc
ADDED
Binary file (1.04 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_sort_values.cpython-310.pyc
ADDED
Binary file (7.53 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_to_csv.cpython-310.pyc
ADDED
Binary file (5.45 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_to_dict.cpython-310.pyc
ADDED
Binary file (1.44 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_truncate.cpython-310.pyc
ADDED
Binary file (2.46 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_tz_localize.cpython-310.pyc
ADDED
Binary file (3.68 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_unique.cpython-310.pyc
ADDED
Binary file (2.69 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_update.cpython-310.pyc
ADDED
Binary file (4.27 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_value_counts.cpython-310.pyc
ADDED
Binary file (6.6 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_values.cpython-310.pyc
ADDED
Binary file (1.2 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_view.cpython-310.pyc
ADDED
Binary file (2.09 kB). View file
|
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_add_prefix_suffix.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pytest
|
2 |
+
|
3 |
+
from pandas import Index
|
4 |
+
import pandas._testing as tm
|
5 |
+
|
6 |
+
|
7 |
+
def test_add_prefix_suffix(string_series):
|
8 |
+
with_prefix = string_series.add_prefix("foo#")
|
9 |
+
expected = Index([f"foo#{c}" for c in string_series.index])
|
10 |
+
tm.assert_index_equal(with_prefix.index, expected)
|
11 |
+
|
12 |
+
with_suffix = string_series.add_suffix("#foo")
|
13 |
+
expected = Index([f"{c}#foo" for c in string_series.index])
|
14 |
+
tm.assert_index_equal(with_suffix.index, expected)
|
15 |
+
|
16 |
+
with_pct_prefix = string_series.add_prefix("%")
|
17 |
+
expected = Index([f"%{c}" for c in string_series.index])
|
18 |
+
tm.assert_index_equal(with_pct_prefix.index, expected)
|
19 |
+
|
20 |
+
with_pct_suffix = string_series.add_suffix("%")
|
21 |
+
expected = Index([f"{c}%" for c in string_series.index])
|
22 |
+
tm.assert_index_equal(with_pct_suffix.index, expected)
|
23 |
+
|
24 |
+
|
25 |
+
def test_add_prefix_suffix_axis(string_series):
|
26 |
+
# GH 47819
|
27 |
+
with_prefix = string_series.add_prefix("foo#", axis=0)
|
28 |
+
expected = Index([f"foo#{c}" for c in string_series.index])
|
29 |
+
tm.assert_index_equal(with_prefix.index, expected)
|
30 |
+
|
31 |
+
with_pct_suffix = string_series.add_suffix("#foo", axis=0)
|
32 |
+
expected = Index([f"{c}#foo" for c in string_series.index])
|
33 |
+
tm.assert_index_equal(with_pct_suffix.index, expected)
|
34 |
+
|
35 |
+
|
36 |
+
def test_add_prefix_suffix_invalid_axis(string_series):
|
37 |
+
with pytest.raises(ValueError, match="No axis named 1 for object type Series"):
|
38 |
+
string_series.add_prefix("foo#", axis=1)
|
39 |
+
|
40 |
+
with pytest.raises(ValueError, match="No axis named 1 for object type Series"):
|
41 |
+
string_series.add_suffix("foo#", axis=1)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_align.py
ADDED
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import timezone
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
import pandas as pd
|
7 |
+
from pandas import (
|
8 |
+
Series,
|
9 |
+
date_range,
|
10 |
+
period_range,
|
11 |
+
)
|
12 |
+
import pandas._testing as tm
|
13 |
+
|
14 |
+
|
15 |
+
@pytest.mark.parametrize(
|
16 |
+
"first_slice,second_slice",
|
17 |
+
[
|
18 |
+
[[2, None], [None, -5]],
|
19 |
+
[[None, 0], [None, -5]],
|
20 |
+
[[None, -5], [None, 0]],
|
21 |
+
[[None, 0], [None, 0]],
|
22 |
+
],
|
23 |
+
)
|
24 |
+
@pytest.mark.parametrize("fill", [None, -1])
|
25 |
+
def test_align(datetime_series, first_slice, second_slice, join_type, fill):
|
26 |
+
a = datetime_series[slice(*first_slice)]
|
27 |
+
b = datetime_series[slice(*second_slice)]
|
28 |
+
|
29 |
+
aa, ab = a.align(b, join=join_type, fill_value=fill)
|
30 |
+
|
31 |
+
join_index = a.index.join(b.index, how=join_type)
|
32 |
+
if fill is not None:
|
33 |
+
diff_a = aa.index.difference(join_index)
|
34 |
+
diff_b = ab.index.difference(join_index)
|
35 |
+
if len(diff_a) > 0:
|
36 |
+
assert (aa.reindex(diff_a) == fill).all()
|
37 |
+
if len(diff_b) > 0:
|
38 |
+
assert (ab.reindex(diff_b) == fill).all()
|
39 |
+
|
40 |
+
ea = a.reindex(join_index)
|
41 |
+
eb = b.reindex(join_index)
|
42 |
+
|
43 |
+
if fill is not None:
|
44 |
+
ea = ea.fillna(fill)
|
45 |
+
eb = eb.fillna(fill)
|
46 |
+
|
47 |
+
tm.assert_series_equal(aa, ea)
|
48 |
+
tm.assert_series_equal(ab, eb)
|
49 |
+
assert aa.name == "ts"
|
50 |
+
assert ea.name == "ts"
|
51 |
+
assert ab.name == "ts"
|
52 |
+
assert eb.name == "ts"
|
53 |
+
|
54 |
+
|
55 |
+
@pytest.mark.parametrize(
|
56 |
+
"first_slice,second_slice",
|
57 |
+
[
|
58 |
+
[[2, None], [None, -5]],
|
59 |
+
[[None, 0], [None, -5]],
|
60 |
+
[[None, -5], [None, 0]],
|
61 |
+
[[None, 0], [None, 0]],
|
62 |
+
],
|
63 |
+
)
|
64 |
+
@pytest.mark.parametrize("method", ["pad", "bfill"])
|
65 |
+
@pytest.mark.parametrize("limit", [None, 1])
|
66 |
+
def test_align_fill_method(
|
67 |
+
datetime_series, first_slice, second_slice, join_type, method, limit
|
68 |
+
):
|
69 |
+
a = datetime_series[slice(*first_slice)]
|
70 |
+
b = datetime_series[slice(*second_slice)]
|
71 |
+
|
72 |
+
msg = (
|
73 |
+
"The 'method', 'limit', and 'fill_axis' keywords in Series.align "
|
74 |
+
"are deprecated"
|
75 |
+
)
|
76 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
77 |
+
aa, ab = a.align(b, join=join_type, method=method, limit=limit)
|
78 |
+
|
79 |
+
join_index = a.index.join(b.index, how=join_type)
|
80 |
+
ea = a.reindex(join_index)
|
81 |
+
eb = b.reindex(join_index)
|
82 |
+
|
83 |
+
msg2 = "Series.fillna with 'method' is deprecated"
|
84 |
+
with tm.assert_produces_warning(FutureWarning, match=msg2):
|
85 |
+
ea = ea.fillna(method=method, limit=limit)
|
86 |
+
eb = eb.fillna(method=method, limit=limit)
|
87 |
+
|
88 |
+
tm.assert_series_equal(aa, ea)
|
89 |
+
tm.assert_series_equal(ab, eb)
|
90 |
+
|
91 |
+
|
92 |
+
def test_align_nocopy(datetime_series, using_copy_on_write):
|
93 |
+
b = datetime_series[:5].copy()
|
94 |
+
|
95 |
+
# do copy
|
96 |
+
a = datetime_series.copy()
|
97 |
+
ra, _ = a.align(b, join="left")
|
98 |
+
ra[:5] = 5
|
99 |
+
assert not (a[:5] == 5).any()
|
100 |
+
|
101 |
+
# do not copy
|
102 |
+
a = datetime_series.copy()
|
103 |
+
ra, _ = a.align(b, join="left", copy=False)
|
104 |
+
ra[:5] = 5
|
105 |
+
if using_copy_on_write:
|
106 |
+
assert not (a[:5] == 5).any()
|
107 |
+
else:
|
108 |
+
assert (a[:5] == 5).all()
|
109 |
+
|
110 |
+
# do copy
|
111 |
+
a = datetime_series.copy()
|
112 |
+
b = datetime_series[:5].copy()
|
113 |
+
_, rb = a.align(b, join="right")
|
114 |
+
rb[:3] = 5
|
115 |
+
assert not (b[:3] == 5).any()
|
116 |
+
|
117 |
+
# do not copy
|
118 |
+
a = datetime_series.copy()
|
119 |
+
b = datetime_series[:5].copy()
|
120 |
+
_, rb = a.align(b, join="right", copy=False)
|
121 |
+
rb[:2] = 5
|
122 |
+
if using_copy_on_write:
|
123 |
+
assert not (b[:2] == 5).any()
|
124 |
+
else:
|
125 |
+
assert (b[:2] == 5).all()
|
126 |
+
|
127 |
+
|
128 |
+
def test_align_same_index(datetime_series, using_copy_on_write):
|
129 |
+
a, b = datetime_series.align(datetime_series, copy=False)
|
130 |
+
if not using_copy_on_write:
|
131 |
+
assert a.index is datetime_series.index
|
132 |
+
assert b.index is datetime_series.index
|
133 |
+
else:
|
134 |
+
assert a.index.is_(datetime_series.index)
|
135 |
+
assert b.index.is_(datetime_series.index)
|
136 |
+
|
137 |
+
a, b = datetime_series.align(datetime_series, copy=True)
|
138 |
+
assert a.index is not datetime_series.index
|
139 |
+
assert b.index is not datetime_series.index
|
140 |
+
assert a.index.is_(datetime_series.index)
|
141 |
+
assert b.index.is_(datetime_series.index)
|
142 |
+
|
143 |
+
|
144 |
+
def test_align_multiindex():
|
145 |
+
# GH 10665
|
146 |
+
|
147 |
+
midx = pd.MultiIndex.from_product(
|
148 |
+
[range(2), range(3), range(2)], names=("a", "b", "c")
|
149 |
+
)
|
150 |
+
idx = pd.Index(range(2), name="b")
|
151 |
+
s1 = Series(np.arange(12, dtype="int64"), index=midx)
|
152 |
+
s2 = Series(np.arange(2, dtype="int64"), index=idx)
|
153 |
+
|
154 |
+
# these must be the same results (but flipped)
|
155 |
+
res1l, res1r = s1.align(s2, join="left")
|
156 |
+
res2l, res2r = s2.align(s1, join="right")
|
157 |
+
|
158 |
+
expl = s1
|
159 |
+
tm.assert_series_equal(expl, res1l)
|
160 |
+
tm.assert_series_equal(expl, res2r)
|
161 |
+
expr = Series([0, 0, 1, 1, np.nan, np.nan] * 2, index=midx)
|
162 |
+
tm.assert_series_equal(expr, res1r)
|
163 |
+
tm.assert_series_equal(expr, res2l)
|
164 |
+
|
165 |
+
res1l, res1r = s1.align(s2, join="right")
|
166 |
+
res2l, res2r = s2.align(s1, join="left")
|
167 |
+
|
168 |
+
exp_idx = pd.MultiIndex.from_product(
|
169 |
+
[range(2), range(2), range(2)], names=("a", "b", "c")
|
170 |
+
)
|
171 |
+
expl = Series([0, 1, 2, 3, 6, 7, 8, 9], index=exp_idx)
|
172 |
+
tm.assert_series_equal(expl, res1l)
|
173 |
+
tm.assert_series_equal(expl, res2r)
|
174 |
+
expr = Series([0, 0, 1, 1] * 2, index=exp_idx)
|
175 |
+
tm.assert_series_equal(expr, res1r)
|
176 |
+
tm.assert_series_equal(expr, res2l)
|
177 |
+
|
178 |
+
|
179 |
+
@pytest.mark.parametrize("method", ["backfill", "bfill", "pad", "ffill", None])
|
180 |
+
def test_align_with_dataframe_method(method):
|
181 |
+
# GH31788
|
182 |
+
ser = Series(range(3), index=range(3))
|
183 |
+
df = pd.DataFrame(0.0, index=range(3), columns=range(3))
|
184 |
+
|
185 |
+
msg = (
|
186 |
+
"The 'method', 'limit', and 'fill_axis' keywords in Series.align "
|
187 |
+
"are deprecated"
|
188 |
+
)
|
189 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
190 |
+
result_ser, result_df = ser.align(df, method=method)
|
191 |
+
tm.assert_series_equal(result_ser, ser)
|
192 |
+
tm.assert_frame_equal(result_df, df)
|
193 |
+
|
194 |
+
|
195 |
+
def test_align_dt64tzindex_mismatched_tzs():
|
196 |
+
idx1 = date_range("2001", periods=5, freq="h", tz="US/Eastern")
|
197 |
+
ser = Series(np.random.default_rng(2).standard_normal(len(idx1)), index=idx1)
|
198 |
+
ser_central = ser.tz_convert("US/Central")
|
199 |
+
# different timezones convert to UTC
|
200 |
+
|
201 |
+
new1, new2 = ser.align(ser_central)
|
202 |
+
assert new1.index.tz is timezone.utc
|
203 |
+
assert new2.index.tz is timezone.utc
|
204 |
+
|
205 |
+
|
206 |
+
def test_align_periodindex(join_type):
|
207 |
+
rng = period_range("1/1/2000", "1/1/2010", freq="Y")
|
208 |
+
ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
|
209 |
+
|
210 |
+
# TODO: assert something?
|
211 |
+
ts.align(ts[::2], join=join_type)
|
212 |
+
|
213 |
+
|
214 |
+
def test_align_left_fewer_levels():
|
215 |
+
# GH#45224
|
216 |
+
left = Series([2], index=pd.MultiIndex.from_tuples([(1, 3)], names=["a", "c"]))
|
217 |
+
right = Series(
|
218 |
+
[1], index=pd.MultiIndex.from_tuples([(1, 2, 3)], names=["a", "b", "c"])
|
219 |
+
)
|
220 |
+
result_left, result_right = left.align(right)
|
221 |
+
|
222 |
+
expected_right = Series(
|
223 |
+
[1], index=pd.MultiIndex.from_tuples([(1, 3, 2)], names=["a", "c", "b"])
|
224 |
+
)
|
225 |
+
expected_left = Series(
|
226 |
+
[2], index=pd.MultiIndex.from_tuples([(1, 3, 2)], names=["a", "c", "b"])
|
227 |
+
)
|
228 |
+
tm.assert_series_equal(result_left, expected_left)
|
229 |
+
tm.assert_series_equal(result_right, expected_right)
|
230 |
+
|
231 |
+
|
232 |
+
def test_align_left_different_named_levels():
|
233 |
+
# GH#45224
|
234 |
+
left = Series(
|
235 |
+
[2], index=pd.MultiIndex.from_tuples([(1, 4, 3)], names=["a", "d", "c"])
|
236 |
+
)
|
237 |
+
right = Series(
|
238 |
+
[1], index=pd.MultiIndex.from_tuples([(1, 2, 3)], names=["a", "b", "c"])
|
239 |
+
)
|
240 |
+
result_left, result_right = left.align(right)
|
241 |
+
|
242 |
+
expected_left = Series(
|
243 |
+
[2], index=pd.MultiIndex.from_tuples([(1, 4, 3, 2)], names=["a", "d", "c", "b"])
|
244 |
+
)
|
245 |
+
expected_right = Series(
|
246 |
+
[1], index=pd.MultiIndex.from_tuples([(1, 4, 3, 2)], names=["a", "d", "c", "b"])
|
247 |
+
)
|
248 |
+
tm.assert_series_equal(result_left, expected_left)
|
249 |
+
tm.assert_series_equal(result_right, expected_right)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_autocorr.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
|
4 |
+
class TestAutoCorr:
|
5 |
+
def test_autocorr(self, datetime_series):
|
6 |
+
# Just run the function
|
7 |
+
corr1 = datetime_series.autocorr()
|
8 |
+
|
9 |
+
# Now run it with the lag parameter
|
10 |
+
corr2 = datetime_series.autocorr(lag=1)
|
11 |
+
|
12 |
+
# corr() with lag needs Series of at least length 2
|
13 |
+
if len(datetime_series) <= 2:
|
14 |
+
assert np.isnan(corr1)
|
15 |
+
assert np.isnan(corr2)
|
16 |
+
else:
|
17 |
+
assert corr1 == corr2
|
18 |
+
|
19 |
+
# Choose a random lag between 1 and length of Series - 2
|
20 |
+
# and compare the result with the Series corr() function
|
21 |
+
n = 1 + np.random.default_rng(2).integers(max(1, len(datetime_series) - 2))
|
22 |
+
corr1 = datetime_series.corr(datetime_series.shift(n))
|
23 |
+
corr2 = datetime_series.autocorr(lag=n)
|
24 |
+
|
25 |
+
# corr() with lag needs Series of at least length 2
|
26 |
+
if len(datetime_series) <= 2:
|
27 |
+
assert np.isnan(corr1)
|
28 |
+
assert np.isnan(corr2)
|
29 |
+
else:
|
30 |
+
assert corr1 == corr2
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_between.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
Series,
|
6 |
+
bdate_range,
|
7 |
+
date_range,
|
8 |
+
period_range,
|
9 |
+
)
|
10 |
+
import pandas._testing as tm
|
11 |
+
|
12 |
+
|
13 |
+
class TestBetween:
|
14 |
+
def test_between(self):
|
15 |
+
series = Series(date_range("1/1/2000", periods=10))
|
16 |
+
left, right = series[[2, 7]]
|
17 |
+
|
18 |
+
result = series.between(left, right)
|
19 |
+
expected = (series >= left) & (series <= right)
|
20 |
+
tm.assert_series_equal(result, expected)
|
21 |
+
|
22 |
+
def test_between_datetime_object_dtype(self):
|
23 |
+
ser = Series(bdate_range("1/1/2000", periods=20), dtype=object)
|
24 |
+
ser[::2] = np.nan
|
25 |
+
|
26 |
+
result = ser[ser.between(ser[3], ser[17])]
|
27 |
+
expected = ser[3:18].dropna()
|
28 |
+
tm.assert_series_equal(result, expected)
|
29 |
+
|
30 |
+
result = ser[ser.between(ser[3], ser[17], inclusive="neither")]
|
31 |
+
expected = ser[5:16].dropna()
|
32 |
+
tm.assert_series_equal(result, expected)
|
33 |
+
|
34 |
+
def test_between_period_values(self):
|
35 |
+
ser = Series(period_range("2000-01-01", periods=10, freq="D"))
|
36 |
+
left, right = ser[[2, 7]]
|
37 |
+
result = ser.between(left, right)
|
38 |
+
expected = (ser >= left) & (ser <= right)
|
39 |
+
tm.assert_series_equal(result, expected)
|
40 |
+
|
41 |
+
def test_between_inclusive_string(self):
|
42 |
+
# GH 40628
|
43 |
+
series = Series(date_range("1/1/2000", periods=10))
|
44 |
+
left, right = series[[2, 7]]
|
45 |
+
|
46 |
+
result = series.between(left, right, inclusive="both")
|
47 |
+
expected = (series >= left) & (series <= right)
|
48 |
+
tm.assert_series_equal(result, expected)
|
49 |
+
|
50 |
+
result = series.between(left, right, inclusive="left")
|
51 |
+
expected = (series >= left) & (series < right)
|
52 |
+
tm.assert_series_equal(result, expected)
|
53 |
+
|
54 |
+
result = series.between(left, right, inclusive="right")
|
55 |
+
expected = (series > left) & (series <= right)
|
56 |
+
tm.assert_series_equal(result, expected)
|
57 |
+
|
58 |
+
result = series.between(left, right, inclusive="neither")
|
59 |
+
expected = (series > left) & (series < right)
|
60 |
+
tm.assert_series_equal(result, expected)
|
61 |
+
|
62 |
+
@pytest.mark.parametrize("inclusive", ["yes", True, False])
|
63 |
+
def test_between_error_args(self, inclusive):
|
64 |
+
# GH 40628
|
65 |
+
series = Series(date_range("1/1/2000", periods=10))
|
66 |
+
left, right = series[[2, 7]]
|
67 |
+
|
68 |
+
value_error_msg = (
|
69 |
+
"Inclusive has to be either string of 'both',"
|
70 |
+
"'left', 'right', or 'neither'."
|
71 |
+
)
|
72 |
+
|
73 |
+
with pytest.raises(ValueError, match=value_error_msg):
|
74 |
+
series = Series(date_range("1/1/2000", periods=10))
|
75 |
+
series.between(left, right, inclusive=inclusive)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_case_when.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
DataFrame,
|
6 |
+
Series,
|
7 |
+
array as pd_array,
|
8 |
+
date_range,
|
9 |
+
)
|
10 |
+
import pandas._testing as tm
|
11 |
+
|
12 |
+
|
13 |
+
@pytest.fixture
|
14 |
+
def df():
|
15 |
+
"""
|
16 |
+
base dataframe for testing
|
17 |
+
"""
|
18 |
+
return DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
|
19 |
+
|
20 |
+
|
21 |
+
def test_case_when_caselist_is_not_a_list(df):
|
22 |
+
"""
|
23 |
+
Raise ValueError if caselist is not a list.
|
24 |
+
"""
|
25 |
+
msg = "The caselist argument should be a list; "
|
26 |
+
msg += "instead got.+"
|
27 |
+
with pytest.raises(TypeError, match=msg): # GH39154
|
28 |
+
df["a"].case_when(caselist=())
|
29 |
+
|
30 |
+
|
31 |
+
def test_case_when_no_caselist(df):
|
32 |
+
"""
|
33 |
+
Raise ValueError if no caselist is provided.
|
34 |
+
"""
|
35 |
+
msg = "provide at least one boolean condition, "
|
36 |
+
msg += "with a corresponding replacement."
|
37 |
+
with pytest.raises(ValueError, match=msg): # GH39154
|
38 |
+
df["a"].case_when([])
|
39 |
+
|
40 |
+
|
41 |
+
def test_case_when_odd_caselist(df):
|
42 |
+
"""
|
43 |
+
Raise ValueError if no of caselist is odd.
|
44 |
+
"""
|
45 |
+
msg = "Argument 0 must have length 2; "
|
46 |
+
msg += "a condition and replacement; instead got length 3."
|
47 |
+
|
48 |
+
with pytest.raises(ValueError, match=msg):
|
49 |
+
df["a"].case_when([(df["a"].eq(1), 1, df.a.gt(1))])
|
50 |
+
|
51 |
+
|
52 |
+
def test_case_when_raise_error_from_mask(df):
|
53 |
+
"""
|
54 |
+
Raise Error from within Series.mask
|
55 |
+
"""
|
56 |
+
msg = "Failed to apply condition0 and replacement0."
|
57 |
+
with pytest.raises(ValueError, match=msg):
|
58 |
+
df["a"].case_when([(df["a"].eq(1), [1, 2])])
|
59 |
+
|
60 |
+
|
61 |
+
def test_case_when_single_condition(df):
|
62 |
+
"""
|
63 |
+
Test output on a single condition.
|
64 |
+
"""
|
65 |
+
result = Series([np.nan, np.nan, np.nan]).case_when([(df.a.eq(1), 1)])
|
66 |
+
expected = Series([1, np.nan, np.nan])
|
67 |
+
tm.assert_series_equal(result, expected)
|
68 |
+
|
69 |
+
|
70 |
+
def test_case_when_multiple_conditions(df):
|
71 |
+
"""
|
72 |
+
Test output when booleans are derived from a computation
|
73 |
+
"""
|
74 |
+
result = Series([np.nan, np.nan, np.nan]).case_when(
|
75 |
+
[(df.a.eq(1), 1), (Series([False, True, False]), 2)]
|
76 |
+
)
|
77 |
+
expected = Series([1, 2, np.nan])
|
78 |
+
tm.assert_series_equal(result, expected)
|
79 |
+
|
80 |
+
|
81 |
+
def test_case_when_multiple_conditions_replacement_list(df):
|
82 |
+
"""
|
83 |
+
Test output when replacement is a list
|
84 |
+
"""
|
85 |
+
result = Series([np.nan, np.nan, np.nan]).case_when(
|
86 |
+
[([True, False, False], 1), (df["a"].gt(1) & df["b"].eq(5), [1, 2, 3])]
|
87 |
+
)
|
88 |
+
expected = Series([1, 2, np.nan])
|
89 |
+
tm.assert_series_equal(result, expected)
|
90 |
+
|
91 |
+
|
92 |
+
def test_case_when_multiple_conditions_replacement_extension_dtype(df):
|
93 |
+
"""
|
94 |
+
Test output when replacement has an extension dtype
|
95 |
+
"""
|
96 |
+
result = Series([np.nan, np.nan, np.nan]).case_when(
|
97 |
+
[
|
98 |
+
([True, False, False], 1),
|
99 |
+
(df["a"].gt(1) & df["b"].eq(5), pd_array([1, 2, 3], dtype="Int64")),
|
100 |
+
],
|
101 |
+
)
|
102 |
+
expected = Series([1, 2, np.nan], dtype="Float64")
|
103 |
+
tm.assert_series_equal(result, expected)
|
104 |
+
|
105 |
+
|
106 |
+
def test_case_when_multiple_conditions_replacement_series(df):
|
107 |
+
"""
|
108 |
+
Test output when replacement is a Series
|
109 |
+
"""
|
110 |
+
result = Series([np.nan, np.nan, np.nan]).case_when(
|
111 |
+
[
|
112 |
+
(np.array([True, False, False]), 1),
|
113 |
+
(df["a"].gt(1) & df["b"].eq(5), Series([1, 2, 3])),
|
114 |
+
],
|
115 |
+
)
|
116 |
+
expected = Series([1, 2, np.nan])
|
117 |
+
tm.assert_series_equal(result, expected)
|
118 |
+
|
119 |
+
|
120 |
+
def test_case_when_non_range_index():
|
121 |
+
"""
|
122 |
+
Test output if index is not RangeIndex
|
123 |
+
"""
|
124 |
+
rng = np.random.default_rng(seed=123)
|
125 |
+
dates = date_range("1/1/2000", periods=8)
|
126 |
+
df = DataFrame(
|
127 |
+
rng.standard_normal(size=(8, 4)), index=dates, columns=["A", "B", "C", "D"]
|
128 |
+
)
|
129 |
+
result = Series(5, index=df.index, name="A").case_when([(df.A.gt(0), df.B)])
|
130 |
+
expected = df.A.mask(df.A.gt(0), df.B).where(df.A.gt(0), 5)
|
131 |
+
tm.assert_series_equal(result, expected)
|
132 |
+
|
133 |
+
|
134 |
+
def test_case_when_callable():
|
135 |
+
"""
|
136 |
+
Test output on a callable
|
137 |
+
"""
|
138 |
+
# https://numpy.org/doc/stable/reference/generated/numpy.piecewise.html
|
139 |
+
x = np.linspace(-2.5, 2.5, 6)
|
140 |
+
ser = Series(x)
|
141 |
+
result = ser.case_when(
|
142 |
+
caselist=[
|
143 |
+
(lambda df: df < 0, lambda df: -df),
|
144 |
+
(lambda df: df >= 0, lambda df: df),
|
145 |
+
]
|
146 |
+
)
|
147 |
+
expected = np.piecewise(x, [x < 0, x >= 0], [lambda x: -x, lambda x: x])
|
148 |
+
tm.assert_series_equal(result, Series(expected))
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_combine.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pandas import Series
|
2 |
+
import pandas._testing as tm
|
3 |
+
|
4 |
+
|
5 |
+
class TestCombine:
|
6 |
+
def test_combine_scalar(self):
|
7 |
+
# GH#21248
|
8 |
+
# Note - combine() with another Series is tested elsewhere because
|
9 |
+
# it is used when testing operators
|
10 |
+
ser = Series([i * 10 for i in range(5)])
|
11 |
+
result = ser.combine(3, lambda x, y: x + y)
|
12 |
+
expected = Series([i * 10 + 3 for i in range(5)])
|
13 |
+
tm.assert_series_equal(result, expected)
|
14 |
+
|
15 |
+
result = ser.combine(22, lambda x, y: min(x, y))
|
16 |
+
expected = Series([min(i * 10, 22) for i in range(5)])
|
17 |
+
tm.assert_series_equal(result, expected)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_combine_first.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
import pandas as pd
|
6 |
+
from pandas import (
|
7 |
+
Period,
|
8 |
+
Series,
|
9 |
+
date_range,
|
10 |
+
period_range,
|
11 |
+
to_datetime,
|
12 |
+
)
|
13 |
+
import pandas._testing as tm
|
14 |
+
|
15 |
+
|
16 |
+
class TestCombineFirst:
|
17 |
+
def test_combine_first_period_datetime(self):
|
18 |
+
# GH#3367
|
19 |
+
didx = date_range(start="1950-01-31", end="1950-07-31", freq="ME")
|
20 |
+
pidx = period_range(start=Period("1950-1"), end=Period("1950-7"), freq="M")
|
21 |
+
# check to be consistent with DatetimeIndex
|
22 |
+
for idx in [didx, pidx]:
|
23 |
+
a = Series([1, np.nan, np.nan, 4, 5, np.nan, 7], index=idx)
|
24 |
+
b = Series([9, 9, 9, 9, 9, 9, 9], index=idx)
|
25 |
+
|
26 |
+
result = a.combine_first(b)
|
27 |
+
expected = Series([1, 9, 9, 4, 5, 9, 7], index=idx, dtype=np.float64)
|
28 |
+
tm.assert_series_equal(result, expected)
|
29 |
+
|
30 |
+
def test_combine_first_name(self, datetime_series):
|
31 |
+
result = datetime_series.combine_first(datetime_series[:5])
|
32 |
+
assert result.name == datetime_series.name
|
33 |
+
|
34 |
+
def test_combine_first(self):
|
35 |
+
values = np.arange(20, dtype=np.float64)
|
36 |
+
series = Series(values, index=np.arange(20, dtype=np.int64))
|
37 |
+
|
38 |
+
series_copy = series * 2
|
39 |
+
series_copy[::2] = np.nan
|
40 |
+
|
41 |
+
# nothing used from the input
|
42 |
+
combined = series.combine_first(series_copy)
|
43 |
+
|
44 |
+
tm.assert_series_equal(combined, series)
|
45 |
+
|
46 |
+
# Holes filled from input
|
47 |
+
combined = series_copy.combine_first(series)
|
48 |
+
assert np.isfinite(combined).all()
|
49 |
+
|
50 |
+
tm.assert_series_equal(combined[::2], series[::2])
|
51 |
+
tm.assert_series_equal(combined[1::2], series_copy[1::2])
|
52 |
+
|
53 |
+
# mixed types
|
54 |
+
index = pd.Index([str(i) for i in range(20)])
|
55 |
+
floats = Series(np.random.default_rng(2).standard_normal(20), index=index)
|
56 |
+
strings = Series([str(i) for i in range(10)], index=index[::2], dtype=object)
|
57 |
+
|
58 |
+
combined = strings.combine_first(floats)
|
59 |
+
|
60 |
+
tm.assert_series_equal(strings, combined.loc[index[::2]])
|
61 |
+
tm.assert_series_equal(floats[1::2].astype(object), combined.loc[index[1::2]])
|
62 |
+
|
63 |
+
# corner case
|
64 |
+
ser = Series([1.0, 2, 3], index=[0, 1, 2])
|
65 |
+
empty = Series([], index=[], dtype=object)
|
66 |
+
msg = "The behavior of array concatenation with empty entries is deprecated"
|
67 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
68 |
+
result = ser.combine_first(empty)
|
69 |
+
ser.index = ser.index.astype("O")
|
70 |
+
tm.assert_series_equal(ser, result)
|
71 |
+
|
72 |
+
def test_combine_first_dt64(self, unit):
|
73 |
+
s0 = to_datetime(Series(["2010", np.nan])).dt.as_unit(unit)
|
74 |
+
s1 = to_datetime(Series([np.nan, "2011"])).dt.as_unit(unit)
|
75 |
+
rs = s0.combine_first(s1)
|
76 |
+
xp = to_datetime(Series(["2010", "2011"])).dt.as_unit(unit)
|
77 |
+
tm.assert_series_equal(rs, xp)
|
78 |
+
|
79 |
+
s0 = to_datetime(Series(["2010", np.nan])).dt.as_unit(unit)
|
80 |
+
s1 = Series([np.nan, "2011"])
|
81 |
+
rs = s0.combine_first(s1)
|
82 |
+
|
83 |
+
xp = Series([datetime(2010, 1, 1), "2011"], dtype="datetime64[ns]")
|
84 |
+
|
85 |
+
tm.assert_series_equal(rs, xp)
|
86 |
+
|
87 |
+
def test_combine_first_dt_tz_values(self, tz_naive_fixture):
|
88 |
+
ser1 = Series(
|
89 |
+
pd.DatetimeIndex(["20150101", "20150102", "20150103"], tz=tz_naive_fixture),
|
90 |
+
name="ser1",
|
91 |
+
)
|
92 |
+
ser2 = Series(
|
93 |
+
pd.DatetimeIndex(["20160514", "20160515", "20160516"], tz=tz_naive_fixture),
|
94 |
+
index=[2, 3, 4],
|
95 |
+
name="ser2",
|
96 |
+
)
|
97 |
+
result = ser1.combine_first(ser2)
|
98 |
+
exp_vals = pd.DatetimeIndex(
|
99 |
+
["20150101", "20150102", "20150103", "20160515", "20160516"],
|
100 |
+
tz=tz_naive_fixture,
|
101 |
+
)
|
102 |
+
exp = Series(exp_vals, name="ser1")
|
103 |
+
tm.assert_series_equal(exp, result)
|
104 |
+
|
105 |
+
def test_combine_first_timezone_series_with_empty_series(self):
|
106 |
+
# GH 41800
|
107 |
+
time_index = date_range(
|
108 |
+
datetime(2021, 1, 1, 1),
|
109 |
+
datetime(2021, 1, 1, 10),
|
110 |
+
freq="h",
|
111 |
+
tz="Europe/Rome",
|
112 |
+
)
|
113 |
+
s1 = Series(range(10), index=time_index)
|
114 |
+
s2 = Series(index=time_index)
|
115 |
+
msg = "The behavior of array concatenation with empty entries is deprecated"
|
116 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
117 |
+
result = s1.combine_first(s2)
|
118 |
+
tm.assert_series_equal(result, s1)
|
119 |
+
|
120 |
+
def test_combine_first_preserves_dtype(self):
|
121 |
+
# GH51764
|
122 |
+
s1 = Series([1666880195890293744, 1666880195890293837])
|
123 |
+
s2 = Series([1, 2, 3])
|
124 |
+
result = s1.combine_first(s2)
|
125 |
+
expected = Series([1666880195890293744, 1666880195890293837, 3])
|
126 |
+
tm.assert_series_equal(result, expected)
|
127 |
+
|
128 |
+
def test_combine_mixed_timezone(self):
|
129 |
+
# GH 26283
|
130 |
+
uniform_tz = Series({pd.Timestamp("2019-05-01", tz="UTC"): 1.0})
|
131 |
+
multi_tz = Series(
|
132 |
+
{
|
133 |
+
pd.Timestamp("2019-05-01 01:00:00+0100", tz="Europe/London"): 2.0,
|
134 |
+
pd.Timestamp("2019-05-02", tz="UTC"): 3.0,
|
135 |
+
}
|
136 |
+
)
|
137 |
+
|
138 |
+
result = uniform_tz.combine_first(multi_tz)
|
139 |
+
expected = Series(
|
140 |
+
[1.0, 3.0],
|
141 |
+
index=pd.Index(
|
142 |
+
[
|
143 |
+
pd.Timestamp("2019-05-01 00:00:00+00:00", tz="UTC"),
|
144 |
+
pd.Timestamp("2019-05-02 00:00:00+00:00", tz="UTC"),
|
145 |
+
],
|
146 |
+
dtype="object",
|
147 |
+
),
|
148 |
+
)
|
149 |
+
tm.assert_series_equal(result, expected)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_compare.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
import pandas._testing as tm
|
6 |
+
|
7 |
+
|
8 |
+
@pytest.mark.parametrize("align_axis", [0, 1, "index", "columns"])
|
9 |
+
def test_compare_axis(align_axis):
|
10 |
+
# GH#30429
|
11 |
+
s1 = pd.Series(["a", "b", "c"])
|
12 |
+
s2 = pd.Series(["x", "b", "z"])
|
13 |
+
|
14 |
+
result = s1.compare(s2, align_axis=align_axis)
|
15 |
+
|
16 |
+
if align_axis in (1, "columns"):
|
17 |
+
indices = pd.Index([0, 2])
|
18 |
+
columns = pd.Index(["self", "other"])
|
19 |
+
expected = pd.DataFrame(
|
20 |
+
[["a", "x"], ["c", "z"]], index=indices, columns=columns
|
21 |
+
)
|
22 |
+
tm.assert_frame_equal(result, expected)
|
23 |
+
else:
|
24 |
+
indices = pd.MultiIndex.from_product([[0, 2], ["self", "other"]])
|
25 |
+
expected = pd.Series(["a", "x", "c", "z"], index=indices)
|
26 |
+
tm.assert_series_equal(result, expected)
|
27 |
+
|
28 |
+
|
29 |
+
@pytest.mark.parametrize(
|
30 |
+
"keep_shape, keep_equal",
|
31 |
+
[
|
32 |
+
(True, False),
|
33 |
+
(False, True),
|
34 |
+
(True, True),
|
35 |
+
# False, False case is already covered in test_compare_axis
|
36 |
+
],
|
37 |
+
)
|
38 |
+
def test_compare_various_formats(keep_shape, keep_equal):
|
39 |
+
s1 = pd.Series(["a", "b", "c"])
|
40 |
+
s2 = pd.Series(["x", "b", "z"])
|
41 |
+
|
42 |
+
result = s1.compare(s2, keep_shape=keep_shape, keep_equal=keep_equal)
|
43 |
+
|
44 |
+
if keep_shape:
|
45 |
+
indices = pd.Index([0, 1, 2])
|
46 |
+
columns = pd.Index(["self", "other"])
|
47 |
+
if keep_equal:
|
48 |
+
expected = pd.DataFrame(
|
49 |
+
[["a", "x"], ["b", "b"], ["c", "z"]], index=indices, columns=columns
|
50 |
+
)
|
51 |
+
else:
|
52 |
+
expected = pd.DataFrame(
|
53 |
+
[["a", "x"], [np.nan, np.nan], ["c", "z"]],
|
54 |
+
index=indices,
|
55 |
+
columns=columns,
|
56 |
+
)
|
57 |
+
else:
|
58 |
+
indices = pd.Index([0, 2])
|
59 |
+
columns = pd.Index(["self", "other"])
|
60 |
+
expected = pd.DataFrame(
|
61 |
+
[["a", "x"], ["c", "z"]], index=indices, columns=columns
|
62 |
+
)
|
63 |
+
tm.assert_frame_equal(result, expected)
|
64 |
+
|
65 |
+
|
66 |
+
def test_compare_with_equal_nulls():
|
67 |
+
# We want to make sure two NaNs are considered the same
|
68 |
+
# and dropped where applicable
|
69 |
+
s1 = pd.Series(["a", "b", np.nan])
|
70 |
+
s2 = pd.Series(["x", "b", np.nan])
|
71 |
+
|
72 |
+
result = s1.compare(s2)
|
73 |
+
expected = pd.DataFrame([["a", "x"]], columns=["self", "other"])
|
74 |
+
tm.assert_frame_equal(result, expected)
|
75 |
+
|
76 |
+
|
77 |
+
def test_compare_with_non_equal_nulls():
|
78 |
+
# We want to make sure the relevant NaNs do not get dropped
|
79 |
+
s1 = pd.Series(["a", "b", "c"])
|
80 |
+
s2 = pd.Series(["x", "b", np.nan])
|
81 |
+
|
82 |
+
result = s1.compare(s2, align_axis=0)
|
83 |
+
|
84 |
+
indices = pd.MultiIndex.from_product([[0, 2], ["self", "other"]])
|
85 |
+
expected = pd.Series(["a", "x", "c", np.nan], index=indices)
|
86 |
+
tm.assert_series_equal(result, expected)
|
87 |
+
|
88 |
+
|
89 |
+
def test_compare_multi_index():
|
90 |
+
index = pd.MultiIndex.from_arrays([[0, 0, 1], [0, 1, 2]])
|
91 |
+
s1 = pd.Series(["a", "b", "c"], index=index)
|
92 |
+
s2 = pd.Series(["x", "b", "z"], index=index)
|
93 |
+
|
94 |
+
result = s1.compare(s2, align_axis=0)
|
95 |
+
|
96 |
+
indices = pd.MultiIndex.from_arrays(
|
97 |
+
[[0, 0, 1, 1], [0, 0, 2, 2], ["self", "other", "self", "other"]]
|
98 |
+
)
|
99 |
+
expected = pd.Series(["a", "x", "c", "z"], index=indices)
|
100 |
+
tm.assert_series_equal(result, expected)
|
101 |
+
|
102 |
+
|
103 |
+
def test_compare_unaligned_objects():
|
104 |
+
# test Series with different indices
|
105 |
+
msg = "Can only compare identically-labeled Series objects"
|
106 |
+
with pytest.raises(ValueError, match=msg):
|
107 |
+
ser1 = pd.Series([1, 2, 3], index=["a", "b", "c"])
|
108 |
+
ser2 = pd.Series([1, 2, 3], index=["a", "b", "d"])
|
109 |
+
ser1.compare(ser2)
|
110 |
+
|
111 |
+
# test Series with different lengths
|
112 |
+
msg = "Can only compare identically-labeled Series objects"
|
113 |
+
with pytest.raises(ValueError, match=msg):
|
114 |
+
ser1 = pd.Series([1, 2, 3])
|
115 |
+
ser2 = pd.Series([1, 2, 3, 4])
|
116 |
+
ser1.compare(ser2)
|
117 |
+
|
118 |
+
|
119 |
+
def test_compare_datetime64_and_string():
|
120 |
+
# Issue https://github.com/pandas-dev/pandas/issues/45506
|
121 |
+
# Catch OverflowError when comparing datetime64 and string
|
122 |
+
data = [
|
123 |
+
{"a": "2015-07-01", "b": "08335394550"},
|
124 |
+
{"a": "2015-07-02", "b": "+49 (0) 0345 300033"},
|
125 |
+
{"a": "2015-07-03", "b": "+49(0)2598 04457"},
|
126 |
+
{"a": "2015-07-04", "b": "0741470003"},
|
127 |
+
{"a": "2015-07-05", "b": "04181 83668"},
|
128 |
+
]
|
129 |
+
dtypes = {"a": "datetime64[ns]", "b": "string"}
|
130 |
+
df = pd.DataFrame(data=data).astype(dtypes)
|
131 |
+
|
132 |
+
result_eq1 = df["a"].eq(df["b"])
|
133 |
+
result_eq2 = df["a"] == df["b"]
|
134 |
+
result_neq = df["a"] != df["b"]
|
135 |
+
|
136 |
+
expected_eq = pd.Series([False] * 5) # For .eq and ==
|
137 |
+
expected_neq = pd.Series([True] * 5) # For !=
|
138 |
+
|
139 |
+
tm.assert_series_equal(result_eq1, expected_eq)
|
140 |
+
tm.assert_series_equal(result_eq2, expected_eq)
|
141 |
+
tm.assert_series_equal(result_neq, expected_neq)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_convert_dtypes.py
ADDED
@@ -0,0 +1,306 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from itertools import product
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
from pandas._libs import lib
|
7 |
+
|
8 |
+
import pandas as pd
|
9 |
+
import pandas._testing as tm
|
10 |
+
|
11 |
+
# Each test case consists of a tuple with the data and dtype to create the
|
12 |
+
# test Series, the default dtype for the expected result (which is valid
|
13 |
+
# for most cases), and the specific cases where the result deviates from
|
14 |
+
# this default. Those overrides are defined as a dict with (keyword, val) as
|
15 |
+
# dictionary key. In case of multiple items, the last override takes precedence.
|
16 |
+
|
17 |
+
|
18 |
+
@pytest.fixture(
|
19 |
+
params=[
|
20 |
+
(
|
21 |
+
# data
|
22 |
+
[1, 2, 3],
|
23 |
+
# original dtype
|
24 |
+
np.dtype("int32"),
|
25 |
+
# default expected dtype
|
26 |
+
"Int32",
|
27 |
+
# exceptions on expected dtype
|
28 |
+
{("convert_integer", False): np.dtype("int32")},
|
29 |
+
),
|
30 |
+
(
|
31 |
+
[1, 2, 3],
|
32 |
+
np.dtype("int64"),
|
33 |
+
"Int64",
|
34 |
+
{("convert_integer", False): np.dtype("int64")},
|
35 |
+
),
|
36 |
+
(
|
37 |
+
["x", "y", "z"],
|
38 |
+
np.dtype("O"),
|
39 |
+
pd.StringDtype(),
|
40 |
+
{("convert_string", False): np.dtype("O")},
|
41 |
+
),
|
42 |
+
(
|
43 |
+
[True, False, np.nan],
|
44 |
+
np.dtype("O"),
|
45 |
+
pd.BooleanDtype(),
|
46 |
+
{("convert_boolean", False): np.dtype("O")},
|
47 |
+
),
|
48 |
+
(
|
49 |
+
["h", "i", np.nan],
|
50 |
+
np.dtype("O"),
|
51 |
+
pd.StringDtype(),
|
52 |
+
{("convert_string", False): np.dtype("O")},
|
53 |
+
),
|
54 |
+
( # GH32117
|
55 |
+
["h", "i", 1],
|
56 |
+
np.dtype("O"),
|
57 |
+
np.dtype("O"),
|
58 |
+
{},
|
59 |
+
),
|
60 |
+
(
|
61 |
+
[10, np.nan, 20],
|
62 |
+
np.dtype("float"),
|
63 |
+
"Int64",
|
64 |
+
{
|
65 |
+
("convert_integer", False, "convert_floating", True): "Float64",
|
66 |
+
("convert_integer", False, "convert_floating", False): np.dtype(
|
67 |
+
"float"
|
68 |
+
),
|
69 |
+
},
|
70 |
+
),
|
71 |
+
(
|
72 |
+
[np.nan, 100.5, 200],
|
73 |
+
np.dtype("float"),
|
74 |
+
"Float64",
|
75 |
+
{("convert_floating", False): np.dtype("float")},
|
76 |
+
),
|
77 |
+
(
|
78 |
+
[3, 4, 5],
|
79 |
+
"Int8",
|
80 |
+
"Int8",
|
81 |
+
{},
|
82 |
+
),
|
83 |
+
(
|
84 |
+
[[1, 2], [3, 4], [5]],
|
85 |
+
None,
|
86 |
+
np.dtype("O"),
|
87 |
+
{},
|
88 |
+
),
|
89 |
+
(
|
90 |
+
[4, 5, 6],
|
91 |
+
np.dtype("uint32"),
|
92 |
+
"UInt32",
|
93 |
+
{("convert_integer", False): np.dtype("uint32")},
|
94 |
+
),
|
95 |
+
(
|
96 |
+
[-10, 12, 13],
|
97 |
+
np.dtype("i1"),
|
98 |
+
"Int8",
|
99 |
+
{("convert_integer", False): np.dtype("i1")},
|
100 |
+
),
|
101 |
+
(
|
102 |
+
[1.2, 1.3],
|
103 |
+
np.dtype("float32"),
|
104 |
+
"Float32",
|
105 |
+
{("convert_floating", False): np.dtype("float32")},
|
106 |
+
),
|
107 |
+
(
|
108 |
+
[1, 2.0],
|
109 |
+
object,
|
110 |
+
"Int64",
|
111 |
+
{
|
112 |
+
("convert_integer", False): "Float64",
|
113 |
+
("convert_integer", False, "convert_floating", False): np.dtype(
|
114 |
+
"float"
|
115 |
+
),
|
116 |
+
("infer_objects", False): np.dtype("object"),
|
117 |
+
},
|
118 |
+
),
|
119 |
+
(
|
120 |
+
[1, 2.5],
|
121 |
+
object,
|
122 |
+
"Float64",
|
123 |
+
{
|
124 |
+
("convert_floating", False): np.dtype("float"),
|
125 |
+
("infer_objects", False): np.dtype("object"),
|
126 |
+
},
|
127 |
+
),
|
128 |
+
(["a", "b"], pd.CategoricalDtype(), pd.CategoricalDtype(), {}),
|
129 |
+
(
|
130 |
+
pd.to_datetime(["2020-01-14 10:00", "2020-01-15 11:11"]).as_unit("s"),
|
131 |
+
pd.DatetimeTZDtype(tz="UTC"),
|
132 |
+
pd.DatetimeTZDtype(tz="UTC"),
|
133 |
+
{},
|
134 |
+
),
|
135 |
+
(
|
136 |
+
pd.to_datetime(["2020-01-14 10:00", "2020-01-15 11:11"]).as_unit("ms"),
|
137 |
+
pd.DatetimeTZDtype(tz="UTC"),
|
138 |
+
pd.DatetimeTZDtype(tz="UTC"),
|
139 |
+
{},
|
140 |
+
),
|
141 |
+
(
|
142 |
+
pd.to_datetime(["2020-01-14 10:00", "2020-01-15 11:11"]).as_unit("us"),
|
143 |
+
pd.DatetimeTZDtype(tz="UTC"),
|
144 |
+
pd.DatetimeTZDtype(tz="UTC"),
|
145 |
+
{},
|
146 |
+
),
|
147 |
+
(
|
148 |
+
pd.to_datetime(["2020-01-14 10:00", "2020-01-15 11:11"]).as_unit("ns"),
|
149 |
+
pd.DatetimeTZDtype(tz="UTC"),
|
150 |
+
pd.DatetimeTZDtype(tz="UTC"),
|
151 |
+
{},
|
152 |
+
),
|
153 |
+
(
|
154 |
+
pd.to_datetime(["2020-01-14 10:00", "2020-01-15 11:11"]).as_unit("ns"),
|
155 |
+
"datetime64[ns]",
|
156 |
+
np.dtype("datetime64[ns]"),
|
157 |
+
{},
|
158 |
+
),
|
159 |
+
(
|
160 |
+
pd.to_datetime(["2020-01-14 10:00", "2020-01-15 11:11"]).as_unit("ns"),
|
161 |
+
object,
|
162 |
+
np.dtype("datetime64[ns]"),
|
163 |
+
{("infer_objects", False): np.dtype("object")},
|
164 |
+
),
|
165 |
+
(
|
166 |
+
pd.period_range("1/1/2011", freq="M", periods=3),
|
167 |
+
None,
|
168 |
+
pd.PeriodDtype("M"),
|
169 |
+
{},
|
170 |
+
),
|
171 |
+
(
|
172 |
+
pd.arrays.IntervalArray([pd.Interval(0, 1), pd.Interval(1, 5)]),
|
173 |
+
None,
|
174 |
+
pd.IntervalDtype("int64", "right"),
|
175 |
+
{},
|
176 |
+
),
|
177 |
+
]
|
178 |
+
)
|
179 |
+
def test_cases(request):
|
180 |
+
return request.param
|
181 |
+
|
182 |
+
|
183 |
+
class TestSeriesConvertDtypes:
|
184 |
+
@pytest.mark.parametrize("params", product(*[(True, False)] * 5))
|
185 |
+
def test_convert_dtypes(
|
186 |
+
self,
|
187 |
+
test_cases,
|
188 |
+
params,
|
189 |
+
using_infer_string,
|
190 |
+
):
|
191 |
+
data, maindtype, expected_default, expected_other = test_cases
|
192 |
+
if (
|
193 |
+
hasattr(data, "dtype")
|
194 |
+
and lib.is_np_dtype(data.dtype, "M")
|
195 |
+
and isinstance(maindtype, pd.DatetimeTZDtype)
|
196 |
+
):
|
197 |
+
# this astype is deprecated in favor of tz_localize
|
198 |
+
msg = "Cannot use .astype to convert from timezone-naive dtype"
|
199 |
+
with pytest.raises(TypeError, match=msg):
|
200 |
+
pd.Series(data, dtype=maindtype)
|
201 |
+
return
|
202 |
+
|
203 |
+
if maindtype is not None:
|
204 |
+
series = pd.Series(data, dtype=maindtype)
|
205 |
+
else:
|
206 |
+
series = pd.Series(data)
|
207 |
+
|
208 |
+
result = series.convert_dtypes(*params)
|
209 |
+
|
210 |
+
param_names = [
|
211 |
+
"infer_objects",
|
212 |
+
"convert_string",
|
213 |
+
"convert_integer",
|
214 |
+
"convert_boolean",
|
215 |
+
"convert_floating",
|
216 |
+
]
|
217 |
+
params_dict = dict(zip(param_names, params))
|
218 |
+
|
219 |
+
expected_dtype = expected_default
|
220 |
+
for spec, dtype in expected_other.items():
|
221 |
+
if all(params_dict[key] is val for key, val in zip(spec[::2], spec[1::2])):
|
222 |
+
expected_dtype = dtype
|
223 |
+
if (
|
224 |
+
using_infer_string
|
225 |
+
and expected_default == "string"
|
226 |
+
and expected_dtype == object
|
227 |
+
and params[0]
|
228 |
+
and not params[1]
|
229 |
+
):
|
230 |
+
# If we would convert with convert strings then infer_objects converts
|
231 |
+
# with the option
|
232 |
+
expected_dtype = "string[pyarrow_numpy]"
|
233 |
+
|
234 |
+
expected = pd.Series(data, dtype=expected_dtype)
|
235 |
+
tm.assert_series_equal(result, expected)
|
236 |
+
|
237 |
+
# Test that it is a copy
|
238 |
+
copy = series.copy(deep=True)
|
239 |
+
|
240 |
+
if result.notna().sum() > 0 and result.dtype in ["interval[int64, right]"]:
|
241 |
+
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
|
242 |
+
result[result.notna()] = np.nan
|
243 |
+
else:
|
244 |
+
result[result.notna()] = np.nan
|
245 |
+
|
246 |
+
# Make sure original not changed
|
247 |
+
tm.assert_series_equal(series, copy)
|
248 |
+
|
249 |
+
def test_convert_string_dtype(self, nullable_string_dtype):
|
250 |
+
# https://github.com/pandas-dev/pandas/issues/31731 -> converting columns
|
251 |
+
# that are already string dtype
|
252 |
+
df = pd.DataFrame(
|
253 |
+
{"A": ["a", "b", pd.NA], "B": ["ä", "ö", "ü"]}, dtype=nullable_string_dtype
|
254 |
+
)
|
255 |
+
result = df.convert_dtypes()
|
256 |
+
tm.assert_frame_equal(df, result)
|
257 |
+
|
258 |
+
def test_convert_bool_dtype(self):
|
259 |
+
# GH32287
|
260 |
+
df = pd.DataFrame({"A": pd.array([True])})
|
261 |
+
tm.assert_frame_equal(df, df.convert_dtypes())
|
262 |
+
|
263 |
+
def test_convert_byte_string_dtype(self):
|
264 |
+
# GH-43183
|
265 |
+
byte_str = b"binary-string"
|
266 |
+
|
267 |
+
df = pd.DataFrame(data={"A": byte_str}, index=[0])
|
268 |
+
result = df.convert_dtypes()
|
269 |
+
expected = df
|
270 |
+
tm.assert_frame_equal(result, expected)
|
271 |
+
|
272 |
+
@pytest.mark.parametrize(
|
273 |
+
"infer_objects, dtype", [(True, "Int64"), (False, "object")]
|
274 |
+
)
|
275 |
+
def test_convert_dtype_object_with_na(self, infer_objects, dtype):
|
276 |
+
# GH#48791
|
277 |
+
ser = pd.Series([1, pd.NA])
|
278 |
+
result = ser.convert_dtypes(infer_objects=infer_objects)
|
279 |
+
expected = pd.Series([1, pd.NA], dtype=dtype)
|
280 |
+
tm.assert_series_equal(result, expected)
|
281 |
+
|
282 |
+
@pytest.mark.parametrize(
|
283 |
+
"infer_objects, dtype", [(True, "Float64"), (False, "object")]
|
284 |
+
)
|
285 |
+
def test_convert_dtype_object_with_na_float(self, infer_objects, dtype):
|
286 |
+
# GH#48791
|
287 |
+
ser = pd.Series([1.5, pd.NA])
|
288 |
+
result = ser.convert_dtypes(infer_objects=infer_objects)
|
289 |
+
expected = pd.Series([1.5, pd.NA], dtype=dtype)
|
290 |
+
tm.assert_series_equal(result, expected)
|
291 |
+
|
292 |
+
def test_convert_dtypes_pyarrow_to_np_nullable(self):
|
293 |
+
# GH 53648
|
294 |
+
pytest.importorskip("pyarrow")
|
295 |
+
ser = pd.Series(range(2), dtype="int32[pyarrow]")
|
296 |
+
result = ser.convert_dtypes(dtype_backend="numpy_nullable")
|
297 |
+
expected = pd.Series(range(2), dtype="Int32")
|
298 |
+
tm.assert_series_equal(result, expected)
|
299 |
+
|
300 |
+
def test_convert_dtypes_pyarrow_null(self):
|
301 |
+
# GH#55346
|
302 |
+
pa = pytest.importorskip("pyarrow")
|
303 |
+
ser = pd.Series([None, None])
|
304 |
+
result = ser.convert_dtypes(dtype_backend="pyarrow")
|
305 |
+
expected = pd.Series([None, None], dtype=pd.ArrowDtype(pa.null()))
|
306 |
+
tm.assert_series_equal(result, expected)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_count.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
import pandas as pd
|
4 |
+
from pandas import (
|
5 |
+
Categorical,
|
6 |
+
Series,
|
7 |
+
)
|
8 |
+
import pandas._testing as tm
|
9 |
+
|
10 |
+
|
11 |
+
class TestSeriesCount:
|
12 |
+
def test_count(self, datetime_series):
|
13 |
+
assert datetime_series.count() == len(datetime_series)
|
14 |
+
|
15 |
+
datetime_series[::2] = np.nan
|
16 |
+
|
17 |
+
assert datetime_series.count() == np.isfinite(datetime_series).sum()
|
18 |
+
|
19 |
+
def test_count_inf_as_na(self):
|
20 |
+
# GH#29478
|
21 |
+
ser = Series([pd.Timestamp("1990/1/1")])
|
22 |
+
msg = "use_inf_as_na option is deprecated"
|
23 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
24 |
+
with pd.option_context("use_inf_as_na", True):
|
25 |
+
assert ser.count() == 1
|
26 |
+
|
27 |
+
def test_count_categorical(self):
|
28 |
+
ser = Series(
|
29 |
+
Categorical(
|
30 |
+
[np.nan, 1, 2, np.nan], categories=[5, 4, 3, 2, 1], ordered=True
|
31 |
+
)
|
32 |
+
)
|
33 |
+
result = ser.count()
|
34 |
+
assert result == 2
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_cov_corr.py
ADDED
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import pytest
|
5 |
+
|
6 |
+
import pandas as pd
|
7 |
+
from pandas import (
|
8 |
+
Series,
|
9 |
+
date_range,
|
10 |
+
isna,
|
11 |
+
)
|
12 |
+
import pandas._testing as tm
|
13 |
+
|
14 |
+
|
15 |
+
class TestSeriesCov:
|
16 |
+
def test_cov(self, datetime_series):
|
17 |
+
# full overlap
|
18 |
+
tm.assert_almost_equal(
|
19 |
+
datetime_series.cov(datetime_series), datetime_series.std() ** 2
|
20 |
+
)
|
21 |
+
|
22 |
+
# partial overlap
|
23 |
+
tm.assert_almost_equal(
|
24 |
+
datetime_series[:15].cov(datetime_series[5:]),
|
25 |
+
datetime_series[5:15].std() ** 2,
|
26 |
+
)
|
27 |
+
|
28 |
+
# No overlap
|
29 |
+
assert np.isnan(datetime_series[::2].cov(datetime_series[1::2]))
|
30 |
+
|
31 |
+
# all NA
|
32 |
+
cp = datetime_series[:10].copy()
|
33 |
+
cp[:] = np.nan
|
34 |
+
assert isna(cp.cov(cp))
|
35 |
+
|
36 |
+
# min_periods
|
37 |
+
assert isna(datetime_series[:15].cov(datetime_series[5:], min_periods=12))
|
38 |
+
|
39 |
+
ts1 = datetime_series[:15].reindex(datetime_series.index)
|
40 |
+
ts2 = datetime_series[5:].reindex(datetime_series.index)
|
41 |
+
assert isna(ts1.cov(ts2, min_periods=12))
|
42 |
+
|
43 |
+
@pytest.mark.parametrize("test_ddof", [None, 0, 1, 2, 3])
|
44 |
+
@pytest.mark.parametrize("dtype", ["float64", "Float64"])
|
45 |
+
def test_cov_ddof(self, test_ddof, dtype):
|
46 |
+
# GH#34611
|
47 |
+
np_array1 = np.random.default_rng(2).random(10)
|
48 |
+
np_array2 = np.random.default_rng(2).random(10)
|
49 |
+
|
50 |
+
s1 = Series(np_array1, dtype=dtype)
|
51 |
+
s2 = Series(np_array2, dtype=dtype)
|
52 |
+
|
53 |
+
result = s1.cov(s2, ddof=test_ddof)
|
54 |
+
expected = np.cov(np_array1, np_array2, ddof=test_ddof)[0][1]
|
55 |
+
assert math.isclose(expected, result)
|
56 |
+
|
57 |
+
|
58 |
+
class TestSeriesCorr:
|
59 |
+
@pytest.mark.parametrize("dtype", ["float64", "Float64"])
|
60 |
+
def test_corr(self, datetime_series, dtype):
|
61 |
+
stats = pytest.importorskip("scipy.stats")
|
62 |
+
|
63 |
+
datetime_series = datetime_series.astype(dtype)
|
64 |
+
|
65 |
+
# full overlap
|
66 |
+
tm.assert_almost_equal(datetime_series.corr(datetime_series), 1)
|
67 |
+
|
68 |
+
# partial overlap
|
69 |
+
tm.assert_almost_equal(datetime_series[:15].corr(datetime_series[5:]), 1)
|
70 |
+
|
71 |
+
assert isna(datetime_series[:15].corr(datetime_series[5:], min_periods=12))
|
72 |
+
|
73 |
+
ts1 = datetime_series[:15].reindex(datetime_series.index)
|
74 |
+
ts2 = datetime_series[5:].reindex(datetime_series.index)
|
75 |
+
assert isna(ts1.corr(ts2, min_periods=12))
|
76 |
+
|
77 |
+
# No overlap
|
78 |
+
assert np.isnan(datetime_series[::2].corr(datetime_series[1::2]))
|
79 |
+
|
80 |
+
# all NA
|
81 |
+
cp = datetime_series[:10].copy()
|
82 |
+
cp[:] = np.nan
|
83 |
+
assert isna(cp.corr(cp))
|
84 |
+
|
85 |
+
A = Series(
|
86 |
+
np.arange(10, dtype=np.float64),
|
87 |
+
index=date_range("2020-01-01", periods=10),
|
88 |
+
name="ts",
|
89 |
+
)
|
90 |
+
B = A.copy()
|
91 |
+
result = A.corr(B)
|
92 |
+
expected, _ = stats.pearsonr(A, B)
|
93 |
+
tm.assert_almost_equal(result, expected)
|
94 |
+
|
95 |
+
def test_corr_rank(self):
|
96 |
+
stats = pytest.importorskip("scipy.stats")
|
97 |
+
|
98 |
+
# kendall and spearman
|
99 |
+
A = Series(
|
100 |
+
np.arange(10, dtype=np.float64),
|
101 |
+
index=date_range("2020-01-01", periods=10),
|
102 |
+
name="ts",
|
103 |
+
)
|
104 |
+
B = A.copy()
|
105 |
+
A[-5:] = A[:5].copy()
|
106 |
+
result = A.corr(B, method="kendall")
|
107 |
+
expected = stats.kendalltau(A, B)[0]
|
108 |
+
tm.assert_almost_equal(result, expected)
|
109 |
+
|
110 |
+
result = A.corr(B, method="spearman")
|
111 |
+
expected = stats.spearmanr(A, B)[0]
|
112 |
+
tm.assert_almost_equal(result, expected)
|
113 |
+
|
114 |
+
# results from R
|
115 |
+
A = Series(
|
116 |
+
[
|
117 |
+
-0.89926396,
|
118 |
+
0.94209606,
|
119 |
+
-1.03289164,
|
120 |
+
-0.95445587,
|
121 |
+
0.76910310,
|
122 |
+
-0.06430576,
|
123 |
+
-2.09704447,
|
124 |
+
0.40660407,
|
125 |
+
-0.89926396,
|
126 |
+
0.94209606,
|
127 |
+
]
|
128 |
+
)
|
129 |
+
B = Series(
|
130 |
+
[
|
131 |
+
-1.01270225,
|
132 |
+
-0.62210117,
|
133 |
+
-1.56895827,
|
134 |
+
0.59592943,
|
135 |
+
-0.01680292,
|
136 |
+
1.17258718,
|
137 |
+
-1.06009347,
|
138 |
+
-0.10222060,
|
139 |
+
-0.89076239,
|
140 |
+
0.89372375,
|
141 |
+
]
|
142 |
+
)
|
143 |
+
kexp = 0.4319297
|
144 |
+
sexp = 0.5853767
|
145 |
+
tm.assert_almost_equal(A.corr(B, method="kendall"), kexp)
|
146 |
+
tm.assert_almost_equal(A.corr(B, method="spearman"), sexp)
|
147 |
+
|
148 |
+
def test_corr_invalid_method(self):
|
149 |
+
# GH PR #22298
|
150 |
+
s1 = Series(np.random.default_rng(2).standard_normal(10))
|
151 |
+
s2 = Series(np.random.default_rng(2).standard_normal(10))
|
152 |
+
msg = "method must be either 'pearson', 'spearman', 'kendall', or a callable, "
|
153 |
+
with pytest.raises(ValueError, match=msg):
|
154 |
+
s1.corr(s2, method="____")
|
155 |
+
|
156 |
+
def test_corr_callable_method(self, datetime_series):
|
157 |
+
# simple correlation example
|
158 |
+
# returns 1 if exact equality, 0 otherwise
|
159 |
+
my_corr = lambda a, b: 1.0 if (a == b).all() else 0.0
|
160 |
+
|
161 |
+
# simple example
|
162 |
+
s1 = Series([1, 2, 3, 4, 5])
|
163 |
+
s2 = Series([5, 4, 3, 2, 1])
|
164 |
+
expected = 0
|
165 |
+
tm.assert_almost_equal(s1.corr(s2, method=my_corr), expected)
|
166 |
+
|
167 |
+
# full overlap
|
168 |
+
tm.assert_almost_equal(
|
169 |
+
datetime_series.corr(datetime_series, method=my_corr), 1.0
|
170 |
+
)
|
171 |
+
|
172 |
+
# partial overlap
|
173 |
+
tm.assert_almost_equal(
|
174 |
+
datetime_series[:15].corr(datetime_series[5:], method=my_corr), 1.0
|
175 |
+
)
|
176 |
+
|
177 |
+
# No overlap
|
178 |
+
assert np.isnan(
|
179 |
+
datetime_series[::2].corr(datetime_series[1::2], method=my_corr)
|
180 |
+
)
|
181 |
+
|
182 |
+
# dataframe example
|
183 |
+
df = pd.DataFrame([s1, s2])
|
184 |
+
expected = pd.DataFrame([{0: 1.0, 1: 0}, {0: 0, 1: 1.0}])
|
185 |
+
tm.assert_almost_equal(df.transpose().corr(method=my_corr), expected)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_diff.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
Series,
|
6 |
+
TimedeltaIndex,
|
7 |
+
date_range,
|
8 |
+
)
|
9 |
+
import pandas._testing as tm
|
10 |
+
|
11 |
+
|
12 |
+
class TestSeriesDiff:
|
13 |
+
def test_diff_np(self):
|
14 |
+
# TODO(__array_function__): could make np.diff return a Series
|
15 |
+
# matching ser.diff()
|
16 |
+
|
17 |
+
ser = Series(np.arange(5))
|
18 |
+
|
19 |
+
res = np.diff(ser)
|
20 |
+
expected = np.array([1, 1, 1, 1])
|
21 |
+
tm.assert_numpy_array_equal(res, expected)
|
22 |
+
|
23 |
+
def test_diff_int(self):
|
24 |
+
# int dtype
|
25 |
+
a = 10000000000000000
|
26 |
+
b = a + 1
|
27 |
+
ser = Series([a, b])
|
28 |
+
|
29 |
+
result = ser.diff()
|
30 |
+
assert result[1] == 1
|
31 |
+
|
32 |
+
def test_diff_tz(self):
|
33 |
+
# Combined datetime diff, normal diff and boolean diff test
|
34 |
+
ts = Series(
|
35 |
+
np.arange(10, dtype=np.float64),
|
36 |
+
index=date_range("2020-01-01", periods=10),
|
37 |
+
name="ts",
|
38 |
+
)
|
39 |
+
ts.diff()
|
40 |
+
|
41 |
+
# neg n
|
42 |
+
result = ts.diff(-1)
|
43 |
+
expected = ts - ts.shift(-1)
|
44 |
+
tm.assert_series_equal(result, expected)
|
45 |
+
|
46 |
+
# 0
|
47 |
+
result = ts.diff(0)
|
48 |
+
expected = ts - ts
|
49 |
+
tm.assert_series_equal(result, expected)
|
50 |
+
|
51 |
+
def test_diff_dt64(self):
|
52 |
+
# datetime diff (GH#3100)
|
53 |
+
ser = Series(date_range("20130102", periods=5))
|
54 |
+
result = ser.diff()
|
55 |
+
expected = ser - ser.shift(1)
|
56 |
+
tm.assert_series_equal(result, expected)
|
57 |
+
|
58 |
+
# timedelta diff
|
59 |
+
result = result - result.shift(1) # previous result
|
60 |
+
expected = expected.diff() # previously expected
|
61 |
+
tm.assert_series_equal(result, expected)
|
62 |
+
|
63 |
+
def test_diff_dt64tz(self):
|
64 |
+
# with tz
|
65 |
+
ser = Series(
|
66 |
+
date_range("2000-01-01 09:00:00", periods=5, tz="US/Eastern"), name="foo"
|
67 |
+
)
|
68 |
+
result = ser.diff()
|
69 |
+
expected = Series(TimedeltaIndex(["NaT"] + ["1 days"] * 4), name="foo")
|
70 |
+
tm.assert_series_equal(result, expected)
|
71 |
+
|
72 |
+
@pytest.mark.parametrize(
|
73 |
+
"input,output,diff",
|
74 |
+
[([False, True, True, False, False], [np.nan, True, False, True, False], 1)],
|
75 |
+
)
|
76 |
+
def test_diff_bool(self, input, output, diff):
|
77 |
+
# boolean series (test for fixing #17294)
|
78 |
+
ser = Series(input)
|
79 |
+
result = ser.diff()
|
80 |
+
expected = Series(output)
|
81 |
+
tm.assert_series_equal(result, expected)
|
82 |
+
|
83 |
+
def test_diff_object_dtype(self):
|
84 |
+
# object series
|
85 |
+
ser = Series([False, True, 5.0, np.nan, True, False])
|
86 |
+
result = ser.diff()
|
87 |
+
expected = ser - ser.shift(1)
|
88 |
+
tm.assert_series_equal(result, expected)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_drop_duplicates.py
ADDED
@@ -0,0 +1,267 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas as pd
|
5 |
+
from pandas import (
|
6 |
+
Categorical,
|
7 |
+
Series,
|
8 |
+
)
|
9 |
+
import pandas._testing as tm
|
10 |
+
|
11 |
+
|
12 |
+
@pytest.mark.parametrize(
|
13 |
+
"keep, expected",
|
14 |
+
[
|
15 |
+
("first", Series([False, False, False, False, True, True, False])),
|
16 |
+
("last", Series([False, True, True, False, False, False, False])),
|
17 |
+
(False, Series([False, True, True, False, True, True, False])),
|
18 |
+
],
|
19 |
+
)
|
20 |
+
def test_drop_duplicates(any_numpy_dtype, keep, expected):
|
21 |
+
tc = Series([1, 0, 3, 5, 3, 0, 4], dtype=np.dtype(any_numpy_dtype))
|
22 |
+
|
23 |
+
if tc.dtype == "bool":
|
24 |
+
pytest.skip("tested separately in test_drop_duplicates_bool")
|
25 |
+
|
26 |
+
tm.assert_series_equal(tc.duplicated(keep=keep), expected)
|
27 |
+
tm.assert_series_equal(tc.drop_duplicates(keep=keep), tc[~expected])
|
28 |
+
sc = tc.copy()
|
29 |
+
return_value = sc.drop_duplicates(keep=keep, inplace=True)
|
30 |
+
assert return_value is None
|
31 |
+
tm.assert_series_equal(sc, tc[~expected])
|
32 |
+
|
33 |
+
|
34 |
+
@pytest.mark.parametrize(
|
35 |
+
"keep, expected",
|
36 |
+
[
|
37 |
+
("first", Series([False, False, True, True])),
|
38 |
+
("last", Series([True, True, False, False])),
|
39 |
+
(False, Series([True, True, True, True])),
|
40 |
+
],
|
41 |
+
)
|
42 |
+
def test_drop_duplicates_bool(keep, expected):
|
43 |
+
tc = Series([True, False, True, False])
|
44 |
+
|
45 |
+
tm.assert_series_equal(tc.duplicated(keep=keep), expected)
|
46 |
+
tm.assert_series_equal(tc.drop_duplicates(keep=keep), tc[~expected])
|
47 |
+
sc = tc.copy()
|
48 |
+
return_value = sc.drop_duplicates(keep=keep, inplace=True)
|
49 |
+
tm.assert_series_equal(sc, tc[~expected])
|
50 |
+
assert return_value is None
|
51 |
+
|
52 |
+
|
53 |
+
@pytest.mark.parametrize("values", [[], list(range(5))])
|
54 |
+
def test_drop_duplicates_no_duplicates(any_numpy_dtype, keep, values):
|
55 |
+
tc = Series(values, dtype=np.dtype(any_numpy_dtype))
|
56 |
+
expected = Series([False] * len(tc), dtype="bool")
|
57 |
+
|
58 |
+
if tc.dtype == "bool":
|
59 |
+
# 0 -> False and 1-> True
|
60 |
+
# any other value would be duplicated
|
61 |
+
tc = tc[:2]
|
62 |
+
expected = expected[:2]
|
63 |
+
|
64 |
+
tm.assert_series_equal(tc.duplicated(keep=keep), expected)
|
65 |
+
|
66 |
+
result_dropped = tc.drop_duplicates(keep=keep)
|
67 |
+
tm.assert_series_equal(result_dropped, tc)
|
68 |
+
|
69 |
+
# validate shallow copy
|
70 |
+
assert result_dropped is not tc
|
71 |
+
|
72 |
+
|
73 |
+
class TestSeriesDropDuplicates:
|
74 |
+
@pytest.fixture(
|
75 |
+
params=["int_", "uint", "float64", "str_", "timedelta64[h]", "datetime64[D]"]
|
76 |
+
)
|
77 |
+
def dtype(self, request):
|
78 |
+
return request.param
|
79 |
+
|
80 |
+
@pytest.fixture
|
81 |
+
def cat_series_unused_category(self, dtype, ordered):
|
82 |
+
# Test case 1
|
83 |
+
cat_array = np.array([1, 2, 3, 4, 5], dtype=np.dtype(dtype))
|
84 |
+
|
85 |
+
input1 = np.array([1, 2, 3, 3], dtype=np.dtype(dtype))
|
86 |
+
cat = Categorical(input1, categories=cat_array, ordered=ordered)
|
87 |
+
tc1 = Series(cat)
|
88 |
+
return tc1
|
89 |
+
|
90 |
+
def test_drop_duplicates_categorical_non_bool(self, cat_series_unused_category):
|
91 |
+
tc1 = cat_series_unused_category
|
92 |
+
|
93 |
+
expected = Series([False, False, False, True])
|
94 |
+
|
95 |
+
result = tc1.duplicated()
|
96 |
+
tm.assert_series_equal(result, expected)
|
97 |
+
|
98 |
+
result = tc1.drop_duplicates()
|
99 |
+
tm.assert_series_equal(result, tc1[~expected])
|
100 |
+
|
101 |
+
sc = tc1.copy()
|
102 |
+
return_value = sc.drop_duplicates(inplace=True)
|
103 |
+
assert return_value is None
|
104 |
+
tm.assert_series_equal(sc, tc1[~expected])
|
105 |
+
|
106 |
+
def test_drop_duplicates_categorical_non_bool_keeplast(
|
107 |
+
self, cat_series_unused_category
|
108 |
+
):
|
109 |
+
tc1 = cat_series_unused_category
|
110 |
+
|
111 |
+
expected = Series([False, False, True, False])
|
112 |
+
|
113 |
+
result = tc1.duplicated(keep="last")
|
114 |
+
tm.assert_series_equal(result, expected)
|
115 |
+
|
116 |
+
result = tc1.drop_duplicates(keep="last")
|
117 |
+
tm.assert_series_equal(result, tc1[~expected])
|
118 |
+
|
119 |
+
sc = tc1.copy()
|
120 |
+
return_value = sc.drop_duplicates(keep="last", inplace=True)
|
121 |
+
assert return_value is None
|
122 |
+
tm.assert_series_equal(sc, tc1[~expected])
|
123 |
+
|
124 |
+
def test_drop_duplicates_categorical_non_bool_keepfalse(
|
125 |
+
self, cat_series_unused_category
|
126 |
+
):
|
127 |
+
tc1 = cat_series_unused_category
|
128 |
+
|
129 |
+
expected = Series([False, False, True, True])
|
130 |
+
|
131 |
+
result = tc1.duplicated(keep=False)
|
132 |
+
tm.assert_series_equal(result, expected)
|
133 |
+
|
134 |
+
result = tc1.drop_duplicates(keep=False)
|
135 |
+
tm.assert_series_equal(result, tc1[~expected])
|
136 |
+
|
137 |
+
sc = tc1.copy()
|
138 |
+
return_value = sc.drop_duplicates(keep=False, inplace=True)
|
139 |
+
assert return_value is None
|
140 |
+
tm.assert_series_equal(sc, tc1[~expected])
|
141 |
+
|
142 |
+
@pytest.fixture
|
143 |
+
def cat_series(self, dtype, ordered):
|
144 |
+
# no unused categories, unlike cat_series_unused_category
|
145 |
+
cat_array = np.array([1, 2, 3, 4, 5], dtype=np.dtype(dtype))
|
146 |
+
|
147 |
+
input2 = np.array([1, 2, 3, 5, 3, 2, 4], dtype=np.dtype(dtype))
|
148 |
+
cat = Categorical(input2, categories=cat_array, ordered=ordered)
|
149 |
+
tc2 = Series(cat)
|
150 |
+
return tc2
|
151 |
+
|
152 |
+
def test_drop_duplicates_categorical_non_bool2(self, cat_series):
|
153 |
+
tc2 = cat_series
|
154 |
+
|
155 |
+
expected = Series([False, False, False, False, True, True, False])
|
156 |
+
|
157 |
+
result = tc2.duplicated()
|
158 |
+
tm.assert_series_equal(result, expected)
|
159 |
+
|
160 |
+
result = tc2.drop_duplicates()
|
161 |
+
tm.assert_series_equal(result, tc2[~expected])
|
162 |
+
|
163 |
+
sc = tc2.copy()
|
164 |
+
return_value = sc.drop_duplicates(inplace=True)
|
165 |
+
assert return_value is None
|
166 |
+
tm.assert_series_equal(sc, tc2[~expected])
|
167 |
+
|
168 |
+
def test_drop_duplicates_categorical_non_bool2_keeplast(self, cat_series):
|
169 |
+
tc2 = cat_series
|
170 |
+
|
171 |
+
expected = Series([False, True, True, False, False, False, False])
|
172 |
+
|
173 |
+
result = tc2.duplicated(keep="last")
|
174 |
+
tm.assert_series_equal(result, expected)
|
175 |
+
|
176 |
+
result = tc2.drop_duplicates(keep="last")
|
177 |
+
tm.assert_series_equal(result, tc2[~expected])
|
178 |
+
|
179 |
+
sc = tc2.copy()
|
180 |
+
return_value = sc.drop_duplicates(keep="last", inplace=True)
|
181 |
+
assert return_value is None
|
182 |
+
tm.assert_series_equal(sc, tc2[~expected])
|
183 |
+
|
184 |
+
def test_drop_duplicates_categorical_non_bool2_keepfalse(self, cat_series):
|
185 |
+
tc2 = cat_series
|
186 |
+
|
187 |
+
expected = Series([False, True, True, False, True, True, False])
|
188 |
+
|
189 |
+
result = tc2.duplicated(keep=False)
|
190 |
+
tm.assert_series_equal(result, expected)
|
191 |
+
|
192 |
+
result = tc2.drop_duplicates(keep=False)
|
193 |
+
tm.assert_series_equal(result, tc2[~expected])
|
194 |
+
|
195 |
+
sc = tc2.copy()
|
196 |
+
return_value = sc.drop_duplicates(keep=False, inplace=True)
|
197 |
+
assert return_value is None
|
198 |
+
tm.assert_series_equal(sc, tc2[~expected])
|
199 |
+
|
200 |
+
def test_drop_duplicates_categorical_bool(self, ordered):
|
201 |
+
tc = Series(
|
202 |
+
Categorical(
|
203 |
+
[True, False, True, False], categories=[True, False], ordered=ordered
|
204 |
+
)
|
205 |
+
)
|
206 |
+
|
207 |
+
expected = Series([False, False, True, True])
|
208 |
+
tm.assert_series_equal(tc.duplicated(), expected)
|
209 |
+
tm.assert_series_equal(tc.drop_duplicates(), tc[~expected])
|
210 |
+
sc = tc.copy()
|
211 |
+
return_value = sc.drop_duplicates(inplace=True)
|
212 |
+
assert return_value is None
|
213 |
+
tm.assert_series_equal(sc, tc[~expected])
|
214 |
+
|
215 |
+
expected = Series([True, True, False, False])
|
216 |
+
tm.assert_series_equal(tc.duplicated(keep="last"), expected)
|
217 |
+
tm.assert_series_equal(tc.drop_duplicates(keep="last"), tc[~expected])
|
218 |
+
sc = tc.copy()
|
219 |
+
return_value = sc.drop_duplicates(keep="last", inplace=True)
|
220 |
+
assert return_value is None
|
221 |
+
tm.assert_series_equal(sc, tc[~expected])
|
222 |
+
|
223 |
+
expected = Series([True, True, True, True])
|
224 |
+
tm.assert_series_equal(tc.duplicated(keep=False), expected)
|
225 |
+
tm.assert_series_equal(tc.drop_duplicates(keep=False), tc[~expected])
|
226 |
+
sc = tc.copy()
|
227 |
+
return_value = sc.drop_duplicates(keep=False, inplace=True)
|
228 |
+
assert return_value is None
|
229 |
+
tm.assert_series_equal(sc, tc[~expected])
|
230 |
+
|
231 |
+
def test_drop_duplicates_categorical_bool_na(self, nulls_fixture):
|
232 |
+
# GH#44351
|
233 |
+
ser = Series(
|
234 |
+
Categorical(
|
235 |
+
[True, False, True, False, nulls_fixture],
|
236 |
+
categories=[True, False],
|
237 |
+
ordered=True,
|
238 |
+
)
|
239 |
+
)
|
240 |
+
result = ser.drop_duplicates()
|
241 |
+
expected = Series(
|
242 |
+
Categorical([True, False, np.nan], categories=[True, False], ordered=True),
|
243 |
+
index=[0, 1, 4],
|
244 |
+
)
|
245 |
+
tm.assert_series_equal(result, expected)
|
246 |
+
|
247 |
+
def test_drop_duplicates_ignore_index(self):
|
248 |
+
# GH#48304
|
249 |
+
ser = Series([1, 2, 2, 3])
|
250 |
+
result = ser.drop_duplicates(ignore_index=True)
|
251 |
+
expected = Series([1, 2, 3])
|
252 |
+
tm.assert_series_equal(result, expected)
|
253 |
+
|
254 |
+
def test_duplicated_arrow_dtype(self):
|
255 |
+
pytest.importorskip("pyarrow")
|
256 |
+
ser = Series([True, False, None, False], dtype="bool[pyarrow]")
|
257 |
+
result = ser.drop_duplicates()
|
258 |
+
expected = Series([True, False, None], dtype="bool[pyarrow]")
|
259 |
+
tm.assert_series_equal(result, expected)
|
260 |
+
|
261 |
+
def test_drop_duplicates_arrow_strings(self):
|
262 |
+
# GH#54904
|
263 |
+
pa = pytest.importorskip("pyarrow")
|
264 |
+
ser = Series(["a", "a"], dtype=pd.ArrowDtype(pa.string()))
|
265 |
+
result = ser.drop_duplicates()
|
266 |
+
expecetd = Series(["a"], dtype=pd.ArrowDtype(pa.string()))
|
267 |
+
tm.assert_series_equal(result, expecetd)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_dropna.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
DatetimeIndex,
|
6 |
+
IntervalIndex,
|
7 |
+
NaT,
|
8 |
+
Period,
|
9 |
+
Series,
|
10 |
+
Timestamp,
|
11 |
+
)
|
12 |
+
import pandas._testing as tm
|
13 |
+
|
14 |
+
|
15 |
+
class TestDropna:
|
16 |
+
def test_dropna_empty(self):
|
17 |
+
ser = Series([], dtype=object)
|
18 |
+
|
19 |
+
assert len(ser.dropna()) == 0
|
20 |
+
return_value = ser.dropna(inplace=True)
|
21 |
+
assert return_value is None
|
22 |
+
assert len(ser) == 0
|
23 |
+
|
24 |
+
# invalid axis
|
25 |
+
msg = "No axis named 1 for object type Series"
|
26 |
+
with pytest.raises(ValueError, match=msg):
|
27 |
+
ser.dropna(axis=1)
|
28 |
+
|
29 |
+
def test_dropna_preserve_name(self, datetime_series):
|
30 |
+
datetime_series[:5] = np.nan
|
31 |
+
result = datetime_series.dropna()
|
32 |
+
assert result.name == datetime_series.name
|
33 |
+
name = datetime_series.name
|
34 |
+
ts = datetime_series.copy()
|
35 |
+
return_value = ts.dropna(inplace=True)
|
36 |
+
assert return_value is None
|
37 |
+
assert ts.name == name
|
38 |
+
|
39 |
+
def test_dropna_no_nan(self):
|
40 |
+
for ser in [
|
41 |
+
Series([1, 2, 3], name="x"),
|
42 |
+
Series([False, True, False], name="x"),
|
43 |
+
]:
|
44 |
+
result = ser.dropna()
|
45 |
+
tm.assert_series_equal(result, ser)
|
46 |
+
assert result is not ser
|
47 |
+
|
48 |
+
s2 = ser.copy()
|
49 |
+
return_value = s2.dropna(inplace=True)
|
50 |
+
assert return_value is None
|
51 |
+
tm.assert_series_equal(s2, ser)
|
52 |
+
|
53 |
+
def test_dropna_intervals(self):
|
54 |
+
ser = Series(
|
55 |
+
[np.nan, 1, 2, 3],
|
56 |
+
IntervalIndex.from_arrays([np.nan, 0, 1, 2], [np.nan, 1, 2, 3]),
|
57 |
+
)
|
58 |
+
|
59 |
+
result = ser.dropna()
|
60 |
+
expected = ser.iloc[1:]
|
61 |
+
tm.assert_series_equal(result, expected)
|
62 |
+
|
63 |
+
def test_dropna_period_dtype(self):
|
64 |
+
# GH#13737
|
65 |
+
ser = Series([Period("2011-01", freq="M"), Period("NaT", freq="M")])
|
66 |
+
result = ser.dropna()
|
67 |
+
expected = Series([Period("2011-01", freq="M")])
|
68 |
+
|
69 |
+
tm.assert_series_equal(result, expected)
|
70 |
+
|
71 |
+
def test_datetime64_tz_dropna(self, unit):
|
72 |
+
# DatetimeLikeBlock
|
73 |
+
ser = Series(
|
74 |
+
[
|
75 |
+
Timestamp("2011-01-01 10:00"),
|
76 |
+
NaT,
|
77 |
+
Timestamp("2011-01-03 10:00"),
|
78 |
+
NaT,
|
79 |
+
],
|
80 |
+
dtype=f"M8[{unit}]",
|
81 |
+
)
|
82 |
+
result = ser.dropna()
|
83 |
+
expected = Series(
|
84 |
+
[Timestamp("2011-01-01 10:00"), Timestamp("2011-01-03 10:00")],
|
85 |
+
index=[0, 2],
|
86 |
+
dtype=f"M8[{unit}]",
|
87 |
+
)
|
88 |
+
tm.assert_series_equal(result, expected)
|
89 |
+
|
90 |
+
# DatetimeTZBlock
|
91 |
+
idx = DatetimeIndex(
|
92 |
+
["2011-01-01 10:00", NaT, "2011-01-03 10:00", NaT], tz="Asia/Tokyo"
|
93 |
+
).as_unit(unit)
|
94 |
+
ser = Series(idx)
|
95 |
+
assert ser.dtype == f"datetime64[{unit}, Asia/Tokyo]"
|
96 |
+
result = ser.dropna()
|
97 |
+
expected = Series(
|
98 |
+
[
|
99 |
+
Timestamp("2011-01-01 10:00", tz="Asia/Tokyo"),
|
100 |
+
Timestamp("2011-01-03 10:00", tz="Asia/Tokyo"),
|
101 |
+
],
|
102 |
+
index=[0, 2],
|
103 |
+
dtype=f"datetime64[{unit}, Asia/Tokyo]",
|
104 |
+
)
|
105 |
+
assert result.dtype == f"datetime64[{unit}, Asia/Tokyo]"
|
106 |
+
tm.assert_series_equal(result, expected)
|
107 |
+
|
108 |
+
@pytest.mark.parametrize("val", [1, 1.5])
|
109 |
+
def test_dropna_ignore_index(self, val):
|
110 |
+
# GH#31725
|
111 |
+
ser = Series([1, 2, val], index=[3, 2, 1])
|
112 |
+
result = ser.dropna(ignore_index=True)
|
113 |
+
expected = Series([1, 2, val])
|
114 |
+
tm.assert_series_equal(result, expected)
|
115 |
+
|
116 |
+
ser.dropna(ignore_index=True, inplace=True)
|
117 |
+
tm.assert_series_equal(ser, expected)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_duplicated.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
from pandas import (
|
5 |
+
NA,
|
6 |
+
Categorical,
|
7 |
+
Series,
|
8 |
+
)
|
9 |
+
import pandas._testing as tm
|
10 |
+
|
11 |
+
|
12 |
+
@pytest.mark.parametrize(
|
13 |
+
"keep, expected",
|
14 |
+
[
|
15 |
+
("first", Series([False, False, True, False, True], name="name")),
|
16 |
+
("last", Series([True, True, False, False, False], name="name")),
|
17 |
+
(False, Series([True, True, True, False, True], name="name")),
|
18 |
+
],
|
19 |
+
)
|
20 |
+
def test_duplicated_keep(keep, expected):
|
21 |
+
ser = Series(["a", "b", "b", "c", "a"], name="name")
|
22 |
+
|
23 |
+
result = ser.duplicated(keep=keep)
|
24 |
+
tm.assert_series_equal(result, expected)
|
25 |
+
|
26 |
+
|
27 |
+
@pytest.mark.parametrize(
|
28 |
+
"keep, expected",
|
29 |
+
[
|
30 |
+
("first", Series([False, False, True, False, True])),
|
31 |
+
("last", Series([True, True, False, False, False])),
|
32 |
+
(False, Series([True, True, True, False, True])),
|
33 |
+
],
|
34 |
+
)
|
35 |
+
def test_duplicated_nan_none(keep, expected):
|
36 |
+
ser = Series([np.nan, 3, 3, None, np.nan], dtype=object)
|
37 |
+
|
38 |
+
result = ser.duplicated(keep=keep)
|
39 |
+
tm.assert_series_equal(result, expected)
|
40 |
+
|
41 |
+
|
42 |
+
def test_duplicated_categorical_bool_na(nulls_fixture):
|
43 |
+
# GH#44351
|
44 |
+
ser = Series(
|
45 |
+
Categorical(
|
46 |
+
[True, False, True, False, nulls_fixture],
|
47 |
+
categories=[True, False],
|
48 |
+
ordered=True,
|
49 |
+
)
|
50 |
+
)
|
51 |
+
result = ser.duplicated()
|
52 |
+
expected = Series([False, False, True, True, False])
|
53 |
+
tm.assert_series_equal(result, expected)
|
54 |
+
|
55 |
+
|
56 |
+
@pytest.mark.parametrize(
|
57 |
+
"keep, vals",
|
58 |
+
[
|
59 |
+
("last", [True, True, False]),
|
60 |
+
("first", [False, True, True]),
|
61 |
+
(False, [True, True, True]),
|
62 |
+
],
|
63 |
+
)
|
64 |
+
def test_duplicated_mask(keep, vals):
|
65 |
+
# GH#48150
|
66 |
+
ser = Series([1, 2, NA, NA, NA], dtype="Int64")
|
67 |
+
result = ser.duplicated(keep=keep)
|
68 |
+
expected = Series([False, False] + vals)
|
69 |
+
tm.assert_series_equal(result, expected)
|
70 |
+
|
71 |
+
|
72 |
+
def test_duplicated_mask_no_duplicated_na(keep):
|
73 |
+
# GH#48150
|
74 |
+
ser = Series([1, 2, NA], dtype="Int64")
|
75 |
+
result = ser.duplicated(keep=keep)
|
76 |
+
expected = Series([False, False, False])
|
77 |
+
tm.assert_series_equal(result, expected)
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_head_tail.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas._testing as tm
|
2 |
+
|
3 |
+
|
4 |
+
def test_head_tail(string_series):
|
5 |
+
tm.assert_series_equal(string_series.head(), string_series[:5])
|
6 |
+
tm.assert_series_equal(string_series.head(0), string_series[0:0])
|
7 |
+
tm.assert_series_equal(string_series.tail(), string_series[-5:])
|
8 |
+
tm.assert_series_equal(string_series.tail(0), string_series[0:0])
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_info.py
ADDED
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from io import StringIO
|
2 |
+
from string import ascii_uppercase
|
3 |
+
import textwrap
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
import pytest
|
7 |
+
|
8 |
+
from pandas.compat import PYPY
|
9 |
+
|
10 |
+
from pandas import (
|
11 |
+
CategoricalIndex,
|
12 |
+
MultiIndex,
|
13 |
+
Series,
|
14 |
+
date_range,
|
15 |
+
)
|
16 |
+
|
17 |
+
|
18 |
+
def test_info_categorical_column_just_works():
|
19 |
+
n = 2500
|
20 |
+
data = np.array(list("abcdefghij")).take(
|
21 |
+
np.random.default_rng(2).integers(0, 10, size=n, dtype=int)
|
22 |
+
)
|
23 |
+
s = Series(data).astype("category")
|
24 |
+
s.isna()
|
25 |
+
buf = StringIO()
|
26 |
+
s.info(buf=buf)
|
27 |
+
|
28 |
+
s2 = s[s == "d"]
|
29 |
+
buf = StringIO()
|
30 |
+
s2.info(buf=buf)
|
31 |
+
|
32 |
+
|
33 |
+
def test_info_categorical():
|
34 |
+
# GH14298
|
35 |
+
idx = CategoricalIndex(["a", "b"])
|
36 |
+
s = Series(np.zeros(2), index=idx)
|
37 |
+
buf = StringIO()
|
38 |
+
s.info(buf=buf)
|
39 |
+
|
40 |
+
|
41 |
+
@pytest.mark.parametrize("verbose", [True, False])
|
42 |
+
def test_info_series(lexsorted_two_level_string_multiindex, verbose):
|
43 |
+
index = lexsorted_two_level_string_multiindex
|
44 |
+
ser = Series(range(len(index)), index=index, name="sth")
|
45 |
+
buf = StringIO()
|
46 |
+
ser.info(verbose=verbose, buf=buf)
|
47 |
+
result = buf.getvalue()
|
48 |
+
|
49 |
+
expected = textwrap.dedent(
|
50 |
+
"""\
|
51 |
+
<class 'pandas.core.series.Series'>
|
52 |
+
MultiIndex: 10 entries, ('foo', 'one') to ('qux', 'three')
|
53 |
+
"""
|
54 |
+
)
|
55 |
+
if verbose:
|
56 |
+
expected += textwrap.dedent(
|
57 |
+
"""\
|
58 |
+
Series name: sth
|
59 |
+
Non-Null Count Dtype
|
60 |
+
-------------- -----
|
61 |
+
10 non-null int64
|
62 |
+
"""
|
63 |
+
)
|
64 |
+
expected += textwrap.dedent(
|
65 |
+
f"""\
|
66 |
+
dtypes: int64(1)
|
67 |
+
memory usage: {ser.memory_usage()}.0+ bytes
|
68 |
+
"""
|
69 |
+
)
|
70 |
+
assert result == expected
|
71 |
+
|
72 |
+
|
73 |
+
def test_info_memory():
|
74 |
+
s = Series([1, 2], dtype="i8")
|
75 |
+
buf = StringIO()
|
76 |
+
s.info(buf=buf)
|
77 |
+
result = buf.getvalue()
|
78 |
+
memory_bytes = float(s.memory_usage())
|
79 |
+
expected = textwrap.dedent(
|
80 |
+
f"""\
|
81 |
+
<class 'pandas.core.series.Series'>
|
82 |
+
RangeIndex: 2 entries, 0 to 1
|
83 |
+
Series name: None
|
84 |
+
Non-Null Count Dtype
|
85 |
+
-------------- -----
|
86 |
+
2 non-null int64
|
87 |
+
dtypes: int64(1)
|
88 |
+
memory usage: {memory_bytes} bytes
|
89 |
+
"""
|
90 |
+
)
|
91 |
+
assert result == expected
|
92 |
+
|
93 |
+
|
94 |
+
def test_info_wide():
|
95 |
+
s = Series(np.random.default_rng(2).standard_normal(101))
|
96 |
+
msg = "Argument `max_cols` can only be passed in DataFrame.info, not Series.info"
|
97 |
+
with pytest.raises(ValueError, match=msg):
|
98 |
+
s.info(max_cols=1)
|
99 |
+
|
100 |
+
|
101 |
+
def test_info_shows_dtypes():
|
102 |
+
dtypes = [
|
103 |
+
"int64",
|
104 |
+
"float64",
|
105 |
+
"datetime64[ns]",
|
106 |
+
"timedelta64[ns]",
|
107 |
+
"complex128",
|
108 |
+
"object",
|
109 |
+
"bool",
|
110 |
+
]
|
111 |
+
n = 10
|
112 |
+
for dtype in dtypes:
|
113 |
+
s = Series(np.random.default_rng(2).integers(2, size=n).astype(dtype))
|
114 |
+
buf = StringIO()
|
115 |
+
s.info(buf=buf)
|
116 |
+
res = buf.getvalue()
|
117 |
+
name = f"{n:d} non-null {dtype}"
|
118 |
+
assert name in res
|
119 |
+
|
120 |
+
|
121 |
+
@pytest.mark.xfail(PYPY, reason="on PyPy deep=True doesn't change result")
|
122 |
+
def test_info_memory_usage_deep_not_pypy():
|
123 |
+
s_with_object_index = Series({"a": [1]}, index=["foo"])
|
124 |
+
assert s_with_object_index.memory_usage(
|
125 |
+
index=True, deep=True
|
126 |
+
) > s_with_object_index.memory_usage(index=True)
|
127 |
+
|
128 |
+
s_object = Series({"a": ["a"]})
|
129 |
+
assert s_object.memory_usage(deep=True) > s_object.memory_usage()
|
130 |
+
|
131 |
+
|
132 |
+
@pytest.mark.xfail(not PYPY, reason="on PyPy deep=True does not change result")
|
133 |
+
def test_info_memory_usage_deep_pypy():
|
134 |
+
s_with_object_index = Series({"a": [1]}, index=["foo"])
|
135 |
+
assert s_with_object_index.memory_usage(
|
136 |
+
index=True, deep=True
|
137 |
+
) == s_with_object_index.memory_usage(index=True)
|
138 |
+
|
139 |
+
s_object = Series({"a": ["a"]})
|
140 |
+
assert s_object.memory_usage(deep=True) == s_object.memory_usage()
|
141 |
+
|
142 |
+
|
143 |
+
@pytest.mark.parametrize(
|
144 |
+
"series, plus",
|
145 |
+
[
|
146 |
+
(Series(1, index=[1, 2, 3]), False),
|
147 |
+
(Series(1, index=list("ABC")), True),
|
148 |
+
(Series(1, index=MultiIndex.from_product([range(3), range(3)])), False),
|
149 |
+
(
|
150 |
+
Series(1, index=MultiIndex.from_product([range(3), ["foo", "bar"]])),
|
151 |
+
True,
|
152 |
+
),
|
153 |
+
],
|
154 |
+
)
|
155 |
+
def test_info_memory_usage_qualified(series, plus):
|
156 |
+
buf = StringIO()
|
157 |
+
series.info(buf=buf)
|
158 |
+
if plus:
|
159 |
+
assert "+" in buf.getvalue()
|
160 |
+
else:
|
161 |
+
assert "+" not in buf.getvalue()
|
162 |
+
|
163 |
+
|
164 |
+
def test_info_memory_usage_bug_on_multiindex():
|
165 |
+
# GH 14308
|
166 |
+
# memory usage introspection should not materialize .values
|
167 |
+
N = 100
|
168 |
+
M = len(ascii_uppercase)
|
169 |
+
index = MultiIndex.from_product(
|
170 |
+
[list(ascii_uppercase), date_range("20160101", periods=N)],
|
171 |
+
names=["id", "date"],
|
172 |
+
)
|
173 |
+
s = Series(np.random.default_rng(2).standard_normal(N * M), index=index)
|
174 |
+
|
175 |
+
unstacked = s.unstack("id")
|
176 |
+
assert s.values.nbytes == unstacked.values.nbytes
|
177 |
+
assert s.memory_usage(deep=True) > unstacked.memory_usage(deep=True).sum()
|
178 |
+
|
179 |
+
# high upper bound
|
180 |
+
diff = unstacked.memory_usage(deep=True).sum() - s.memory_usage(deep=True)
|
181 |
+
assert diff < 2000
|
llmeval-env/lib/python3.10/site-packages/pandas/tests/series/methods/test_interpolate.py
ADDED
@@ -0,0 +1,868 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pytest
|
3 |
+
|
4 |
+
import pandas.util._test_decorators as td
|
5 |
+
|
6 |
+
import pandas as pd
|
7 |
+
from pandas import (
|
8 |
+
Index,
|
9 |
+
MultiIndex,
|
10 |
+
Series,
|
11 |
+
date_range,
|
12 |
+
isna,
|
13 |
+
)
|
14 |
+
import pandas._testing as tm
|
15 |
+
|
16 |
+
|
17 |
+
@pytest.fixture(
|
18 |
+
params=[
|
19 |
+
"linear",
|
20 |
+
"index",
|
21 |
+
"values",
|
22 |
+
"nearest",
|
23 |
+
"slinear",
|
24 |
+
"zero",
|
25 |
+
"quadratic",
|
26 |
+
"cubic",
|
27 |
+
"barycentric",
|
28 |
+
"krogh",
|
29 |
+
"polynomial",
|
30 |
+
"spline",
|
31 |
+
"piecewise_polynomial",
|
32 |
+
"from_derivatives",
|
33 |
+
"pchip",
|
34 |
+
"akima",
|
35 |
+
"cubicspline",
|
36 |
+
]
|
37 |
+
)
|
38 |
+
def nontemporal_method(request):
|
39 |
+
"""Fixture that returns an (method name, required kwargs) pair.
|
40 |
+
|
41 |
+
This fixture does not include method 'time' as a parameterization; that
|
42 |
+
method requires a Series with a DatetimeIndex, and is generally tested
|
43 |
+
separately from these non-temporal methods.
|
44 |
+
"""
|
45 |
+
method = request.param
|
46 |
+
kwargs = {"order": 1} if method in ("spline", "polynomial") else {}
|
47 |
+
return method, kwargs
|
48 |
+
|
49 |
+
|
50 |
+
@pytest.fixture(
|
51 |
+
params=[
|
52 |
+
"linear",
|
53 |
+
"slinear",
|
54 |
+
"zero",
|
55 |
+
"quadratic",
|
56 |
+
"cubic",
|
57 |
+
"barycentric",
|
58 |
+
"krogh",
|
59 |
+
"polynomial",
|
60 |
+
"spline",
|
61 |
+
"piecewise_polynomial",
|
62 |
+
"from_derivatives",
|
63 |
+
"pchip",
|
64 |
+
"akima",
|
65 |
+
"cubicspline",
|
66 |
+
]
|
67 |
+
)
|
68 |
+
def interp_methods_ind(request):
|
69 |
+
"""Fixture that returns a (method name, required kwargs) pair to
|
70 |
+
be tested for various Index types.
|
71 |
+
|
72 |
+
This fixture does not include methods - 'time', 'index', 'nearest',
|
73 |
+
'values' as a parameterization
|
74 |
+
"""
|
75 |
+
method = request.param
|
76 |
+
kwargs = {"order": 1} if method in ("spline", "polynomial") else {}
|
77 |
+
return method, kwargs
|
78 |
+
|
79 |
+
|
80 |
+
class TestSeriesInterpolateData:
|
81 |
+
@pytest.mark.xfail(reason="EA.fillna does not handle 'linear' method")
|
82 |
+
def test_interpolate_period_values(self):
|
83 |
+
orig = Series(date_range("2012-01-01", periods=5))
|
84 |
+
ser = orig.copy()
|
85 |
+
ser[2] = pd.NaT
|
86 |
+
|
87 |
+
# period cast
|
88 |
+
ser_per = ser.dt.to_period("D")
|
89 |
+
res_per = ser_per.interpolate()
|
90 |
+
expected_per = orig.dt.to_period("D")
|
91 |
+
tm.assert_series_equal(res_per, expected_per)
|
92 |
+
|
93 |
+
def test_interpolate(self, datetime_series):
|
94 |
+
ts = Series(np.arange(len(datetime_series), dtype=float), datetime_series.index)
|
95 |
+
|
96 |
+
ts_copy = ts.copy()
|
97 |
+
ts_copy[5:10] = np.nan
|
98 |
+
|
99 |
+
linear_interp = ts_copy.interpolate(method="linear")
|
100 |
+
tm.assert_series_equal(linear_interp, ts)
|
101 |
+
|
102 |
+
ord_ts = Series(
|
103 |
+
[d.toordinal() for d in datetime_series.index], index=datetime_series.index
|
104 |
+
).astype(float)
|
105 |
+
|
106 |
+
ord_ts_copy = ord_ts.copy()
|
107 |
+
ord_ts_copy[5:10] = np.nan
|
108 |
+
|
109 |
+
time_interp = ord_ts_copy.interpolate(method="time")
|
110 |
+
tm.assert_series_equal(time_interp, ord_ts)
|
111 |
+
|
112 |
+
def test_interpolate_time_raises_for_non_timeseries(self):
|
113 |
+
# When method='time' is used on a non-TimeSeries that contains a null
|
114 |
+
# value, a ValueError should be raised.
|
115 |
+
non_ts = Series([0, 1, 2, np.nan])
|
116 |
+
msg = "time-weighted interpolation only works on Series.* with a DatetimeIndex"
|
117 |
+
with pytest.raises(ValueError, match=msg):
|
118 |
+
non_ts.interpolate(method="time")
|
119 |
+
|
120 |
+
def test_interpolate_cubicspline(self):
|
121 |
+
pytest.importorskip("scipy")
|
122 |
+
ser = Series([10, 11, 12, 13])
|
123 |
+
|
124 |
+
expected = Series(
|
125 |
+
[11.00, 11.25, 11.50, 11.75, 12.00, 12.25, 12.50, 12.75, 13.00],
|
126 |
+
index=Index([1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0]),
|
127 |
+
)
|
128 |
+
# interpolate at new_index
|
129 |
+
new_index = ser.index.union(Index([1.25, 1.5, 1.75, 2.25, 2.5, 2.75])).astype(
|
130 |
+
float
|
131 |
+
)
|
132 |
+
result = ser.reindex(new_index).interpolate(method="cubicspline").loc[1:3]
|
133 |
+
tm.assert_series_equal(result, expected)
|
134 |
+
|
135 |
+
def test_interpolate_pchip(self):
|
136 |
+
pytest.importorskip("scipy")
|
137 |
+
ser = Series(np.sort(np.random.default_rng(2).uniform(size=100)))
|
138 |
+
|
139 |
+
# interpolate at new_index
|
140 |
+
new_index = ser.index.union(
|
141 |
+
Index([49.25, 49.5, 49.75, 50.25, 50.5, 50.75])
|
142 |
+
).astype(float)
|
143 |
+
interp_s = ser.reindex(new_index).interpolate(method="pchip")
|
144 |
+
# does not blow up, GH5977
|
145 |
+
interp_s.loc[49:51]
|
146 |
+
|
147 |
+
def test_interpolate_akima(self):
|
148 |
+
pytest.importorskip("scipy")
|
149 |
+
ser = Series([10, 11, 12, 13])
|
150 |
+
|
151 |
+
# interpolate at new_index where `der` is zero
|
152 |
+
expected = Series(
|
153 |
+
[11.00, 11.25, 11.50, 11.75, 12.00, 12.25, 12.50, 12.75, 13.00],
|
154 |
+
index=Index([1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0]),
|
155 |
+
)
|
156 |
+
new_index = ser.index.union(Index([1.25, 1.5, 1.75, 2.25, 2.5, 2.75])).astype(
|
157 |
+
float
|
158 |
+
)
|
159 |
+
interp_s = ser.reindex(new_index).interpolate(method="akima")
|
160 |
+
tm.assert_series_equal(interp_s.loc[1:3], expected)
|
161 |
+
|
162 |
+
# interpolate at new_index where `der` is a non-zero int
|
163 |
+
expected = Series(
|
164 |
+
[11.0, 1.0, 1.0, 1.0, 12.0, 1.0, 1.0, 1.0, 13.0],
|
165 |
+
index=Index([1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0]),
|
166 |
+
)
|
167 |
+
new_index = ser.index.union(Index([1.25, 1.5, 1.75, 2.25, 2.5, 2.75])).astype(
|
168 |
+
float
|
169 |
+
)
|
170 |
+
interp_s = ser.reindex(new_index).interpolate(method="akima", der=1)
|
171 |
+
tm.assert_series_equal(interp_s.loc[1:3], expected)
|
172 |
+
|
173 |
+
def test_interpolate_piecewise_polynomial(self):
|
174 |
+
pytest.importorskip("scipy")
|
175 |
+
ser = Series([10, 11, 12, 13])
|
176 |
+
|
177 |
+
expected = Series(
|
178 |
+
[11.00, 11.25, 11.50, 11.75, 12.00, 12.25, 12.50, 12.75, 13.00],
|
179 |
+
index=Index([1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0]),
|
180 |
+
)
|
181 |
+
# interpolate at new_index
|
182 |
+
new_index = ser.index.union(Index([1.25, 1.5, 1.75, 2.25, 2.5, 2.75])).astype(
|
183 |
+
float
|
184 |
+
)
|
185 |
+
interp_s = ser.reindex(new_index).interpolate(method="piecewise_polynomial")
|
186 |
+
tm.assert_series_equal(interp_s.loc[1:3], expected)
|
187 |
+
|
188 |
+
def test_interpolate_from_derivatives(self):
|
189 |
+
pytest.importorskip("scipy")
|
190 |
+
ser = Series([10, 11, 12, 13])
|
191 |
+
|
192 |
+
expected = Series(
|
193 |
+
[11.00, 11.25, 11.50, 11.75, 12.00, 12.25, 12.50, 12.75, 13.00],
|
194 |
+
index=Index([1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0]),
|
195 |
+
)
|
196 |
+
# interpolate at new_index
|
197 |
+
new_index = ser.index.union(Index([1.25, 1.5, 1.75, 2.25, 2.5, 2.75])).astype(
|
198 |
+
float
|
199 |
+
)
|
200 |
+
interp_s = ser.reindex(new_index).interpolate(method="from_derivatives")
|
201 |
+
tm.assert_series_equal(interp_s.loc[1:3], expected)
|
202 |
+
|
203 |
+
@pytest.mark.parametrize(
|
204 |
+
"kwargs",
|
205 |
+
[
|
206 |
+
{},
|
207 |
+
pytest.param(
|
208 |
+
{"method": "polynomial", "order": 1}, marks=td.skip_if_no("scipy")
|
209 |
+
),
|
210 |
+
],
|
211 |
+
)
|
212 |
+
def test_interpolate_corners(self, kwargs):
|
213 |
+
s = Series([np.nan, np.nan])
|
214 |
+
tm.assert_series_equal(s.interpolate(**kwargs), s)
|
215 |
+
|
216 |
+
s = Series([], dtype=object).interpolate()
|
217 |
+
tm.assert_series_equal(s.interpolate(**kwargs), s)
|
218 |
+
|
219 |
+
def test_interpolate_index_values(self):
|
220 |
+
s = Series(np.nan, index=np.sort(np.random.default_rng(2).random(30)))
|
221 |
+
s.loc[::3] = np.random.default_rng(2).standard_normal(10)
|
222 |
+
|
223 |
+
vals = s.index.values.astype(float)
|
224 |
+
|
225 |
+
result = s.interpolate(method="index")
|
226 |
+
|
227 |
+
expected = s.copy()
|
228 |
+
bad = isna(expected.values)
|
229 |
+
good = ~bad
|
230 |
+
expected = Series(
|
231 |
+
np.interp(vals[bad], vals[good], s.values[good]), index=s.index[bad]
|
232 |
+
)
|
233 |
+
|
234 |
+
tm.assert_series_equal(result[bad], expected)
|
235 |
+
|
236 |
+
# 'values' is synonymous with 'index' for the method kwarg
|
237 |
+
other_result = s.interpolate(method="values")
|
238 |
+
|
239 |
+
tm.assert_series_equal(other_result, result)
|
240 |
+
tm.assert_series_equal(other_result[bad], expected)
|
241 |
+
|
242 |
+
def test_interpolate_non_ts(self):
|
243 |
+
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
|
244 |
+
msg = (
|
245 |
+
"time-weighted interpolation only works on Series or DataFrames "
|
246 |
+
"with a DatetimeIndex"
|
247 |
+
)
|
248 |
+
with pytest.raises(ValueError, match=msg):
|
249 |
+
s.interpolate(method="time")
|
250 |
+
|
251 |
+
@pytest.mark.parametrize(
|
252 |
+
"kwargs",
|
253 |
+
[
|
254 |
+
{},
|
255 |
+
pytest.param(
|
256 |
+
{"method": "polynomial", "order": 1}, marks=td.skip_if_no("scipy")
|
257 |
+
),
|
258 |
+
],
|
259 |
+
)
|
260 |
+
def test_nan_interpolate(self, kwargs):
|
261 |
+
s = Series([0, 1, np.nan, 3])
|
262 |
+
result = s.interpolate(**kwargs)
|
263 |
+
expected = Series([0.0, 1.0, 2.0, 3.0])
|
264 |
+
tm.assert_series_equal(result, expected)
|
265 |
+
|
266 |
+
def test_nan_irregular_index(self):
|
267 |
+
s = Series([1, 2, np.nan, 4], index=[1, 3, 5, 9])
|
268 |
+
result = s.interpolate()
|
269 |
+
expected = Series([1.0, 2.0, 3.0, 4.0], index=[1, 3, 5, 9])
|
270 |
+
tm.assert_series_equal(result, expected)
|
271 |
+
|
272 |
+
def test_nan_str_index(self):
|
273 |
+
s = Series([0, 1, 2, np.nan], index=list("abcd"))
|
274 |
+
result = s.interpolate()
|
275 |
+
expected = Series([0.0, 1.0, 2.0, 2.0], index=list("abcd"))
|
276 |
+
tm.assert_series_equal(result, expected)
|
277 |
+
|
278 |
+
def test_interp_quad(self):
|
279 |
+
pytest.importorskip("scipy")
|
280 |
+
sq = Series([1, 4, np.nan, 16], index=[1, 2, 3, 4])
|
281 |
+
result = sq.interpolate(method="quadratic")
|
282 |
+
expected = Series([1.0, 4.0, 9.0, 16.0], index=[1, 2, 3, 4])
|
283 |
+
tm.assert_series_equal(result, expected)
|
284 |
+
|
285 |
+
def test_interp_scipy_basic(self):
|
286 |
+
pytest.importorskip("scipy")
|
287 |
+
s = Series([1, 3, np.nan, 12, np.nan, 25])
|
288 |
+
# slinear
|
289 |
+
expected = Series([1.0, 3.0, 7.5, 12.0, 18.5, 25.0])
|
290 |
+
result = s.interpolate(method="slinear")
|
291 |
+
tm.assert_series_equal(result, expected)
|
292 |
+
|
293 |
+
msg = "The 'downcast' keyword in Series.interpolate is deprecated"
|
294 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
295 |
+
result = s.interpolate(method="slinear", downcast="infer")
|
296 |
+
tm.assert_series_equal(result, expected)
|
297 |
+
# nearest
|
298 |
+
expected = Series([1, 3, 3, 12, 12, 25])
|
299 |
+
result = s.interpolate(method="nearest")
|
300 |
+
tm.assert_series_equal(result, expected.astype("float"))
|
301 |
+
|
302 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
303 |
+
result = s.interpolate(method="nearest", downcast="infer")
|
304 |
+
tm.assert_series_equal(result, expected)
|
305 |
+
# zero
|
306 |
+
expected = Series([1, 3, 3, 12, 12, 25])
|
307 |
+
result = s.interpolate(method="zero")
|
308 |
+
tm.assert_series_equal(result, expected.astype("float"))
|
309 |
+
|
310 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
311 |
+
result = s.interpolate(method="zero", downcast="infer")
|
312 |
+
tm.assert_series_equal(result, expected)
|
313 |
+
# quadratic
|
314 |
+
# GH #15662.
|
315 |
+
expected = Series([1, 3.0, 6.823529, 12.0, 18.058824, 25.0])
|
316 |
+
result = s.interpolate(method="quadratic")
|
317 |
+
tm.assert_series_equal(result, expected)
|
318 |
+
|
319 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
320 |
+
result = s.interpolate(method="quadratic", downcast="infer")
|
321 |
+
tm.assert_series_equal(result, expected)
|
322 |
+
# cubic
|
323 |
+
expected = Series([1.0, 3.0, 6.8, 12.0, 18.2, 25.0])
|
324 |
+
result = s.interpolate(method="cubic")
|
325 |
+
tm.assert_series_equal(result, expected)
|
326 |
+
|
327 |
+
def test_interp_limit(self):
|
328 |
+
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
|
329 |
+
|
330 |
+
expected = Series([1.0, 3.0, 5.0, 7.0, np.nan, 11.0])
|
331 |
+
result = s.interpolate(method="linear", limit=2)
|
332 |
+
tm.assert_series_equal(result, expected)
|
333 |
+
|
334 |
+
@pytest.mark.parametrize("limit", [-1, 0])
|
335 |
+
def test_interpolate_invalid_nonpositive_limit(self, nontemporal_method, limit):
|
336 |
+
# GH 9217: make sure limit is greater than zero.
|
337 |
+
s = Series([1, 2, np.nan, 4])
|
338 |
+
method, kwargs = nontemporal_method
|
339 |
+
with pytest.raises(ValueError, match="Limit must be greater than 0"):
|
340 |
+
s.interpolate(limit=limit, method=method, **kwargs)
|
341 |
+
|
342 |
+
def test_interpolate_invalid_float_limit(self, nontemporal_method):
|
343 |
+
# GH 9217: make sure limit is an integer.
|
344 |
+
s = Series([1, 2, np.nan, 4])
|
345 |
+
method, kwargs = nontemporal_method
|
346 |
+
limit = 2.0
|
347 |
+
with pytest.raises(ValueError, match="Limit must be an integer"):
|
348 |
+
s.interpolate(limit=limit, method=method, **kwargs)
|
349 |
+
|
350 |
+
@pytest.mark.parametrize("invalid_method", [None, "nonexistent_method"])
|
351 |
+
def test_interp_invalid_method(self, invalid_method):
|
352 |
+
s = Series([1, 3, np.nan, 12, np.nan, 25])
|
353 |
+
|
354 |
+
msg = f"method must be one of.* Got '{invalid_method}' instead"
|
355 |
+
if invalid_method is None:
|
356 |
+
msg = "'method' should be a string, not None"
|
357 |
+
with pytest.raises(ValueError, match=msg):
|
358 |
+
s.interpolate(method=invalid_method)
|
359 |
+
|
360 |
+
# When an invalid method and invalid limit (such as -1) are
|
361 |
+
# provided, the error message reflects the invalid method.
|
362 |
+
with pytest.raises(ValueError, match=msg):
|
363 |
+
s.interpolate(method=invalid_method, limit=-1)
|
364 |
+
|
365 |
+
def test_interp_invalid_method_and_value(self):
|
366 |
+
# GH#36624
|
367 |
+
ser = Series([1, 3, np.nan, 12, np.nan, 25])
|
368 |
+
|
369 |
+
msg = "'fill_value' is not a valid keyword for Series.interpolate"
|
370 |
+
msg2 = "Series.interpolate with method=pad"
|
371 |
+
with pytest.raises(ValueError, match=msg):
|
372 |
+
with tm.assert_produces_warning(FutureWarning, match=msg2):
|
373 |
+
ser.interpolate(fill_value=3, method="pad")
|
374 |
+
|
375 |
+
def test_interp_limit_forward(self):
|
376 |
+
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
|
377 |
+
|
378 |
+
# Provide 'forward' (the default) explicitly here.
|
379 |
+
expected = Series([1.0, 3.0, 5.0, 7.0, np.nan, 11.0])
|
380 |
+
|
381 |
+
result = s.interpolate(method="linear", limit=2, limit_direction="forward")
|
382 |
+
tm.assert_series_equal(result, expected)
|
383 |
+
|
384 |
+
result = s.interpolate(method="linear", limit=2, limit_direction="FORWARD")
|
385 |
+
tm.assert_series_equal(result, expected)
|
386 |
+
|
387 |
+
def test_interp_unlimited(self):
|
388 |
+
# these test are for issue #16282 default Limit=None is unlimited
|
389 |
+
s = Series([np.nan, 1.0, 3.0, np.nan, np.nan, np.nan, 11.0, np.nan])
|
390 |
+
expected = Series([1.0, 1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 11.0])
|
391 |
+
result = s.interpolate(method="linear", limit_direction="both")
|
392 |
+
tm.assert_series_equal(result, expected)
|
393 |
+
|
394 |
+
expected = Series([np.nan, 1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 11.0])
|
395 |
+
result = s.interpolate(method="linear", limit_direction="forward")
|
396 |
+
tm.assert_series_equal(result, expected)
|
397 |
+
|
398 |
+
expected = Series([1.0, 1.0, 3.0, 5.0, 7.0, 9.0, 11.0, np.nan])
|
399 |
+
result = s.interpolate(method="linear", limit_direction="backward")
|
400 |
+
tm.assert_series_equal(result, expected)
|
401 |
+
|
402 |
+
def test_interp_limit_bad_direction(self):
|
403 |
+
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
|
404 |
+
|
405 |
+
msg = (
|
406 |
+
r"Invalid limit_direction: expecting one of \['forward', "
|
407 |
+
r"'backward', 'both'\], got 'abc'"
|
408 |
+
)
|
409 |
+
with pytest.raises(ValueError, match=msg):
|
410 |
+
s.interpolate(method="linear", limit=2, limit_direction="abc")
|
411 |
+
|
412 |
+
# raises an error even if no limit is specified.
|
413 |
+
with pytest.raises(ValueError, match=msg):
|
414 |
+
s.interpolate(method="linear", limit_direction="abc")
|
415 |
+
|
416 |
+
# limit_area introduced GH #16284
|
417 |
+
def test_interp_limit_area(self):
|
418 |
+
# These tests are for issue #9218 -- fill NaNs in both directions.
|
419 |
+
s = Series([np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, np.nan, np.nan])
|
420 |
+
|
421 |
+
expected = Series([np.nan, np.nan, 3.0, 4.0, 5.0, 6.0, 7.0, np.nan, np.nan])
|
422 |
+
result = s.interpolate(method="linear", limit_area="inside")
|
423 |
+
tm.assert_series_equal(result, expected)
|
424 |
+
|
425 |
+
expected = Series(
|
426 |
+
[np.nan, np.nan, 3.0, 4.0, np.nan, np.nan, 7.0, np.nan, np.nan]
|
427 |
+
)
|
428 |
+
result = s.interpolate(method="linear", limit_area="inside", limit=1)
|
429 |
+
tm.assert_series_equal(result, expected)
|
430 |
+
|
431 |
+
expected = Series([np.nan, np.nan, 3.0, 4.0, np.nan, 6.0, 7.0, np.nan, np.nan])
|
432 |
+
result = s.interpolate(
|
433 |
+
method="linear", limit_area="inside", limit_direction="both", limit=1
|
434 |
+
)
|
435 |
+
tm.assert_series_equal(result, expected)
|
436 |
+
|
437 |
+
expected = Series([np.nan, np.nan, 3.0, np.nan, np.nan, np.nan, 7.0, 7.0, 7.0])
|
438 |
+
result = s.interpolate(method="linear", limit_area="outside")
|
439 |
+
tm.assert_series_equal(result, expected)
|
440 |
+
|
441 |
+
expected = Series(
|
442 |
+
[np.nan, np.nan, 3.0, np.nan, np.nan, np.nan, 7.0, 7.0, np.nan]
|
443 |
+
)
|
444 |
+
result = s.interpolate(method="linear", limit_area="outside", limit=1)
|
445 |
+
tm.assert_series_equal(result, expected)
|
446 |
+
|
447 |
+
expected = Series([np.nan, 3.0, 3.0, np.nan, np.nan, np.nan, 7.0, 7.0, np.nan])
|
448 |
+
result = s.interpolate(
|
449 |
+
method="linear", limit_area="outside", limit_direction="both", limit=1
|
450 |
+
)
|
451 |
+
tm.assert_series_equal(result, expected)
|
452 |
+
|
453 |
+
expected = Series([3.0, 3.0, 3.0, np.nan, np.nan, np.nan, 7.0, np.nan, np.nan])
|
454 |
+
result = s.interpolate(
|
455 |
+
method="linear", limit_area="outside", limit_direction="backward"
|
456 |
+
)
|
457 |
+
tm.assert_series_equal(result, expected)
|
458 |
+
|
459 |
+
# raises an error even if limit type is wrong.
|
460 |
+
msg = r"Invalid limit_area: expecting one of \['inside', 'outside'\], got abc"
|
461 |
+
with pytest.raises(ValueError, match=msg):
|
462 |
+
s.interpolate(method="linear", limit_area="abc")
|
463 |
+
|
464 |
+
@pytest.mark.parametrize(
|
465 |
+
"method, limit_direction, expected",
|
466 |
+
[
|
467 |
+
("pad", "backward", "forward"),
|
468 |
+
("ffill", "backward", "forward"),
|
469 |
+
("backfill", "forward", "backward"),
|
470 |
+
("bfill", "forward", "backward"),
|
471 |
+
("pad", "both", "forward"),
|
472 |
+
("ffill", "both", "forward"),
|
473 |
+
("backfill", "both", "backward"),
|
474 |
+
("bfill", "both", "backward"),
|
475 |
+
],
|
476 |
+
)
|
477 |
+
def test_interp_limit_direction_raises(self, method, limit_direction, expected):
|
478 |
+
# https://github.com/pandas-dev/pandas/pull/34746
|
479 |
+
s = Series([1, 2, 3])
|
480 |
+
|
481 |
+
msg = f"`limit_direction` must be '{expected}' for method `{method}`"
|
482 |
+
msg2 = "Series.interpolate with method="
|
483 |
+
with pytest.raises(ValueError, match=msg):
|
484 |
+
with tm.assert_produces_warning(FutureWarning, match=msg2):
|
485 |
+
s.interpolate(method=method, limit_direction=limit_direction)
|
486 |
+
|
487 |
+
@pytest.mark.parametrize(
|
488 |
+
"data, expected_data, kwargs",
|
489 |
+
(
|
490 |
+
(
|
491 |
+
[np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, np.nan, np.nan],
|
492 |
+
[np.nan, np.nan, 3.0, 3.0, 3.0, 3.0, 7.0, np.nan, np.nan],
|
493 |
+
{"method": "pad", "limit_area": "inside"},
|
494 |
+
),
|
495 |
+
(
|
496 |
+
[np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, np.nan, np.nan],
|
497 |
+
[np.nan, np.nan, 3.0, 3.0, np.nan, np.nan, 7.0, np.nan, np.nan],
|
498 |
+
{"method": "pad", "limit_area": "inside", "limit": 1},
|
499 |
+
),
|
500 |
+
(
|
501 |
+
[np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, np.nan, np.nan],
|
502 |
+
[np.nan, np.nan, 3.0, np.nan, np.nan, np.nan, 7.0, 7.0, 7.0],
|
503 |
+
{"method": "pad", "limit_area": "outside"},
|
504 |
+
),
|
505 |
+
(
|
506 |
+
[np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, np.nan, np.nan],
|
507 |
+
[np.nan, np.nan, 3.0, np.nan, np.nan, np.nan, 7.0, 7.0, np.nan],
|
508 |
+
{"method": "pad", "limit_area": "outside", "limit": 1},
|
509 |
+
),
|
510 |
+
(
|
511 |
+
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
|
512 |
+
[np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
|
513 |
+
{"method": "pad", "limit_area": "outside", "limit": 1},
|
514 |
+
),
|
515 |
+
(
|
516 |
+
range(5),
|
517 |
+
range(5),
|
518 |
+
{"method": "pad", "limit_area": "outside", "limit": 1},
|
519 |
+
),
|
520 |
+
),
|
521 |
+
)
|
522 |
+
def test_interp_limit_area_with_pad(self, data, expected_data, kwargs):
|
523 |
+
# GH26796
|
524 |
+
|
525 |
+
s = Series(data)
|
526 |
+
expected = Series(expected_data)
|
527 |
+
msg = "Series.interpolate with method=pad"
|
528 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
529 |
+
result = s.interpolate(**kwargs)
|
530 |
+
tm.assert_series_equal(result, expected)
|
531 |
+
|
532 |
+
@pytest.mark.parametrize(
|
533 |
+
"data, expected_data, kwargs",
|
534 |
+
(
|
535 |
+
(
|
536 |
+
[np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, np.nan, np.nan],
|
537 |
+
[np.nan, np.nan, 3.0, 7.0, 7.0, 7.0, 7.0, np.nan, np.nan],
|
538 |
+
{"method": "bfill", "limit_area": "inside"},
|
539 |
+
),
|
540 |
+
(
|
541 |
+
[np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, np.nan, np.nan],
|
542 |
+
[np.nan, np.nan, 3.0, np.nan, np.nan, 7.0, 7.0, np.nan, np.nan],
|
543 |
+
{"method": "bfill", "limit_area": "inside", "limit": 1},
|
544 |
+
),
|
545 |
+
(
|
546 |
+
[np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, np.nan, np.nan],
|
547 |
+
[3.0, 3.0, 3.0, np.nan, np.nan, np.nan, 7.0, np.nan, np.nan],
|
548 |
+
{"method": "bfill", "limit_area": "outside"},
|
549 |
+
),
|
550 |
+
(
|
551 |
+
[np.nan, np.nan, 3, np.nan, np.nan, np.nan, 7, np.nan, np.nan],
|
552 |
+
[np.nan, 3.0, 3.0, np.nan, np.nan, np.nan, 7.0, np.nan, np.nan],
|
553 |
+
{"method": "bfill", "limit_area": "outside", "limit": 1},
|
554 |
+
),
|
555 |
+
),
|
556 |
+
)
|
557 |
+
def test_interp_limit_area_with_backfill(self, data, expected_data, kwargs):
|
558 |
+
# GH26796
|
559 |
+
|
560 |
+
s = Series(data)
|
561 |
+
expected = Series(expected_data)
|
562 |
+
msg = "Series.interpolate with method=bfill"
|
563 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
564 |
+
result = s.interpolate(**kwargs)
|
565 |
+
tm.assert_series_equal(result, expected)
|
566 |
+
|
567 |
+
def test_interp_limit_direction(self):
|
568 |
+
# These tests are for issue #9218 -- fill NaNs in both directions.
|
569 |
+
s = Series([1, 3, np.nan, np.nan, np.nan, 11])
|
570 |
+
|
571 |
+
expected = Series([1.0, 3.0, np.nan, 7.0, 9.0, 11.0])
|
572 |
+
result = s.interpolate(method="linear", limit=2, limit_direction="backward")
|
573 |
+
tm.assert_series_equal(result, expected)
|
574 |
+
|
575 |
+
expected = Series([1.0, 3.0, 5.0, np.nan, 9.0, 11.0])
|
576 |
+
result = s.interpolate(method="linear", limit=1, limit_direction="both")
|
577 |
+
tm.assert_series_equal(result, expected)
|
578 |
+
|
579 |
+
# Check that this works on a longer series of nans.
|
580 |
+
s = Series([1, 3, np.nan, np.nan, np.nan, 7, 9, np.nan, np.nan, 12, np.nan])
|
581 |
+
|
582 |
+
expected = Series([1.0, 3.0, 4.0, 5.0, 6.0, 7.0, 9.0, 10.0, 11.0, 12.0, 12.0])
|
583 |
+
result = s.interpolate(method="linear", limit=2, limit_direction="both")
|
584 |
+
tm.assert_series_equal(result, expected)
|
585 |
+
|
586 |
+
expected = Series(
|
587 |
+
[1.0, 3.0, 4.0, np.nan, 6.0, 7.0, 9.0, 10.0, 11.0, 12.0, 12.0]
|
588 |
+
)
|
589 |
+
result = s.interpolate(method="linear", limit=1, limit_direction="both")
|
590 |
+
tm.assert_series_equal(result, expected)
|
591 |
+
|
592 |
+
def test_interp_limit_to_ends(self):
|
593 |
+
# These test are for issue #10420 -- flow back to beginning.
|
594 |
+
s = Series([np.nan, np.nan, 5, 7, 9, np.nan])
|
595 |
+
|
596 |
+
expected = Series([5.0, 5.0, 5.0, 7.0, 9.0, np.nan])
|
597 |
+
result = s.interpolate(method="linear", limit=2, limit_direction="backward")
|
598 |
+
tm.assert_series_equal(result, expected)
|
599 |
+
|
600 |
+
expected = Series([5.0, 5.0, 5.0, 7.0, 9.0, 9.0])
|
601 |
+
result = s.interpolate(method="linear", limit=2, limit_direction="both")
|
602 |
+
tm.assert_series_equal(result, expected)
|
603 |
+
|
604 |
+
def test_interp_limit_before_ends(self):
|
605 |
+
# These test are for issue #11115 -- limit ends properly.
|
606 |
+
s = Series([np.nan, np.nan, 5, 7, np.nan, np.nan])
|
607 |
+
|
608 |
+
expected = Series([np.nan, np.nan, 5.0, 7.0, 7.0, np.nan])
|
609 |
+
result = s.interpolate(method="linear", limit=1, limit_direction="forward")
|
610 |
+
tm.assert_series_equal(result, expected)
|
611 |
+
|
612 |
+
expected = Series([np.nan, 5.0, 5.0, 7.0, np.nan, np.nan])
|
613 |
+
result = s.interpolate(method="linear", limit=1, limit_direction="backward")
|
614 |
+
tm.assert_series_equal(result, expected)
|
615 |
+
|
616 |
+
expected = Series([np.nan, 5.0, 5.0, 7.0, 7.0, np.nan])
|
617 |
+
result = s.interpolate(method="linear", limit=1, limit_direction="both")
|
618 |
+
tm.assert_series_equal(result, expected)
|
619 |
+
|
620 |
+
def test_interp_all_good(self):
|
621 |
+
pytest.importorskip("scipy")
|
622 |
+
s = Series([1, 2, 3])
|
623 |
+
result = s.interpolate(method="polynomial", order=1)
|
624 |
+
tm.assert_series_equal(result, s)
|
625 |
+
|
626 |
+
# non-scipy
|
627 |
+
result = s.interpolate()
|
628 |
+
tm.assert_series_equal(result, s)
|
629 |
+
|
630 |
+
@pytest.mark.parametrize(
|
631 |
+
"check_scipy", [False, pytest.param(True, marks=td.skip_if_no("scipy"))]
|
632 |
+
)
|
633 |
+
def test_interp_multiIndex(self, check_scipy):
|
634 |
+
idx = MultiIndex.from_tuples([(0, "a"), (1, "b"), (2, "c")])
|
635 |
+
s = Series([1, 2, np.nan], index=idx)
|
636 |
+
|
637 |
+
expected = s.copy()
|
638 |
+
expected.loc[2] = 2
|
639 |
+
result = s.interpolate()
|
640 |
+
tm.assert_series_equal(result, expected)
|
641 |
+
|
642 |
+
msg = "Only `method=linear` interpolation is supported on MultiIndexes"
|
643 |
+
if check_scipy:
|
644 |
+
with pytest.raises(ValueError, match=msg):
|
645 |
+
s.interpolate(method="polynomial", order=1)
|
646 |
+
|
647 |
+
def test_interp_nonmono_raise(self):
|
648 |
+
pytest.importorskip("scipy")
|
649 |
+
s = Series([1, np.nan, 3], index=[0, 2, 1])
|
650 |
+
msg = "krogh interpolation requires that the index be monotonic"
|
651 |
+
with pytest.raises(ValueError, match=msg):
|
652 |
+
s.interpolate(method="krogh")
|
653 |
+
|
654 |
+
@pytest.mark.parametrize("method", ["nearest", "pad"])
|
655 |
+
def test_interp_datetime64(self, method, tz_naive_fixture):
|
656 |
+
pytest.importorskip("scipy")
|
657 |
+
df = Series(
|
658 |
+
[1, np.nan, 3], index=date_range("1/1/2000", periods=3, tz=tz_naive_fixture)
|
659 |
+
)
|
660 |
+
warn = None if method == "nearest" else FutureWarning
|
661 |
+
msg = "Series.interpolate with method=pad is deprecated"
|
662 |
+
with tm.assert_produces_warning(warn, match=msg):
|
663 |
+
result = df.interpolate(method=method)
|
664 |
+
if warn is not None:
|
665 |
+
# check the "use ffill instead" is equivalent
|
666 |
+
alt = df.ffill()
|
667 |
+
tm.assert_series_equal(result, alt)
|
668 |
+
|
669 |
+
expected = Series(
|
670 |
+
[1.0, 1.0, 3.0],
|
671 |
+
index=date_range("1/1/2000", periods=3, tz=tz_naive_fixture),
|
672 |
+
)
|
673 |
+
tm.assert_series_equal(result, expected)
|
674 |
+
|
675 |
+
def test_interp_pad_datetime64tz_values(self):
|
676 |
+
# GH#27628 missing.interpolate_2d should handle datetimetz values
|
677 |
+
dti = date_range("2015-04-05", periods=3, tz="US/Central")
|
678 |
+
ser = Series(dti)
|
679 |
+
ser[1] = pd.NaT
|
680 |
+
|
681 |
+
msg = "Series.interpolate with method=pad is deprecated"
|
682 |
+
with tm.assert_produces_warning(FutureWarning, match=msg):
|
683 |
+
result = ser.interpolate(method="pad")
|
684 |
+
# check the "use ffill instead" is equivalent
|
685 |
+
alt = ser.ffill()
|
686 |
+
tm.assert_series_equal(result, alt)
|
687 |
+
|
688 |
+
expected = Series(dti)
|
689 |
+
expected[1] = expected[0]
|
690 |
+
tm.assert_series_equal(result, expected)
|
691 |
+
|
692 |
+
def test_interp_limit_no_nans(self):
|
693 |
+
# GH 7173
|
694 |
+
s = Series([1.0, 2.0, 3.0])
|
695 |
+
result = s.interpolate(limit=1)
|
696 |
+
expected = s
|
697 |
+
tm.assert_series_equal(result, expected)
|
698 |
+
|
699 |
+
@pytest.mark.parametrize("method", ["polynomial", "spline"])
|
700 |
+
def test_no_order(self, method):
|
701 |
+
# see GH-10633, GH-24014
|
702 |
+
pytest.importorskip("scipy")
|
703 |
+
s = Series([0, 1, np.nan, 3])
|
704 |
+
msg = "You must specify the order of the spline or polynomial"
|
705 |
+
with pytest.raises(ValueError, match=msg):
|
706 |
+
s.interpolate(method=method)
|
707 |
+
|
708 |
+
@pytest.mark.parametrize("order", [-1, -1.0, 0, 0.0, np.nan])
|
709 |
+
def test_interpolate_spline_invalid_order(self, order):
|
710 |
+
pytest.importorskip("scipy")
|
711 |
+
s = Series([0, 1, np.nan, 3])
|
712 |
+
msg = "order needs to be specified and greater than 0"
|
713 |
+
with pytest.raises(ValueError, match=msg):
|
714 |
+
s.interpolate(method="spline", order=order)
|
715 |
+
|
716 |
+
def test_spline(self):
|
717 |
+
pytest.importorskip("scipy")
|
718 |
+
s = Series([1, 2, np.nan, 4, 5, np.nan, 7])
|
719 |
+
result = s.interpolate(method="spline", order=1)
|
720 |
+
expected = Series([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
|
721 |
+
tm.assert_series_equal(result, expected)
|
722 |
+
|
723 |
+
def test_spline_extrapolate(self):
|
724 |
+
pytest.importorskip("scipy")
|
725 |
+
s = Series([1, 2, 3, 4, np.nan, 6, np.nan])
|
726 |
+
result3 = s.interpolate(method="spline", order=1, ext=3)
|
727 |
+
expected3 = Series([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 6.0])
|
728 |
+
tm.assert_series_equal(result3, expected3)
|
729 |
+
|
730 |
+
result1 = s.interpolate(method="spline", order=1, ext=0)
|
731 |
+
expected1 = Series([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
|
732 |
+
tm.assert_series_equal(result1, expected1)
|
733 |
+
|
734 |
+
def test_spline_smooth(self):
|
735 |
+
pytest.importorskip("scipy")
|
736 |
+
s = Series([1, 2, np.nan, 4, 5.1, np.nan, 7])
|
737 |
+
assert (
|
738 |
+
s.interpolate(method="spline", order=3, s=0)[5]
|
739 |
+
!= s.interpolate(method="spline", order=3)[5]
|
740 |
+
)
|
741 |
+
|
742 |
+
def test_spline_interpolation(self):
|
743 |
+
# Explicit cast to float to avoid implicit cast when setting np.nan
|
744 |
+
pytest.importorskip("scipy")
|
745 |
+
s = Series(np.arange(10) ** 2, dtype="float")
|
746 |
+
s[np.random.default_rng(2).integers(0, 9, 3)] = np.nan
|
747 |
+
result1 = s.interpolate(method="spline", order=1)
|
748 |
+
expected1 = s.interpolate(method="spline", order=1)
|
749 |
+
tm.assert_series_equal(result1, expected1)
|
750 |
+
|
751 |
+
def test_interp_timedelta64(self):
|
752 |
+
# GH 6424
|
753 |
+
df = Series([1, np.nan, 3], index=pd.to_timedelta([1, 2, 3]))
|
754 |
+
result = df.interpolate(method="time")
|
755 |
+
expected = Series([1.0, 2.0, 3.0], index=pd.to_timedelta([1, 2, 3]))
|
756 |
+
tm.assert_series_equal(result, expected)
|
757 |
+
|
758 |
+
# test for non uniform spacing
|
759 |
+
df = Series([1, np.nan, 3], index=pd.to_timedelta([1, 2, 4]))
|
760 |
+
result = df.interpolate(method="time")
|
761 |
+
expected = Series([1.0, 1.666667, 3.0], index=pd.to_timedelta([1, 2, 4]))
|
762 |
+
tm.assert_series_equal(result, expected)
|
763 |
+
|
764 |
+
def test_series_interpolate_method_values(self):
|
765 |
+
# GH#1646
|
766 |
+
rng = date_range("1/1/2000", "1/20/2000", freq="D")
|
767 |
+
ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
|
768 |
+
|
769 |
+
ts[::2] = np.nan
|
770 |
+
|
771 |
+
result = ts.interpolate(method="values")
|
772 |
+
exp = ts.interpolate()
|
773 |
+
tm.assert_series_equal(result, exp)
|
774 |
+
|
775 |
+
def test_series_interpolate_intraday(self):
|
776 |
+
# #1698
|
777 |
+
index = date_range("1/1/2012", periods=4, freq="12D")
|
778 |
+
ts = Series([0, 12, 24, 36], index)
|
779 |
+
new_index = index.append(index + pd.DateOffset(days=1)).sort_values()
|
780 |
+
|
781 |
+
exp = ts.reindex(new_index).interpolate(method="time")
|
782 |
+
|
783 |
+
index = date_range("1/1/2012", periods=4, freq="12h")
|
784 |
+
ts = Series([0, 12, 24, 36], index)
|
785 |
+
new_index = index.append(index + pd.DateOffset(hours=1)).sort_values()
|
786 |
+
result = ts.reindex(new_index).interpolate(method="time")
|
787 |
+
|
788 |
+
tm.assert_numpy_array_equal(result.values, exp.values)
|
789 |
+
|
790 |
+
@pytest.mark.parametrize(
|
791 |
+
"ind",
|
792 |
+
[
|
793 |
+
["a", "b", "c", "d"],
|
794 |
+
pd.period_range(start="2019-01-01", periods=4),
|
795 |
+
pd.interval_range(start=0, end=4),
|
796 |
+
],
|
797 |
+
)
|
798 |
+
def test_interp_non_timedelta_index(self, interp_methods_ind, ind):
|
799 |
+
# gh 21662
|
800 |
+
df = pd.DataFrame([0, 1, np.nan, 3], index=ind)
|
801 |
+
|
802 |
+
method, kwargs = interp_methods_ind
|
803 |
+
if method == "pchip":
|
804 |
+
pytest.importorskip("scipy")
|
805 |
+
|
806 |
+
if method == "linear":
|
807 |
+
result = df[0].interpolate(**kwargs)
|
808 |
+
expected = Series([0.0, 1.0, 2.0, 3.0], name=0, index=ind)
|
809 |
+
tm.assert_series_equal(result, expected)
|
810 |
+
else:
|
811 |
+
expected_error = (
|
812 |
+
"Index column must be numeric or datetime type when "
|
813 |
+
f"using {method} method other than linear. "
|
814 |
+
"Try setting a numeric or datetime index column before "
|
815 |
+
"interpolating."
|
816 |
+
)
|
817 |
+
with pytest.raises(ValueError, match=expected_error):
|
818 |
+
df[0].interpolate(method=method, **kwargs)
|
819 |
+
|
820 |
+
def test_interpolate_timedelta_index(self, request, interp_methods_ind):
|
821 |
+
"""
|
822 |
+
Tests for non numerical index types - object, period, timedelta
|
823 |
+
Note that all methods except time, index, nearest and values
|
824 |
+
are tested here.
|
825 |
+
"""
|
826 |
+
# gh 21662
|
827 |
+
pytest.importorskip("scipy")
|
828 |
+
ind = pd.timedelta_range(start=1, periods=4)
|
829 |
+
df = pd.DataFrame([0, 1, np.nan, 3], index=ind)
|
830 |
+
|
831 |
+
method, kwargs = interp_methods_ind
|
832 |
+
|
833 |
+
if method in {"cubic", "zero"}:
|
834 |
+
request.applymarker(
|
835 |
+
pytest.mark.xfail(
|
836 |
+
reason=f"{method} interpolation is not supported for TimedeltaIndex"
|
837 |
+
)
|
838 |
+
)
|
839 |
+
result = df[0].interpolate(method=method, **kwargs)
|
840 |
+
expected = Series([0.0, 1.0, 2.0, 3.0], name=0, index=ind)
|
841 |
+
tm.assert_series_equal(result, expected)
|
842 |
+
|
843 |
+
@pytest.mark.parametrize(
|
844 |
+
"ascending, expected_values",
|
845 |
+
[(True, [1, 2, 3, 9, 10]), (False, [10, 9, 3, 2, 1])],
|
846 |
+
)
|
847 |
+
def test_interpolate_unsorted_index(self, ascending, expected_values):
|
848 |
+
# GH 21037
|
849 |
+
ts = Series(data=[10, 9, np.nan, 2, 1], index=[10, 9, 3, 2, 1])
|
850 |
+
result = ts.sort_index(ascending=ascending).interpolate(method="index")
|
851 |
+
expected = Series(data=expected_values, index=expected_values, dtype=float)
|
852 |
+
tm.assert_series_equal(result, expected)
|
853 |
+
|
854 |
+
def test_interpolate_asfreq_raises(self):
|
855 |
+
ser = Series(["a", None, "b"], dtype=object)
|
856 |
+
msg2 = "Series.interpolate with object dtype"
|
857 |
+
msg = "Invalid fill method"
|
858 |
+
with pytest.raises(ValueError, match=msg):
|
859 |
+
with tm.assert_produces_warning(FutureWarning, match=msg2):
|
860 |
+
ser.interpolate(method="asfreq")
|
861 |
+
|
862 |
+
def test_interpolate_fill_value(self):
|
863 |
+
# GH#54920
|
864 |
+
pytest.importorskip("scipy")
|
865 |
+
ser = Series([np.nan, 0, 1, np.nan, 3, np.nan])
|
866 |
+
result = ser.interpolate(method="nearest", fill_value=0)
|
867 |
+
expected = Series([np.nan, 0, 1, 1, 3, 0])
|
868 |
+
tm.assert_series_equal(result, expected)
|