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 +1 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/io/data/legacy_pickle/1.2.4/empty_frame_v1_2_4-GH#42345.pkl +3 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/accessors/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/accessors/test_cat_accessor.py +253 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/accessors/test_dt_accessor.py +827 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/__init__.py +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/__pycache__/test_datetime.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/__pycache__/test_set_value.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/__pycache__/test_setitem.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/__pycache__/test_take.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_datetime.py +475 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_delitem.py +73 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_get.py +217 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_getitem.py +703 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_indexing.py +439 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_mask.py +69 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_setitem.py +1642 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_where.py +466 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/__init__.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_add_prefix_suffix.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_align.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_argsort.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_asof.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_astype.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_autocorr.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_between.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_clip.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_combine.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_combine_first.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_compare.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_convert_dtypes.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_copy.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_count.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_describe.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_diff.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_drop.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_drop_duplicates.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_dropna.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_dtypes.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_duplicated.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_equals.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_explode.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_fillna.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_get_numeric_data.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_head_tail.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_infer_objects.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_interpolate.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_is_monotonic.cpython-310.pyc +0 -0
- videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_isin.cpython-310.pyc +0 -0
.gitattributes
CHANGED
|
@@ -1287,3 +1287,4 @@ videochat2/lib/python3.10/site-packages/pandas/io/__pycache__/stata.cpython-310.
|
|
| 1287 |
videochat2/lib/python3.10/site-packages/pandas/tests/frame/__pycache__/test_constructors.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1288 |
videochat2/lib/python3.10/site-packages/pandas/tests/io/formats/__pycache__/test_format.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1289 |
videochat2/lib/python3.10/site-packages/pandas/tests/indexing/__pycache__/test_loc.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 1287 |
videochat2/lib/python3.10/site-packages/pandas/tests/frame/__pycache__/test_constructors.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1288 |
videochat2/lib/python3.10/site-packages/pandas/tests/io/formats/__pycache__/test_format.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1289 |
videochat2/lib/python3.10/site-packages/pandas/tests/indexing/__pycache__/test_loc.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1290 |
+
videochat2/lib/python3.10/site-packages/pandas/tests/tools/__pycache__/test_to_datetime.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
videochat2/lib/python3.10/site-packages/pandas/tests/io/data/legacy_pickle/1.2.4/empty_frame_v1_2_4-GH#42345.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f552b912ec49ab3028060c57b6e1c2b08717f59710fecc363e506cb6195fa6b2
|
| 3 |
+
size 501
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/accessors/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/accessors/test_cat_accessor.py
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from pandas import (
|
| 7 |
+
Categorical,
|
| 8 |
+
DataFrame,
|
| 9 |
+
Index,
|
| 10 |
+
Series,
|
| 11 |
+
Timestamp,
|
| 12 |
+
date_range,
|
| 13 |
+
period_range,
|
| 14 |
+
timedelta_range,
|
| 15 |
+
)
|
| 16 |
+
import pandas._testing as tm
|
| 17 |
+
from pandas.core.arrays.categorical import CategoricalAccessor
|
| 18 |
+
from pandas.core.indexes.accessors import Properties
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class TestCatAccessor:
|
| 22 |
+
@pytest.mark.parametrize(
|
| 23 |
+
"method",
|
| 24 |
+
[
|
| 25 |
+
lambda x: x.cat.set_categories([1, 2, 3]),
|
| 26 |
+
lambda x: x.cat.reorder_categories([2, 3, 1], ordered=True),
|
| 27 |
+
lambda x: x.cat.rename_categories([1, 2, 3]),
|
| 28 |
+
lambda x: x.cat.remove_unused_categories(),
|
| 29 |
+
lambda x: x.cat.remove_categories([2]),
|
| 30 |
+
lambda x: x.cat.add_categories([4]),
|
| 31 |
+
lambda x: x.cat.as_ordered(),
|
| 32 |
+
lambda x: x.cat.as_unordered(),
|
| 33 |
+
],
|
| 34 |
+
)
|
| 35 |
+
def test_getname_categorical_accessor(self, method):
|
| 36 |
+
# GH#17509
|
| 37 |
+
ser = Series([1, 2, 3], name="A").astype("category")
|
| 38 |
+
expected = "A"
|
| 39 |
+
result = method(ser).name
|
| 40 |
+
assert result == expected
|
| 41 |
+
|
| 42 |
+
def test_cat_accessor(self):
|
| 43 |
+
ser = Series(Categorical(["a", "b", np.nan, "a"]))
|
| 44 |
+
tm.assert_index_equal(ser.cat.categories, Index(["a", "b"]))
|
| 45 |
+
assert not ser.cat.ordered, False
|
| 46 |
+
|
| 47 |
+
exp = Categorical(["a", "b", np.nan, "a"], categories=["b", "a"])
|
| 48 |
+
|
| 49 |
+
res = ser.cat.set_categories(["b", "a"])
|
| 50 |
+
tm.assert_categorical_equal(res.values, exp)
|
| 51 |
+
|
| 52 |
+
ser[:] = "a"
|
| 53 |
+
ser = ser.cat.remove_unused_categories()
|
| 54 |
+
tm.assert_index_equal(ser.cat.categories, Index(["a"]))
|
| 55 |
+
|
| 56 |
+
def test_cat_accessor_api(self):
|
| 57 |
+
# GH#9322
|
| 58 |
+
|
| 59 |
+
assert Series.cat is CategoricalAccessor
|
| 60 |
+
ser = Series(list("aabbcde")).astype("category")
|
| 61 |
+
assert isinstance(ser.cat, CategoricalAccessor)
|
| 62 |
+
|
| 63 |
+
invalid = Series([1])
|
| 64 |
+
with pytest.raises(AttributeError, match="only use .cat accessor"):
|
| 65 |
+
invalid.cat
|
| 66 |
+
assert not hasattr(invalid, "cat")
|
| 67 |
+
|
| 68 |
+
def test_cat_accessor_no_new_attributes(self):
|
| 69 |
+
# https://github.com/pandas-dev/pandas/issues/10673
|
| 70 |
+
cat = Series(list("aabbcde")).astype("category")
|
| 71 |
+
with pytest.raises(AttributeError, match="You cannot add any new attribute"):
|
| 72 |
+
cat.cat.xlabel = "a"
|
| 73 |
+
|
| 74 |
+
def test_categorical_delegations(self):
|
| 75 |
+
# invalid accessor
|
| 76 |
+
msg = r"Can only use \.cat accessor with a 'category' dtype"
|
| 77 |
+
with pytest.raises(AttributeError, match=msg):
|
| 78 |
+
Series([1, 2, 3]).cat
|
| 79 |
+
with pytest.raises(AttributeError, match=msg):
|
| 80 |
+
Series([1, 2, 3]).cat()
|
| 81 |
+
with pytest.raises(AttributeError, match=msg):
|
| 82 |
+
Series(["a", "b", "c"]).cat
|
| 83 |
+
with pytest.raises(AttributeError, match=msg):
|
| 84 |
+
Series(np.arange(5.0)).cat
|
| 85 |
+
with pytest.raises(AttributeError, match=msg):
|
| 86 |
+
Series([Timestamp("20130101")]).cat
|
| 87 |
+
|
| 88 |
+
# Series should delegate calls to '.categories', '.codes', '.ordered'
|
| 89 |
+
# and the methods '.set_categories()' 'drop_unused_categories()' to the
|
| 90 |
+
# categorical
|
| 91 |
+
ser = Series(Categorical(["a", "b", "c", "a"], ordered=True))
|
| 92 |
+
exp_categories = Index(["a", "b", "c"])
|
| 93 |
+
tm.assert_index_equal(ser.cat.categories, exp_categories)
|
| 94 |
+
ser = ser.cat.rename_categories([1, 2, 3])
|
| 95 |
+
exp_categories = Index([1, 2, 3])
|
| 96 |
+
tm.assert_index_equal(ser.cat.categories, exp_categories)
|
| 97 |
+
|
| 98 |
+
exp_codes = Series([0, 1, 2, 0], dtype="int8")
|
| 99 |
+
tm.assert_series_equal(ser.cat.codes, exp_codes)
|
| 100 |
+
|
| 101 |
+
assert ser.cat.ordered
|
| 102 |
+
ser = ser.cat.as_unordered()
|
| 103 |
+
assert not ser.cat.ordered
|
| 104 |
+
|
| 105 |
+
ser = ser.cat.as_ordered()
|
| 106 |
+
assert ser.cat.ordered
|
| 107 |
+
|
| 108 |
+
# reorder
|
| 109 |
+
ser = Series(Categorical(["a", "b", "c", "a"], ordered=True))
|
| 110 |
+
exp_categories = Index(["c", "b", "a"])
|
| 111 |
+
exp_values = np.array(["a", "b", "c", "a"], dtype=np.object_)
|
| 112 |
+
ser = ser.cat.set_categories(["c", "b", "a"])
|
| 113 |
+
tm.assert_index_equal(ser.cat.categories, exp_categories)
|
| 114 |
+
tm.assert_numpy_array_equal(ser.values.__array__(), exp_values)
|
| 115 |
+
tm.assert_numpy_array_equal(ser.__array__(), exp_values)
|
| 116 |
+
|
| 117 |
+
# remove unused categories
|
| 118 |
+
ser = Series(Categorical(["a", "b", "b", "a"], categories=["a", "b", "c"]))
|
| 119 |
+
exp_categories = Index(["a", "b"])
|
| 120 |
+
exp_values = np.array(["a", "b", "b", "a"], dtype=np.object_)
|
| 121 |
+
ser = ser.cat.remove_unused_categories()
|
| 122 |
+
tm.assert_index_equal(ser.cat.categories, exp_categories)
|
| 123 |
+
tm.assert_numpy_array_equal(ser.values.__array__(), exp_values)
|
| 124 |
+
tm.assert_numpy_array_equal(ser.__array__(), exp_values)
|
| 125 |
+
|
| 126 |
+
# This method is likely to be confused, so test that it raises an error
|
| 127 |
+
# on wrong inputs:
|
| 128 |
+
msg = "'Series' object has no attribute 'set_categories'"
|
| 129 |
+
with pytest.raises(AttributeError, match=msg):
|
| 130 |
+
ser.set_categories([4, 3, 2, 1])
|
| 131 |
+
|
| 132 |
+
# right: ser.cat.set_categories([4,3,2,1])
|
| 133 |
+
|
| 134 |
+
# GH#18862 (let Series.cat.rename_categories take callables)
|
| 135 |
+
ser = Series(Categorical(["a", "b", "c", "a"], ordered=True))
|
| 136 |
+
result = ser.cat.rename_categories(lambda x: x.upper())
|
| 137 |
+
expected = Series(
|
| 138 |
+
Categorical(["A", "B", "C", "A"], categories=["A", "B", "C"], ordered=True)
|
| 139 |
+
)
|
| 140 |
+
tm.assert_series_equal(result, expected)
|
| 141 |
+
|
| 142 |
+
@pytest.mark.parametrize(
|
| 143 |
+
"idx",
|
| 144 |
+
[
|
| 145 |
+
date_range("1/1/2015", periods=5),
|
| 146 |
+
date_range("1/1/2015", periods=5, tz="MET"),
|
| 147 |
+
period_range("1/1/2015", freq="D", periods=5),
|
| 148 |
+
timedelta_range("1 days", "10 days"),
|
| 149 |
+
],
|
| 150 |
+
)
|
| 151 |
+
def test_dt_accessor_api_for_categorical(self, idx):
|
| 152 |
+
# https://github.com/pandas-dev/pandas/issues/10661
|
| 153 |
+
|
| 154 |
+
ser = Series(idx)
|
| 155 |
+
cat = ser.astype("category")
|
| 156 |
+
|
| 157 |
+
# only testing field (like .day)
|
| 158 |
+
# and bool (is_month_start)
|
| 159 |
+
attr_names = type(ser._values)._datetimelike_ops
|
| 160 |
+
|
| 161 |
+
assert isinstance(cat.dt, Properties)
|
| 162 |
+
|
| 163 |
+
special_func_defs = [
|
| 164 |
+
("strftime", ("%Y-%m-%d",), {}),
|
| 165 |
+
("round", ("D",), {}),
|
| 166 |
+
("floor", ("D",), {}),
|
| 167 |
+
("ceil", ("D",), {}),
|
| 168 |
+
("asfreq", ("D",), {}),
|
| 169 |
+
("as_unit", ("s"), {}),
|
| 170 |
+
]
|
| 171 |
+
if idx.dtype == "M8[ns]":
|
| 172 |
+
# exclude dt64tz since that is already localized and would raise
|
| 173 |
+
tup = ("tz_localize", ("UTC",), {})
|
| 174 |
+
special_func_defs.append(tup)
|
| 175 |
+
elif idx.dtype.kind == "M":
|
| 176 |
+
# exclude dt64 since that is not localized so would raise
|
| 177 |
+
tup = ("tz_convert", ("EST",), {})
|
| 178 |
+
special_func_defs.append(tup)
|
| 179 |
+
|
| 180 |
+
_special_func_names = [f[0] for f in special_func_defs]
|
| 181 |
+
|
| 182 |
+
_ignore_names = ["components", "tz_localize", "tz_convert"]
|
| 183 |
+
|
| 184 |
+
func_names = [
|
| 185 |
+
fname
|
| 186 |
+
for fname in dir(ser.dt)
|
| 187 |
+
if not (
|
| 188 |
+
fname.startswith("_")
|
| 189 |
+
or fname in attr_names
|
| 190 |
+
or fname in _special_func_names
|
| 191 |
+
or fname in _ignore_names
|
| 192 |
+
)
|
| 193 |
+
]
|
| 194 |
+
|
| 195 |
+
func_defs = [(fname, (), {}) for fname in func_names]
|
| 196 |
+
|
| 197 |
+
for f_def in special_func_defs:
|
| 198 |
+
if f_def[0] in dir(ser.dt):
|
| 199 |
+
func_defs.append(f_def)
|
| 200 |
+
|
| 201 |
+
for func, args, kwargs in func_defs:
|
| 202 |
+
with warnings.catch_warnings():
|
| 203 |
+
if func == "to_period":
|
| 204 |
+
# dropping TZ
|
| 205 |
+
warnings.simplefilter("ignore", UserWarning)
|
| 206 |
+
res = getattr(cat.dt, func)(*args, **kwargs)
|
| 207 |
+
exp = getattr(ser.dt, func)(*args, **kwargs)
|
| 208 |
+
|
| 209 |
+
tm.assert_equal(res, exp)
|
| 210 |
+
|
| 211 |
+
for attr in attr_names:
|
| 212 |
+
res = getattr(cat.dt, attr)
|
| 213 |
+
exp = getattr(ser.dt, attr)
|
| 214 |
+
|
| 215 |
+
tm.assert_equal(res, exp)
|
| 216 |
+
|
| 217 |
+
def test_dt_accessor_api_for_categorical_invalid(self):
|
| 218 |
+
invalid = Series([1, 2, 3]).astype("category")
|
| 219 |
+
msg = "Can only use .dt accessor with datetimelike"
|
| 220 |
+
|
| 221 |
+
with pytest.raises(AttributeError, match=msg):
|
| 222 |
+
invalid.dt
|
| 223 |
+
assert not hasattr(invalid, "str")
|
| 224 |
+
|
| 225 |
+
def test_set_categories_setitem(self):
|
| 226 |
+
# GH#43334
|
| 227 |
+
|
| 228 |
+
df = DataFrame({"Survived": [1, 0, 1], "Sex": [0, 1, 1]}, dtype="category")
|
| 229 |
+
|
| 230 |
+
df["Survived"] = df["Survived"].cat.rename_categories(["No", "Yes"])
|
| 231 |
+
df["Sex"] = df["Sex"].cat.rename_categories(["female", "male"])
|
| 232 |
+
|
| 233 |
+
# values should not be coerced to NaN
|
| 234 |
+
assert list(df["Sex"]) == ["female", "male", "male"]
|
| 235 |
+
assert list(df["Survived"]) == ["Yes", "No", "Yes"]
|
| 236 |
+
|
| 237 |
+
df["Sex"] = Categorical(df["Sex"], categories=["female", "male"], ordered=False)
|
| 238 |
+
df["Survived"] = Categorical(
|
| 239 |
+
df["Survived"], categories=["No", "Yes"], ordered=False
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
# values should not be coerced to NaN
|
| 243 |
+
assert list(df["Sex"]) == ["female", "male", "male"]
|
| 244 |
+
assert list(df["Survived"]) == ["Yes", "No", "Yes"]
|
| 245 |
+
|
| 246 |
+
def test_categorical_of_booleans_is_boolean(self):
|
| 247 |
+
# https://github.com/pandas-dev/pandas/issues/46313
|
| 248 |
+
df = DataFrame(
|
| 249 |
+
{"int_cat": [1, 2, 3], "bool_cat": [True, False, False]}, dtype="category"
|
| 250 |
+
)
|
| 251 |
+
value = df["bool_cat"].cat.categories.dtype
|
| 252 |
+
expected = np.dtype(np.bool_)
|
| 253 |
+
assert value is expected
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/accessors/test_dt_accessor.py
ADDED
|
@@ -0,0 +1,827 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import calendar
|
| 2 |
+
from datetime import (
|
| 3 |
+
date,
|
| 4 |
+
datetime,
|
| 5 |
+
time,
|
| 6 |
+
)
|
| 7 |
+
import locale
|
| 8 |
+
import unicodedata
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
import pytest
|
| 12 |
+
import pytz
|
| 13 |
+
|
| 14 |
+
from pandas._libs.tslibs.timezones import maybe_get_tz
|
| 15 |
+
from pandas.errors import SettingWithCopyError
|
| 16 |
+
|
| 17 |
+
from pandas.core.dtypes.common import (
|
| 18 |
+
is_integer_dtype,
|
| 19 |
+
is_list_like,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
import pandas as pd
|
| 23 |
+
from pandas import (
|
| 24 |
+
DataFrame,
|
| 25 |
+
DatetimeIndex,
|
| 26 |
+
Index,
|
| 27 |
+
Period,
|
| 28 |
+
PeriodIndex,
|
| 29 |
+
Series,
|
| 30 |
+
TimedeltaIndex,
|
| 31 |
+
date_range,
|
| 32 |
+
period_range,
|
| 33 |
+
timedelta_range,
|
| 34 |
+
)
|
| 35 |
+
import pandas._testing as tm
|
| 36 |
+
from pandas.core.arrays import (
|
| 37 |
+
DatetimeArray,
|
| 38 |
+
PeriodArray,
|
| 39 |
+
TimedeltaArray,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
ok_for_period = PeriodArray._datetimelike_ops
|
| 43 |
+
ok_for_period_methods = ["strftime", "to_timestamp", "asfreq"]
|
| 44 |
+
ok_for_dt = DatetimeArray._datetimelike_ops
|
| 45 |
+
ok_for_dt_methods = [
|
| 46 |
+
"to_period",
|
| 47 |
+
"to_pydatetime",
|
| 48 |
+
"tz_localize",
|
| 49 |
+
"tz_convert",
|
| 50 |
+
"normalize",
|
| 51 |
+
"strftime",
|
| 52 |
+
"round",
|
| 53 |
+
"floor",
|
| 54 |
+
"ceil",
|
| 55 |
+
"day_name",
|
| 56 |
+
"month_name",
|
| 57 |
+
"isocalendar",
|
| 58 |
+
"as_unit",
|
| 59 |
+
]
|
| 60 |
+
ok_for_td = TimedeltaArray._datetimelike_ops
|
| 61 |
+
ok_for_td_methods = [
|
| 62 |
+
"components",
|
| 63 |
+
"to_pytimedelta",
|
| 64 |
+
"total_seconds",
|
| 65 |
+
"round",
|
| 66 |
+
"floor",
|
| 67 |
+
"ceil",
|
| 68 |
+
"as_unit",
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def get_dir(ser):
|
| 73 |
+
# check limited display api
|
| 74 |
+
results = [r for r in ser.dt.__dir__() if not r.startswith("_")]
|
| 75 |
+
return sorted(set(results))
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class TestSeriesDatetimeValues:
|
| 79 |
+
def _compare(self, ser, name):
|
| 80 |
+
# GH 7207, 11128
|
| 81 |
+
# test .dt namespace accessor
|
| 82 |
+
|
| 83 |
+
def get_expected(ser, prop):
|
| 84 |
+
result = getattr(Index(ser._values), prop)
|
| 85 |
+
if isinstance(result, np.ndarray):
|
| 86 |
+
if is_integer_dtype(result):
|
| 87 |
+
result = result.astype("int64")
|
| 88 |
+
elif not is_list_like(result) or isinstance(result, DataFrame):
|
| 89 |
+
return result
|
| 90 |
+
return Series(result, index=ser.index, name=ser.name)
|
| 91 |
+
|
| 92 |
+
left = getattr(ser.dt, name)
|
| 93 |
+
right = get_expected(ser, name)
|
| 94 |
+
if not (is_list_like(left) and is_list_like(right)):
|
| 95 |
+
assert left == right
|
| 96 |
+
elif isinstance(left, DataFrame):
|
| 97 |
+
tm.assert_frame_equal(left, right)
|
| 98 |
+
else:
|
| 99 |
+
tm.assert_series_equal(left, right)
|
| 100 |
+
|
| 101 |
+
@pytest.mark.parametrize("freq", ["D", "s", "ms"])
|
| 102 |
+
def test_dt_namespace_accessor_datetime64(self, freq):
|
| 103 |
+
# GH#7207, GH#11128
|
| 104 |
+
# test .dt namespace accessor
|
| 105 |
+
|
| 106 |
+
# datetimeindex
|
| 107 |
+
dti = date_range("20130101", periods=5, freq=freq)
|
| 108 |
+
ser = Series(dti, name="xxx")
|
| 109 |
+
|
| 110 |
+
for prop in ok_for_dt:
|
| 111 |
+
# we test freq below
|
| 112 |
+
if prop != "freq":
|
| 113 |
+
self._compare(ser, prop)
|
| 114 |
+
|
| 115 |
+
for prop in ok_for_dt_methods:
|
| 116 |
+
getattr(ser.dt, prop)
|
| 117 |
+
|
| 118 |
+
result = ser.dt.to_pydatetime()
|
| 119 |
+
assert isinstance(result, np.ndarray)
|
| 120 |
+
assert result.dtype == object
|
| 121 |
+
|
| 122 |
+
result = ser.dt.tz_localize("US/Eastern")
|
| 123 |
+
exp_values = DatetimeIndex(ser.values).tz_localize("US/Eastern")
|
| 124 |
+
expected = Series(exp_values, index=ser.index, name="xxx")
|
| 125 |
+
tm.assert_series_equal(result, expected)
|
| 126 |
+
|
| 127 |
+
tz_result = result.dt.tz
|
| 128 |
+
assert str(tz_result) == "US/Eastern"
|
| 129 |
+
freq_result = ser.dt.freq
|
| 130 |
+
assert freq_result == DatetimeIndex(ser.values, freq="infer").freq
|
| 131 |
+
|
| 132 |
+
# let's localize, then convert
|
| 133 |
+
result = ser.dt.tz_localize("UTC").dt.tz_convert("US/Eastern")
|
| 134 |
+
exp_values = (
|
| 135 |
+
DatetimeIndex(ser.values).tz_localize("UTC").tz_convert("US/Eastern")
|
| 136 |
+
)
|
| 137 |
+
expected = Series(exp_values, index=ser.index, name="xxx")
|
| 138 |
+
tm.assert_series_equal(result, expected)
|
| 139 |
+
|
| 140 |
+
def test_dt_namespace_accessor_datetime64tz(self):
|
| 141 |
+
# GH#7207, GH#11128
|
| 142 |
+
# test .dt namespace accessor
|
| 143 |
+
|
| 144 |
+
# datetimeindex with tz
|
| 145 |
+
dti = date_range("20130101", periods=5, tz="US/Eastern")
|
| 146 |
+
ser = Series(dti, name="xxx")
|
| 147 |
+
for prop in ok_for_dt:
|
| 148 |
+
# we test freq below
|
| 149 |
+
if prop != "freq":
|
| 150 |
+
self._compare(ser, prop)
|
| 151 |
+
|
| 152 |
+
for prop in ok_for_dt_methods:
|
| 153 |
+
getattr(ser.dt, prop)
|
| 154 |
+
|
| 155 |
+
result = ser.dt.to_pydatetime()
|
| 156 |
+
assert isinstance(result, np.ndarray)
|
| 157 |
+
assert result.dtype == object
|
| 158 |
+
|
| 159 |
+
result = ser.dt.tz_convert("CET")
|
| 160 |
+
expected = Series(ser._values.tz_convert("CET"), index=ser.index, name="xxx")
|
| 161 |
+
tm.assert_series_equal(result, expected)
|
| 162 |
+
|
| 163 |
+
tz_result = result.dt.tz
|
| 164 |
+
assert str(tz_result) == "CET"
|
| 165 |
+
freq_result = ser.dt.freq
|
| 166 |
+
assert freq_result == DatetimeIndex(ser.values, freq="infer").freq
|
| 167 |
+
|
| 168 |
+
def test_dt_namespace_accessor_timedelta(self):
|
| 169 |
+
# GH#7207, GH#11128
|
| 170 |
+
# test .dt namespace accessor
|
| 171 |
+
|
| 172 |
+
# timedelta index
|
| 173 |
+
cases = [
|
| 174 |
+
Series(
|
| 175 |
+
timedelta_range("1 day", periods=5), index=list("abcde"), name="xxx"
|
| 176 |
+
),
|
| 177 |
+
Series(timedelta_range("1 day 01:23:45", periods=5, freq="s"), name="xxx"),
|
| 178 |
+
Series(
|
| 179 |
+
timedelta_range("2 days 01:23:45.012345", periods=5, freq="ms"),
|
| 180 |
+
name="xxx",
|
| 181 |
+
),
|
| 182 |
+
]
|
| 183 |
+
for ser in cases:
|
| 184 |
+
for prop in ok_for_td:
|
| 185 |
+
# we test freq below
|
| 186 |
+
if prop != "freq":
|
| 187 |
+
self._compare(ser, prop)
|
| 188 |
+
|
| 189 |
+
for prop in ok_for_td_methods:
|
| 190 |
+
getattr(ser.dt, prop)
|
| 191 |
+
|
| 192 |
+
result = ser.dt.components
|
| 193 |
+
assert isinstance(result, DataFrame)
|
| 194 |
+
tm.assert_index_equal(result.index, ser.index)
|
| 195 |
+
|
| 196 |
+
result = ser.dt.to_pytimedelta()
|
| 197 |
+
assert isinstance(result, np.ndarray)
|
| 198 |
+
assert result.dtype == object
|
| 199 |
+
|
| 200 |
+
result = ser.dt.total_seconds()
|
| 201 |
+
assert isinstance(result, Series)
|
| 202 |
+
assert result.dtype == "float64"
|
| 203 |
+
|
| 204 |
+
freq_result = ser.dt.freq
|
| 205 |
+
assert freq_result == TimedeltaIndex(ser.values, freq="infer").freq
|
| 206 |
+
|
| 207 |
+
def test_dt_namespace_accessor_period(self):
|
| 208 |
+
# GH#7207, GH#11128
|
| 209 |
+
# test .dt namespace accessor
|
| 210 |
+
|
| 211 |
+
# periodindex
|
| 212 |
+
pi = period_range("20130101", periods=5, freq="D")
|
| 213 |
+
ser = Series(pi, name="xxx")
|
| 214 |
+
|
| 215 |
+
for prop in ok_for_period:
|
| 216 |
+
# we test freq below
|
| 217 |
+
if prop != "freq":
|
| 218 |
+
self._compare(ser, prop)
|
| 219 |
+
|
| 220 |
+
for prop in ok_for_period_methods:
|
| 221 |
+
getattr(ser.dt, prop)
|
| 222 |
+
|
| 223 |
+
freq_result = ser.dt.freq
|
| 224 |
+
assert freq_result == PeriodIndex(ser.values).freq
|
| 225 |
+
|
| 226 |
+
def test_dt_namespace_accessor_index_and_values(self):
|
| 227 |
+
# both
|
| 228 |
+
index = date_range("20130101", periods=3, freq="D")
|
| 229 |
+
dti = date_range("20140204", periods=3, freq="s")
|
| 230 |
+
ser = Series(dti, index=index, name="xxx")
|
| 231 |
+
exp = Series(
|
| 232 |
+
np.array([2014, 2014, 2014], dtype="int32"), index=index, name="xxx"
|
| 233 |
+
)
|
| 234 |
+
tm.assert_series_equal(ser.dt.year, exp)
|
| 235 |
+
|
| 236 |
+
exp = Series(np.array([2, 2, 2], dtype="int32"), index=index, name="xxx")
|
| 237 |
+
tm.assert_series_equal(ser.dt.month, exp)
|
| 238 |
+
|
| 239 |
+
exp = Series(np.array([0, 1, 2], dtype="int32"), index=index, name="xxx")
|
| 240 |
+
tm.assert_series_equal(ser.dt.second, exp)
|
| 241 |
+
|
| 242 |
+
exp = Series([ser[0]] * 3, index=index, name="xxx")
|
| 243 |
+
tm.assert_series_equal(ser.dt.normalize(), exp)
|
| 244 |
+
|
| 245 |
+
def test_dt_accessor_limited_display_api(self):
|
| 246 |
+
# tznaive
|
| 247 |
+
ser = Series(date_range("20130101", periods=5, freq="D"), name="xxx")
|
| 248 |
+
results = get_dir(ser)
|
| 249 |
+
tm.assert_almost_equal(results, sorted(set(ok_for_dt + ok_for_dt_methods)))
|
| 250 |
+
|
| 251 |
+
# tzaware
|
| 252 |
+
ser = Series(date_range("2015-01-01", "2016-01-01", freq="T"), name="xxx")
|
| 253 |
+
ser = ser.dt.tz_localize("UTC").dt.tz_convert("America/Chicago")
|
| 254 |
+
results = get_dir(ser)
|
| 255 |
+
tm.assert_almost_equal(results, sorted(set(ok_for_dt + ok_for_dt_methods)))
|
| 256 |
+
|
| 257 |
+
# Period
|
| 258 |
+
ser = Series(
|
| 259 |
+
period_range("20130101", periods=5, freq="D", name="xxx").astype(object)
|
| 260 |
+
)
|
| 261 |
+
results = get_dir(ser)
|
| 262 |
+
tm.assert_almost_equal(
|
| 263 |
+
results, sorted(set(ok_for_period + ok_for_period_methods))
|
| 264 |
+
)
|
| 265 |
+
|
| 266 |
+
def test_dt_accessor_ambiguous_freq_conversions(self):
|
| 267 |
+
# GH#11295
|
| 268 |
+
# ambiguous time error on the conversions
|
| 269 |
+
ser = Series(date_range("2015-01-01", "2016-01-01", freq="T"), name="xxx")
|
| 270 |
+
ser = ser.dt.tz_localize("UTC").dt.tz_convert("America/Chicago")
|
| 271 |
+
|
| 272 |
+
exp_values = date_range(
|
| 273 |
+
"2015-01-01", "2016-01-01", freq="T", tz="UTC"
|
| 274 |
+
).tz_convert("America/Chicago")
|
| 275 |
+
# freq not preserved by tz_localize above
|
| 276 |
+
exp_values = exp_values._with_freq(None)
|
| 277 |
+
expected = Series(exp_values, name="xxx")
|
| 278 |
+
tm.assert_series_equal(ser, expected)
|
| 279 |
+
|
| 280 |
+
def test_dt_accessor_not_writeable(self, using_copy_on_write):
|
| 281 |
+
# no setting allowed
|
| 282 |
+
ser = Series(date_range("20130101", periods=5, freq="D"), name="xxx")
|
| 283 |
+
with pytest.raises(ValueError, match="modifications"):
|
| 284 |
+
ser.dt.hour = 5
|
| 285 |
+
|
| 286 |
+
# trying to set a copy
|
| 287 |
+
msg = "modifications to a property of a datetimelike.+not supported"
|
| 288 |
+
with pd.option_context("chained_assignment", "raise"):
|
| 289 |
+
if using_copy_on_write:
|
| 290 |
+
with tm.raises_chained_assignment_error():
|
| 291 |
+
ser.dt.hour[0] = 5
|
| 292 |
+
else:
|
| 293 |
+
with pytest.raises(SettingWithCopyError, match=msg):
|
| 294 |
+
ser.dt.hour[0] = 5
|
| 295 |
+
|
| 296 |
+
@pytest.mark.parametrize(
|
| 297 |
+
"method, dates",
|
| 298 |
+
[
|
| 299 |
+
["round", ["2012-01-02", "2012-01-02", "2012-01-01"]],
|
| 300 |
+
["floor", ["2012-01-01", "2012-01-01", "2012-01-01"]],
|
| 301 |
+
["ceil", ["2012-01-02", "2012-01-02", "2012-01-02"]],
|
| 302 |
+
],
|
| 303 |
+
)
|
| 304 |
+
def test_dt_round(self, method, dates):
|
| 305 |
+
# round
|
| 306 |
+
ser = Series(
|
| 307 |
+
pd.to_datetime(
|
| 308 |
+
["2012-01-01 13:00:00", "2012-01-01 12:01:00", "2012-01-01 08:00:00"]
|
| 309 |
+
),
|
| 310 |
+
name="xxx",
|
| 311 |
+
)
|
| 312 |
+
result = getattr(ser.dt, method)("D")
|
| 313 |
+
expected = Series(pd.to_datetime(dates), name="xxx")
|
| 314 |
+
tm.assert_series_equal(result, expected)
|
| 315 |
+
|
| 316 |
+
def test_dt_round_tz(self):
|
| 317 |
+
ser = Series(
|
| 318 |
+
pd.to_datetime(
|
| 319 |
+
["2012-01-01 13:00:00", "2012-01-01 12:01:00", "2012-01-01 08:00:00"]
|
| 320 |
+
),
|
| 321 |
+
name="xxx",
|
| 322 |
+
)
|
| 323 |
+
result = ser.dt.tz_localize("UTC").dt.tz_convert("US/Eastern").dt.round("D")
|
| 324 |
+
|
| 325 |
+
exp_values = pd.to_datetime(
|
| 326 |
+
["2012-01-01", "2012-01-01", "2012-01-01"]
|
| 327 |
+
).tz_localize("US/Eastern")
|
| 328 |
+
expected = Series(exp_values, name="xxx")
|
| 329 |
+
tm.assert_series_equal(result, expected)
|
| 330 |
+
|
| 331 |
+
@pytest.mark.parametrize("method", ["ceil", "round", "floor"])
|
| 332 |
+
def test_dt_round_tz_ambiguous(self, method):
|
| 333 |
+
# GH 18946 round near "fall back" DST
|
| 334 |
+
df1 = DataFrame(
|
| 335 |
+
[
|
| 336 |
+
pd.to_datetime("2017-10-29 02:00:00+02:00", utc=True),
|
| 337 |
+
pd.to_datetime("2017-10-29 02:00:00+01:00", utc=True),
|
| 338 |
+
pd.to_datetime("2017-10-29 03:00:00+01:00", utc=True),
|
| 339 |
+
],
|
| 340 |
+
columns=["date"],
|
| 341 |
+
)
|
| 342 |
+
df1["date"] = df1["date"].dt.tz_convert("Europe/Madrid")
|
| 343 |
+
# infer
|
| 344 |
+
result = getattr(df1.date.dt, method)("H", ambiguous="infer")
|
| 345 |
+
expected = df1["date"]
|
| 346 |
+
tm.assert_series_equal(result, expected)
|
| 347 |
+
|
| 348 |
+
# bool-array
|
| 349 |
+
result = getattr(df1.date.dt, method)("H", ambiguous=[True, False, False])
|
| 350 |
+
tm.assert_series_equal(result, expected)
|
| 351 |
+
|
| 352 |
+
# NaT
|
| 353 |
+
result = getattr(df1.date.dt, method)("H", ambiguous="NaT")
|
| 354 |
+
expected = df1["date"].copy()
|
| 355 |
+
expected.iloc[0:2] = pd.NaT
|
| 356 |
+
tm.assert_series_equal(result, expected)
|
| 357 |
+
|
| 358 |
+
# raise
|
| 359 |
+
with tm.external_error_raised(pytz.AmbiguousTimeError):
|
| 360 |
+
getattr(df1.date.dt, method)("H", ambiguous="raise")
|
| 361 |
+
|
| 362 |
+
@pytest.mark.parametrize(
|
| 363 |
+
"method, ts_str, freq",
|
| 364 |
+
[
|
| 365 |
+
["ceil", "2018-03-11 01:59:00-0600", "5min"],
|
| 366 |
+
["round", "2018-03-11 01:59:00-0600", "5min"],
|
| 367 |
+
["floor", "2018-03-11 03:01:00-0500", "2H"],
|
| 368 |
+
],
|
| 369 |
+
)
|
| 370 |
+
def test_dt_round_tz_nonexistent(self, method, ts_str, freq):
|
| 371 |
+
# GH 23324 round near "spring forward" DST
|
| 372 |
+
ser = Series([pd.Timestamp(ts_str, tz="America/Chicago")])
|
| 373 |
+
result = getattr(ser.dt, method)(freq, nonexistent="shift_forward")
|
| 374 |
+
expected = Series([pd.Timestamp("2018-03-11 03:00:00", tz="America/Chicago")])
|
| 375 |
+
tm.assert_series_equal(result, expected)
|
| 376 |
+
|
| 377 |
+
result = getattr(ser.dt, method)(freq, nonexistent="NaT")
|
| 378 |
+
expected = Series([pd.NaT]).dt.tz_localize(result.dt.tz)
|
| 379 |
+
tm.assert_series_equal(result, expected)
|
| 380 |
+
|
| 381 |
+
with pytest.raises(pytz.NonExistentTimeError, match="2018-03-11 02:00:00"):
|
| 382 |
+
getattr(ser.dt, method)(freq, nonexistent="raise")
|
| 383 |
+
|
| 384 |
+
@pytest.mark.parametrize("freq", ["ns", "U", "1000U"])
|
| 385 |
+
def test_dt_round_nonnano_higher_resolution_no_op(self, freq):
|
| 386 |
+
# GH 52761
|
| 387 |
+
ser = Series(
|
| 388 |
+
["2020-05-31 08:00:00", "2000-12-31 04:00:05", "1800-03-14 07:30:20"],
|
| 389 |
+
dtype="datetime64[ms]",
|
| 390 |
+
)
|
| 391 |
+
expected = ser.copy()
|
| 392 |
+
result = ser.dt.round(freq)
|
| 393 |
+
tm.assert_series_equal(result, expected)
|
| 394 |
+
|
| 395 |
+
assert not np.shares_memory(ser.array._ndarray, result.array._ndarray)
|
| 396 |
+
|
| 397 |
+
def test_dt_namespace_accessor_categorical(self):
|
| 398 |
+
# GH 19468
|
| 399 |
+
dti = DatetimeIndex(["20171111", "20181212"]).repeat(2)
|
| 400 |
+
ser = Series(pd.Categorical(dti), name="foo")
|
| 401 |
+
result = ser.dt.year
|
| 402 |
+
expected = Series([2017, 2017, 2018, 2018], dtype="int32", name="foo")
|
| 403 |
+
tm.assert_series_equal(result, expected)
|
| 404 |
+
|
| 405 |
+
def test_dt_tz_localize_categorical(self, tz_aware_fixture):
|
| 406 |
+
# GH 27952
|
| 407 |
+
tz = tz_aware_fixture
|
| 408 |
+
datetimes = Series(
|
| 409 |
+
["2019-01-01", "2019-01-01", "2019-01-02"], dtype="datetime64[ns]"
|
| 410 |
+
)
|
| 411 |
+
categorical = datetimes.astype("category")
|
| 412 |
+
result = categorical.dt.tz_localize(tz)
|
| 413 |
+
expected = datetimes.dt.tz_localize(tz)
|
| 414 |
+
tm.assert_series_equal(result, expected)
|
| 415 |
+
|
| 416 |
+
def test_dt_tz_convert_categorical(self, tz_aware_fixture):
|
| 417 |
+
# GH 27952
|
| 418 |
+
tz = tz_aware_fixture
|
| 419 |
+
datetimes = Series(
|
| 420 |
+
["2019-01-01", "2019-01-01", "2019-01-02"], dtype="datetime64[ns, MET]"
|
| 421 |
+
)
|
| 422 |
+
categorical = datetimes.astype("category")
|
| 423 |
+
result = categorical.dt.tz_convert(tz)
|
| 424 |
+
expected = datetimes.dt.tz_convert(tz)
|
| 425 |
+
tm.assert_series_equal(result, expected)
|
| 426 |
+
|
| 427 |
+
@pytest.mark.parametrize("accessor", ["year", "month", "day"])
|
| 428 |
+
def test_dt_other_accessors_categorical(self, accessor):
|
| 429 |
+
# GH 27952
|
| 430 |
+
datetimes = Series(
|
| 431 |
+
["2018-01-01", "2018-01-01", "2019-01-02"], dtype="datetime64[ns]"
|
| 432 |
+
)
|
| 433 |
+
categorical = datetimes.astype("category")
|
| 434 |
+
result = getattr(categorical.dt, accessor)
|
| 435 |
+
expected = getattr(datetimes.dt, accessor)
|
| 436 |
+
tm.assert_series_equal(result, expected)
|
| 437 |
+
|
| 438 |
+
def test_dt_accessor_no_new_attributes(self):
|
| 439 |
+
# https://github.com/pandas-dev/pandas/issues/10673
|
| 440 |
+
ser = Series(date_range("20130101", periods=5, freq="D"))
|
| 441 |
+
with pytest.raises(AttributeError, match="You cannot add any new attribute"):
|
| 442 |
+
ser.dt.xlabel = "a"
|
| 443 |
+
|
| 444 |
+
# error: Unsupported operand types for + ("List[None]" and "List[str]")
|
| 445 |
+
@pytest.mark.parametrize(
|
| 446 |
+
"time_locale", [None] + tm.get_locales() # type: ignore[operator]
|
| 447 |
+
)
|
| 448 |
+
def test_dt_accessor_datetime_name_accessors(self, time_locale):
|
| 449 |
+
# Test Monday -> Sunday and January -> December, in that sequence
|
| 450 |
+
if time_locale is None:
|
| 451 |
+
# If the time_locale is None, day-name and month_name should
|
| 452 |
+
# return the english attributes
|
| 453 |
+
expected_days = [
|
| 454 |
+
"Monday",
|
| 455 |
+
"Tuesday",
|
| 456 |
+
"Wednesday",
|
| 457 |
+
"Thursday",
|
| 458 |
+
"Friday",
|
| 459 |
+
"Saturday",
|
| 460 |
+
"Sunday",
|
| 461 |
+
]
|
| 462 |
+
expected_months = [
|
| 463 |
+
"January",
|
| 464 |
+
"February",
|
| 465 |
+
"March",
|
| 466 |
+
"April",
|
| 467 |
+
"May",
|
| 468 |
+
"June",
|
| 469 |
+
"July",
|
| 470 |
+
"August",
|
| 471 |
+
"September",
|
| 472 |
+
"October",
|
| 473 |
+
"November",
|
| 474 |
+
"December",
|
| 475 |
+
]
|
| 476 |
+
else:
|
| 477 |
+
with tm.set_locale(time_locale, locale.LC_TIME):
|
| 478 |
+
expected_days = calendar.day_name[:]
|
| 479 |
+
expected_months = calendar.month_name[1:]
|
| 480 |
+
|
| 481 |
+
ser = Series(date_range(freq="D", start=datetime(1998, 1, 1), periods=365))
|
| 482 |
+
english_days = [
|
| 483 |
+
"Monday",
|
| 484 |
+
"Tuesday",
|
| 485 |
+
"Wednesday",
|
| 486 |
+
"Thursday",
|
| 487 |
+
"Friday",
|
| 488 |
+
"Saturday",
|
| 489 |
+
"Sunday",
|
| 490 |
+
]
|
| 491 |
+
for day, name, eng_name in zip(range(4, 11), expected_days, english_days):
|
| 492 |
+
name = name.capitalize()
|
| 493 |
+
assert ser.dt.day_name(locale=time_locale)[day] == name
|
| 494 |
+
assert ser.dt.day_name(locale=None)[day] == eng_name
|
| 495 |
+
ser = pd.concat([ser, Series([pd.NaT])])
|
| 496 |
+
assert np.isnan(ser.dt.day_name(locale=time_locale).iloc[-1])
|
| 497 |
+
|
| 498 |
+
ser = Series(date_range(freq="M", start="2012", end="2013"))
|
| 499 |
+
result = ser.dt.month_name(locale=time_locale)
|
| 500 |
+
expected = Series([month.capitalize() for month in expected_months])
|
| 501 |
+
|
| 502 |
+
# work around https://github.com/pandas-dev/pandas/issues/22342
|
| 503 |
+
result = result.str.normalize("NFD")
|
| 504 |
+
expected = expected.str.normalize("NFD")
|
| 505 |
+
|
| 506 |
+
tm.assert_series_equal(result, expected)
|
| 507 |
+
|
| 508 |
+
for s_date, expected in zip(ser, expected_months):
|
| 509 |
+
result = s_date.month_name(locale=time_locale)
|
| 510 |
+
expected = expected.capitalize()
|
| 511 |
+
|
| 512 |
+
result = unicodedata.normalize("NFD", result)
|
| 513 |
+
expected = unicodedata.normalize("NFD", expected)
|
| 514 |
+
|
| 515 |
+
assert result == expected
|
| 516 |
+
|
| 517 |
+
ser = pd.concat([ser, Series([pd.NaT])])
|
| 518 |
+
assert np.isnan(ser.dt.month_name(locale=time_locale).iloc[-1])
|
| 519 |
+
|
| 520 |
+
def test_strftime(self):
|
| 521 |
+
# GH 10086
|
| 522 |
+
ser = Series(date_range("20130101", periods=5))
|
| 523 |
+
result = ser.dt.strftime("%Y/%m/%d")
|
| 524 |
+
expected = Series(
|
| 525 |
+
["2013/01/01", "2013/01/02", "2013/01/03", "2013/01/04", "2013/01/05"]
|
| 526 |
+
)
|
| 527 |
+
tm.assert_series_equal(result, expected)
|
| 528 |
+
|
| 529 |
+
ser = Series(date_range("2015-02-03 11:22:33.4567", periods=5))
|
| 530 |
+
result = ser.dt.strftime("%Y/%m/%d %H-%M-%S")
|
| 531 |
+
expected = Series(
|
| 532 |
+
[
|
| 533 |
+
"2015/02/03 11-22-33",
|
| 534 |
+
"2015/02/04 11-22-33",
|
| 535 |
+
"2015/02/05 11-22-33",
|
| 536 |
+
"2015/02/06 11-22-33",
|
| 537 |
+
"2015/02/07 11-22-33",
|
| 538 |
+
]
|
| 539 |
+
)
|
| 540 |
+
tm.assert_series_equal(result, expected)
|
| 541 |
+
|
| 542 |
+
ser = Series(period_range("20130101", periods=5))
|
| 543 |
+
result = ser.dt.strftime("%Y/%m/%d")
|
| 544 |
+
expected = Series(
|
| 545 |
+
["2013/01/01", "2013/01/02", "2013/01/03", "2013/01/04", "2013/01/05"]
|
| 546 |
+
)
|
| 547 |
+
tm.assert_series_equal(result, expected)
|
| 548 |
+
|
| 549 |
+
ser = Series(period_range("2015-02-03 11:22:33.4567", periods=5, freq="s"))
|
| 550 |
+
result = ser.dt.strftime("%Y/%m/%d %H-%M-%S")
|
| 551 |
+
expected = Series(
|
| 552 |
+
[
|
| 553 |
+
"2015/02/03 11-22-33",
|
| 554 |
+
"2015/02/03 11-22-34",
|
| 555 |
+
"2015/02/03 11-22-35",
|
| 556 |
+
"2015/02/03 11-22-36",
|
| 557 |
+
"2015/02/03 11-22-37",
|
| 558 |
+
]
|
| 559 |
+
)
|
| 560 |
+
tm.assert_series_equal(result, expected)
|
| 561 |
+
|
| 562 |
+
def test_strftime_dt64_days(self):
|
| 563 |
+
ser = Series(date_range("20130101", periods=5))
|
| 564 |
+
ser.iloc[0] = pd.NaT
|
| 565 |
+
result = ser.dt.strftime("%Y/%m/%d")
|
| 566 |
+
expected = Series(
|
| 567 |
+
[np.nan, "2013/01/02", "2013/01/03", "2013/01/04", "2013/01/05"]
|
| 568 |
+
)
|
| 569 |
+
tm.assert_series_equal(result, expected)
|
| 570 |
+
|
| 571 |
+
datetime_index = date_range("20150301", periods=5)
|
| 572 |
+
result = datetime_index.strftime("%Y/%m/%d")
|
| 573 |
+
|
| 574 |
+
expected = Index(
|
| 575 |
+
["2015/03/01", "2015/03/02", "2015/03/03", "2015/03/04", "2015/03/05"],
|
| 576 |
+
dtype=np.object_,
|
| 577 |
+
)
|
| 578 |
+
# dtype may be S10 or U10 depending on python version
|
| 579 |
+
tm.assert_index_equal(result, expected)
|
| 580 |
+
|
| 581 |
+
def test_strftime_period_days(self):
|
| 582 |
+
period_index = period_range("20150301", periods=5)
|
| 583 |
+
result = period_index.strftime("%Y/%m/%d")
|
| 584 |
+
expected = Index(
|
| 585 |
+
["2015/03/01", "2015/03/02", "2015/03/03", "2015/03/04", "2015/03/05"],
|
| 586 |
+
dtype="=U10",
|
| 587 |
+
)
|
| 588 |
+
tm.assert_index_equal(result, expected)
|
| 589 |
+
|
| 590 |
+
def test_strftime_dt64_microsecond_resolution(self):
|
| 591 |
+
ser = Series([datetime(2013, 1, 1, 2, 32, 59), datetime(2013, 1, 2, 14, 32, 1)])
|
| 592 |
+
result = ser.dt.strftime("%Y-%m-%d %H:%M:%S")
|
| 593 |
+
expected = Series(["2013-01-01 02:32:59", "2013-01-02 14:32:01"])
|
| 594 |
+
tm.assert_series_equal(result, expected)
|
| 595 |
+
|
| 596 |
+
def test_strftime_period_hours(self):
|
| 597 |
+
ser = Series(period_range("20130101", periods=4, freq="H"))
|
| 598 |
+
result = ser.dt.strftime("%Y/%m/%d %H:%M:%S")
|
| 599 |
+
expected = Series(
|
| 600 |
+
[
|
| 601 |
+
"2013/01/01 00:00:00",
|
| 602 |
+
"2013/01/01 01:00:00",
|
| 603 |
+
"2013/01/01 02:00:00",
|
| 604 |
+
"2013/01/01 03:00:00",
|
| 605 |
+
]
|
| 606 |
+
)
|
| 607 |
+
tm.assert_series_equal(result, expected)
|
| 608 |
+
|
| 609 |
+
def test_strftime_period_minutes(self):
|
| 610 |
+
ser = Series(period_range("20130101", periods=4, freq="L"))
|
| 611 |
+
result = ser.dt.strftime("%Y/%m/%d %H:%M:%S.%l")
|
| 612 |
+
expected = Series(
|
| 613 |
+
[
|
| 614 |
+
"2013/01/01 00:00:00.000",
|
| 615 |
+
"2013/01/01 00:00:00.001",
|
| 616 |
+
"2013/01/01 00:00:00.002",
|
| 617 |
+
"2013/01/01 00:00:00.003",
|
| 618 |
+
]
|
| 619 |
+
)
|
| 620 |
+
tm.assert_series_equal(result, expected)
|
| 621 |
+
|
| 622 |
+
@pytest.mark.parametrize(
|
| 623 |
+
"data",
|
| 624 |
+
[
|
| 625 |
+
DatetimeIndex(["2019-01-01", pd.NaT]),
|
| 626 |
+
PeriodIndex(["2019-01-01", pd.NaT], dtype="period[D]"),
|
| 627 |
+
],
|
| 628 |
+
)
|
| 629 |
+
def test_strftime_nat(self, data):
|
| 630 |
+
# GH 29578
|
| 631 |
+
ser = Series(data)
|
| 632 |
+
result = ser.dt.strftime("%Y-%m-%d")
|
| 633 |
+
expected = Series(["2019-01-01", np.nan])
|
| 634 |
+
tm.assert_series_equal(result, expected)
|
| 635 |
+
|
| 636 |
+
@pytest.mark.parametrize(
|
| 637 |
+
"data", [DatetimeIndex([pd.NaT]), PeriodIndex([pd.NaT], dtype="period[D]")]
|
| 638 |
+
)
|
| 639 |
+
def test_strftime_all_nat(self, data):
|
| 640 |
+
# https://github.com/pandas-dev/pandas/issues/45858
|
| 641 |
+
ser = Series(data)
|
| 642 |
+
with tm.assert_produces_warning(None):
|
| 643 |
+
result = ser.dt.strftime("%Y-%m-%d")
|
| 644 |
+
expected = Series([np.nan], dtype=object)
|
| 645 |
+
tm.assert_series_equal(result, expected)
|
| 646 |
+
|
| 647 |
+
def test_valid_dt_with_missing_values(self):
|
| 648 |
+
# GH 8689
|
| 649 |
+
ser = Series(date_range("20130101", periods=5, freq="D"))
|
| 650 |
+
ser.iloc[2] = pd.NaT
|
| 651 |
+
|
| 652 |
+
for attr in ["microsecond", "nanosecond", "second", "minute", "hour", "day"]:
|
| 653 |
+
expected = getattr(ser.dt, attr).copy()
|
| 654 |
+
expected.iloc[2] = np.nan
|
| 655 |
+
result = getattr(ser.dt, attr)
|
| 656 |
+
tm.assert_series_equal(result, expected)
|
| 657 |
+
|
| 658 |
+
result = ser.dt.date
|
| 659 |
+
expected = Series(
|
| 660 |
+
[
|
| 661 |
+
date(2013, 1, 1),
|
| 662 |
+
date(2013, 1, 2),
|
| 663 |
+
np.nan,
|
| 664 |
+
date(2013, 1, 4),
|
| 665 |
+
date(2013, 1, 5),
|
| 666 |
+
],
|
| 667 |
+
dtype="object",
|
| 668 |
+
)
|
| 669 |
+
tm.assert_series_equal(result, expected)
|
| 670 |
+
|
| 671 |
+
result = ser.dt.time
|
| 672 |
+
expected = Series([time(0), time(0), np.nan, time(0), time(0)], dtype="object")
|
| 673 |
+
tm.assert_series_equal(result, expected)
|
| 674 |
+
|
| 675 |
+
def test_dt_accessor_api(self):
|
| 676 |
+
# GH 9322
|
| 677 |
+
from pandas.core.indexes.accessors import (
|
| 678 |
+
CombinedDatetimelikeProperties,
|
| 679 |
+
DatetimeProperties,
|
| 680 |
+
)
|
| 681 |
+
|
| 682 |
+
assert Series.dt is CombinedDatetimelikeProperties
|
| 683 |
+
|
| 684 |
+
ser = Series(date_range("2000-01-01", periods=3))
|
| 685 |
+
assert isinstance(ser.dt, DatetimeProperties)
|
| 686 |
+
|
| 687 |
+
@pytest.mark.parametrize(
|
| 688 |
+
"ser", [Series(np.arange(5)), Series(list("abcde")), Series(np.random.randn(5))]
|
| 689 |
+
)
|
| 690 |
+
def test_dt_accessor_invalid(self, ser):
|
| 691 |
+
# GH#9322 check that series with incorrect dtypes don't have attr
|
| 692 |
+
with pytest.raises(AttributeError, match="only use .dt accessor"):
|
| 693 |
+
ser.dt
|
| 694 |
+
assert not hasattr(ser, "dt")
|
| 695 |
+
|
| 696 |
+
def test_dt_accessor_updates_on_inplace(self):
|
| 697 |
+
ser = Series(date_range("2018-01-01", periods=10))
|
| 698 |
+
ser[2] = None
|
| 699 |
+
return_value = ser.fillna(pd.Timestamp("2018-01-01"), inplace=True)
|
| 700 |
+
assert return_value is None
|
| 701 |
+
result = ser.dt.date
|
| 702 |
+
assert result[0] == result[2]
|
| 703 |
+
|
| 704 |
+
def test_date_tz(self):
|
| 705 |
+
# GH11757
|
| 706 |
+
rng = DatetimeIndex(
|
| 707 |
+
["2014-04-04 23:56", "2014-07-18 21:24", "2015-11-22 22:14"],
|
| 708 |
+
tz="US/Eastern",
|
| 709 |
+
)
|
| 710 |
+
ser = Series(rng)
|
| 711 |
+
expected = Series([date(2014, 4, 4), date(2014, 7, 18), date(2015, 11, 22)])
|
| 712 |
+
tm.assert_series_equal(ser.dt.date, expected)
|
| 713 |
+
tm.assert_series_equal(ser.apply(lambda x: x.date()), expected)
|
| 714 |
+
|
| 715 |
+
def test_dt_timetz_accessor(self, tz_naive_fixture):
|
| 716 |
+
# GH21358
|
| 717 |
+
tz = maybe_get_tz(tz_naive_fixture)
|
| 718 |
+
|
| 719 |
+
dtindex = DatetimeIndex(
|
| 720 |
+
["2014-04-04 23:56", "2014-07-18 21:24", "2015-11-22 22:14"], tz=tz
|
| 721 |
+
)
|
| 722 |
+
ser = Series(dtindex)
|
| 723 |
+
expected = Series(
|
| 724 |
+
[time(23, 56, tzinfo=tz), time(21, 24, tzinfo=tz), time(22, 14, tzinfo=tz)]
|
| 725 |
+
)
|
| 726 |
+
result = ser.dt.timetz
|
| 727 |
+
tm.assert_series_equal(result, expected)
|
| 728 |
+
|
| 729 |
+
@pytest.mark.parametrize(
|
| 730 |
+
"input_series, expected_output",
|
| 731 |
+
[
|
| 732 |
+
[["2020-01-01"], [[2020, 1, 3]]],
|
| 733 |
+
[[pd.NaT], [[np.NaN, np.NaN, np.NaN]]],
|
| 734 |
+
[["2019-12-31", "2019-12-29"], [[2020, 1, 2], [2019, 52, 7]]],
|
| 735 |
+
[["2010-01-01", pd.NaT], [[2009, 53, 5], [np.NaN, np.NaN, np.NaN]]],
|
| 736 |
+
# see GH#36032
|
| 737 |
+
[["2016-01-08", "2016-01-04"], [[2016, 1, 5], [2016, 1, 1]]],
|
| 738 |
+
[["2016-01-07", "2016-01-01"], [[2016, 1, 4], [2015, 53, 5]]],
|
| 739 |
+
],
|
| 740 |
+
)
|
| 741 |
+
def test_isocalendar(self, input_series, expected_output):
|
| 742 |
+
result = pd.to_datetime(Series(input_series)).dt.isocalendar()
|
| 743 |
+
expected_frame = DataFrame(
|
| 744 |
+
expected_output, columns=["year", "week", "day"], dtype="UInt32"
|
| 745 |
+
)
|
| 746 |
+
tm.assert_frame_equal(result, expected_frame)
|
| 747 |
+
|
| 748 |
+
def test_hour_index(self):
|
| 749 |
+
dt_series = Series(
|
| 750 |
+
date_range(start="2021-01-01", periods=5, freq="h"),
|
| 751 |
+
index=[2, 6, 7, 8, 11],
|
| 752 |
+
dtype="category",
|
| 753 |
+
)
|
| 754 |
+
result = dt_series.dt.hour
|
| 755 |
+
expected = Series(
|
| 756 |
+
[0, 1, 2, 3, 4],
|
| 757 |
+
dtype="int32",
|
| 758 |
+
index=[2, 6, 7, 8, 11],
|
| 759 |
+
)
|
| 760 |
+
tm.assert_series_equal(result, expected)
|
| 761 |
+
|
| 762 |
+
|
| 763 |
+
class TestSeriesPeriodValuesDtAccessor:
|
| 764 |
+
@pytest.mark.parametrize(
|
| 765 |
+
"input_vals",
|
| 766 |
+
[
|
| 767 |
+
[Period("2016-01", freq="M"), Period("2016-02", freq="M")],
|
| 768 |
+
[Period("2016-01-01", freq="D"), Period("2016-01-02", freq="D")],
|
| 769 |
+
[
|
| 770 |
+
Period("2016-01-01 00:00:00", freq="H"),
|
| 771 |
+
Period("2016-01-01 01:00:00", freq="H"),
|
| 772 |
+
],
|
| 773 |
+
[
|
| 774 |
+
Period("2016-01-01 00:00:00", freq="M"),
|
| 775 |
+
Period("2016-01-01 00:01:00", freq="M"),
|
| 776 |
+
],
|
| 777 |
+
[
|
| 778 |
+
Period("2016-01-01 00:00:00", freq="S"),
|
| 779 |
+
Period("2016-01-01 00:00:01", freq="S"),
|
| 780 |
+
],
|
| 781 |
+
],
|
| 782 |
+
)
|
| 783 |
+
def test_end_time_timevalues(self, input_vals):
|
| 784 |
+
# GH#17157
|
| 785 |
+
# Check that the time part of the Period is adjusted by end_time
|
| 786 |
+
# when using the dt accessor on a Series
|
| 787 |
+
input_vals = PeriodArray._from_sequence(np.asarray(input_vals))
|
| 788 |
+
|
| 789 |
+
ser = Series(input_vals)
|
| 790 |
+
result = ser.dt.end_time
|
| 791 |
+
expected = ser.apply(lambda x: x.end_time)
|
| 792 |
+
tm.assert_series_equal(result, expected)
|
| 793 |
+
|
| 794 |
+
@pytest.mark.parametrize("input_vals", [("2001"), ("NaT")])
|
| 795 |
+
def test_to_period(self, input_vals):
|
| 796 |
+
# GH#21205
|
| 797 |
+
expected = Series([input_vals], dtype="Period[D]")
|
| 798 |
+
result = Series([input_vals], dtype="datetime64[ns]").dt.to_period("D")
|
| 799 |
+
tm.assert_series_equal(result, expected)
|
| 800 |
+
|
| 801 |
+
|
| 802 |
+
def test_normalize_pre_epoch_dates():
|
| 803 |
+
# GH: 36294
|
| 804 |
+
ser = pd.to_datetime(Series(["1969-01-01 09:00:00", "2016-01-01 09:00:00"]))
|
| 805 |
+
result = ser.dt.normalize()
|
| 806 |
+
expected = pd.to_datetime(Series(["1969-01-01", "2016-01-01"]))
|
| 807 |
+
tm.assert_series_equal(result, expected)
|
| 808 |
+
|
| 809 |
+
|
| 810 |
+
def test_day_attribute_non_nano_beyond_int32():
|
| 811 |
+
# GH 52386
|
| 812 |
+
data = np.array(
|
| 813 |
+
[
|
| 814 |
+
136457654736252,
|
| 815 |
+
134736784364431,
|
| 816 |
+
245345345545332,
|
| 817 |
+
223432411,
|
| 818 |
+
2343241,
|
| 819 |
+
3634548734,
|
| 820 |
+
23234,
|
| 821 |
+
],
|
| 822 |
+
dtype="timedelta64[s]",
|
| 823 |
+
)
|
| 824 |
+
ser = Series(data)
|
| 825 |
+
result = ser.dt.days
|
| 826 |
+
expected = Series([1579371003, 1559453522, 2839645203, 2586, 27, 42066, 0])
|
| 827 |
+
tm.assert_series_equal(result, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/__init__.py
ADDED
|
File without changes
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (184 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/__pycache__/test_datetime.cpython-310.pyc
ADDED
|
Binary file (10.8 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/__pycache__/test_set_value.cpython-310.pyc
ADDED
|
Binary file (1.25 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/__pycache__/test_setitem.cpython-310.pyc
ADDED
|
Binary file (49.3 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/__pycache__/test_take.cpython-310.pyc
ADDED
|
Binary file (1.41 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_datetime.py
ADDED
|
@@ -0,0 +1,475 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Also test support for datetime64[ns] in Series / DataFrame
|
| 3 |
+
"""
|
| 4 |
+
from datetime import (
|
| 5 |
+
datetime,
|
| 6 |
+
timedelta,
|
| 7 |
+
)
|
| 8 |
+
import re
|
| 9 |
+
|
| 10 |
+
from dateutil.tz import (
|
| 11 |
+
gettz,
|
| 12 |
+
tzutc,
|
| 13 |
+
)
|
| 14 |
+
import numpy as np
|
| 15 |
+
import pytest
|
| 16 |
+
import pytz
|
| 17 |
+
|
| 18 |
+
from pandas._libs import index as libindex
|
| 19 |
+
|
| 20 |
+
import pandas as pd
|
| 21 |
+
from pandas import (
|
| 22 |
+
DataFrame,
|
| 23 |
+
Series,
|
| 24 |
+
Timestamp,
|
| 25 |
+
date_range,
|
| 26 |
+
period_range,
|
| 27 |
+
)
|
| 28 |
+
import pandas._testing as tm
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def test_fancy_getitem():
|
| 32 |
+
dti = date_range(
|
| 33 |
+
freq="WOM-1FRI", start=datetime(2005, 1, 1), end=datetime(2010, 1, 1)
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
s = Series(np.arange(len(dti)), index=dti)
|
| 37 |
+
|
| 38 |
+
assert s[48] == 48
|
| 39 |
+
assert s["1/2/2009"] == 48
|
| 40 |
+
assert s["2009-1-2"] == 48
|
| 41 |
+
assert s[datetime(2009, 1, 2)] == 48
|
| 42 |
+
assert s[Timestamp(datetime(2009, 1, 2))] == 48
|
| 43 |
+
with pytest.raises(KeyError, match=r"^'2009-1-3'$"):
|
| 44 |
+
s["2009-1-3"]
|
| 45 |
+
tm.assert_series_equal(
|
| 46 |
+
s["3/6/2009":"2009-06-05"], s[datetime(2009, 3, 6) : datetime(2009, 6, 5)]
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def test_fancy_setitem():
|
| 51 |
+
dti = date_range(
|
| 52 |
+
freq="WOM-1FRI", start=datetime(2005, 1, 1), end=datetime(2010, 1, 1)
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
s = Series(np.arange(len(dti)), index=dti)
|
| 56 |
+
s[48] = -1
|
| 57 |
+
assert s[48] == -1
|
| 58 |
+
s["1/2/2009"] = -2
|
| 59 |
+
assert s[48] == -2
|
| 60 |
+
s["1/2/2009":"2009-06-05"] = -3
|
| 61 |
+
assert (s[48:54] == -3).all()
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
@pytest.mark.parametrize("tz_source", ["pytz", "dateutil"])
|
| 65 |
+
def test_getitem_setitem_datetime_tz(tz_source):
|
| 66 |
+
if tz_source == "pytz":
|
| 67 |
+
tzget = pytz.timezone
|
| 68 |
+
else:
|
| 69 |
+
# handle special case for utc in dateutil
|
| 70 |
+
tzget = lambda x: tzutc() if x == "UTC" else gettz(x)
|
| 71 |
+
|
| 72 |
+
N = 50
|
| 73 |
+
# testing with timezone, GH #2785
|
| 74 |
+
rng = date_range("1/1/1990", periods=N, freq="H", tz=tzget("US/Eastern"))
|
| 75 |
+
ts = Series(np.random.randn(N), index=rng)
|
| 76 |
+
|
| 77 |
+
# also test Timestamp tz handling, GH #2789
|
| 78 |
+
result = ts.copy()
|
| 79 |
+
result["1990-01-01 09:00:00+00:00"] = 0
|
| 80 |
+
result["1990-01-01 09:00:00+00:00"] = ts[4]
|
| 81 |
+
tm.assert_series_equal(result, ts)
|
| 82 |
+
|
| 83 |
+
result = ts.copy()
|
| 84 |
+
result["1990-01-01 03:00:00-06:00"] = 0
|
| 85 |
+
result["1990-01-01 03:00:00-06:00"] = ts[4]
|
| 86 |
+
tm.assert_series_equal(result, ts)
|
| 87 |
+
|
| 88 |
+
# repeat with datetimes
|
| 89 |
+
result = ts.copy()
|
| 90 |
+
result[datetime(1990, 1, 1, 9, tzinfo=tzget("UTC"))] = 0
|
| 91 |
+
result[datetime(1990, 1, 1, 9, tzinfo=tzget("UTC"))] = ts[4]
|
| 92 |
+
tm.assert_series_equal(result, ts)
|
| 93 |
+
|
| 94 |
+
result = ts.copy()
|
| 95 |
+
dt = Timestamp(1990, 1, 1, 3).tz_localize(tzget("US/Central"))
|
| 96 |
+
dt = dt.to_pydatetime()
|
| 97 |
+
result[dt] = 0
|
| 98 |
+
result[dt] = ts[4]
|
| 99 |
+
tm.assert_series_equal(result, ts)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def test_getitem_setitem_datetimeindex():
|
| 103 |
+
N = 50
|
| 104 |
+
# testing with timezone, GH #2785
|
| 105 |
+
rng = date_range("1/1/1990", periods=N, freq="H", tz="US/Eastern")
|
| 106 |
+
ts = Series(np.random.randn(N), index=rng)
|
| 107 |
+
|
| 108 |
+
result = ts["1990-01-01 04:00:00"]
|
| 109 |
+
expected = ts[4]
|
| 110 |
+
assert result == expected
|
| 111 |
+
|
| 112 |
+
result = ts.copy()
|
| 113 |
+
result["1990-01-01 04:00:00"] = 0
|
| 114 |
+
result["1990-01-01 04:00:00"] = ts[4]
|
| 115 |
+
tm.assert_series_equal(result, ts)
|
| 116 |
+
|
| 117 |
+
result = ts["1990-01-01 04:00:00":"1990-01-01 07:00:00"]
|
| 118 |
+
expected = ts[4:8]
|
| 119 |
+
tm.assert_series_equal(result, expected)
|
| 120 |
+
|
| 121 |
+
result = ts.copy()
|
| 122 |
+
result["1990-01-01 04:00:00":"1990-01-01 07:00:00"] = 0
|
| 123 |
+
result["1990-01-01 04:00:00":"1990-01-01 07:00:00"] = ts[4:8]
|
| 124 |
+
tm.assert_series_equal(result, ts)
|
| 125 |
+
|
| 126 |
+
lb = "1990-01-01 04:00:00"
|
| 127 |
+
rb = "1990-01-01 07:00:00"
|
| 128 |
+
# GH#18435 strings get a pass from tzawareness compat
|
| 129 |
+
result = ts[(ts.index >= lb) & (ts.index <= rb)]
|
| 130 |
+
expected = ts[4:8]
|
| 131 |
+
tm.assert_series_equal(result, expected)
|
| 132 |
+
|
| 133 |
+
lb = "1990-01-01 04:00:00-0500"
|
| 134 |
+
rb = "1990-01-01 07:00:00-0500"
|
| 135 |
+
result = ts[(ts.index >= lb) & (ts.index <= rb)]
|
| 136 |
+
expected = ts[4:8]
|
| 137 |
+
tm.assert_series_equal(result, expected)
|
| 138 |
+
|
| 139 |
+
# But we do not give datetimes a pass on tzawareness compat
|
| 140 |
+
msg = "Cannot compare tz-naive and tz-aware datetime-like objects"
|
| 141 |
+
naive = datetime(1990, 1, 1, 4)
|
| 142 |
+
for key in [naive, Timestamp(naive), np.datetime64(naive, "ns")]:
|
| 143 |
+
with pytest.raises(KeyError, match=re.escape(repr(key))):
|
| 144 |
+
# GH#36148 as of 2.0 we require tzawareness-compat
|
| 145 |
+
ts[key]
|
| 146 |
+
|
| 147 |
+
result = ts.copy()
|
| 148 |
+
# GH#36148 as of 2.0 we do not ignore tzawareness mismatch in indexing,
|
| 149 |
+
# so setting it as a new key casts to object rather than matching
|
| 150 |
+
# rng[4]
|
| 151 |
+
result[naive] = ts[4]
|
| 152 |
+
assert result.index.dtype == object
|
| 153 |
+
tm.assert_index_equal(result.index[:-1], rng.astype(object))
|
| 154 |
+
assert result.index[-1] == naive
|
| 155 |
+
|
| 156 |
+
msg = "Cannot compare tz-naive and tz-aware datetime-like objects"
|
| 157 |
+
with pytest.raises(TypeError, match=msg):
|
| 158 |
+
# GH#36148 require tzawareness compat as of 2.0
|
| 159 |
+
ts[naive : datetime(1990, 1, 1, 7)]
|
| 160 |
+
|
| 161 |
+
result = ts.copy()
|
| 162 |
+
with pytest.raises(TypeError, match=msg):
|
| 163 |
+
# GH#36148 require tzawareness compat as of 2.0
|
| 164 |
+
result[naive : datetime(1990, 1, 1, 7)] = 0
|
| 165 |
+
with pytest.raises(TypeError, match=msg):
|
| 166 |
+
# GH#36148 require tzawareness compat as of 2.0
|
| 167 |
+
result[naive : datetime(1990, 1, 1, 7)] = 99
|
| 168 |
+
# the __setitems__ here failed, so result should still match ts
|
| 169 |
+
tm.assert_series_equal(result, ts)
|
| 170 |
+
|
| 171 |
+
lb = naive
|
| 172 |
+
rb = datetime(1990, 1, 1, 7)
|
| 173 |
+
msg = r"Invalid comparison between dtype=datetime64\[ns, US/Eastern\] and datetime"
|
| 174 |
+
with pytest.raises(TypeError, match=msg):
|
| 175 |
+
# tznaive vs tzaware comparison is invalid
|
| 176 |
+
# see GH#18376, GH#18162
|
| 177 |
+
ts[(ts.index >= lb) & (ts.index <= rb)]
|
| 178 |
+
|
| 179 |
+
lb = Timestamp(naive).tz_localize(rng.tzinfo)
|
| 180 |
+
rb = Timestamp(datetime(1990, 1, 1, 7)).tz_localize(rng.tzinfo)
|
| 181 |
+
result = ts[(ts.index >= lb) & (ts.index <= rb)]
|
| 182 |
+
expected = ts[4:8]
|
| 183 |
+
tm.assert_series_equal(result, expected)
|
| 184 |
+
|
| 185 |
+
result = ts[ts.index[4]]
|
| 186 |
+
expected = ts[4]
|
| 187 |
+
assert result == expected
|
| 188 |
+
|
| 189 |
+
result = ts[ts.index[4:8]]
|
| 190 |
+
expected = ts[4:8]
|
| 191 |
+
tm.assert_series_equal(result, expected)
|
| 192 |
+
|
| 193 |
+
result = ts.copy()
|
| 194 |
+
result[ts.index[4:8]] = 0
|
| 195 |
+
result.iloc[4:8] = ts.iloc[4:8]
|
| 196 |
+
tm.assert_series_equal(result, ts)
|
| 197 |
+
|
| 198 |
+
# also test partial date slicing
|
| 199 |
+
result = ts["1990-01-02"]
|
| 200 |
+
expected = ts[24:48]
|
| 201 |
+
tm.assert_series_equal(result, expected)
|
| 202 |
+
|
| 203 |
+
result = ts.copy()
|
| 204 |
+
result["1990-01-02"] = 0
|
| 205 |
+
result["1990-01-02"] = ts[24:48]
|
| 206 |
+
tm.assert_series_equal(result, ts)
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
def test_getitem_setitem_periodindex():
|
| 210 |
+
N = 50
|
| 211 |
+
rng = period_range("1/1/1990", periods=N, freq="H")
|
| 212 |
+
ts = Series(np.random.randn(N), index=rng)
|
| 213 |
+
|
| 214 |
+
result = ts["1990-01-01 04"]
|
| 215 |
+
expected = ts[4]
|
| 216 |
+
assert result == expected
|
| 217 |
+
|
| 218 |
+
result = ts.copy()
|
| 219 |
+
result["1990-01-01 04"] = 0
|
| 220 |
+
result["1990-01-01 04"] = ts[4]
|
| 221 |
+
tm.assert_series_equal(result, ts)
|
| 222 |
+
|
| 223 |
+
result = ts["1990-01-01 04":"1990-01-01 07"]
|
| 224 |
+
expected = ts[4:8]
|
| 225 |
+
tm.assert_series_equal(result, expected)
|
| 226 |
+
|
| 227 |
+
result = ts.copy()
|
| 228 |
+
result["1990-01-01 04":"1990-01-01 07"] = 0
|
| 229 |
+
result["1990-01-01 04":"1990-01-01 07"] = ts[4:8]
|
| 230 |
+
tm.assert_series_equal(result, ts)
|
| 231 |
+
|
| 232 |
+
lb = "1990-01-01 04"
|
| 233 |
+
rb = "1990-01-01 07"
|
| 234 |
+
result = ts[(ts.index >= lb) & (ts.index <= rb)]
|
| 235 |
+
expected = ts[4:8]
|
| 236 |
+
tm.assert_series_equal(result, expected)
|
| 237 |
+
|
| 238 |
+
# GH 2782
|
| 239 |
+
result = ts[ts.index[4]]
|
| 240 |
+
expected = ts[4]
|
| 241 |
+
assert result == expected
|
| 242 |
+
|
| 243 |
+
result = ts[ts.index[4:8]]
|
| 244 |
+
expected = ts[4:8]
|
| 245 |
+
tm.assert_series_equal(result, expected)
|
| 246 |
+
|
| 247 |
+
result = ts.copy()
|
| 248 |
+
result[ts.index[4:8]] = 0
|
| 249 |
+
result.iloc[4:8] = ts.iloc[4:8]
|
| 250 |
+
tm.assert_series_equal(result, ts)
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
def test_datetime_indexing():
|
| 254 |
+
index = date_range("1/1/2000", "1/7/2000")
|
| 255 |
+
index = index.repeat(3)
|
| 256 |
+
|
| 257 |
+
s = Series(len(index), index=index)
|
| 258 |
+
stamp = Timestamp("1/8/2000")
|
| 259 |
+
|
| 260 |
+
with pytest.raises(KeyError, match=re.escape(repr(stamp))):
|
| 261 |
+
s[stamp]
|
| 262 |
+
s[stamp] = 0
|
| 263 |
+
assert s[stamp] == 0
|
| 264 |
+
|
| 265 |
+
# not monotonic
|
| 266 |
+
s = Series(len(index), index=index)
|
| 267 |
+
s = s[::-1]
|
| 268 |
+
|
| 269 |
+
with pytest.raises(KeyError, match=re.escape(repr(stamp))):
|
| 270 |
+
s[stamp]
|
| 271 |
+
s[stamp] = 0
|
| 272 |
+
assert s[stamp] == 0
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
# test duplicates in time series
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
def test_indexing_with_duplicate_datetimeindex(
|
| 279 |
+
rand_series_with_duplicate_datetimeindex,
|
| 280 |
+
):
|
| 281 |
+
ts = rand_series_with_duplicate_datetimeindex
|
| 282 |
+
|
| 283 |
+
uniques = ts.index.unique()
|
| 284 |
+
for date in uniques:
|
| 285 |
+
result = ts[date]
|
| 286 |
+
|
| 287 |
+
mask = ts.index == date
|
| 288 |
+
total = (ts.index == date).sum()
|
| 289 |
+
expected = ts[mask]
|
| 290 |
+
if total > 1:
|
| 291 |
+
tm.assert_series_equal(result, expected)
|
| 292 |
+
else:
|
| 293 |
+
tm.assert_almost_equal(result, expected[0])
|
| 294 |
+
|
| 295 |
+
cp = ts.copy()
|
| 296 |
+
cp[date] = 0
|
| 297 |
+
expected = Series(np.where(mask, 0, ts), index=ts.index)
|
| 298 |
+
tm.assert_series_equal(cp, expected)
|
| 299 |
+
|
| 300 |
+
key = datetime(2000, 1, 6)
|
| 301 |
+
with pytest.raises(KeyError, match=re.escape(repr(key))):
|
| 302 |
+
ts[key]
|
| 303 |
+
|
| 304 |
+
# new index
|
| 305 |
+
ts[datetime(2000, 1, 6)] = 0
|
| 306 |
+
assert ts[datetime(2000, 1, 6)] == 0
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
def test_loc_getitem_over_size_cutoff(monkeypatch):
|
| 310 |
+
# #1821
|
| 311 |
+
|
| 312 |
+
monkeypatch.setattr(libindex, "_SIZE_CUTOFF", 1000)
|
| 313 |
+
|
| 314 |
+
# create large list of non periodic datetime
|
| 315 |
+
dates = []
|
| 316 |
+
sec = timedelta(seconds=1)
|
| 317 |
+
half_sec = timedelta(microseconds=500000)
|
| 318 |
+
d = datetime(2011, 12, 5, 20, 30)
|
| 319 |
+
n = 1100
|
| 320 |
+
for i in range(n):
|
| 321 |
+
dates.append(d)
|
| 322 |
+
dates.append(d + sec)
|
| 323 |
+
dates.append(d + sec + half_sec)
|
| 324 |
+
dates.append(d + sec + sec + half_sec)
|
| 325 |
+
d += 3 * sec
|
| 326 |
+
|
| 327 |
+
# duplicate some values in the list
|
| 328 |
+
duplicate_positions = np.random.randint(0, len(dates) - 1, 20)
|
| 329 |
+
for p in duplicate_positions:
|
| 330 |
+
dates[p + 1] = dates[p]
|
| 331 |
+
|
| 332 |
+
df = DataFrame(np.random.randn(len(dates), 4), index=dates, columns=list("ABCD"))
|
| 333 |
+
|
| 334 |
+
pos = n * 3
|
| 335 |
+
timestamp = df.index[pos]
|
| 336 |
+
assert timestamp in df.index
|
| 337 |
+
|
| 338 |
+
# it works!
|
| 339 |
+
df.loc[timestamp]
|
| 340 |
+
assert len(df.loc[[timestamp]]) > 0
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
def test_indexing_over_size_cutoff_period_index(monkeypatch):
|
| 344 |
+
# GH 27136
|
| 345 |
+
|
| 346 |
+
monkeypatch.setattr(libindex, "_SIZE_CUTOFF", 1000)
|
| 347 |
+
|
| 348 |
+
n = 1100
|
| 349 |
+
idx = period_range("1/1/2000", freq="T", periods=n)
|
| 350 |
+
assert idx._engine.over_size_threshold
|
| 351 |
+
|
| 352 |
+
s = Series(np.random.randn(len(idx)), index=idx)
|
| 353 |
+
|
| 354 |
+
pos = n - 1
|
| 355 |
+
timestamp = idx[pos]
|
| 356 |
+
assert timestamp in s.index
|
| 357 |
+
|
| 358 |
+
# it works!
|
| 359 |
+
s[timestamp]
|
| 360 |
+
assert len(s.loc[[timestamp]]) > 0
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
def test_indexing_unordered():
|
| 364 |
+
# GH 2437
|
| 365 |
+
rng = date_range(start="2011-01-01", end="2011-01-15")
|
| 366 |
+
ts = Series(np.random.rand(len(rng)), index=rng)
|
| 367 |
+
ts2 = pd.concat([ts[0:4], ts[-4:], ts[4:-4]])
|
| 368 |
+
|
| 369 |
+
for t in ts.index:
|
| 370 |
+
expected = ts[t]
|
| 371 |
+
result = ts2[t]
|
| 372 |
+
assert expected == result
|
| 373 |
+
|
| 374 |
+
# GH 3448 (ranges)
|
| 375 |
+
def compare(slobj):
|
| 376 |
+
result = ts2[slobj].copy()
|
| 377 |
+
result = result.sort_index()
|
| 378 |
+
expected = ts[slobj]
|
| 379 |
+
expected.index = expected.index._with_freq(None)
|
| 380 |
+
tm.assert_series_equal(result, expected)
|
| 381 |
+
|
| 382 |
+
compare(slice("2011-01-01", "2011-01-15"))
|
| 383 |
+
with pytest.raises(KeyError, match="Value based partial slicing on non-monotonic"):
|
| 384 |
+
compare(slice("2010-12-30", "2011-01-15"))
|
| 385 |
+
compare(slice("2011-01-01", "2011-01-16"))
|
| 386 |
+
|
| 387 |
+
# partial ranges
|
| 388 |
+
compare(slice("2011-01-01", "2011-01-6"))
|
| 389 |
+
compare(slice("2011-01-06", "2011-01-8"))
|
| 390 |
+
compare(slice("2011-01-06", "2011-01-12"))
|
| 391 |
+
|
| 392 |
+
# single values
|
| 393 |
+
result = ts2["2011"].sort_index()
|
| 394 |
+
expected = ts["2011"]
|
| 395 |
+
expected.index = expected.index._with_freq(None)
|
| 396 |
+
tm.assert_series_equal(result, expected)
|
| 397 |
+
|
| 398 |
+
|
| 399 |
+
def test_indexing_unordered2():
|
| 400 |
+
# diff freq
|
| 401 |
+
rng = date_range(datetime(2005, 1, 1), periods=20, freq="M")
|
| 402 |
+
ts = Series(np.arange(len(rng)), index=rng)
|
| 403 |
+
ts = ts.take(np.random.permutation(20))
|
| 404 |
+
|
| 405 |
+
result = ts["2005"]
|
| 406 |
+
for t in result.index:
|
| 407 |
+
assert t.year == 2005
|
| 408 |
+
|
| 409 |
+
|
| 410 |
+
def test_indexing():
|
| 411 |
+
idx = date_range("2001-1-1", periods=20, freq="M")
|
| 412 |
+
ts = Series(np.random.rand(len(idx)), index=idx)
|
| 413 |
+
|
| 414 |
+
# getting
|
| 415 |
+
|
| 416 |
+
# GH 3070, make sure semantics work on Series/Frame
|
| 417 |
+
expected = ts["2001"]
|
| 418 |
+
expected.name = "A"
|
| 419 |
+
|
| 420 |
+
df = DataFrame({"A": ts})
|
| 421 |
+
|
| 422 |
+
# GH#36179 pre-2.0 df["2001"] operated as slicing on rows. in 2.0 it behaves
|
| 423 |
+
# like any other key, so raises
|
| 424 |
+
with pytest.raises(KeyError, match="2001"):
|
| 425 |
+
df["2001"]
|
| 426 |
+
|
| 427 |
+
# setting
|
| 428 |
+
ts["2001"] = 1
|
| 429 |
+
expected = ts["2001"]
|
| 430 |
+
expected.name = "A"
|
| 431 |
+
|
| 432 |
+
df.loc["2001", "A"] = 1
|
| 433 |
+
|
| 434 |
+
with pytest.raises(KeyError, match="2001"):
|
| 435 |
+
df["2001"]
|
| 436 |
+
|
| 437 |
+
|
| 438 |
+
def test_getitem_str_month_with_datetimeindex():
|
| 439 |
+
# GH3546 (not including times on the last day)
|
| 440 |
+
idx = date_range(start="2013-05-31 00:00", end="2013-05-31 23:00", freq="H")
|
| 441 |
+
ts = Series(range(len(idx)), index=idx)
|
| 442 |
+
expected = ts["2013-05"]
|
| 443 |
+
tm.assert_series_equal(expected, ts)
|
| 444 |
+
|
| 445 |
+
idx = date_range(start="2013-05-31 00:00", end="2013-05-31 23:59", freq="S")
|
| 446 |
+
ts = Series(range(len(idx)), index=idx)
|
| 447 |
+
expected = ts["2013-05"]
|
| 448 |
+
tm.assert_series_equal(expected, ts)
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
def test_getitem_str_year_with_datetimeindex():
|
| 452 |
+
idx = [
|
| 453 |
+
Timestamp("2013-05-31 00:00"),
|
| 454 |
+
Timestamp(datetime(2013, 5, 31, 23, 59, 59, 999999)),
|
| 455 |
+
]
|
| 456 |
+
ts = Series(range(len(idx)), index=idx)
|
| 457 |
+
expected = ts["2013"]
|
| 458 |
+
tm.assert_series_equal(expected, ts)
|
| 459 |
+
|
| 460 |
+
|
| 461 |
+
def test_getitem_str_second_with_datetimeindex():
|
| 462 |
+
# GH14826, indexing with a seconds resolution string / datetime object
|
| 463 |
+
df = DataFrame(
|
| 464 |
+
np.random.rand(5, 5),
|
| 465 |
+
columns=["open", "high", "low", "close", "volume"],
|
| 466 |
+
index=date_range("2012-01-02 18:01:00", periods=5, tz="US/Central", freq="s"),
|
| 467 |
+
)
|
| 468 |
+
|
| 469 |
+
# this is a single date, so will raise
|
| 470 |
+
with pytest.raises(KeyError, match=r"^'2012-01-02 18:01:02'$"):
|
| 471 |
+
df["2012-01-02 18:01:02"]
|
| 472 |
+
|
| 473 |
+
msg = r"Timestamp\('2012-01-02 18:01:02-0600', tz='US/Central'\)"
|
| 474 |
+
with pytest.raises(KeyError, match=msg):
|
| 475 |
+
df[df.index[2]]
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_delitem.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from pandas import (
|
| 4 |
+
Index,
|
| 5 |
+
Series,
|
| 6 |
+
date_range,
|
| 7 |
+
)
|
| 8 |
+
import pandas._testing as tm
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TestSeriesDelItem:
|
| 12 |
+
def test_delitem(self):
|
| 13 |
+
# GH#5542
|
| 14 |
+
# should delete the item inplace
|
| 15 |
+
s = Series(range(5))
|
| 16 |
+
del s[0]
|
| 17 |
+
|
| 18 |
+
expected = Series(range(1, 5), index=range(1, 5))
|
| 19 |
+
tm.assert_series_equal(s, expected)
|
| 20 |
+
|
| 21 |
+
del s[1]
|
| 22 |
+
expected = Series(range(2, 5), index=range(2, 5))
|
| 23 |
+
tm.assert_series_equal(s, expected)
|
| 24 |
+
|
| 25 |
+
# only 1 left, del, add, del
|
| 26 |
+
s = Series(1)
|
| 27 |
+
del s[0]
|
| 28 |
+
tm.assert_series_equal(s, Series(dtype="int64", index=Index([], dtype="int64")))
|
| 29 |
+
s[0] = 1
|
| 30 |
+
tm.assert_series_equal(s, Series(1))
|
| 31 |
+
del s[0]
|
| 32 |
+
tm.assert_series_equal(s, Series(dtype="int64", index=Index([], dtype="int64")))
|
| 33 |
+
|
| 34 |
+
def test_delitem_object_index(self):
|
| 35 |
+
# Index(dtype=object)
|
| 36 |
+
s = Series(1, index=["a"])
|
| 37 |
+
del s["a"]
|
| 38 |
+
tm.assert_series_equal(
|
| 39 |
+
s, Series(dtype="int64", index=Index([], dtype="object"))
|
| 40 |
+
)
|
| 41 |
+
s["a"] = 1
|
| 42 |
+
tm.assert_series_equal(s, Series(1, index=["a"]))
|
| 43 |
+
del s["a"]
|
| 44 |
+
tm.assert_series_equal(
|
| 45 |
+
s, Series(dtype="int64", index=Index([], dtype="object"))
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
def test_delitem_missing_key(self):
|
| 49 |
+
# empty
|
| 50 |
+
s = Series(dtype=object)
|
| 51 |
+
|
| 52 |
+
with pytest.raises(KeyError, match=r"^0$"):
|
| 53 |
+
del s[0]
|
| 54 |
+
|
| 55 |
+
def test_delitem_extension_dtype(self):
|
| 56 |
+
# GH#40386
|
| 57 |
+
# DatetimeTZDtype
|
| 58 |
+
dti = date_range("2016-01-01", periods=3, tz="US/Pacific")
|
| 59 |
+
ser = Series(dti)
|
| 60 |
+
|
| 61 |
+
expected = ser[[0, 2]]
|
| 62 |
+
del ser[1]
|
| 63 |
+
assert ser.dtype == dti.dtype
|
| 64 |
+
tm.assert_series_equal(ser, expected)
|
| 65 |
+
|
| 66 |
+
# PeriodDtype
|
| 67 |
+
pi = dti.tz_localize(None).to_period("D")
|
| 68 |
+
ser = Series(pi)
|
| 69 |
+
|
| 70 |
+
expected = ser[:2]
|
| 71 |
+
del ser[2]
|
| 72 |
+
assert ser.dtype == pi.dtype
|
| 73 |
+
tm.assert_series_equal(ser, expected)
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_get.py
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from pandas import (
|
| 6 |
+
Index,
|
| 7 |
+
Series,
|
| 8 |
+
)
|
| 9 |
+
import pandas._testing as tm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_get():
|
| 13 |
+
# GH 6383
|
| 14 |
+
s = Series(
|
| 15 |
+
np.array(
|
| 16 |
+
[
|
| 17 |
+
43,
|
| 18 |
+
48,
|
| 19 |
+
60,
|
| 20 |
+
48,
|
| 21 |
+
50,
|
| 22 |
+
51,
|
| 23 |
+
50,
|
| 24 |
+
45,
|
| 25 |
+
57,
|
| 26 |
+
48,
|
| 27 |
+
56,
|
| 28 |
+
45,
|
| 29 |
+
51,
|
| 30 |
+
39,
|
| 31 |
+
55,
|
| 32 |
+
43,
|
| 33 |
+
54,
|
| 34 |
+
52,
|
| 35 |
+
51,
|
| 36 |
+
54,
|
| 37 |
+
]
|
| 38 |
+
)
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
result = s.get(25, 0)
|
| 42 |
+
expected = 0
|
| 43 |
+
assert result == expected
|
| 44 |
+
|
| 45 |
+
s = Series(
|
| 46 |
+
np.array(
|
| 47 |
+
[
|
| 48 |
+
43,
|
| 49 |
+
48,
|
| 50 |
+
60,
|
| 51 |
+
48,
|
| 52 |
+
50,
|
| 53 |
+
51,
|
| 54 |
+
50,
|
| 55 |
+
45,
|
| 56 |
+
57,
|
| 57 |
+
48,
|
| 58 |
+
56,
|
| 59 |
+
45,
|
| 60 |
+
51,
|
| 61 |
+
39,
|
| 62 |
+
55,
|
| 63 |
+
43,
|
| 64 |
+
54,
|
| 65 |
+
52,
|
| 66 |
+
51,
|
| 67 |
+
54,
|
| 68 |
+
]
|
| 69 |
+
),
|
| 70 |
+
index=Index(
|
| 71 |
+
[
|
| 72 |
+
25.0,
|
| 73 |
+
36.0,
|
| 74 |
+
49.0,
|
| 75 |
+
64.0,
|
| 76 |
+
81.0,
|
| 77 |
+
100.0,
|
| 78 |
+
121.0,
|
| 79 |
+
144.0,
|
| 80 |
+
169.0,
|
| 81 |
+
196.0,
|
| 82 |
+
1225.0,
|
| 83 |
+
1296.0,
|
| 84 |
+
1369.0,
|
| 85 |
+
1444.0,
|
| 86 |
+
1521.0,
|
| 87 |
+
1600.0,
|
| 88 |
+
1681.0,
|
| 89 |
+
1764.0,
|
| 90 |
+
1849.0,
|
| 91 |
+
1936.0,
|
| 92 |
+
],
|
| 93 |
+
dtype=np.float64,
|
| 94 |
+
),
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
result = s.get(25, 0)
|
| 98 |
+
expected = 43
|
| 99 |
+
assert result == expected
|
| 100 |
+
|
| 101 |
+
# GH 7407
|
| 102 |
+
# with a boolean accessor
|
| 103 |
+
df = pd.DataFrame({"i": [0] * 3, "b": [False] * 3})
|
| 104 |
+
vc = df.i.value_counts()
|
| 105 |
+
result = vc.get(99, default="Missing")
|
| 106 |
+
assert result == "Missing"
|
| 107 |
+
|
| 108 |
+
vc = df.b.value_counts()
|
| 109 |
+
result = vc.get(False, default="Missing")
|
| 110 |
+
assert result == 3
|
| 111 |
+
|
| 112 |
+
result = vc.get(True, default="Missing")
|
| 113 |
+
assert result == "Missing"
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def test_get_nan(float_numpy_dtype):
|
| 117 |
+
# GH 8569
|
| 118 |
+
s = Index(range(10), dtype=float_numpy_dtype).to_series()
|
| 119 |
+
assert s.get(np.nan) is None
|
| 120 |
+
assert s.get(np.nan, default="Missing") == "Missing"
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def test_get_nan_multiple(float_numpy_dtype):
|
| 124 |
+
# GH 8569
|
| 125 |
+
# ensure that fixing "test_get_nan" above hasn't broken get
|
| 126 |
+
# with multiple elements
|
| 127 |
+
s = Index(range(10), dtype=float_numpy_dtype).to_series()
|
| 128 |
+
|
| 129 |
+
idx = [2, 30]
|
| 130 |
+
assert s.get(idx) is None
|
| 131 |
+
|
| 132 |
+
idx = [2, np.nan]
|
| 133 |
+
assert s.get(idx) is None
|
| 134 |
+
|
| 135 |
+
# GH 17295 - all missing keys
|
| 136 |
+
idx = [20, 30]
|
| 137 |
+
assert s.get(idx) is None
|
| 138 |
+
|
| 139 |
+
idx = [np.nan, np.nan]
|
| 140 |
+
assert s.get(idx) is None
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def test_get_with_default():
|
| 144 |
+
# GH#7725
|
| 145 |
+
d0 = ["a", "b", "c", "d"]
|
| 146 |
+
d1 = np.arange(4, dtype="int64")
|
| 147 |
+
others = ["e", 10]
|
| 148 |
+
|
| 149 |
+
for data, index in ((d0, d1), (d1, d0)):
|
| 150 |
+
s = Series(data, index=index)
|
| 151 |
+
for i, d in zip(index, data):
|
| 152 |
+
assert s.get(i) == d
|
| 153 |
+
assert s.get(i, d) == d
|
| 154 |
+
assert s.get(i, "z") == d
|
| 155 |
+
for other in others:
|
| 156 |
+
assert s.get(other, "z") == "z"
|
| 157 |
+
assert s.get(other, other) == other
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
@pytest.mark.parametrize(
|
| 161 |
+
"arr",
|
| 162 |
+
[np.random.randn(10), tm.makeDateIndex(10, name="a").tz_localize(tz="US/Eastern")],
|
| 163 |
+
)
|
| 164 |
+
def test_get_with_ea(arr):
|
| 165 |
+
# GH#21260
|
| 166 |
+
ser = Series(arr, index=[2 * i for i in range(len(arr))])
|
| 167 |
+
assert ser.get(4) == ser.iloc[2]
|
| 168 |
+
|
| 169 |
+
result = ser.get([4, 6])
|
| 170 |
+
expected = ser.iloc[[2, 3]]
|
| 171 |
+
tm.assert_series_equal(result, expected)
|
| 172 |
+
|
| 173 |
+
result = ser.get(slice(2))
|
| 174 |
+
expected = ser.iloc[[0, 1]]
|
| 175 |
+
tm.assert_series_equal(result, expected)
|
| 176 |
+
|
| 177 |
+
assert ser.get(-1) is None
|
| 178 |
+
assert ser.get(ser.index.max() + 1) is None
|
| 179 |
+
|
| 180 |
+
ser = Series(arr[:6], index=list("abcdef"))
|
| 181 |
+
assert ser.get("c") == ser.iloc[2]
|
| 182 |
+
|
| 183 |
+
result = ser.get(slice("b", "d"))
|
| 184 |
+
expected = ser.iloc[[1, 2, 3]]
|
| 185 |
+
tm.assert_series_equal(result, expected)
|
| 186 |
+
|
| 187 |
+
result = ser.get("Z")
|
| 188 |
+
assert result is None
|
| 189 |
+
|
| 190 |
+
assert ser.get(4) == ser.iloc[4]
|
| 191 |
+
assert ser.get(-1) == ser.iloc[-1]
|
| 192 |
+
assert ser.get(len(ser)) is None
|
| 193 |
+
|
| 194 |
+
# GH#21257
|
| 195 |
+
ser = Series(arr)
|
| 196 |
+
ser2 = ser[::2]
|
| 197 |
+
assert ser2.get(1) is None
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
def test_getitem_get(string_series, object_series):
|
| 201 |
+
for obj in [string_series, object_series]:
|
| 202 |
+
idx = obj.index[5]
|
| 203 |
+
|
| 204 |
+
assert obj[idx] == obj.get(idx)
|
| 205 |
+
assert obj[idx] == obj[5]
|
| 206 |
+
|
| 207 |
+
assert string_series.get(-1) == string_series.get(string_series.index[-1])
|
| 208 |
+
assert string_series[5] == string_series.get(string_series.index[5])
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def test_get_none():
|
| 212 |
+
# GH#5652
|
| 213 |
+
s1 = Series(dtype=object)
|
| 214 |
+
s2 = Series(dtype=object, index=list("abc"))
|
| 215 |
+
for s in [s1, s2]:
|
| 216 |
+
result = s.get(None)
|
| 217 |
+
assert result is None
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Series.__getitem__ test classes are organized by the type of key passed.
|
| 3 |
+
"""
|
| 4 |
+
from datetime import (
|
| 5 |
+
date,
|
| 6 |
+
datetime,
|
| 7 |
+
time,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
import pytest
|
| 12 |
+
|
| 13 |
+
from pandas._libs.tslibs import (
|
| 14 |
+
conversion,
|
| 15 |
+
timezones,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
from pandas.core.dtypes.common import is_scalar
|
| 19 |
+
|
| 20 |
+
import pandas as pd
|
| 21 |
+
from pandas import (
|
| 22 |
+
Categorical,
|
| 23 |
+
DataFrame,
|
| 24 |
+
DatetimeIndex,
|
| 25 |
+
Index,
|
| 26 |
+
Series,
|
| 27 |
+
Timestamp,
|
| 28 |
+
date_range,
|
| 29 |
+
period_range,
|
| 30 |
+
timedelta_range,
|
| 31 |
+
)
|
| 32 |
+
import pandas._testing as tm
|
| 33 |
+
from pandas.core.indexing import IndexingError
|
| 34 |
+
|
| 35 |
+
from pandas.tseries.offsets import BDay
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class TestSeriesGetitemScalars:
|
| 39 |
+
def test_getitem_object_index_float_string(self):
|
| 40 |
+
# GH#17286
|
| 41 |
+
ser = Series([1] * 4, index=Index(["a", "b", "c", 1.0]))
|
| 42 |
+
assert ser["a"] == 1
|
| 43 |
+
assert ser[1.0] == 1
|
| 44 |
+
|
| 45 |
+
def test_getitem_float_keys_tuple_values(self):
|
| 46 |
+
# see GH#13509
|
| 47 |
+
|
| 48 |
+
# unique Index
|
| 49 |
+
ser = Series([(1, 1), (2, 2), (3, 3)], index=[0.0, 0.1, 0.2], name="foo")
|
| 50 |
+
result = ser[0.0]
|
| 51 |
+
assert result == (1, 1)
|
| 52 |
+
|
| 53 |
+
# non-unique Index
|
| 54 |
+
expected = Series([(1, 1), (2, 2)], index=[0.0, 0.0], name="foo")
|
| 55 |
+
ser = Series([(1, 1), (2, 2), (3, 3)], index=[0.0, 0.0, 0.2], name="foo")
|
| 56 |
+
|
| 57 |
+
result = ser[0.0]
|
| 58 |
+
tm.assert_series_equal(result, expected)
|
| 59 |
+
|
| 60 |
+
def test_getitem_unrecognized_scalar(self):
|
| 61 |
+
# GH#32684 a scalar key that is not recognized by lib.is_scalar
|
| 62 |
+
|
| 63 |
+
# a series that might be produced via `frame.dtypes`
|
| 64 |
+
ser = Series([1, 2], index=[np.dtype("O"), np.dtype("i8")])
|
| 65 |
+
|
| 66 |
+
key = ser.index[1]
|
| 67 |
+
|
| 68 |
+
result = ser[key]
|
| 69 |
+
assert result == 2
|
| 70 |
+
|
| 71 |
+
def test_getitem_negative_out_of_bounds(self):
|
| 72 |
+
ser = Series(tm.rands_array(5, 10), index=tm.rands_array(10, 10))
|
| 73 |
+
|
| 74 |
+
msg = "index -11 is out of bounds for axis 0 with size 10"
|
| 75 |
+
with pytest.raises(IndexError, match=msg):
|
| 76 |
+
ser[-11]
|
| 77 |
+
|
| 78 |
+
def test_getitem_out_of_bounds_indexerror(self, datetime_series):
|
| 79 |
+
# don't segfault, GH#495
|
| 80 |
+
msg = r"index \d+ is out of bounds for axis 0 with size \d+"
|
| 81 |
+
with pytest.raises(IndexError, match=msg):
|
| 82 |
+
datetime_series[len(datetime_series)]
|
| 83 |
+
|
| 84 |
+
def test_getitem_out_of_bounds_empty_rangeindex_keyerror(self):
|
| 85 |
+
# GH#917
|
| 86 |
+
# With a RangeIndex, an int key gives a KeyError
|
| 87 |
+
ser = Series([], dtype=object)
|
| 88 |
+
with pytest.raises(KeyError, match="-1"):
|
| 89 |
+
ser[-1]
|
| 90 |
+
|
| 91 |
+
def test_getitem_keyerror_with_integer_index(self, any_int_numpy_dtype):
|
| 92 |
+
dtype = any_int_numpy_dtype
|
| 93 |
+
ser = Series(np.random.randn(6), index=Index([0, 0, 1, 1, 2, 2], dtype=dtype))
|
| 94 |
+
|
| 95 |
+
with pytest.raises(KeyError, match=r"^5$"):
|
| 96 |
+
ser[5]
|
| 97 |
+
|
| 98 |
+
with pytest.raises(KeyError, match=r"^'c'$"):
|
| 99 |
+
ser["c"]
|
| 100 |
+
|
| 101 |
+
# not monotonic
|
| 102 |
+
ser = Series(np.random.randn(6), index=[2, 2, 0, 0, 1, 1])
|
| 103 |
+
|
| 104 |
+
with pytest.raises(KeyError, match=r"^5$"):
|
| 105 |
+
ser[5]
|
| 106 |
+
|
| 107 |
+
with pytest.raises(KeyError, match=r"^'c'$"):
|
| 108 |
+
ser["c"]
|
| 109 |
+
|
| 110 |
+
def test_getitem_int64(self, datetime_series):
|
| 111 |
+
idx = np.int64(5)
|
| 112 |
+
assert datetime_series[idx] == datetime_series[5]
|
| 113 |
+
|
| 114 |
+
def test_getitem_full_range(self):
|
| 115 |
+
# github.com/pandas-dev/pandas/commit/4f433773141d2eb384325714a2776bcc5b2e20f7
|
| 116 |
+
ser = Series(range(5), index=list(range(5)))
|
| 117 |
+
result = ser[list(range(5))]
|
| 118 |
+
tm.assert_series_equal(result, ser)
|
| 119 |
+
|
| 120 |
+
# ------------------------------------------------------------------
|
| 121 |
+
# Series with DatetimeIndex
|
| 122 |
+
|
| 123 |
+
@pytest.mark.parametrize("tzstr", ["Europe/Berlin", "dateutil/Europe/Berlin"])
|
| 124 |
+
def test_getitem_pydatetime_tz(self, tzstr):
|
| 125 |
+
tz = timezones.maybe_get_tz(tzstr)
|
| 126 |
+
|
| 127 |
+
index = date_range(
|
| 128 |
+
start="2012-12-24 16:00", end="2012-12-24 18:00", freq="H", tz=tzstr
|
| 129 |
+
)
|
| 130 |
+
ts = Series(index=index, data=index.hour)
|
| 131 |
+
time_pandas = Timestamp("2012-12-24 17:00", tz=tzstr)
|
| 132 |
+
|
| 133 |
+
dt = datetime(2012, 12, 24, 17, 0)
|
| 134 |
+
time_datetime = conversion.localize_pydatetime(dt, tz)
|
| 135 |
+
assert ts[time_pandas] == ts[time_datetime]
|
| 136 |
+
|
| 137 |
+
@pytest.mark.parametrize("tz", ["US/Eastern", "dateutil/US/Eastern"])
|
| 138 |
+
def test_string_index_alias_tz_aware(self, tz):
|
| 139 |
+
rng = date_range("1/1/2000", periods=10, tz=tz)
|
| 140 |
+
ser = Series(np.random.randn(len(rng)), index=rng)
|
| 141 |
+
|
| 142 |
+
result = ser["1/3/2000"]
|
| 143 |
+
tm.assert_almost_equal(result, ser[2])
|
| 144 |
+
|
| 145 |
+
def test_getitem_time_object(self):
|
| 146 |
+
rng = date_range("1/1/2000", "1/5/2000", freq="5min")
|
| 147 |
+
ts = Series(np.random.randn(len(rng)), index=rng)
|
| 148 |
+
|
| 149 |
+
mask = (rng.hour == 9) & (rng.minute == 30)
|
| 150 |
+
result = ts[time(9, 30)]
|
| 151 |
+
expected = ts[mask]
|
| 152 |
+
result.index = result.index._with_freq(None)
|
| 153 |
+
tm.assert_series_equal(result, expected)
|
| 154 |
+
|
| 155 |
+
# ------------------------------------------------------------------
|
| 156 |
+
# Series with CategoricalIndex
|
| 157 |
+
|
| 158 |
+
def test_getitem_scalar_categorical_index(self):
|
| 159 |
+
cats = Categorical([Timestamp("12-31-1999"), Timestamp("12-31-2000")])
|
| 160 |
+
|
| 161 |
+
ser = Series([1, 2], index=cats)
|
| 162 |
+
|
| 163 |
+
expected = ser.iloc[0]
|
| 164 |
+
result = ser[cats[0]]
|
| 165 |
+
assert result == expected
|
| 166 |
+
|
| 167 |
+
def test_getitem_numeric_categorical_listlike_matches_scalar(self):
|
| 168 |
+
# GH#15470
|
| 169 |
+
ser = Series(["a", "b", "c"], index=pd.CategoricalIndex([2, 1, 0]))
|
| 170 |
+
|
| 171 |
+
# 0 is treated as a label
|
| 172 |
+
assert ser[0] == "c"
|
| 173 |
+
|
| 174 |
+
# the listlike analogue should also be treated as labels
|
| 175 |
+
res = ser[[0]]
|
| 176 |
+
expected = ser.iloc[-1:]
|
| 177 |
+
tm.assert_series_equal(res, expected)
|
| 178 |
+
|
| 179 |
+
res2 = ser[[0, 1, 2]]
|
| 180 |
+
tm.assert_series_equal(res2, ser.iloc[::-1])
|
| 181 |
+
|
| 182 |
+
def test_getitem_integer_categorical_not_positional(self):
|
| 183 |
+
# GH#14865
|
| 184 |
+
ser = Series(["a", "b", "c"], index=Index([1, 2, 3], dtype="category"))
|
| 185 |
+
assert ser.get(3) == "c"
|
| 186 |
+
assert ser[3] == "c"
|
| 187 |
+
|
| 188 |
+
def test_getitem_str_with_timedeltaindex(self):
|
| 189 |
+
rng = timedelta_range("1 day 10:11:12", freq="h", periods=500)
|
| 190 |
+
ser = Series(np.arange(len(rng)), index=rng)
|
| 191 |
+
|
| 192 |
+
key = "6 days, 23:11:12"
|
| 193 |
+
indexer = rng.get_loc(key)
|
| 194 |
+
assert indexer == 133
|
| 195 |
+
|
| 196 |
+
result = ser[key]
|
| 197 |
+
assert result == ser.iloc[133]
|
| 198 |
+
|
| 199 |
+
msg = r"^Timedelta\('50 days 00:00:00'\)$"
|
| 200 |
+
with pytest.raises(KeyError, match=msg):
|
| 201 |
+
rng.get_loc("50 days")
|
| 202 |
+
with pytest.raises(KeyError, match=msg):
|
| 203 |
+
ser["50 days"]
|
| 204 |
+
|
| 205 |
+
def test_getitem_bool_index_positional(self):
|
| 206 |
+
# GH#48653
|
| 207 |
+
ser = Series({True: 1, False: 0})
|
| 208 |
+
result = ser[0]
|
| 209 |
+
assert result == 1
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
class TestSeriesGetitemSlices:
|
| 213 |
+
def test_getitem_partial_str_slice_with_datetimeindex(self):
|
| 214 |
+
# GH#34860
|
| 215 |
+
arr = date_range("1/1/2008", "1/1/2009")
|
| 216 |
+
ser = arr.to_series()
|
| 217 |
+
result = ser["2008"]
|
| 218 |
+
|
| 219 |
+
rng = date_range(start="2008-01-01", end="2008-12-31")
|
| 220 |
+
expected = Series(rng, index=rng)
|
| 221 |
+
|
| 222 |
+
tm.assert_series_equal(result, expected)
|
| 223 |
+
|
| 224 |
+
def test_getitem_slice_strings_with_datetimeindex(self):
|
| 225 |
+
idx = DatetimeIndex(
|
| 226 |
+
["1/1/2000", "1/2/2000", "1/2/2000", "1/3/2000", "1/4/2000"]
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
ts = Series(np.random.randn(len(idx)), index=idx)
|
| 230 |
+
|
| 231 |
+
result = ts["1/2/2000":]
|
| 232 |
+
expected = ts[1:]
|
| 233 |
+
tm.assert_series_equal(result, expected)
|
| 234 |
+
|
| 235 |
+
result = ts["1/2/2000":"1/3/2000"]
|
| 236 |
+
expected = ts[1:4]
|
| 237 |
+
tm.assert_series_equal(result, expected)
|
| 238 |
+
|
| 239 |
+
def test_getitem_partial_str_slice_with_timedeltaindex(self):
|
| 240 |
+
rng = timedelta_range("1 day 10:11:12", freq="h", periods=500)
|
| 241 |
+
ser = Series(np.arange(len(rng)), index=rng)
|
| 242 |
+
|
| 243 |
+
result = ser["5 day":"6 day"]
|
| 244 |
+
expected = ser.iloc[86:134]
|
| 245 |
+
tm.assert_series_equal(result, expected)
|
| 246 |
+
|
| 247 |
+
result = ser["5 day":]
|
| 248 |
+
expected = ser.iloc[86:]
|
| 249 |
+
tm.assert_series_equal(result, expected)
|
| 250 |
+
|
| 251 |
+
result = ser[:"6 day"]
|
| 252 |
+
expected = ser.iloc[:134]
|
| 253 |
+
tm.assert_series_equal(result, expected)
|
| 254 |
+
|
| 255 |
+
def test_getitem_partial_str_slice_high_reso_with_timedeltaindex(self):
|
| 256 |
+
# higher reso
|
| 257 |
+
rng = timedelta_range("1 day 10:11:12", freq="us", periods=2000)
|
| 258 |
+
ser = Series(np.arange(len(rng)), index=rng)
|
| 259 |
+
|
| 260 |
+
result = ser["1 day 10:11:12":]
|
| 261 |
+
expected = ser.iloc[0:]
|
| 262 |
+
tm.assert_series_equal(result, expected)
|
| 263 |
+
|
| 264 |
+
result = ser["1 day 10:11:12.001":]
|
| 265 |
+
expected = ser.iloc[1000:]
|
| 266 |
+
tm.assert_series_equal(result, expected)
|
| 267 |
+
|
| 268 |
+
result = ser["1 days, 10:11:12.001001"]
|
| 269 |
+
assert result == ser.iloc[1001]
|
| 270 |
+
|
| 271 |
+
def test_getitem_slice_2d(self, datetime_series):
|
| 272 |
+
# GH#30588 multi-dimensional indexing deprecated
|
| 273 |
+
with pytest.raises(ValueError, match="Multi-dimensional indexing"):
|
| 274 |
+
datetime_series[:, np.newaxis]
|
| 275 |
+
|
| 276 |
+
def test_getitem_median_slice_bug(self):
|
| 277 |
+
index = date_range("20090415", "20090519", freq="2B")
|
| 278 |
+
ser = Series(np.random.randn(13), index=index)
|
| 279 |
+
|
| 280 |
+
indexer = [slice(6, 7, None)]
|
| 281 |
+
msg = "Indexing with a single-item list"
|
| 282 |
+
with pytest.raises(ValueError, match=msg):
|
| 283 |
+
# GH#31299
|
| 284 |
+
ser[indexer]
|
| 285 |
+
# but we're OK with a single-element tuple
|
| 286 |
+
result = ser[(indexer[0],)]
|
| 287 |
+
expected = ser[indexer[0]]
|
| 288 |
+
tm.assert_series_equal(result, expected)
|
| 289 |
+
|
| 290 |
+
@pytest.mark.parametrize(
|
| 291 |
+
"slc, positions",
|
| 292 |
+
[
|
| 293 |
+
[slice(date(2018, 1, 1), None), [0, 1, 2]],
|
| 294 |
+
[slice(date(2019, 1, 2), None), [2]],
|
| 295 |
+
[slice(date(2020, 1, 1), None), []],
|
| 296 |
+
[slice(None, date(2020, 1, 1)), [0, 1, 2]],
|
| 297 |
+
[slice(None, date(2019, 1, 1)), [0]],
|
| 298 |
+
],
|
| 299 |
+
)
|
| 300 |
+
def test_getitem_slice_date(self, slc, positions):
|
| 301 |
+
# https://github.com/pandas-dev/pandas/issues/31501
|
| 302 |
+
ser = Series(
|
| 303 |
+
[0, 1, 2],
|
| 304 |
+
DatetimeIndex(["2019-01-01", "2019-01-01T06:00:00", "2019-01-02"]),
|
| 305 |
+
)
|
| 306 |
+
result = ser[slc]
|
| 307 |
+
expected = ser.take(positions)
|
| 308 |
+
tm.assert_series_equal(result, expected)
|
| 309 |
+
|
| 310 |
+
def test_getitem_slice_float_raises(self, datetime_series):
|
| 311 |
+
msg = (
|
| 312 |
+
"cannot do slice indexing on DatetimeIndex with these indexers "
|
| 313 |
+
r"\[{key}\] of type float"
|
| 314 |
+
)
|
| 315 |
+
with pytest.raises(TypeError, match=msg.format(key=r"4\.0")):
|
| 316 |
+
datetime_series[4.0:10.0]
|
| 317 |
+
|
| 318 |
+
with pytest.raises(TypeError, match=msg.format(key=r"4\.5")):
|
| 319 |
+
datetime_series[4.5:10.0]
|
| 320 |
+
|
| 321 |
+
def test_getitem_slice_bug(self):
|
| 322 |
+
ser = Series(range(10), index=list(range(10)))
|
| 323 |
+
result = ser[-12:]
|
| 324 |
+
tm.assert_series_equal(result, ser)
|
| 325 |
+
|
| 326 |
+
result = ser[-7:]
|
| 327 |
+
tm.assert_series_equal(result, ser[3:])
|
| 328 |
+
|
| 329 |
+
result = ser[:-12]
|
| 330 |
+
tm.assert_series_equal(result, ser[:0])
|
| 331 |
+
|
| 332 |
+
def test_getitem_slice_integers(self):
|
| 333 |
+
ser = Series(np.random.randn(8), index=[2, 4, 6, 8, 10, 12, 14, 16])
|
| 334 |
+
|
| 335 |
+
result = ser[:4]
|
| 336 |
+
expected = Series(ser.values[:4], index=[2, 4, 6, 8])
|
| 337 |
+
tm.assert_series_equal(result, expected)
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
class TestSeriesGetitemListLike:
|
| 341 |
+
@pytest.mark.parametrize("box", [list, np.array, Index, Series])
|
| 342 |
+
def test_getitem_no_matches(self, box):
|
| 343 |
+
# GH#33462 we expect the same behavior for list/ndarray/Index/Series
|
| 344 |
+
ser = Series(["A", "B"])
|
| 345 |
+
|
| 346 |
+
key = Series(["C"], dtype=object)
|
| 347 |
+
key = box(key)
|
| 348 |
+
|
| 349 |
+
msg = r"None of \[Index\(\['C'\], dtype='object'\)\] are in the \[index\]"
|
| 350 |
+
with pytest.raises(KeyError, match=msg):
|
| 351 |
+
ser[key]
|
| 352 |
+
|
| 353 |
+
def test_getitem_intlist_intindex_periodvalues(self):
|
| 354 |
+
ser = Series(period_range("2000-01-01", periods=10, freq="D"))
|
| 355 |
+
|
| 356 |
+
result = ser[[2, 4]]
|
| 357 |
+
exp = Series(
|
| 358 |
+
[pd.Period("2000-01-03", freq="D"), pd.Period("2000-01-05", freq="D")],
|
| 359 |
+
index=[2, 4],
|
| 360 |
+
dtype="Period[D]",
|
| 361 |
+
)
|
| 362 |
+
tm.assert_series_equal(result, exp)
|
| 363 |
+
assert result.dtype == "Period[D]"
|
| 364 |
+
|
| 365 |
+
@pytest.mark.parametrize("box", [list, np.array, Index])
|
| 366 |
+
def test_getitem_intlist_intervalindex_non_int(self, box):
|
| 367 |
+
# GH#33404 fall back to positional since ints are unambiguous
|
| 368 |
+
dti = date_range("2000-01-03", periods=3)._with_freq(None)
|
| 369 |
+
ii = pd.IntervalIndex.from_breaks(dti)
|
| 370 |
+
ser = Series(range(len(ii)), index=ii)
|
| 371 |
+
|
| 372 |
+
expected = ser.iloc[:1]
|
| 373 |
+
key = box([0])
|
| 374 |
+
result = ser[key]
|
| 375 |
+
tm.assert_series_equal(result, expected)
|
| 376 |
+
|
| 377 |
+
@pytest.mark.parametrize("box", [list, np.array, Index])
|
| 378 |
+
@pytest.mark.parametrize("dtype", [np.int64, np.float64, np.uint64])
|
| 379 |
+
def test_getitem_intlist_multiindex_numeric_level(self, dtype, box):
|
| 380 |
+
# GH#33404 do _not_ fall back to positional since ints are ambiguous
|
| 381 |
+
idx = Index(range(4)).astype(dtype)
|
| 382 |
+
dti = date_range("2000-01-03", periods=3)
|
| 383 |
+
mi = pd.MultiIndex.from_product([idx, dti])
|
| 384 |
+
ser = Series(range(len(mi))[::-1], index=mi)
|
| 385 |
+
|
| 386 |
+
key = box([5])
|
| 387 |
+
with pytest.raises(KeyError, match="5"):
|
| 388 |
+
ser[key]
|
| 389 |
+
|
| 390 |
+
def test_getitem_uint_array_key(self, any_unsigned_int_numpy_dtype):
|
| 391 |
+
# GH #37218
|
| 392 |
+
ser = Series([1, 2, 3])
|
| 393 |
+
key = np.array([4], dtype=any_unsigned_int_numpy_dtype)
|
| 394 |
+
|
| 395 |
+
with pytest.raises(KeyError, match="4"):
|
| 396 |
+
ser[key]
|
| 397 |
+
with pytest.raises(KeyError, match="4"):
|
| 398 |
+
ser.loc[key]
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
class TestGetitemBooleanMask:
|
| 402 |
+
def test_getitem_boolean(self, string_series):
|
| 403 |
+
ser = string_series
|
| 404 |
+
mask = ser > ser.median()
|
| 405 |
+
|
| 406 |
+
# passing list is OK
|
| 407 |
+
result = ser[list(mask)]
|
| 408 |
+
expected = ser[mask]
|
| 409 |
+
tm.assert_series_equal(result, expected)
|
| 410 |
+
tm.assert_index_equal(result.index, ser.index[mask])
|
| 411 |
+
|
| 412 |
+
def test_getitem_boolean_empty(self):
|
| 413 |
+
ser = Series([], dtype=np.int64)
|
| 414 |
+
ser.index.name = "index_name"
|
| 415 |
+
ser = ser[ser.isna()]
|
| 416 |
+
assert ser.index.name == "index_name"
|
| 417 |
+
assert ser.dtype == np.int64
|
| 418 |
+
|
| 419 |
+
# GH#5877
|
| 420 |
+
# indexing with empty series
|
| 421 |
+
ser = Series(["A", "B"])
|
| 422 |
+
expected = Series(dtype=object, index=Index([], dtype="int64"))
|
| 423 |
+
result = ser[Series([], dtype=object)]
|
| 424 |
+
tm.assert_series_equal(result, expected)
|
| 425 |
+
|
| 426 |
+
# invalid because of the boolean indexer
|
| 427 |
+
# that's empty or not-aligned
|
| 428 |
+
msg = (
|
| 429 |
+
r"Unalignable boolean Series provided as indexer \(index of "
|
| 430 |
+
r"the boolean Series and of the indexed object do not match"
|
| 431 |
+
)
|
| 432 |
+
with pytest.raises(IndexingError, match=msg):
|
| 433 |
+
ser[Series([], dtype=bool)]
|
| 434 |
+
|
| 435 |
+
with pytest.raises(IndexingError, match=msg):
|
| 436 |
+
ser[Series([True], dtype=bool)]
|
| 437 |
+
|
| 438 |
+
def test_getitem_boolean_object(self, string_series):
|
| 439 |
+
# using column from DataFrame
|
| 440 |
+
|
| 441 |
+
ser = string_series
|
| 442 |
+
mask = ser > ser.median()
|
| 443 |
+
omask = mask.astype(object)
|
| 444 |
+
|
| 445 |
+
# getitem
|
| 446 |
+
result = ser[omask]
|
| 447 |
+
expected = ser[mask]
|
| 448 |
+
tm.assert_series_equal(result, expected)
|
| 449 |
+
|
| 450 |
+
# setitem
|
| 451 |
+
s2 = ser.copy()
|
| 452 |
+
cop = ser.copy()
|
| 453 |
+
cop[omask] = 5
|
| 454 |
+
s2[mask] = 5
|
| 455 |
+
tm.assert_series_equal(cop, s2)
|
| 456 |
+
|
| 457 |
+
# nans raise exception
|
| 458 |
+
omask[5:10] = np.nan
|
| 459 |
+
msg = "Cannot mask with non-boolean array containing NA / NaN values"
|
| 460 |
+
with pytest.raises(ValueError, match=msg):
|
| 461 |
+
ser[omask]
|
| 462 |
+
with pytest.raises(ValueError, match=msg):
|
| 463 |
+
ser[omask] = 5
|
| 464 |
+
|
| 465 |
+
def test_getitem_boolean_dt64_copies(self):
|
| 466 |
+
# GH#36210
|
| 467 |
+
dti = date_range("2016-01-01", periods=4, tz="US/Pacific")
|
| 468 |
+
key = np.array([True, True, False, False])
|
| 469 |
+
|
| 470 |
+
ser = Series(dti._data)
|
| 471 |
+
|
| 472 |
+
res = ser[key]
|
| 473 |
+
assert res._values._ndarray.base is None
|
| 474 |
+
|
| 475 |
+
# compare with numeric case for reference
|
| 476 |
+
ser2 = Series(range(4))
|
| 477 |
+
res2 = ser2[key]
|
| 478 |
+
assert res2._values.base is None
|
| 479 |
+
|
| 480 |
+
def test_getitem_boolean_corner(self, datetime_series):
|
| 481 |
+
ts = datetime_series
|
| 482 |
+
mask_shifted = ts.shift(1, freq=BDay()) > ts.median()
|
| 483 |
+
|
| 484 |
+
msg = (
|
| 485 |
+
r"Unalignable boolean Series provided as indexer \(index of "
|
| 486 |
+
r"the boolean Series and of the indexed object do not match"
|
| 487 |
+
)
|
| 488 |
+
with pytest.raises(IndexingError, match=msg):
|
| 489 |
+
ts[mask_shifted]
|
| 490 |
+
|
| 491 |
+
with pytest.raises(IndexingError, match=msg):
|
| 492 |
+
ts.loc[mask_shifted]
|
| 493 |
+
|
| 494 |
+
def test_getitem_boolean_different_order(self, string_series):
|
| 495 |
+
ordered = string_series.sort_values()
|
| 496 |
+
|
| 497 |
+
sel = string_series[ordered > 0]
|
| 498 |
+
exp = string_series[string_series > 0]
|
| 499 |
+
tm.assert_series_equal(sel, exp)
|
| 500 |
+
|
| 501 |
+
def test_getitem_boolean_contiguous_preserve_freq(self):
|
| 502 |
+
rng = date_range("1/1/2000", "3/1/2000", freq="B")
|
| 503 |
+
|
| 504 |
+
mask = np.zeros(len(rng), dtype=bool)
|
| 505 |
+
mask[10:20] = True
|
| 506 |
+
|
| 507 |
+
masked = rng[mask]
|
| 508 |
+
expected = rng[10:20]
|
| 509 |
+
assert expected.freq == rng.freq
|
| 510 |
+
tm.assert_index_equal(masked, expected)
|
| 511 |
+
|
| 512 |
+
mask[22] = True
|
| 513 |
+
masked = rng[mask]
|
| 514 |
+
assert masked.freq is None
|
| 515 |
+
|
| 516 |
+
|
| 517 |
+
class TestGetitemCallable:
|
| 518 |
+
def test_getitem_callable(self):
|
| 519 |
+
# GH#12533
|
| 520 |
+
ser = Series(4, index=list("ABCD"))
|
| 521 |
+
result = ser[lambda x: "A"]
|
| 522 |
+
assert result == ser.loc["A"]
|
| 523 |
+
|
| 524 |
+
result = ser[lambda x: ["A", "B"]]
|
| 525 |
+
expected = ser.loc[["A", "B"]]
|
| 526 |
+
tm.assert_series_equal(result, expected)
|
| 527 |
+
|
| 528 |
+
result = ser[lambda x: [True, False, True, True]]
|
| 529 |
+
expected = ser.iloc[[0, 2, 3]]
|
| 530 |
+
tm.assert_series_equal(result, expected)
|
| 531 |
+
|
| 532 |
+
|
| 533 |
+
def test_getitem_generator(string_series):
|
| 534 |
+
gen = (x > 0 for x in string_series)
|
| 535 |
+
result = string_series[gen]
|
| 536 |
+
result2 = string_series[iter(string_series > 0)]
|
| 537 |
+
expected = string_series[string_series > 0]
|
| 538 |
+
tm.assert_series_equal(result, expected)
|
| 539 |
+
tm.assert_series_equal(result2, expected)
|
| 540 |
+
|
| 541 |
+
|
| 542 |
+
@pytest.mark.parametrize(
|
| 543 |
+
"series",
|
| 544 |
+
[
|
| 545 |
+
Series([0, 1]),
|
| 546 |
+
Series(date_range("2012-01-01", periods=2)),
|
| 547 |
+
Series(date_range("2012-01-01", periods=2, tz="CET")),
|
| 548 |
+
],
|
| 549 |
+
)
|
| 550 |
+
def test_getitem_ndim_deprecated(series):
|
| 551 |
+
with pytest.raises(ValueError, match="Multi-dimensional indexing"):
|
| 552 |
+
series[:, None]
|
| 553 |
+
|
| 554 |
+
|
| 555 |
+
def test_getitem_multilevel_scalar_slice_not_implemented(
|
| 556 |
+
multiindex_year_month_day_dataframe_random_data,
|
| 557 |
+
):
|
| 558 |
+
# not implementing this for now
|
| 559 |
+
df = multiindex_year_month_day_dataframe_random_data
|
| 560 |
+
ser = df["A"]
|
| 561 |
+
|
| 562 |
+
msg = r"\(2000, slice\(3, 4, None\)\)"
|
| 563 |
+
with pytest.raises(TypeError, match=msg):
|
| 564 |
+
ser[2000, 3:4]
|
| 565 |
+
|
| 566 |
+
|
| 567 |
+
def test_getitem_dataframe_raises():
|
| 568 |
+
rng = list(range(10))
|
| 569 |
+
ser = Series(10, index=rng)
|
| 570 |
+
df = DataFrame(rng, index=rng)
|
| 571 |
+
msg = (
|
| 572 |
+
"Indexing a Series with DataFrame is not supported, "
|
| 573 |
+
"use the appropriate DataFrame column"
|
| 574 |
+
)
|
| 575 |
+
with pytest.raises(TypeError, match=msg):
|
| 576 |
+
ser[df > 5]
|
| 577 |
+
|
| 578 |
+
|
| 579 |
+
def test_getitem_assignment_series_aligment():
|
| 580 |
+
# https://github.com/pandas-dev/pandas/issues/37427
|
| 581 |
+
# with getitem, when assigning with a Series, it is not first aligned
|
| 582 |
+
ser = Series(range(10))
|
| 583 |
+
idx = np.array([2, 4, 9])
|
| 584 |
+
ser[idx] = Series([10, 11, 12])
|
| 585 |
+
expected = Series([0, 1, 10, 3, 11, 5, 6, 7, 8, 12])
|
| 586 |
+
tm.assert_series_equal(ser, expected)
|
| 587 |
+
|
| 588 |
+
|
| 589 |
+
def test_getitem_duplicate_index_mistyped_key_raises_keyerror():
|
| 590 |
+
# GH#29189 float_index.get_loc(None) should raise KeyError, not TypeError
|
| 591 |
+
ser = Series([2, 5, 6, 8], index=[2.0, 4.0, 4.0, 5.0])
|
| 592 |
+
with pytest.raises(KeyError, match="None"):
|
| 593 |
+
ser[None]
|
| 594 |
+
|
| 595 |
+
with pytest.raises(KeyError, match="None"):
|
| 596 |
+
ser.index.get_loc(None)
|
| 597 |
+
|
| 598 |
+
with pytest.raises(KeyError, match="None"):
|
| 599 |
+
ser.index._engine.get_loc(None)
|
| 600 |
+
|
| 601 |
+
|
| 602 |
+
def test_getitem_1tuple_slice_without_multiindex():
|
| 603 |
+
ser = Series(range(5))
|
| 604 |
+
key = (slice(3),)
|
| 605 |
+
|
| 606 |
+
result = ser[key]
|
| 607 |
+
expected = ser[key[0]]
|
| 608 |
+
tm.assert_series_equal(result, expected)
|
| 609 |
+
|
| 610 |
+
|
| 611 |
+
def test_getitem_preserve_name(datetime_series):
|
| 612 |
+
result = datetime_series[datetime_series > 0]
|
| 613 |
+
assert result.name == datetime_series.name
|
| 614 |
+
|
| 615 |
+
result = datetime_series[[0, 2, 4]]
|
| 616 |
+
assert result.name == datetime_series.name
|
| 617 |
+
|
| 618 |
+
result = datetime_series[5:10]
|
| 619 |
+
assert result.name == datetime_series.name
|
| 620 |
+
|
| 621 |
+
|
| 622 |
+
def test_getitem_with_integer_labels():
|
| 623 |
+
# integer indexes, be careful
|
| 624 |
+
ser = Series(np.random.randn(10), index=list(range(0, 20, 2)))
|
| 625 |
+
inds = [0, 2, 5, 7, 8]
|
| 626 |
+
arr_inds = np.array([0, 2, 5, 7, 8])
|
| 627 |
+
with pytest.raises(KeyError, match="not in index"):
|
| 628 |
+
ser[inds]
|
| 629 |
+
|
| 630 |
+
with pytest.raises(KeyError, match="not in index"):
|
| 631 |
+
ser[arr_inds]
|
| 632 |
+
|
| 633 |
+
|
| 634 |
+
def test_getitem_missing(datetime_series):
|
| 635 |
+
# missing
|
| 636 |
+
d = datetime_series.index[0] - BDay()
|
| 637 |
+
msg = r"Timestamp\('1999-12-31 00:00:00'\)"
|
| 638 |
+
with pytest.raises(KeyError, match=msg):
|
| 639 |
+
datetime_series[d]
|
| 640 |
+
|
| 641 |
+
|
| 642 |
+
def test_getitem_fancy(string_series, object_series):
|
| 643 |
+
slice1 = string_series[[1, 2, 3]]
|
| 644 |
+
slice2 = object_series[[1, 2, 3]]
|
| 645 |
+
assert string_series.index[2] == slice1.index[1]
|
| 646 |
+
assert object_series.index[2] == slice2.index[1]
|
| 647 |
+
assert string_series[2] == slice1[1]
|
| 648 |
+
assert object_series[2] == slice2[1]
|
| 649 |
+
|
| 650 |
+
|
| 651 |
+
def test_getitem_box_float64(datetime_series):
|
| 652 |
+
value = datetime_series[5]
|
| 653 |
+
assert isinstance(value, np.float64)
|
| 654 |
+
|
| 655 |
+
|
| 656 |
+
def test_getitem_unordered_dup():
|
| 657 |
+
obj = Series(range(5), index=["c", "a", "a", "b", "b"])
|
| 658 |
+
assert is_scalar(obj["c"])
|
| 659 |
+
assert obj["c"] == 0
|
| 660 |
+
|
| 661 |
+
|
| 662 |
+
def test_getitem_dups():
|
| 663 |
+
ser = Series(range(5), index=["A", "A", "B", "C", "C"], dtype=np.int64)
|
| 664 |
+
expected = Series([3, 4], index=["C", "C"], dtype=np.int64)
|
| 665 |
+
result = ser["C"]
|
| 666 |
+
tm.assert_series_equal(result, expected)
|
| 667 |
+
|
| 668 |
+
|
| 669 |
+
def test_getitem_categorical_str():
|
| 670 |
+
# GH#31765
|
| 671 |
+
ser = Series(range(5), index=Categorical(["a", "b", "c", "a", "b"]))
|
| 672 |
+
result = ser["a"]
|
| 673 |
+
expected = ser.iloc[[0, 3]]
|
| 674 |
+
tm.assert_series_equal(result, expected)
|
| 675 |
+
|
| 676 |
+
|
| 677 |
+
def test_slice_can_reorder_not_uniquely_indexed():
|
| 678 |
+
ser = Series(1, index=["a", "a", "b", "b", "c"])
|
| 679 |
+
ser[::-1] # it works!
|
| 680 |
+
|
| 681 |
+
|
| 682 |
+
@pytest.mark.parametrize("index_vals", ["aabcd", "aadcb"])
|
| 683 |
+
def test_duplicated_index_getitem_positional_indexer(index_vals):
|
| 684 |
+
# GH 11747
|
| 685 |
+
s = Series(range(5), index=list(index_vals))
|
| 686 |
+
result = s[3]
|
| 687 |
+
assert result == 3
|
| 688 |
+
|
| 689 |
+
|
| 690 |
+
class TestGetitemDeprecatedIndexers:
|
| 691 |
+
@pytest.mark.parametrize("key", [{1}, {1: 1}])
|
| 692 |
+
def test_getitem_dict_and_set_deprecated(self, key):
|
| 693 |
+
# GH#42825 enforced in 2.0
|
| 694 |
+
ser = Series([1, 2, 3])
|
| 695 |
+
with pytest.raises(TypeError, match="as an indexer is not supported"):
|
| 696 |
+
ser[key]
|
| 697 |
+
|
| 698 |
+
@pytest.mark.parametrize("key", [{1}, {1: 1}])
|
| 699 |
+
def test_setitem_dict_and_set_disallowed(self, key):
|
| 700 |
+
# GH#42825 enforced in 2.0
|
| 701 |
+
ser = Series([1, 2, 3])
|
| 702 |
+
with pytest.raises(TypeError, match="as an indexer is not supported"):
|
| 703 |
+
ser[key] = 1
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_indexing.py
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" test get/set & misc """
|
| 2 |
+
from datetime import timedelta
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pytest
|
| 7 |
+
|
| 8 |
+
from pandas.errors import IndexingError
|
| 9 |
+
|
| 10 |
+
from pandas import (
|
| 11 |
+
NA,
|
| 12 |
+
DataFrame,
|
| 13 |
+
Index,
|
| 14 |
+
IndexSlice,
|
| 15 |
+
MultiIndex,
|
| 16 |
+
Series,
|
| 17 |
+
Timedelta,
|
| 18 |
+
Timestamp,
|
| 19 |
+
concat,
|
| 20 |
+
date_range,
|
| 21 |
+
period_range,
|
| 22 |
+
timedelta_range,
|
| 23 |
+
)
|
| 24 |
+
import pandas._testing as tm
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_basic_indexing():
|
| 28 |
+
s = Series(np.random.randn(5), index=["a", "b", "a", "a", "b"])
|
| 29 |
+
|
| 30 |
+
msg = "index 5 is out of bounds for axis 0 with size 5"
|
| 31 |
+
with pytest.raises(IndexError, match=msg):
|
| 32 |
+
s[5]
|
| 33 |
+
with pytest.raises(IndexError, match=msg):
|
| 34 |
+
s[5] = 0
|
| 35 |
+
|
| 36 |
+
with pytest.raises(KeyError, match=r"^'c'$"):
|
| 37 |
+
s["c"]
|
| 38 |
+
|
| 39 |
+
s = s.sort_index()
|
| 40 |
+
|
| 41 |
+
with pytest.raises(IndexError, match=msg):
|
| 42 |
+
s[5]
|
| 43 |
+
msg = r"index 5 is out of bounds for axis (0|1) with size 5|^5$"
|
| 44 |
+
with pytest.raises(IndexError, match=msg):
|
| 45 |
+
s[5] = 0
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def test_getitem_numeric_should_not_fallback_to_positional(any_numeric_dtype):
|
| 49 |
+
# GH51053
|
| 50 |
+
dtype = any_numeric_dtype
|
| 51 |
+
idx = Index([1, 0, 1], dtype=dtype)
|
| 52 |
+
ser = Series(range(3), index=idx)
|
| 53 |
+
result = ser[1]
|
| 54 |
+
expected = Series([0, 2], index=Index([1, 1], dtype=dtype))
|
| 55 |
+
tm.assert_series_equal(result, expected, check_exact=True)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def test_setitem_numeric_should_not_fallback_to_positional(any_numeric_dtype):
|
| 59 |
+
# GH51053
|
| 60 |
+
dtype = any_numeric_dtype
|
| 61 |
+
idx = Index([1, 0, 1], dtype=dtype)
|
| 62 |
+
ser = Series(range(3), index=idx)
|
| 63 |
+
ser[1] = 10
|
| 64 |
+
expected = Series([10, 1, 10], index=idx)
|
| 65 |
+
tm.assert_series_equal(ser, expected, check_exact=True)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_basic_getitem_with_labels(datetime_series):
|
| 69 |
+
indices = datetime_series.index[[5, 10, 15]]
|
| 70 |
+
|
| 71 |
+
result = datetime_series[indices]
|
| 72 |
+
expected = datetime_series.reindex(indices)
|
| 73 |
+
tm.assert_series_equal(result, expected)
|
| 74 |
+
|
| 75 |
+
result = datetime_series[indices[0] : indices[2]]
|
| 76 |
+
expected = datetime_series.loc[indices[0] : indices[2]]
|
| 77 |
+
tm.assert_series_equal(result, expected)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def test_basic_getitem_dt64tz_values():
|
| 81 |
+
# GH12089
|
| 82 |
+
# with tz for values
|
| 83 |
+
ser = Series(
|
| 84 |
+
date_range("2011-01-01", periods=3, tz="US/Eastern"), index=["a", "b", "c"]
|
| 85 |
+
)
|
| 86 |
+
expected = Timestamp("2011-01-01", tz="US/Eastern")
|
| 87 |
+
result = ser.loc["a"]
|
| 88 |
+
assert result == expected
|
| 89 |
+
result = ser.iloc[0]
|
| 90 |
+
assert result == expected
|
| 91 |
+
result = ser["a"]
|
| 92 |
+
assert result == expected
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def test_getitem_setitem_ellipsis():
|
| 96 |
+
s = Series(np.random.randn(10))
|
| 97 |
+
|
| 98 |
+
result = s[...]
|
| 99 |
+
tm.assert_series_equal(result, s)
|
| 100 |
+
|
| 101 |
+
s[...] = 5
|
| 102 |
+
assert (result == 5).all()
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
@pytest.mark.parametrize(
|
| 106 |
+
"result_1, duplicate_item, expected_1",
|
| 107 |
+
[
|
| 108 |
+
[
|
| 109 |
+
Series({1: 12, 2: [1, 2, 2, 3]}),
|
| 110 |
+
Series({1: 313}),
|
| 111 |
+
Series({1: 12}, dtype=object),
|
| 112 |
+
],
|
| 113 |
+
[
|
| 114 |
+
Series({1: [1, 2, 3], 2: [1, 2, 2, 3]}),
|
| 115 |
+
Series({1: [1, 2, 3]}),
|
| 116 |
+
Series({1: [1, 2, 3]}),
|
| 117 |
+
],
|
| 118 |
+
],
|
| 119 |
+
)
|
| 120 |
+
def test_getitem_with_duplicates_indices(result_1, duplicate_item, expected_1):
|
| 121 |
+
# GH 17610
|
| 122 |
+
result = result_1._append(duplicate_item)
|
| 123 |
+
expected = expected_1._append(duplicate_item)
|
| 124 |
+
tm.assert_series_equal(result[1], expected)
|
| 125 |
+
assert result[2] == result_1[2]
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def test_getitem_setitem_integers():
|
| 129 |
+
# caused bug without test
|
| 130 |
+
s = Series([1, 2, 3], ["a", "b", "c"])
|
| 131 |
+
|
| 132 |
+
assert s.iloc[0] == s["a"]
|
| 133 |
+
s.iloc[0] = 5
|
| 134 |
+
tm.assert_almost_equal(s["a"], 5)
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def test_series_box_timestamp():
|
| 138 |
+
rng = date_range("20090415", "20090519", freq="B")
|
| 139 |
+
ser = Series(rng)
|
| 140 |
+
assert isinstance(ser[0], Timestamp)
|
| 141 |
+
assert isinstance(ser.at[1], Timestamp)
|
| 142 |
+
assert isinstance(ser.iat[2], Timestamp)
|
| 143 |
+
assert isinstance(ser.loc[3], Timestamp)
|
| 144 |
+
assert isinstance(ser.iloc[4], Timestamp)
|
| 145 |
+
|
| 146 |
+
ser = Series(rng, index=rng)
|
| 147 |
+
assert isinstance(ser[0], Timestamp)
|
| 148 |
+
assert isinstance(ser.at[rng[1]], Timestamp)
|
| 149 |
+
assert isinstance(ser.iat[2], Timestamp)
|
| 150 |
+
assert isinstance(ser.loc[rng[3]], Timestamp)
|
| 151 |
+
assert isinstance(ser.iloc[4], Timestamp)
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def test_series_box_timedelta():
|
| 155 |
+
rng = timedelta_range("1 day 1 s", periods=5, freq="h")
|
| 156 |
+
ser = Series(rng)
|
| 157 |
+
assert isinstance(ser[0], Timedelta)
|
| 158 |
+
assert isinstance(ser.at[1], Timedelta)
|
| 159 |
+
assert isinstance(ser.iat[2], Timedelta)
|
| 160 |
+
assert isinstance(ser.loc[3], Timedelta)
|
| 161 |
+
assert isinstance(ser.iloc[4], Timedelta)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def test_getitem_ambiguous_keyerror(indexer_sl):
|
| 165 |
+
ser = Series(range(10), index=list(range(0, 20, 2)))
|
| 166 |
+
with pytest.raises(KeyError, match=r"^1$"):
|
| 167 |
+
indexer_sl(ser)[1]
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def test_getitem_dups_with_missing(indexer_sl):
|
| 171 |
+
# breaks reindex, so need to use .loc internally
|
| 172 |
+
# GH 4246
|
| 173 |
+
ser = Series([1, 2, 3, 4], ["foo", "bar", "foo", "bah"])
|
| 174 |
+
with pytest.raises(KeyError, match=re.escape("['bam'] not in index")):
|
| 175 |
+
indexer_sl(ser)[["foo", "bar", "bah", "bam"]]
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def test_setitem_ambiguous_keyerror(indexer_sl):
|
| 179 |
+
s = Series(range(10), index=list(range(0, 20, 2)))
|
| 180 |
+
|
| 181 |
+
# equivalent of an append
|
| 182 |
+
s2 = s.copy()
|
| 183 |
+
indexer_sl(s2)[1] = 5
|
| 184 |
+
expected = concat([s, Series([5], index=[1])])
|
| 185 |
+
tm.assert_series_equal(s2, expected)
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def test_setitem(datetime_series):
|
| 189 |
+
datetime_series[datetime_series.index[5]] = np.NaN
|
| 190 |
+
datetime_series[[1, 2, 17]] = np.NaN
|
| 191 |
+
datetime_series[6] = np.NaN
|
| 192 |
+
assert np.isnan(datetime_series[6])
|
| 193 |
+
assert np.isnan(datetime_series[2])
|
| 194 |
+
datetime_series[np.isnan(datetime_series)] = 5
|
| 195 |
+
assert not np.isnan(datetime_series[2])
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
def test_setslice(datetime_series):
|
| 199 |
+
sl = datetime_series[5:20]
|
| 200 |
+
assert len(sl) == len(sl.index)
|
| 201 |
+
assert sl.index.is_unique is True
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def test_basic_getitem_setitem_corner(datetime_series):
|
| 205 |
+
# invalid tuples, e.g. td.ts[:, None] vs. td.ts[:, 2]
|
| 206 |
+
msg = "key of type tuple not found and not a MultiIndex"
|
| 207 |
+
with pytest.raises(KeyError, match=msg):
|
| 208 |
+
datetime_series[:, 2]
|
| 209 |
+
with pytest.raises(KeyError, match=msg):
|
| 210 |
+
datetime_series[:, 2] = 2
|
| 211 |
+
|
| 212 |
+
# weird lists. [slice(0, 5)] raises but not two slices
|
| 213 |
+
msg = "Indexing with a single-item list"
|
| 214 |
+
with pytest.raises(ValueError, match=msg):
|
| 215 |
+
# GH#31299
|
| 216 |
+
datetime_series[[slice(None, 5)]]
|
| 217 |
+
|
| 218 |
+
# but we're OK with a single-element tuple
|
| 219 |
+
result = datetime_series[(slice(None, 5),)]
|
| 220 |
+
expected = datetime_series[:5]
|
| 221 |
+
tm.assert_series_equal(result, expected)
|
| 222 |
+
|
| 223 |
+
# OK
|
| 224 |
+
msg = r"unhashable type(: 'slice')?"
|
| 225 |
+
with pytest.raises(TypeError, match=msg):
|
| 226 |
+
datetime_series[[5, slice(None, None)]]
|
| 227 |
+
with pytest.raises(TypeError, match=msg):
|
| 228 |
+
datetime_series[[5, slice(None, None)]] = 2
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
def test_slice(string_series, object_series, using_copy_on_write):
|
| 232 |
+
original = string_series.copy()
|
| 233 |
+
numSlice = string_series[10:20]
|
| 234 |
+
numSliceEnd = string_series[-10:]
|
| 235 |
+
objSlice = object_series[10:20]
|
| 236 |
+
|
| 237 |
+
assert string_series.index[9] not in numSlice.index
|
| 238 |
+
assert object_series.index[9] not in objSlice.index
|
| 239 |
+
|
| 240 |
+
assert len(numSlice) == len(numSlice.index)
|
| 241 |
+
assert string_series[numSlice.index[0]] == numSlice[numSlice.index[0]]
|
| 242 |
+
|
| 243 |
+
assert numSlice.index[1] == string_series.index[11]
|
| 244 |
+
assert tm.equalContents(numSliceEnd, np.array(string_series)[-10:])
|
| 245 |
+
|
| 246 |
+
# Test return view.
|
| 247 |
+
sl = string_series[10:20]
|
| 248 |
+
sl[:] = 0
|
| 249 |
+
|
| 250 |
+
if using_copy_on_write:
|
| 251 |
+
# Doesn't modify parent (CoW)
|
| 252 |
+
tm.assert_series_equal(string_series, original)
|
| 253 |
+
else:
|
| 254 |
+
assert (string_series[10:20] == 0).all()
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
def test_timedelta_assignment():
|
| 258 |
+
# GH 8209
|
| 259 |
+
s = Series([], dtype=object)
|
| 260 |
+
s.loc["B"] = timedelta(1)
|
| 261 |
+
tm.assert_series_equal(s, Series(Timedelta("1 days"), index=["B"]))
|
| 262 |
+
|
| 263 |
+
s = s.reindex(s.index.insert(0, "A"))
|
| 264 |
+
tm.assert_series_equal(s, Series([np.nan, Timedelta("1 days")], index=["A", "B"]))
|
| 265 |
+
|
| 266 |
+
s.loc["A"] = timedelta(1)
|
| 267 |
+
expected = Series(Timedelta("1 days"), index=["A", "B"])
|
| 268 |
+
tm.assert_series_equal(s, expected)
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
def test_underlying_data_conversion(using_copy_on_write):
|
| 272 |
+
# GH 4080
|
| 273 |
+
df = DataFrame({c: [1, 2, 3] for c in ["a", "b", "c"]})
|
| 274 |
+
return_value = df.set_index(["a", "b", "c"], inplace=True)
|
| 275 |
+
assert return_value is None
|
| 276 |
+
s = Series([1], index=[(2, 2, 2)])
|
| 277 |
+
df["val"] = 0
|
| 278 |
+
df_original = df.copy()
|
| 279 |
+
df
|
| 280 |
+
df["val"].update(s)
|
| 281 |
+
|
| 282 |
+
if using_copy_on_write:
|
| 283 |
+
expected = df_original
|
| 284 |
+
else:
|
| 285 |
+
expected = DataFrame(
|
| 286 |
+
{"a": [1, 2, 3], "b": [1, 2, 3], "c": [1, 2, 3], "val": [0, 1, 0]}
|
| 287 |
+
)
|
| 288 |
+
return_value = expected.set_index(["a", "b", "c"], inplace=True)
|
| 289 |
+
assert return_value is None
|
| 290 |
+
tm.assert_frame_equal(df, expected)
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def test_preserve_refs(datetime_series):
|
| 294 |
+
seq = datetime_series[[5, 10, 15]]
|
| 295 |
+
seq[1] = np.NaN
|
| 296 |
+
assert not np.isnan(datetime_series[10])
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
def test_multilevel_preserve_name(lexsorted_two_level_string_multiindex, indexer_sl):
|
| 300 |
+
index = lexsorted_two_level_string_multiindex
|
| 301 |
+
ser = Series(np.random.randn(len(index)), index=index, name="sth")
|
| 302 |
+
|
| 303 |
+
result = indexer_sl(ser)["foo"]
|
| 304 |
+
assert result.name == ser.name
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
# miscellaneous methods
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
@pytest.mark.parametrize(
|
| 311 |
+
"index",
|
| 312 |
+
[
|
| 313 |
+
date_range("2014-01-01", periods=20, freq="MS"),
|
| 314 |
+
period_range("2014-01", periods=20, freq="M"),
|
| 315 |
+
timedelta_range("0", periods=20, freq="H"),
|
| 316 |
+
],
|
| 317 |
+
)
|
| 318 |
+
def test_slice_with_negative_step(index):
|
| 319 |
+
keystr1 = str(index[9])
|
| 320 |
+
keystr2 = str(index[13])
|
| 321 |
+
|
| 322 |
+
ser = Series(np.arange(20), index)
|
| 323 |
+
SLC = IndexSlice
|
| 324 |
+
|
| 325 |
+
for key in [keystr1, index[9]]:
|
| 326 |
+
tm.assert_indexing_slices_equivalent(ser, SLC[key::-1], SLC[9::-1])
|
| 327 |
+
tm.assert_indexing_slices_equivalent(ser, SLC[:key:-1], SLC[:8:-1])
|
| 328 |
+
|
| 329 |
+
for key2 in [keystr2, index[13]]:
|
| 330 |
+
tm.assert_indexing_slices_equivalent(ser, SLC[key2:key:-1], SLC[13:8:-1])
|
| 331 |
+
tm.assert_indexing_slices_equivalent(ser, SLC[key:key2:-1], SLC[0:0:-1])
|
| 332 |
+
|
| 333 |
+
|
| 334 |
+
def test_tuple_index():
|
| 335 |
+
# GH 35534 - Selecting values when a Series has an Index of tuples
|
| 336 |
+
s = Series([1, 2], index=[("a",), ("b",)])
|
| 337 |
+
assert s[("a",)] == 1
|
| 338 |
+
assert s[("b",)] == 2
|
| 339 |
+
s[("b",)] = 3
|
| 340 |
+
assert s[("b",)] == 3
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
def test_frozenset_index():
|
| 344 |
+
# GH35747 - Selecting values when a Series has an Index of frozenset
|
| 345 |
+
idx0, idx1 = frozenset("a"), frozenset("b")
|
| 346 |
+
s = Series([1, 2], index=[idx0, idx1])
|
| 347 |
+
assert s[idx0] == 1
|
| 348 |
+
assert s[idx1] == 2
|
| 349 |
+
s[idx1] = 3
|
| 350 |
+
assert s[idx1] == 3
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
def test_loc_setitem_all_false_indexer():
|
| 354 |
+
# GH#45778
|
| 355 |
+
ser = Series([1, 2], index=["a", "b"])
|
| 356 |
+
expected = ser.copy()
|
| 357 |
+
rhs = Series([6, 7], index=["a", "b"])
|
| 358 |
+
ser.loc[ser > 100] = rhs
|
| 359 |
+
tm.assert_series_equal(ser, expected)
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
def test_loc_boolean_indexer_non_matching_index():
|
| 363 |
+
# GH#46551
|
| 364 |
+
ser = Series([1])
|
| 365 |
+
result = ser.loc[Series([NA, False], dtype="boolean")]
|
| 366 |
+
expected = Series([], dtype="int64")
|
| 367 |
+
tm.assert_series_equal(result, expected)
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
def test_loc_boolean_indexer_miss_matching_index():
|
| 371 |
+
# GH#46551
|
| 372 |
+
ser = Series([1])
|
| 373 |
+
indexer = Series([NA, False], dtype="boolean", index=[1, 2])
|
| 374 |
+
with pytest.raises(IndexingError, match="Unalignable"):
|
| 375 |
+
ser.loc[indexer]
|
| 376 |
+
|
| 377 |
+
|
| 378 |
+
def test_loc_setitem_nested_data_enlargement():
|
| 379 |
+
# GH#48614
|
| 380 |
+
df = DataFrame({"a": [1]})
|
| 381 |
+
ser = Series({"label": df})
|
| 382 |
+
ser.loc["new_label"] = df
|
| 383 |
+
expected = Series({"label": df, "new_label": df})
|
| 384 |
+
tm.assert_series_equal(ser, expected)
|
| 385 |
+
|
| 386 |
+
|
| 387 |
+
def test_loc_ea_numeric_index_oob_slice_end():
|
| 388 |
+
# GH#50161
|
| 389 |
+
ser = Series(1, index=Index([0, 1, 2], dtype="Int64"))
|
| 390 |
+
result = ser.loc[2:3]
|
| 391 |
+
expected = Series(1, index=Index([2], dtype="Int64"))
|
| 392 |
+
tm.assert_series_equal(result, expected)
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
def test_getitem_bool_int_key():
|
| 396 |
+
# GH#48653
|
| 397 |
+
ser = Series({True: 1, False: 0})
|
| 398 |
+
with pytest.raises(KeyError, match="0"):
|
| 399 |
+
ser.loc[0]
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
@pytest.mark.parametrize("val", [{}, {"b": "x"}])
|
| 403 |
+
@pytest.mark.parametrize("indexer", [[], [False, False], slice(0, -1), np.array([])])
|
| 404 |
+
def test_setitem_empty_indexer(indexer, val):
|
| 405 |
+
# GH#45981
|
| 406 |
+
df = DataFrame({"a": [1, 2], **val})
|
| 407 |
+
expected = df.copy()
|
| 408 |
+
df.loc[indexer] = 1.5
|
| 409 |
+
tm.assert_frame_equal(df, expected)
|
| 410 |
+
|
| 411 |
+
|
| 412 |
+
class TestDeprecatedIndexers:
|
| 413 |
+
@pytest.mark.parametrize("key", [{1}, {1: 1}])
|
| 414 |
+
def test_getitem_dict_and_set_deprecated(self, key):
|
| 415 |
+
# GH#42825 enforced in 2.0
|
| 416 |
+
ser = Series([1, 2])
|
| 417 |
+
with pytest.raises(TypeError, match="as an indexer is not supported"):
|
| 418 |
+
ser.loc[key]
|
| 419 |
+
|
| 420 |
+
@pytest.mark.parametrize("key", [{1}, {1: 1}, ({1}, 2), ({1: 1}, 2)])
|
| 421 |
+
def test_getitem_dict_and_set_deprecated_multiindex(self, key):
|
| 422 |
+
# GH#42825 enforced in 2.0
|
| 423 |
+
ser = Series([1, 2], index=MultiIndex.from_tuples([(1, 2), (3, 4)]))
|
| 424 |
+
with pytest.raises(TypeError, match="as an indexer is not supported"):
|
| 425 |
+
ser.loc[key]
|
| 426 |
+
|
| 427 |
+
@pytest.mark.parametrize("key", [{1}, {1: 1}])
|
| 428 |
+
def test_setitem_dict_and_set_disallowed(self, key):
|
| 429 |
+
# GH#42825 enforced in 2.0
|
| 430 |
+
ser = Series([1, 2])
|
| 431 |
+
with pytest.raises(TypeError, match="as an indexer is not supported"):
|
| 432 |
+
ser.loc[key] = 1
|
| 433 |
+
|
| 434 |
+
@pytest.mark.parametrize("key", [{1}, {1: 1}, ({1}, 2), ({1: 1}, 2)])
|
| 435 |
+
def test_setitem_dict_and_set_disallowed_multiindex(self, key):
|
| 436 |
+
# GH#42825 enforced in 2.0
|
| 437 |
+
ser = Series([1, 2], index=MultiIndex.from_tuples([(1, 2), (3, 4)]))
|
| 438 |
+
with pytest.raises(TypeError, match="as an indexer is not supported"):
|
| 439 |
+
ser.loc[key] = 1
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_mask.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import Series
|
| 5 |
+
import pandas._testing as tm
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_mask():
|
| 9 |
+
# compare with tested results in test_where
|
| 10 |
+
s = Series(np.random.randn(5))
|
| 11 |
+
cond = s > 0
|
| 12 |
+
|
| 13 |
+
rs = s.where(~cond, np.nan)
|
| 14 |
+
tm.assert_series_equal(rs, s.mask(cond))
|
| 15 |
+
|
| 16 |
+
rs = s.where(~cond)
|
| 17 |
+
rs2 = s.mask(cond)
|
| 18 |
+
tm.assert_series_equal(rs, rs2)
|
| 19 |
+
|
| 20 |
+
rs = s.where(~cond, -s)
|
| 21 |
+
rs2 = s.mask(cond, -s)
|
| 22 |
+
tm.assert_series_equal(rs, rs2)
|
| 23 |
+
|
| 24 |
+
cond = Series([True, False, False, True, False], index=s.index)
|
| 25 |
+
s2 = -(s.abs())
|
| 26 |
+
rs = s2.where(~cond[:3])
|
| 27 |
+
rs2 = s2.mask(cond[:3])
|
| 28 |
+
tm.assert_series_equal(rs, rs2)
|
| 29 |
+
|
| 30 |
+
rs = s2.where(~cond[:3], -s2)
|
| 31 |
+
rs2 = s2.mask(cond[:3], -s2)
|
| 32 |
+
tm.assert_series_equal(rs, rs2)
|
| 33 |
+
|
| 34 |
+
msg = "Array conditional must be same shape as self"
|
| 35 |
+
with pytest.raises(ValueError, match=msg):
|
| 36 |
+
s.mask(1)
|
| 37 |
+
with pytest.raises(ValueError, match=msg):
|
| 38 |
+
s.mask(cond[:3].values, -s)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def test_mask_casts():
|
| 42 |
+
# dtype changes
|
| 43 |
+
ser = Series([1, 2, 3, 4])
|
| 44 |
+
result = ser.mask(ser > 2, np.nan)
|
| 45 |
+
expected = Series([1, 2, np.nan, np.nan])
|
| 46 |
+
tm.assert_series_equal(result, expected)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_mask_casts2():
|
| 50 |
+
# see gh-21891
|
| 51 |
+
ser = Series([1, 2])
|
| 52 |
+
res = ser.mask([True, False])
|
| 53 |
+
|
| 54 |
+
exp = Series([np.nan, 2])
|
| 55 |
+
tm.assert_series_equal(res, exp)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def test_mask_inplace():
|
| 59 |
+
s = Series(np.random.randn(5))
|
| 60 |
+
cond = s > 0
|
| 61 |
+
|
| 62 |
+
rs = s.copy()
|
| 63 |
+
rs.mask(cond, inplace=True)
|
| 64 |
+
tm.assert_series_equal(rs.dropna(), s[~cond])
|
| 65 |
+
tm.assert_series_equal(rs, s.mask(cond))
|
| 66 |
+
|
| 67 |
+
rs = s.copy()
|
| 68 |
+
rs.mask(cond, -s, inplace=True)
|
| 69 |
+
tm.assert_series_equal(rs, s.mask(cond, -s))
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_setitem.py
ADDED
|
@@ -0,0 +1,1642 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import (
|
| 2 |
+
date,
|
| 3 |
+
datetime,
|
| 4 |
+
)
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pytest
|
| 8 |
+
|
| 9 |
+
from pandas.errors import IndexingError
|
| 10 |
+
|
| 11 |
+
from pandas.core.dtypes.common import is_list_like
|
| 12 |
+
|
| 13 |
+
from pandas import (
|
| 14 |
+
NA,
|
| 15 |
+
Categorical,
|
| 16 |
+
DataFrame,
|
| 17 |
+
DatetimeIndex,
|
| 18 |
+
Index,
|
| 19 |
+
Interval,
|
| 20 |
+
IntervalIndex,
|
| 21 |
+
MultiIndex,
|
| 22 |
+
NaT,
|
| 23 |
+
Period,
|
| 24 |
+
Series,
|
| 25 |
+
Timedelta,
|
| 26 |
+
Timestamp,
|
| 27 |
+
array,
|
| 28 |
+
concat,
|
| 29 |
+
date_range,
|
| 30 |
+
interval_range,
|
| 31 |
+
period_range,
|
| 32 |
+
timedelta_range,
|
| 33 |
+
)
|
| 34 |
+
import pandas._testing as tm
|
| 35 |
+
|
| 36 |
+
from pandas.tseries.offsets import BDay
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class TestSetitemDT64Values:
|
| 40 |
+
def test_setitem_none_nan(self):
|
| 41 |
+
series = Series(date_range("1/1/2000", periods=10))
|
| 42 |
+
series[3] = None
|
| 43 |
+
assert series[3] is NaT
|
| 44 |
+
|
| 45 |
+
series[3:5] = None
|
| 46 |
+
assert series[4] is NaT
|
| 47 |
+
|
| 48 |
+
series[5] = np.nan
|
| 49 |
+
assert series[5] is NaT
|
| 50 |
+
|
| 51 |
+
series[5:7] = np.nan
|
| 52 |
+
assert series[6] is NaT
|
| 53 |
+
|
| 54 |
+
def test_setitem_multiindex_empty_slice(self):
|
| 55 |
+
# https://github.com/pandas-dev/pandas/issues/35878
|
| 56 |
+
idx = MultiIndex.from_tuples([("a", 1), ("b", 2)])
|
| 57 |
+
result = Series([1, 2], index=idx)
|
| 58 |
+
expected = result.copy()
|
| 59 |
+
result.loc[[]] = 0
|
| 60 |
+
tm.assert_series_equal(result, expected)
|
| 61 |
+
|
| 62 |
+
def test_setitem_with_string_index(self):
|
| 63 |
+
# GH#23451
|
| 64 |
+
ser = Series([1, 2, 3], index=["Date", "b", "other"])
|
| 65 |
+
ser["Date"] = date.today()
|
| 66 |
+
assert ser.Date == date.today()
|
| 67 |
+
assert ser["Date"] == date.today()
|
| 68 |
+
|
| 69 |
+
def test_setitem_tuple_with_datetimetz_values(self):
|
| 70 |
+
# GH#20441
|
| 71 |
+
arr = date_range("2017", periods=4, tz="US/Eastern")
|
| 72 |
+
index = [(0, 1), (0, 2), (0, 3), (0, 4)]
|
| 73 |
+
result = Series(arr, index=index)
|
| 74 |
+
expected = result.copy()
|
| 75 |
+
result[(0, 1)] = np.nan
|
| 76 |
+
expected.iloc[0] = np.nan
|
| 77 |
+
tm.assert_series_equal(result, expected)
|
| 78 |
+
|
| 79 |
+
@pytest.mark.parametrize("tz", ["US/Eastern", "UTC", "Asia/Tokyo"])
|
| 80 |
+
def test_setitem_with_tz(self, tz, indexer_sli):
|
| 81 |
+
orig = Series(date_range("2016-01-01", freq="H", periods=3, tz=tz))
|
| 82 |
+
assert orig.dtype == f"datetime64[ns, {tz}]"
|
| 83 |
+
|
| 84 |
+
exp = Series(
|
| 85 |
+
[
|
| 86 |
+
Timestamp("2016-01-01 00:00", tz=tz),
|
| 87 |
+
Timestamp("2011-01-01 00:00", tz=tz),
|
| 88 |
+
Timestamp("2016-01-01 02:00", tz=tz),
|
| 89 |
+
]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# scalar
|
| 93 |
+
ser = orig.copy()
|
| 94 |
+
indexer_sli(ser)[1] = Timestamp("2011-01-01", tz=tz)
|
| 95 |
+
tm.assert_series_equal(ser, exp)
|
| 96 |
+
|
| 97 |
+
# vector
|
| 98 |
+
vals = Series(
|
| 99 |
+
[Timestamp("2011-01-01", tz=tz), Timestamp("2012-01-01", tz=tz)],
|
| 100 |
+
index=[1, 2],
|
| 101 |
+
)
|
| 102 |
+
assert vals.dtype == f"datetime64[ns, {tz}]"
|
| 103 |
+
|
| 104 |
+
exp = Series(
|
| 105 |
+
[
|
| 106 |
+
Timestamp("2016-01-01 00:00", tz=tz),
|
| 107 |
+
Timestamp("2011-01-01 00:00", tz=tz),
|
| 108 |
+
Timestamp("2012-01-01 00:00", tz=tz),
|
| 109 |
+
]
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
ser = orig.copy()
|
| 113 |
+
indexer_sli(ser)[[1, 2]] = vals
|
| 114 |
+
tm.assert_series_equal(ser, exp)
|
| 115 |
+
|
| 116 |
+
def test_setitem_with_tz_dst(self, indexer_sli):
|
| 117 |
+
# GH#14146 trouble setting values near DST boundary
|
| 118 |
+
tz = "US/Eastern"
|
| 119 |
+
orig = Series(date_range("2016-11-06", freq="H", periods=3, tz=tz))
|
| 120 |
+
assert orig.dtype == f"datetime64[ns, {tz}]"
|
| 121 |
+
|
| 122 |
+
exp = Series(
|
| 123 |
+
[
|
| 124 |
+
Timestamp("2016-11-06 00:00-04:00", tz=tz),
|
| 125 |
+
Timestamp("2011-01-01 00:00-05:00", tz=tz),
|
| 126 |
+
Timestamp("2016-11-06 01:00-05:00", tz=tz),
|
| 127 |
+
]
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
# scalar
|
| 131 |
+
ser = orig.copy()
|
| 132 |
+
indexer_sli(ser)[1] = Timestamp("2011-01-01", tz=tz)
|
| 133 |
+
tm.assert_series_equal(ser, exp)
|
| 134 |
+
|
| 135 |
+
# vector
|
| 136 |
+
vals = Series(
|
| 137 |
+
[Timestamp("2011-01-01", tz=tz), Timestamp("2012-01-01", tz=tz)],
|
| 138 |
+
index=[1, 2],
|
| 139 |
+
)
|
| 140 |
+
assert vals.dtype == f"datetime64[ns, {tz}]"
|
| 141 |
+
|
| 142 |
+
exp = Series(
|
| 143 |
+
[
|
| 144 |
+
Timestamp("2016-11-06 00:00", tz=tz),
|
| 145 |
+
Timestamp("2011-01-01 00:00", tz=tz),
|
| 146 |
+
Timestamp("2012-01-01 00:00", tz=tz),
|
| 147 |
+
]
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
ser = orig.copy()
|
| 151 |
+
indexer_sli(ser)[[1, 2]] = vals
|
| 152 |
+
tm.assert_series_equal(ser, exp)
|
| 153 |
+
|
| 154 |
+
def test_object_series_setitem_dt64array_exact_match(self):
|
| 155 |
+
# make sure the dt64 isn't cast by numpy to integers
|
| 156 |
+
# https://github.com/numpy/numpy/issues/12550
|
| 157 |
+
|
| 158 |
+
ser = Series({"X": np.nan}, dtype=object)
|
| 159 |
+
|
| 160 |
+
indexer = [True]
|
| 161 |
+
|
| 162 |
+
# "exact_match" -> size of array being set matches size of ser
|
| 163 |
+
value = np.array([4], dtype="M8[ns]")
|
| 164 |
+
|
| 165 |
+
ser.iloc[indexer] = value
|
| 166 |
+
|
| 167 |
+
expected = Series([value[0]], index=["X"], dtype=object)
|
| 168 |
+
assert all(isinstance(x, np.datetime64) for x in expected.values)
|
| 169 |
+
|
| 170 |
+
tm.assert_series_equal(ser, expected)
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
class TestSetitemScalarIndexer:
|
| 174 |
+
def test_setitem_negative_out_of_bounds(self):
|
| 175 |
+
ser = Series(tm.rands_array(5, 10), index=tm.rands_array(10, 10))
|
| 176 |
+
|
| 177 |
+
msg = "index -11 is out of bounds for axis 0 with size 10"
|
| 178 |
+
with pytest.raises(IndexError, match=msg):
|
| 179 |
+
ser[-11] = "foo"
|
| 180 |
+
|
| 181 |
+
@pytest.mark.parametrize("indexer", [tm.loc, tm.at])
|
| 182 |
+
@pytest.mark.parametrize("ser_index", [0, 1])
|
| 183 |
+
def test_setitem_series_object_dtype(self, indexer, ser_index):
|
| 184 |
+
# GH#38303
|
| 185 |
+
ser = Series([0, 0], dtype="object")
|
| 186 |
+
idxr = indexer(ser)
|
| 187 |
+
idxr[0] = Series([42], index=[ser_index])
|
| 188 |
+
expected = Series([Series([42], index=[ser_index]), 0], dtype="object")
|
| 189 |
+
tm.assert_series_equal(ser, expected)
|
| 190 |
+
|
| 191 |
+
@pytest.mark.parametrize("index, exp_value", [(0, 42), (1, np.nan)])
|
| 192 |
+
def test_setitem_series(self, index, exp_value):
|
| 193 |
+
# GH#38303
|
| 194 |
+
ser = Series([0, 0])
|
| 195 |
+
ser.loc[0] = Series([42], index=[index])
|
| 196 |
+
expected = Series([exp_value, 0])
|
| 197 |
+
tm.assert_series_equal(ser, expected)
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
class TestSetitemSlices:
|
| 201 |
+
def test_setitem_slice_float_raises(self, datetime_series):
|
| 202 |
+
msg = (
|
| 203 |
+
"cannot do slice indexing on DatetimeIndex with these indexers "
|
| 204 |
+
r"\[{key}\] of type float"
|
| 205 |
+
)
|
| 206 |
+
with pytest.raises(TypeError, match=msg.format(key=r"4\.0")):
|
| 207 |
+
datetime_series[4.0:10.0] = 0
|
| 208 |
+
|
| 209 |
+
with pytest.raises(TypeError, match=msg.format(key=r"4\.5")):
|
| 210 |
+
datetime_series[4.5:10.0] = 0
|
| 211 |
+
|
| 212 |
+
def test_setitem_slice(self):
|
| 213 |
+
ser = Series(range(10), index=list(range(10)))
|
| 214 |
+
ser[-12:] = 0
|
| 215 |
+
assert (ser == 0).all()
|
| 216 |
+
|
| 217 |
+
ser[:-12] = 5
|
| 218 |
+
assert (ser == 0).all()
|
| 219 |
+
|
| 220 |
+
def test_setitem_slice_integers(self):
|
| 221 |
+
ser = Series(np.random.randn(8), index=[2, 4, 6, 8, 10, 12, 14, 16])
|
| 222 |
+
|
| 223 |
+
ser[:4] = 0
|
| 224 |
+
assert (ser[:4] == 0).all()
|
| 225 |
+
assert not (ser[4:] == 0).any()
|
| 226 |
+
|
| 227 |
+
def test_setitem_slicestep(self):
|
| 228 |
+
# caught this bug when writing tests
|
| 229 |
+
series = Series(tm.makeIntIndex(20).astype(float), index=tm.makeIntIndex(20))
|
| 230 |
+
|
| 231 |
+
series[::2] = 0
|
| 232 |
+
assert (series[::2] == 0).all()
|
| 233 |
+
|
| 234 |
+
def test_setitem_multiindex_slice(self, indexer_sli):
|
| 235 |
+
# GH 8856
|
| 236 |
+
mi = MultiIndex.from_product(([0, 1], list("abcde")))
|
| 237 |
+
result = Series(np.arange(10, dtype=np.int64), mi)
|
| 238 |
+
indexer_sli(result)[::4] = 100
|
| 239 |
+
expected = Series([100, 1, 2, 3, 100, 5, 6, 7, 100, 9], mi)
|
| 240 |
+
tm.assert_series_equal(result, expected)
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
class TestSetitemBooleanMask:
|
| 244 |
+
def test_setitem_mask_cast(self):
|
| 245 |
+
# GH#2746
|
| 246 |
+
# need to upcast
|
| 247 |
+
ser = Series([1, 2], index=[1, 2], dtype="int64")
|
| 248 |
+
ser[[True, False]] = Series([0], index=[1], dtype="int64")
|
| 249 |
+
expected = Series([0, 2], index=[1, 2], dtype="int64")
|
| 250 |
+
|
| 251 |
+
tm.assert_series_equal(ser, expected)
|
| 252 |
+
|
| 253 |
+
def test_setitem_mask_align_and_promote(self):
|
| 254 |
+
# GH#8387: test that changing types does not break alignment
|
| 255 |
+
ts = Series(np.random.randn(100), index=np.arange(100, 0, -1)).round(5)
|
| 256 |
+
mask = ts > 0
|
| 257 |
+
left = ts.copy()
|
| 258 |
+
right = ts[mask].copy().map(str)
|
| 259 |
+
left[mask] = right
|
| 260 |
+
expected = ts.map(lambda t: str(t) if t > 0 else t)
|
| 261 |
+
tm.assert_series_equal(left, expected)
|
| 262 |
+
|
| 263 |
+
def test_setitem_mask_promote_strs(self):
|
| 264 |
+
ser = Series([0, 1, 2, 0])
|
| 265 |
+
mask = ser > 0
|
| 266 |
+
ser2 = ser[mask].map(str)
|
| 267 |
+
ser[mask] = ser2
|
| 268 |
+
|
| 269 |
+
expected = Series([0, "1", "2", 0])
|
| 270 |
+
tm.assert_series_equal(ser, expected)
|
| 271 |
+
|
| 272 |
+
def test_setitem_mask_promote(self):
|
| 273 |
+
ser = Series([0, "foo", "bar", 0])
|
| 274 |
+
mask = Series([False, True, True, False])
|
| 275 |
+
ser2 = ser[mask]
|
| 276 |
+
ser[mask] = ser2
|
| 277 |
+
|
| 278 |
+
expected = Series([0, "foo", "bar", 0])
|
| 279 |
+
tm.assert_series_equal(ser, expected)
|
| 280 |
+
|
| 281 |
+
def test_setitem_boolean(self, string_series):
|
| 282 |
+
mask = string_series > string_series.median()
|
| 283 |
+
|
| 284 |
+
# similar indexed series
|
| 285 |
+
result = string_series.copy()
|
| 286 |
+
result[mask] = string_series * 2
|
| 287 |
+
expected = string_series * 2
|
| 288 |
+
tm.assert_series_equal(result[mask], expected[mask])
|
| 289 |
+
|
| 290 |
+
# needs alignment
|
| 291 |
+
result = string_series.copy()
|
| 292 |
+
result[mask] = (string_series * 2)[0:5]
|
| 293 |
+
expected = (string_series * 2)[0:5].reindex_like(string_series)
|
| 294 |
+
expected[-mask] = string_series[mask]
|
| 295 |
+
tm.assert_series_equal(result[mask], expected[mask])
|
| 296 |
+
|
| 297 |
+
def test_setitem_boolean_corner(self, datetime_series):
|
| 298 |
+
ts = datetime_series
|
| 299 |
+
mask_shifted = ts.shift(1, freq=BDay()) > ts.median()
|
| 300 |
+
|
| 301 |
+
msg = (
|
| 302 |
+
r"Unalignable boolean Series provided as indexer \(index of "
|
| 303 |
+
r"the boolean Series and of the indexed object do not match"
|
| 304 |
+
)
|
| 305 |
+
with pytest.raises(IndexingError, match=msg):
|
| 306 |
+
ts[mask_shifted] = 1
|
| 307 |
+
|
| 308 |
+
with pytest.raises(IndexingError, match=msg):
|
| 309 |
+
ts.loc[mask_shifted] = 1
|
| 310 |
+
|
| 311 |
+
def test_setitem_boolean_different_order(self, string_series):
|
| 312 |
+
ordered = string_series.sort_values()
|
| 313 |
+
|
| 314 |
+
copy = string_series.copy()
|
| 315 |
+
copy[ordered > 0] = 0
|
| 316 |
+
|
| 317 |
+
expected = string_series.copy()
|
| 318 |
+
expected[expected > 0] = 0
|
| 319 |
+
|
| 320 |
+
tm.assert_series_equal(copy, expected)
|
| 321 |
+
|
| 322 |
+
@pytest.mark.parametrize("func", [list, np.array, Series])
|
| 323 |
+
def test_setitem_boolean_python_list(self, func):
|
| 324 |
+
# GH19406
|
| 325 |
+
ser = Series([None, "b", None])
|
| 326 |
+
mask = func([True, False, True])
|
| 327 |
+
ser[mask] = ["a", "c"]
|
| 328 |
+
expected = Series(["a", "b", "c"])
|
| 329 |
+
tm.assert_series_equal(ser, expected)
|
| 330 |
+
|
| 331 |
+
def test_setitem_boolean_nullable_int_types(self, any_numeric_ea_dtype):
|
| 332 |
+
# GH: 26468
|
| 333 |
+
ser = Series([5, 6, 7, 8], dtype=any_numeric_ea_dtype)
|
| 334 |
+
ser[ser > 6] = Series(range(4), dtype=any_numeric_ea_dtype)
|
| 335 |
+
expected = Series([5, 6, 2, 3], dtype=any_numeric_ea_dtype)
|
| 336 |
+
tm.assert_series_equal(ser, expected)
|
| 337 |
+
|
| 338 |
+
ser = Series([5, 6, 7, 8], dtype=any_numeric_ea_dtype)
|
| 339 |
+
ser.loc[ser > 6] = Series(range(4), dtype=any_numeric_ea_dtype)
|
| 340 |
+
tm.assert_series_equal(ser, expected)
|
| 341 |
+
|
| 342 |
+
ser = Series([5, 6, 7, 8], dtype=any_numeric_ea_dtype)
|
| 343 |
+
loc_ser = Series(range(4), dtype=any_numeric_ea_dtype)
|
| 344 |
+
ser.loc[ser > 6] = loc_ser.loc[loc_ser > 1]
|
| 345 |
+
tm.assert_series_equal(ser, expected)
|
| 346 |
+
|
| 347 |
+
def test_setitem_with_bool_mask_and_values_matching_n_trues_in_length(self):
|
| 348 |
+
# GH#30567
|
| 349 |
+
ser = Series([None] * 10)
|
| 350 |
+
mask = [False] * 3 + [True] * 5 + [False] * 2
|
| 351 |
+
ser[mask] = range(5)
|
| 352 |
+
result = ser
|
| 353 |
+
expected = Series([None] * 3 + list(range(5)) + [None] * 2).astype("object")
|
| 354 |
+
tm.assert_series_equal(result, expected)
|
| 355 |
+
|
| 356 |
+
def test_setitem_nan_with_bool(self):
|
| 357 |
+
# GH 13034
|
| 358 |
+
result = Series([True, False, True])
|
| 359 |
+
result[0] = np.nan
|
| 360 |
+
expected = Series([np.nan, False, True], dtype=object)
|
| 361 |
+
tm.assert_series_equal(result, expected)
|
| 362 |
+
|
| 363 |
+
def test_setitem_mask_smallint_upcast(self):
|
| 364 |
+
orig = Series([1, 2, 3], dtype="int8")
|
| 365 |
+
alt = np.array([999, 1000, 1001], dtype=np.int64)
|
| 366 |
+
|
| 367 |
+
mask = np.array([True, False, True])
|
| 368 |
+
|
| 369 |
+
ser = orig.copy()
|
| 370 |
+
ser[mask] = Series(alt)
|
| 371 |
+
expected = Series([999, 2, 1001])
|
| 372 |
+
tm.assert_series_equal(ser, expected)
|
| 373 |
+
|
| 374 |
+
ser2 = orig.copy()
|
| 375 |
+
ser2.mask(mask, alt, inplace=True)
|
| 376 |
+
tm.assert_series_equal(ser2, expected)
|
| 377 |
+
|
| 378 |
+
ser3 = orig.copy()
|
| 379 |
+
res = ser3.where(~mask, Series(alt))
|
| 380 |
+
tm.assert_series_equal(res, expected)
|
| 381 |
+
|
| 382 |
+
def test_setitem_mask_smallint_no_upcast(self):
|
| 383 |
+
# like test_setitem_mask_smallint_upcast, but while we can't hold 'alt',
|
| 384 |
+
# we *can* hold alt[mask] without casting
|
| 385 |
+
orig = Series([1, 2, 3], dtype="uint8")
|
| 386 |
+
alt = Series([245, 1000, 246], dtype=np.int64)
|
| 387 |
+
|
| 388 |
+
mask = np.array([True, False, True])
|
| 389 |
+
|
| 390 |
+
ser = orig.copy()
|
| 391 |
+
ser[mask] = alt
|
| 392 |
+
expected = Series([245, 2, 246], dtype="uint8")
|
| 393 |
+
tm.assert_series_equal(ser, expected)
|
| 394 |
+
|
| 395 |
+
ser2 = orig.copy()
|
| 396 |
+
ser2.mask(mask, alt, inplace=True)
|
| 397 |
+
tm.assert_series_equal(ser2, expected)
|
| 398 |
+
|
| 399 |
+
# FIXME: don't leave commented-out
|
| 400 |
+
# FIXME: ser.where(~mask, alt) unnecessarily upcasts to int64
|
| 401 |
+
# ser3 = orig.copy()
|
| 402 |
+
# res = ser3.where(~mask, alt)
|
| 403 |
+
# tm.assert_series_equal(res, expected)
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
class TestSetitemViewCopySemantics:
|
| 407 |
+
def test_setitem_invalidates_datetime_index_freq(self, using_copy_on_write):
|
| 408 |
+
# GH#24096 altering a datetime64tz Series inplace invalidates the
|
| 409 |
+
# `freq` attribute on the underlying DatetimeIndex
|
| 410 |
+
|
| 411 |
+
dti = date_range("20130101", periods=3, tz="US/Eastern")
|
| 412 |
+
ts = dti[1]
|
| 413 |
+
ser = Series(dti)
|
| 414 |
+
assert ser._values is not dti
|
| 415 |
+
if using_copy_on_write:
|
| 416 |
+
assert ser._values._ndarray.base is dti._data._ndarray.base
|
| 417 |
+
else:
|
| 418 |
+
assert ser._values._ndarray.base is not dti._data._ndarray.base
|
| 419 |
+
assert dti.freq == "D"
|
| 420 |
+
ser.iloc[1] = NaT
|
| 421 |
+
assert ser._values.freq is None
|
| 422 |
+
|
| 423 |
+
# check that the DatetimeIndex was not altered in place
|
| 424 |
+
assert ser._values is not dti
|
| 425 |
+
assert ser._values._ndarray.base is not dti._data._ndarray.base
|
| 426 |
+
assert dti[1] == ts
|
| 427 |
+
assert dti.freq == "D"
|
| 428 |
+
|
| 429 |
+
def test_dt64tz_setitem_does_not_mutate_dti(self, using_copy_on_write):
|
| 430 |
+
# GH#21907, GH#24096
|
| 431 |
+
dti = date_range("2016-01-01", periods=10, tz="US/Pacific")
|
| 432 |
+
ts = dti[0]
|
| 433 |
+
ser = Series(dti)
|
| 434 |
+
assert ser._values is not dti
|
| 435 |
+
if using_copy_on_write:
|
| 436 |
+
assert ser._values._ndarray.base is dti._data._ndarray.base
|
| 437 |
+
assert ser._mgr.arrays[0]._ndarray.base is dti._data._ndarray.base
|
| 438 |
+
else:
|
| 439 |
+
assert ser._values._ndarray.base is not dti._data._ndarray.base
|
| 440 |
+
assert ser._mgr.arrays[0]._ndarray.base is not dti._data._ndarray.base
|
| 441 |
+
|
| 442 |
+
assert ser._mgr.arrays[0] is not dti
|
| 443 |
+
|
| 444 |
+
ser[::3] = NaT
|
| 445 |
+
assert ser[0] is NaT
|
| 446 |
+
assert dti[0] == ts
|
| 447 |
+
|
| 448 |
+
|
| 449 |
+
class TestSetitemCallable:
|
| 450 |
+
def test_setitem_callable_key(self):
|
| 451 |
+
# GH#12533
|
| 452 |
+
ser = Series([1, 2, 3, 4], index=list("ABCD"))
|
| 453 |
+
ser[lambda x: "A"] = -1
|
| 454 |
+
|
| 455 |
+
expected = Series([-1, 2, 3, 4], index=list("ABCD"))
|
| 456 |
+
tm.assert_series_equal(ser, expected)
|
| 457 |
+
|
| 458 |
+
def test_setitem_callable_other(self):
|
| 459 |
+
# GH#13299
|
| 460 |
+
inc = lambda x: x + 1
|
| 461 |
+
|
| 462 |
+
ser = Series([1, 2, -1, 4])
|
| 463 |
+
ser[ser < 0] = inc
|
| 464 |
+
|
| 465 |
+
expected = Series([1, 2, inc, 4])
|
| 466 |
+
tm.assert_series_equal(ser, expected)
|
| 467 |
+
|
| 468 |
+
|
| 469 |
+
class TestSetitemWithExpansion:
|
| 470 |
+
def test_setitem_empty_series(self):
|
| 471 |
+
# GH#10193
|
| 472 |
+
key = Timestamp("2012-01-01")
|
| 473 |
+
series = Series(dtype=object)
|
| 474 |
+
series[key] = 47
|
| 475 |
+
expected = Series(47, [key])
|
| 476 |
+
tm.assert_series_equal(series, expected)
|
| 477 |
+
|
| 478 |
+
def test_setitem_empty_series_datetimeindex_preserves_freq(self):
|
| 479 |
+
# GH#33573 our index should retain its freq
|
| 480 |
+
series = Series([], DatetimeIndex([], freq="D"), dtype=object)
|
| 481 |
+
key = Timestamp("2012-01-01")
|
| 482 |
+
series[key] = 47
|
| 483 |
+
expected = Series(47, DatetimeIndex([key], freq="D"))
|
| 484 |
+
tm.assert_series_equal(series, expected)
|
| 485 |
+
assert series.index.freq == expected.index.freq
|
| 486 |
+
|
| 487 |
+
def test_setitem_empty_series_timestamp_preserves_dtype(self):
|
| 488 |
+
# GH 21881
|
| 489 |
+
timestamp = Timestamp(1412526600000000000)
|
| 490 |
+
series = Series([timestamp], index=["timestamp"], dtype=object)
|
| 491 |
+
expected = series["timestamp"]
|
| 492 |
+
|
| 493 |
+
series = Series([], dtype=object)
|
| 494 |
+
series["anything"] = 300.0
|
| 495 |
+
series["timestamp"] = timestamp
|
| 496 |
+
result = series["timestamp"]
|
| 497 |
+
assert result == expected
|
| 498 |
+
|
| 499 |
+
@pytest.mark.parametrize(
|
| 500 |
+
"td",
|
| 501 |
+
[
|
| 502 |
+
Timedelta("9 days"),
|
| 503 |
+
Timedelta("9 days").to_timedelta64(),
|
| 504 |
+
Timedelta("9 days").to_pytimedelta(),
|
| 505 |
+
],
|
| 506 |
+
)
|
| 507 |
+
def test_append_timedelta_does_not_cast(self, td):
|
| 508 |
+
# GH#22717 inserting a Timedelta should _not_ cast to int64
|
| 509 |
+
expected = Series(["x", td], index=[0, "td"], dtype=object)
|
| 510 |
+
|
| 511 |
+
ser = Series(["x"])
|
| 512 |
+
ser["td"] = td
|
| 513 |
+
tm.assert_series_equal(ser, expected)
|
| 514 |
+
assert isinstance(ser["td"], Timedelta)
|
| 515 |
+
|
| 516 |
+
ser = Series(["x"])
|
| 517 |
+
ser.loc["td"] = Timedelta("9 days")
|
| 518 |
+
tm.assert_series_equal(ser, expected)
|
| 519 |
+
assert isinstance(ser["td"], Timedelta)
|
| 520 |
+
|
| 521 |
+
def test_setitem_with_expansion_type_promotion(self):
|
| 522 |
+
# GH#12599
|
| 523 |
+
ser = Series(dtype=object)
|
| 524 |
+
ser["a"] = Timestamp("2016-01-01")
|
| 525 |
+
ser["b"] = 3.0
|
| 526 |
+
ser["c"] = "foo"
|
| 527 |
+
expected = Series([Timestamp("2016-01-01"), 3.0, "foo"], index=["a", "b", "c"])
|
| 528 |
+
tm.assert_series_equal(ser, expected)
|
| 529 |
+
|
| 530 |
+
def test_setitem_not_contained(self, string_series):
|
| 531 |
+
# set item that's not contained
|
| 532 |
+
ser = string_series.copy()
|
| 533 |
+
assert "foobar" not in ser.index
|
| 534 |
+
ser["foobar"] = 1
|
| 535 |
+
|
| 536 |
+
app = Series([1], index=["foobar"], name="series")
|
| 537 |
+
expected = concat([string_series, app])
|
| 538 |
+
tm.assert_series_equal(ser, expected)
|
| 539 |
+
|
| 540 |
+
def test_setitem_keep_precision(self, any_numeric_ea_dtype):
|
| 541 |
+
# GH#32346
|
| 542 |
+
ser = Series([1, 2], dtype=any_numeric_ea_dtype)
|
| 543 |
+
ser[2] = 10
|
| 544 |
+
expected = Series([1, 2, 10], dtype=any_numeric_ea_dtype)
|
| 545 |
+
tm.assert_series_equal(ser, expected)
|
| 546 |
+
|
| 547 |
+
@pytest.mark.parametrize("indexer", [1, 2])
|
| 548 |
+
@pytest.mark.parametrize(
|
| 549 |
+
"na, target_na, dtype, target_dtype",
|
| 550 |
+
[
|
| 551 |
+
(NA, NA, "Int64", "Int64"),
|
| 552 |
+
(NA, np.nan, "int64", "float64"),
|
| 553 |
+
(NaT, NaT, "int64", "object"),
|
| 554 |
+
(np.nan, NA, "Int64", "Int64"),
|
| 555 |
+
(np.nan, NA, "Float64", "Float64"),
|
| 556 |
+
(np.nan, np.nan, "int64", "float64"),
|
| 557 |
+
],
|
| 558 |
+
)
|
| 559 |
+
def test_setitem_enlarge_with_na(self, na, target_na, dtype, target_dtype, indexer):
|
| 560 |
+
# GH#32346
|
| 561 |
+
ser = Series([1, 2], dtype=dtype)
|
| 562 |
+
ser[indexer] = na
|
| 563 |
+
expected_values = [1, target_na] if indexer == 1 else [1, 2, target_na]
|
| 564 |
+
expected = Series(expected_values, dtype=target_dtype)
|
| 565 |
+
tm.assert_series_equal(ser, expected)
|
| 566 |
+
|
| 567 |
+
def test_setitem_enlargement_object_none(self, nulls_fixture):
|
| 568 |
+
# GH#48665
|
| 569 |
+
ser = Series(["a", "b"])
|
| 570 |
+
ser[3] = nulls_fixture
|
| 571 |
+
expected = Series(["a", "b", nulls_fixture], index=[0, 1, 3])
|
| 572 |
+
tm.assert_series_equal(ser, expected)
|
| 573 |
+
assert ser[3] is nulls_fixture
|
| 574 |
+
|
| 575 |
+
|
| 576 |
+
def test_setitem_scalar_into_readonly_backing_data():
|
| 577 |
+
# GH#14359: test that you cannot mutate a read only buffer
|
| 578 |
+
|
| 579 |
+
array = np.zeros(5)
|
| 580 |
+
array.flags.writeable = False # make the array immutable
|
| 581 |
+
series = Series(array, copy=False)
|
| 582 |
+
|
| 583 |
+
for n in series.index:
|
| 584 |
+
msg = "assignment destination is read-only"
|
| 585 |
+
with pytest.raises(ValueError, match=msg):
|
| 586 |
+
series[n] = 1
|
| 587 |
+
|
| 588 |
+
assert array[n] == 0
|
| 589 |
+
|
| 590 |
+
|
| 591 |
+
def test_setitem_slice_into_readonly_backing_data():
|
| 592 |
+
# GH#14359: test that you cannot mutate a read only buffer
|
| 593 |
+
|
| 594 |
+
array = np.zeros(5)
|
| 595 |
+
array.flags.writeable = False # make the array immutable
|
| 596 |
+
series = Series(array, copy=False)
|
| 597 |
+
|
| 598 |
+
msg = "assignment destination is read-only"
|
| 599 |
+
with pytest.raises(ValueError, match=msg):
|
| 600 |
+
series[1:3] = 1
|
| 601 |
+
|
| 602 |
+
assert not array.any()
|
| 603 |
+
|
| 604 |
+
|
| 605 |
+
def test_setitem_categorical_assigning_ops():
|
| 606 |
+
orig = Series(Categorical(["b", "b"], categories=["a", "b"]))
|
| 607 |
+
ser = orig.copy()
|
| 608 |
+
ser[:] = "a"
|
| 609 |
+
exp = Series(Categorical(["a", "a"], categories=["a", "b"]))
|
| 610 |
+
tm.assert_series_equal(ser, exp)
|
| 611 |
+
|
| 612 |
+
ser = orig.copy()
|
| 613 |
+
ser[1] = "a"
|
| 614 |
+
exp = Series(Categorical(["b", "a"], categories=["a", "b"]))
|
| 615 |
+
tm.assert_series_equal(ser, exp)
|
| 616 |
+
|
| 617 |
+
ser = orig.copy()
|
| 618 |
+
ser[ser.index > 0] = "a"
|
| 619 |
+
exp = Series(Categorical(["b", "a"], categories=["a", "b"]))
|
| 620 |
+
tm.assert_series_equal(ser, exp)
|
| 621 |
+
|
| 622 |
+
ser = orig.copy()
|
| 623 |
+
ser[[False, True]] = "a"
|
| 624 |
+
exp = Series(Categorical(["b", "a"], categories=["a", "b"]))
|
| 625 |
+
tm.assert_series_equal(ser, exp)
|
| 626 |
+
|
| 627 |
+
ser = orig.copy()
|
| 628 |
+
ser.index = ["x", "y"]
|
| 629 |
+
ser["y"] = "a"
|
| 630 |
+
exp = Series(Categorical(["b", "a"], categories=["a", "b"]), index=["x", "y"])
|
| 631 |
+
tm.assert_series_equal(ser, exp)
|
| 632 |
+
|
| 633 |
+
|
| 634 |
+
def test_setitem_nan_into_categorical():
|
| 635 |
+
# ensure that one can set something to np.nan
|
| 636 |
+
ser = Series(Categorical([1, 2, 3]))
|
| 637 |
+
exp = Series(Categorical([1, np.nan, 3], categories=[1, 2, 3]))
|
| 638 |
+
ser[1] = np.nan
|
| 639 |
+
tm.assert_series_equal(ser, exp)
|
| 640 |
+
|
| 641 |
+
|
| 642 |
+
class TestSetitemCasting:
|
| 643 |
+
@pytest.mark.parametrize("unique", [True, False])
|
| 644 |
+
@pytest.mark.parametrize("val", [3, 3.0, "3"], ids=type)
|
| 645 |
+
def test_setitem_non_bool_into_bool(self, val, indexer_sli, unique):
|
| 646 |
+
# dont cast these 3-like values to bool
|
| 647 |
+
ser = Series([True, False])
|
| 648 |
+
if not unique:
|
| 649 |
+
ser.index = [1, 1]
|
| 650 |
+
|
| 651 |
+
indexer_sli(ser)[1] = val
|
| 652 |
+
assert type(ser.iloc[1]) == type(val)
|
| 653 |
+
|
| 654 |
+
expected = Series([True, val], dtype=object, index=ser.index)
|
| 655 |
+
if not unique and indexer_sli is not tm.iloc:
|
| 656 |
+
expected = Series([val, val], dtype=object, index=[1, 1])
|
| 657 |
+
tm.assert_series_equal(ser, expected)
|
| 658 |
+
|
| 659 |
+
def test_setitem_boolean_array_into_npbool(self):
|
| 660 |
+
# GH#45462
|
| 661 |
+
ser = Series([True, False, True])
|
| 662 |
+
values = ser._values
|
| 663 |
+
arr = array([True, False, None])
|
| 664 |
+
|
| 665 |
+
ser[:2] = arr[:2] # no NAs -> can set inplace
|
| 666 |
+
assert ser._values is values
|
| 667 |
+
|
| 668 |
+
ser[1:] = arr[1:] # has an NA -> cast to boolean dtype
|
| 669 |
+
expected = Series(arr)
|
| 670 |
+
tm.assert_series_equal(ser, expected)
|
| 671 |
+
|
| 672 |
+
|
| 673 |
+
class SetitemCastingEquivalents:
|
| 674 |
+
"""
|
| 675 |
+
Check each of several methods that _should_ be equivalent to `obj[key] = val`
|
| 676 |
+
|
| 677 |
+
We assume that
|
| 678 |
+
- obj.index is the default Index(range(len(obj)))
|
| 679 |
+
- the setitem does not expand the obj
|
| 680 |
+
"""
|
| 681 |
+
|
| 682 |
+
@pytest.fixture
|
| 683 |
+
def is_inplace(self, obj, expected):
|
| 684 |
+
"""
|
| 685 |
+
Whether we expect the setting to be in-place or not.
|
| 686 |
+
"""
|
| 687 |
+
try:
|
| 688 |
+
return expected.dtype == obj.dtype
|
| 689 |
+
except TypeError:
|
| 690 |
+
# older numpys
|
| 691 |
+
return False
|
| 692 |
+
|
| 693 |
+
def check_indexer(self, obj, key, expected, val, indexer, is_inplace):
|
| 694 |
+
orig = obj
|
| 695 |
+
obj = obj.copy()
|
| 696 |
+
arr = obj._values
|
| 697 |
+
|
| 698 |
+
indexer(obj)[key] = val
|
| 699 |
+
tm.assert_series_equal(obj, expected)
|
| 700 |
+
|
| 701 |
+
self._check_inplace(is_inplace, orig, arr, obj)
|
| 702 |
+
|
| 703 |
+
def _check_inplace(self, is_inplace, orig, arr, obj):
|
| 704 |
+
if is_inplace is None:
|
| 705 |
+
# We are not (yet) checking whether setting is inplace or not
|
| 706 |
+
pass
|
| 707 |
+
elif is_inplace:
|
| 708 |
+
if arr.dtype.kind in ["m", "M"]:
|
| 709 |
+
# We may not have the same DTA/TDA, but will have the same
|
| 710 |
+
# underlying data
|
| 711 |
+
assert arr._ndarray is obj._values._ndarray
|
| 712 |
+
else:
|
| 713 |
+
assert obj._values is arr
|
| 714 |
+
else:
|
| 715 |
+
# otherwise original array should be unchanged
|
| 716 |
+
tm.assert_equal(arr, orig._values)
|
| 717 |
+
|
| 718 |
+
def test_int_key(self, obj, key, expected, val, indexer_sli, is_inplace):
|
| 719 |
+
if not isinstance(key, int):
|
| 720 |
+
return
|
| 721 |
+
|
| 722 |
+
self.check_indexer(obj, key, expected, val, indexer_sli, is_inplace)
|
| 723 |
+
|
| 724 |
+
if indexer_sli is tm.loc:
|
| 725 |
+
self.check_indexer(obj, key, expected, val, tm.at, is_inplace)
|
| 726 |
+
elif indexer_sli is tm.iloc:
|
| 727 |
+
self.check_indexer(obj, key, expected, val, tm.iat, is_inplace)
|
| 728 |
+
|
| 729 |
+
rng = range(key, key + 1)
|
| 730 |
+
self.check_indexer(obj, rng, expected, val, indexer_sli, is_inplace)
|
| 731 |
+
|
| 732 |
+
if indexer_sli is not tm.loc:
|
| 733 |
+
# Note: no .loc because that handles slice edges differently
|
| 734 |
+
slc = slice(key, key + 1)
|
| 735 |
+
self.check_indexer(obj, slc, expected, val, indexer_sli, is_inplace)
|
| 736 |
+
|
| 737 |
+
ilkey = [key]
|
| 738 |
+
self.check_indexer(obj, ilkey, expected, val, indexer_sli, is_inplace)
|
| 739 |
+
|
| 740 |
+
indkey = np.array(ilkey)
|
| 741 |
+
self.check_indexer(obj, indkey, expected, val, indexer_sli, is_inplace)
|
| 742 |
+
|
| 743 |
+
genkey = (x for x in [key])
|
| 744 |
+
self.check_indexer(obj, genkey, expected, val, indexer_sli, is_inplace)
|
| 745 |
+
|
| 746 |
+
def test_slice_key(self, obj, key, expected, val, indexer_sli, is_inplace):
|
| 747 |
+
if not isinstance(key, slice):
|
| 748 |
+
return
|
| 749 |
+
|
| 750 |
+
if indexer_sli is not tm.loc:
|
| 751 |
+
# Note: no .loc because that handles slice edges differently
|
| 752 |
+
self.check_indexer(obj, key, expected, val, indexer_sli, is_inplace)
|
| 753 |
+
|
| 754 |
+
ilkey = list(range(len(obj)))[key]
|
| 755 |
+
self.check_indexer(obj, ilkey, expected, val, indexer_sli, is_inplace)
|
| 756 |
+
|
| 757 |
+
indkey = np.array(ilkey)
|
| 758 |
+
self.check_indexer(obj, indkey, expected, val, indexer_sli, is_inplace)
|
| 759 |
+
|
| 760 |
+
genkey = (x for x in indkey)
|
| 761 |
+
self.check_indexer(obj, genkey, expected, val, indexer_sli, is_inplace)
|
| 762 |
+
|
| 763 |
+
def test_mask_key(self, obj, key, expected, val, indexer_sli):
|
| 764 |
+
# setitem with boolean mask
|
| 765 |
+
mask = np.zeros(obj.shape, dtype=bool)
|
| 766 |
+
mask[key] = True
|
| 767 |
+
|
| 768 |
+
obj = obj.copy()
|
| 769 |
+
|
| 770 |
+
if is_list_like(val) and len(val) < mask.sum():
|
| 771 |
+
msg = "boolean index did not match indexed array along dimension"
|
| 772 |
+
with pytest.raises(IndexError, match=msg):
|
| 773 |
+
indexer_sli(obj)[mask] = val
|
| 774 |
+
return
|
| 775 |
+
|
| 776 |
+
indexer_sli(obj)[mask] = val
|
| 777 |
+
tm.assert_series_equal(obj, expected)
|
| 778 |
+
|
| 779 |
+
def test_series_where(self, obj, key, expected, val, is_inplace):
|
| 780 |
+
mask = np.zeros(obj.shape, dtype=bool)
|
| 781 |
+
mask[key] = True
|
| 782 |
+
|
| 783 |
+
if is_list_like(val) and len(val) < len(obj):
|
| 784 |
+
# Series.where is not valid here
|
| 785 |
+
msg = "operands could not be broadcast together with shapes"
|
| 786 |
+
with pytest.raises(ValueError, match=msg):
|
| 787 |
+
obj.where(~mask, val)
|
| 788 |
+
return
|
| 789 |
+
|
| 790 |
+
orig = obj
|
| 791 |
+
obj = obj.copy()
|
| 792 |
+
arr = obj._values
|
| 793 |
+
|
| 794 |
+
res = obj.where(~mask, val)
|
| 795 |
+
tm.assert_series_equal(res, expected)
|
| 796 |
+
|
| 797 |
+
self._check_inplace(is_inplace, orig, arr, obj)
|
| 798 |
+
|
| 799 |
+
def test_index_where(self, obj, key, expected, val):
|
| 800 |
+
mask = np.zeros(obj.shape, dtype=bool)
|
| 801 |
+
mask[key] = True
|
| 802 |
+
|
| 803 |
+
res = Index(obj).where(~mask, val)
|
| 804 |
+
expected_idx = Index(expected, dtype=expected.dtype)
|
| 805 |
+
tm.assert_index_equal(res, expected_idx)
|
| 806 |
+
|
| 807 |
+
def test_index_putmask(self, obj, key, expected, val):
|
| 808 |
+
mask = np.zeros(obj.shape, dtype=bool)
|
| 809 |
+
mask[key] = True
|
| 810 |
+
|
| 811 |
+
res = Index(obj).putmask(mask, val)
|
| 812 |
+
tm.assert_index_equal(res, Index(expected, dtype=expected.dtype))
|
| 813 |
+
|
| 814 |
+
|
| 815 |
+
@pytest.mark.parametrize(
|
| 816 |
+
"obj,expected,key",
|
| 817 |
+
[
|
| 818 |
+
pytest.param(
|
| 819 |
+
# GH#45568 setting a valid NA value into IntervalDtype[int] should
|
| 820 |
+
# cast to IntervalDtype[float]
|
| 821 |
+
Series(interval_range(1, 5)),
|
| 822 |
+
Series(
|
| 823 |
+
[Interval(1, 2), np.nan, Interval(3, 4), Interval(4, 5)],
|
| 824 |
+
dtype="interval[float64]",
|
| 825 |
+
),
|
| 826 |
+
1,
|
| 827 |
+
id="interval_int_na_value",
|
| 828 |
+
),
|
| 829 |
+
pytest.param(
|
| 830 |
+
# these induce dtype changes
|
| 831 |
+
Series([2, 3, 4, 5, 6, 7, 8, 9, 10]),
|
| 832 |
+
Series([np.nan, 3, np.nan, 5, np.nan, 7, np.nan, 9, np.nan]),
|
| 833 |
+
slice(None, None, 2),
|
| 834 |
+
id="int_series_slice_key_step",
|
| 835 |
+
),
|
| 836 |
+
pytest.param(
|
| 837 |
+
Series([True, True, False, False]),
|
| 838 |
+
Series([np.nan, True, np.nan, False], dtype=object),
|
| 839 |
+
slice(None, None, 2),
|
| 840 |
+
id="bool_series_slice_key_step",
|
| 841 |
+
),
|
| 842 |
+
pytest.param(
|
| 843 |
+
# these induce dtype changes
|
| 844 |
+
Series(np.arange(10)),
|
| 845 |
+
Series([np.nan, np.nan, np.nan, np.nan, np.nan, 5, 6, 7, 8, 9]),
|
| 846 |
+
slice(None, 5),
|
| 847 |
+
id="int_series_slice_key",
|
| 848 |
+
),
|
| 849 |
+
pytest.param(
|
| 850 |
+
# changes dtype GH#4463
|
| 851 |
+
Series([1, 2, 3]),
|
| 852 |
+
Series([np.nan, 2, 3]),
|
| 853 |
+
0,
|
| 854 |
+
id="int_series_int_key",
|
| 855 |
+
),
|
| 856 |
+
pytest.param(
|
| 857 |
+
# changes dtype GH#4463
|
| 858 |
+
Series([False]),
|
| 859 |
+
Series([np.nan], dtype=object),
|
| 860 |
+
# TODO: maybe go to float64 since we are changing the _whole_ Series?
|
| 861 |
+
0,
|
| 862 |
+
id="bool_series_int_key_change_all",
|
| 863 |
+
),
|
| 864 |
+
pytest.param(
|
| 865 |
+
# changes dtype GH#4463
|
| 866 |
+
Series([False, True]),
|
| 867 |
+
Series([np.nan, True], dtype=object),
|
| 868 |
+
0,
|
| 869 |
+
id="bool_series_int_key",
|
| 870 |
+
),
|
| 871 |
+
],
|
| 872 |
+
)
|
| 873 |
+
class TestSetitemCastingEquivalents(SetitemCastingEquivalents):
|
| 874 |
+
@pytest.fixture(params=[np.nan, np.float64("NaN"), None, NA])
|
| 875 |
+
def val(self, request):
|
| 876 |
+
"""
|
| 877 |
+
NA values that should generally be valid_na for *all* dtypes.
|
| 878 |
+
|
| 879 |
+
Include both python float NaN and np.float64; only np.float64 has a
|
| 880 |
+
`dtype` attribute.
|
| 881 |
+
"""
|
| 882 |
+
return request.param
|
| 883 |
+
|
| 884 |
+
|
| 885 |
+
class TestSetitemTimedelta64IntoNumeric(SetitemCastingEquivalents):
|
| 886 |
+
# timedelta64 should not be treated as integers when setting into
|
| 887 |
+
# numeric Series
|
| 888 |
+
|
| 889 |
+
@pytest.fixture
|
| 890 |
+
def val(self):
|
| 891 |
+
td = np.timedelta64(4, "ns")
|
| 892 |
+
return td
|
| 893 |
+
# TODO: could also try np.full((1,), td)
|
| 894 |
+
|
| 895 |
+
@pytest.fixture(params=[complex, int, float])
|
| 896 |
+
def dtype(self, request):
|
| 897 |
+
return request.param
|
| 898 |
+
|
| 899 |
+
@pytest.fixture
|
| 900 |
+
def obj(self, dtype):
|
| 901 |
+
arr = np.arange(5).astype(dtype)
|
| 902 |
+
ser = Series(arr)
|
| 903 |
+
return ser
|
| 904 |
+
|
| 905 |
+
@pytest.fixture
|
| 906 |
+
def expected(self, dtype):
|
| 907 |
+
arr = np.arange(5).astype(dtype)
|
| 908 |
+
ser = Series(arr)
|
| 909 |
+
ser = ser.astype(object)
|
| 910 |
+
ser.iloc[0] = np.timedelta64(4, "ns")
|
| 911 |
+
return ser
|
| 912 |
+
|
| 913 |
+
@pytest.fixture
|
| 914 |
+
def key(self):
|
| 915 |
+
return 0
|
| 916 |
+
|
| 917 |
+
|
| 918 |
+
class TestSetitemDT64IntoInt(SetitemCastingEquivalents):
|
| 919 |
+
# GH#39619 dont cast dt64 to int when doing this setitem
|
| 920 |
+
|
| 921 |
+
@pytest.fixture(params=["M8[ns]", "m8[ns]"])
|
| 922 |
+
def dtype(self, request):
|
| 923 |
+
return request.param
|
| 924 |
+
|
| 925 |
+
@pytest.fixture
|
| 926 |
+
def scalar(self, dtype):
|
| 927 |
+
val = np.datetime64("2021-01-18 13:25:00", "ns")
|
| 928 |
+
if dtype == "m8[ns]":
|
| 929 |
+
val = val - val
|
| 930 |
+
return val
|
| 931 |
+
|
| 932 |
+
@pytest.fixture
|
| 933 |
+
def expected(self, scalar):
|
| 934 |
+
expected = Series([scalar, scalar, 3], dtype=object)
|
| 935 |
+
assert isinstance(expected[0], type(scalar))
|
| 936 |
+
return expected
|
| 937 |
+
|
| 938 |
+
@pytest.fixture
|
| 939 |
+
def obj(self):
|
| 940 |
+
return Series([1, 2, 3])
|
| 941 |
+
|
| 942 |
+
@pytest.fixture
|
| 943 |
+
def key(self):
|
| 944 |
+
return slice(None, -1)
|
| 945 |
+
|
| 946 |
+
@pytest.fixture(params=[None, list, np.array])
|
| 947 |
+
def val(self, scalar, request):
|
| 948 |
+
box = request.param
|
| 949 |
+
if box is None:
|
| 950 |
+
return scalar
|
| 951 |
+
return box([scalar, scalar])
|
| 952 |
+
|
| 953 |
+
|
| 954 |
+
class TestSetitemNAPeriodDtype(SetitemCastingEquivalents):
|
| 955 |
+
# Setting compatible NA values into Series with PeriodDtype
|
| 956 |
+
|
| 957 |
+
@pytest.fixture
|
| 958 |
+
def expected(self, key):
|
| 959 |
+
exp = Series(period_range("2000-01-01", periods=10, freq="D"))
|
| 960 |
+
exp._values.view("i8")[key] = NaT._value
|
| 961 |
+
assert exp[key] is NaT or all(x is NaT for x in exp[key])
|
| 962 |
+
return exp
|
| 963 |
+
|
| 964 |
+
@pytest.fixture
|
| 965 |
+
def obj(self):
|
| 966 |
+
return Series(period_range("2000-01-01", periods=10, freq="D"))
|
| 967 |
+
|
| 968 |
+
@pytest.fixture(params=[3, slice(3, 5)])
|
| 969 |
+
def key(self, request):
|
| 970 |
+
return request.param
|
| 971 |
+
|
| 972 |
+
@pytest.fixture(params=[None, np.nan])
|
| 973 |
+
def val(self, request):
|
| 974 |
+
return request.param
|
| 975 |
+
|
| 976 |
+
|
| 977 |
+
class TestSetitemNADatetimeLikeDtype(SetitemCastingEquivalents):
|
| 978 |
+
# some nat-like values should be cast to datetime64/timedelta64 when
|
| 979 |
+
# inserting into a datetime64/timedelta64 series. Others should coerce
|
| 980 |
+
# to object and retain their dtypes.
|
| 981 |
+
# GH#18586 for td64 and boolean mask case
|
| 982 |
+
|
| 983 |
+
@pytest.fixture(
|
| 984 |
+
params=["m8[ns]", "M8[ns]", "datetime64[ns, UTC]", "datetime64[ns, US/Central]"]
|
| 985 |
+
)
|
| 986 |
+
def dtype(self, request):
|
| 987 |
+
return request.param
|
| 988 |
+
|
| 989 |
+
@pytest.fixture
|
| 990 |
+
def obj(self, dtype):
|
| 991 |
+
i8vals = date_range("2016-01-01", periods=3).asi8
|
| 992 |
+
idx = Index(i8vals, dtype=dtype)
|
| 993 |
+
assert idx.dtype == dtype
|
| 994 |
+
return Series(idx)
|
| 995 |
+
|
| 996 |
+
@pytest.fixture(
|
| 997 |
+
params=[
|
| 998 |
+
None,
|
| 999 |
+
np.nan,
|
| 1000 |
+
NaT,
|
| 1001 |
+
np.timedelta64("NaT", "ns"),
|
| 1002 |
+
np.datetime64("NaT", "ns"),
|
| 1003 |
+
]
|
| 1004 |
+
)
|
| 1005 |
+
def val(self, request):
|
| 1006 |
+
return request.param
|
| 1007 |
+
|
| 1008 |
+
@pytest.fixture
|
| 1009 |
+
def is_inplace(self, val, obj):
|
| 1010 |
+
# td64 -> cast to object iff val is datetime64("NaT")
|
| 1011 |
+
# dt64 -> cast to object iff val is timedelta64("NaT")
|
| 1012 |
+
# dt64tz -> cast to object with anything _but_ NaT
|
| 1013 |
+
return val is NaT or val is None or val is np.nan or obj.dtype == val.dtype
|
| 1014 |
+
|
| 1015 |
+
@pytest.fixture
|
| 1016 |
+
def expected(self, obj, val, is_inplace):
|
| 1017 |
+
dtype = obj.dtype if is_inplace else object
|
| 1018 |
+
expected = Series([val] + list(obj[1:]), dtype=dtype)
|
| 1019 |
+
return expected
|
| 1020 |
+
|
| 1021 |
+
@pytest.fixture
|
| 1022 |
+
def key(self):
|
| 1023 |
+
return 0
|
| 1024 |
+
|
| 1025 |
+
|
| 1026 |
+
class TestSetitemMismatchedTZCastsToObject(SetitemCastingEquivalents):
|
| 1027 |
+
# GH#24024
|
| 1028 |
+
@pytest.fixture
|
| 1029 |
+
def obj(self):
|
| 1030 |
+
return Series(date_range("2000", periods=2, tz="US/Central"))
|
| 1031 |
+
|
| 1032 |
+
@pytest.fixture
|
| 1033 |
+
def val(self):
|
| 1034 |
+
return Timestamp("2000", tz="US/Eastern")
|
| 1035 |
+
|
| 1036 |
+
@pytest.fixture
|
| 1037 |
+
def key(self):
|
| 1038 |
+
return 0
|
| 1039 |
+
|
| 1040 |
+
@pytest.fixture
|
| 1041 |
+
def expected(self, obj, val):
|
| 1042 |
+
# pre-2.0 this would cast to object, in 2.0 we cast the val to
|
| 1043 |
+
# the target tz
|
| 1044 |
+
expected = Series(
|
| 1045 |
+
[
|
| 1046 |
+
val.tz_convert("US/Central"),
|
| 1047 |
+
Timestamp("2000-01-02 00:00:00-06:00", tz="US/Central"),
|
| 1048 |
+
],
|
| 1049 |
+
dtype=obj.dtype,
|
| 1050 |
+
)
|
| 1051 |
+
return expected
|
| 1052 |
+
|
| 1053 |
+
|
| 1054 |
+
@pytest.mark.parametrize(
|
| 1055 |
+
"obj,expected",
|
| 1056 |
+
[
|
| 1057 |
+
# For numeric series, we should coerce to NaN.
|
| 1058 |
+
(Series([1, 2, 3]), Series([np.nan, 2, 3])),
|
| 1059 |
+
(Series([1.0, 2.0, 3.0]), Series([np.nan, 2.0, 3.0])),
|
| 1060 |
+
# For datetime series, we should coerce to NaT.
|
| 1061 |
+
(
|
| 1062 |
+
Series([datetime(2000, 1, 1), datetime(2000, 1, 2), datetime(2000, 1, 3)]),
|
| 1063 |
+
Series([NaT, datetime(2000, 1, 2), datetime(2000, 1, 3)]),
|
| 1064 |
+
),
|
| 1065 |
+
# For objects, we should preserve the None value.
|
| 1066 |
+
(Series(["foo", "bar", "baz"]), Series([None, "bar", "baz"])),
|
| 1067 |
+
],
|
| 1068 |
+
)
|
| 1069 |
+
class TestSeriesNoneCoercion(SetitemCastingEquivalents):
|
| 1070 |
+
@pytest.fixture
|
| 1071 |
+
def key(self):
|
| 1072 |
+
return 0
|
| 1073 |
+
|
| 1074 |
+
@pytest.fixture
|
| 1075 |
+
def val(self):
|
| 1076 |
+
return None
|
| 1077 |
+
|
| 1078 |
+
|
| 1079 |
+
class TestSetitemFloatIntervalWithIntIntervalValues(SetitemCastingEquivalents):
|
| 1080 |
+
# GH#44201 Cast to shared IntervalDtype rather than object
|
| 1081 |
+
|
| 1082 |
+
def test_setitem_example(self):
|
| 1083 |
+
# Just a case here to make obvious what this test class is aimed at
|
| 1084 |
+
idx = IntervalIndex.from_breaks(range(4))
|
| 1085 |
+
obj = Series(idx)
|
| 1086 |
+
val = Interval(0.5, 1.5)
|
| 1087 |
+
|
| 1088 |
+
obj[0] = val
|
| 1089 |
+
assert obj.dtype == "Interval[float64, right]"
|
| 1090 |
+
|
| 1091 |
+
@pytest.fixture
|
| 1092 |
+
def obj(self):
|
| 1093 |
+
idx = IntervalIndex.from_breaks(range(4))
|
| 1094 |
+
return Series(idx)
|
| 1095 |
+
|
| 1096 |
+
@pytest.fixture
|
| 1097 |
+
def val(self):
|
| 1098 |
+
return Interval(0.5, 1.5)
|
| 1099 |
+
|
| 1100 |
+
@pytest.fixture
|
| 1101 |
+
def key(self):
|
| 1102 |
+
return 0
|
| 1103 |
+
|
| 1104 |
+
@pytest.fixture
|
| 1105 |
+
def expected(self, obj, val):
|
| 1106 |
+
data = [val] + list(obj[1:])
|
| 1107 |
+
idx = IntervalIndex(data, dtype="Interval[float64]")
|
| 1108 |
+
return Series(idx)
|
| 1109 |
+
|
| 1110 |
+
|
| 1111 |
+
class TestSetitemRangeIntoIntegerSeries(SetitemCastingEquivalents):
|
| 1112 |
+
# GH#44261 Setting a range with sufficiently-small integers into
|
| 1113 |
+
# small-itemsize integer dtypes should not need to upcast
|
| 1114 |
+
|
| 1115 |
+
@pytest.fixture
|
| 1116 |
+
def obj(self, any_int_numpy_dtype):
|
| 1117 |
+
dtype = np.dtype(any_int_numpy_dtype)
|
| 1118 |
+
ser = Series(range(5), dtype=dtype)
|
| 1119 |
+
return ser
|
| 1120 |
+
|
| 1121 |
+
@pytest.fixture
|
| 1122 |
+
def val(self):
|
| 1123 |
+
return range(2, 4)
|
| 1124 |
+
|
| 1125 |
+
@pytest.fixture
|
| 1126 |
+
def key(self):
|
| 1127 |
+
return slice(0, 2)
|
| 1128 |
+
|
| 1129 |
+
@pytest.fixture
|
| 1130 |
+
def expected(self, any_int_numpy_dtype):
|
| 1131 |
+
dtype = np.dtype(any_int_numpy_dtype)
|
| 1132 |
+
exp = Series([2, 3, 2, 3, 4], dtype=dtype)
|
| 1133 |
+
return exp
|
| 1134 |
+
|
| 1135 |
+
|
| 1136 |
+
@pytest.mark.parametrize(
|
| 1137 |
+
"val",
|
| 1138 |
+
[
|
| 1139 |
+
np.array([2.0, 3.0]),
|
| 1140 |
+
np.array([2.5, 3.5]),
|
| 1141 |
+
np.array([2**65, 2**65 + 1], dtype=np.float64), # all ints, but can't cast
|
| 1142 |
+
],
|
| 1143 |
+
)
|
| 1144 |
+
class TestSetitemFloatNDarrayIntoIntegerSeries(SetitemCastingEquivalents):
|
| 1145 |
+
@pytest.fixture
|
| 1146 |
+
def obj(self):
|
| 1147 |
+
return Series(range(5), dtype=np.int64)
|
| 1148 |
+
|
| 1149 |
+
@pytest.fixture
|
| 1150 |
+
def key(self):
|
| 1151 |
+
return slice(0, 2)
|
| 1152 |
+
|
| 1153 |
+
@pytest.fixture
|
| 1154 |
+
def expected(self, val):
|
| 1155 |
+
if val[0] == 2:
|
| 1156 |
+
# NB: this condition is based on currently-hardcoded "val" cases
|
| 1157 |
+
dtype = np.int64
|
| 1158 |
+
else:
|
| 1159 |
+
dtype = np.float64
|
| 1160 |
+
res_values = np.array(range(5), dtype=dtype)
|
| 1161 |
+
res_values[:2] = val
|
| 1162 |
+
return Series(res_values)
|
| 1163 |
+
|
| 1164 |
+
|
| 1165 |
+
@pytest.mark.parametrize("val", [512, np.int16(512)])
|
| 1166 |
+
class TestSetitemIntoIntegerSeriesNeedsUpcast(SetitemCastingEquivalents):
|
| 1167 |
+
@pytest.fixture
|
| 1168 |
+
def obj(self):
|
| 1169 |
+
return Series([1, 2, 3], dtype=np.int8)
|
| 1170 |
+
|
| 1171 |
+
@pytest.fixture
|
| 1172 |
+
def key(self):
|
| 1173 |
+
return 1
|
| 1174 |
+
|
| 1175 |
+
@pytest.fixture
|
| 1176 |
+
def expected(self):
|
| 1177 |
+
return Series([1, 512, 3], dtype=np.int16)
|
| 1178 |
+
|
| 1179 |
+
|
| 1180 |
+
@pytest.mark.parametrize("val", [2**33 + 1.0, 2**33 + 1.1, 2**62])
|
| 1181 |
+
class TestSmallIntegerSetitemUpcast(SetitemCastingEquivalents):
|
| 1182 |
+
# https://github.com/pandas-dev/pandas/issues/39584#issuecomment-941212124
|
| 1183 |
+
@pytest.fixture
|
| 1184 |
+
def obj(self):
|
| 1185 |
+
return Series([1, 2, 3], dtype="i4")
|
| 1186 |
+
|
| 1187 |
+
@pytest.fixture
|
| 1188 |
+
def key(self):
|
| 1189 |
+
return 0
|
| 1190 |
+
|
| 1191 |
+
@pytest.fixture
|
| 1192 |
+
def expected(self, val):
|
| 1193 |
+
if val % 1 != 0:
|
| 1194 |
+
dtype = "f8"
|
| 1195 |
+
else:
|
| 1196 |
+
dtype = "i8"
|
| 1197 |
+
return Series([val, 2, 3], dtype=dtype)
|
| 1198 |
+
|
| 1199 |
+
|
| 1200 |
+
class CoercionTest(SetitemCastingEquivalents):
|
| 1201 |
+
# Tests ported from tests.indexing.test_coercion
|
| 1202 |
+
|
| 1203 |
+
@pytest.fixture
|
| 1204 |
+
def key(self):
|
| 1205 |
+
return 1
|
| 1206 |
+
|
| 1207 |
+
@pytest.fixture
|
| 1208 |
+
def expected(self, obj, key, val, exp_dtype):
|
| 1209 |
+
vals = list(obj)
|
| 1210 |
+
vals[key] = val
|
| 1211 |
+
return Series(vals, dtype=exp_dtype)
|
| 1212 |
+
|
| 1213 |
+
|
| 1214 |
+
@pytest.mark.parametrize(
|
| 1215 |
+
"val,exp_dtype", [(np.int32(1), np.int8), (np.int16(2**9), np.int16)]
|
| 1216 |
+
)
|
| 1217 |
+
class TestCoercionInt8(CoercionTest):
|
| 1218 |
+
# previously test_setitem_series_int8 in tests.indexing.test_coercion
|
| 1219 |
+
@pytest.fixture
|
| 1220 |
+
def obj(self):
|
| 1221 |
+
return Series([1, 2, 3, 4], dtype=np.int8)
|
| 1222 |
+
|
| 1223 |
+
|
| 1224 |
+
@pytest.mark.parametrize("val", [1, 1.1, 1 + 1j, True])
|
| 1225 |
+
@pytest.mark.parametrize("exp_dtype", [object])
|
| 1226 |
+
class TestCoercionObject(CoercionTest):
|
| 1227 |
+
# previously test_setitem_series_object in tests.indexing.test_coercion
|
| 1228 |
+
@pytest.fixture
|
| 1229 |
+
def obj(self):
|
| 1230 |
+
return Series(["a", "b", "c", "d"], dtype=object)
|
| 1231 |
+
|
| 1232 |
+
|
| 1233 |
+
@pytest.mark.parametrize(
|
| 1234 |
+
"val,exp_dtype",
|
| 1235 |
+
[(1, np.complex128), (1.1, np.complex128), (1 + 1j, np.complex128), (True, object)],
|
| 1236 |
+
)
|
| 1237 |
+
class TestCoercionComplex(CoercionTest):
|
| 1238 |
+
# previously test_setitem_series_complex128 in tests.indexing.test_coercion
|
| 1239 |
+
@pytest.fixture
|
| 1240 |
+
def obj(self):
|
| 1241 |
+
return Series([1 + 1j, 2 + 2j, 3 + 3j, 4 + 4j])
|
| 1242 |
+
|
| 1243 |
+
|
| 1244 |
+
@pytest.mark.parametrize(
|
| 1245 |
+
"val,exp_dtype",
|
| 1246 |
+
[
|
| 1247 |
+
(1, object),
|
| 1248 |
+
("3", object),
|
| 1249 |
+
(3, object),
|
| 1250 |
+
(1.1, object),
|
| 1251 |
+
(1 + 1j, object),
|
| 1252 |
+
(True, bool),
|
| 1253 |
+
],
|
| 1254 |
+
)
|
| 1255 |
+
class TestCoercionBool(CoercionTest):
|
| 1256 |
+
# previously test_setitem_series_bool in tests.indexing.test_coercion
|
| 1257 |
+
@pytest.fixture
|
| 1258 |
+
def obj(self):
|
| 1259 |
+
return Series([True, False, True, False], dtype=bool)
|
| 1260 |
+
|
| 1261 |
+
|
| 1262 |
+
@pytest.mark.parametrize(
|
| 1263 |
+
"val,exp_dtype",
|
| 1264 |
+
[(1, np.int64), (1.1, np.float64), (1 + 1j, np.complex128), (True, object)],
|
| 1265 |
+
)
|
| 1266 |
+
class TestCoercionInt64(CoercionTest):
|
| 1267 |
+
# previously test_setitem_series_int64 in tests.indexing.test_coercion
|
| 1268 |
+
@pytest.fixture
|
| 1269 |
+
def obj(self):
|
| 1270 |
+
return Series([1, 2, 3, 4])
|
| 1271 |
+
|
| 1272 |
+
|
| 1273 |
+
@pytest.mark.parametrize(
|
| 1274 |
+
"val,exp_dtype",
|
| 1275 |
+
[(1, np.float64), (1.1, np.float64), (1 + 1j, np.complex128), (True, object)],
|
| 1276 |
+
)
|
| 1277 |
+
class TestCoercionFloat64(CoercionTest):
|
| 1278 |
+
# previously test_setitem_series_float64 in tests.indexing.test_coercion
|
| 1279 |
+
@pytest.fixture
|
| 1280 |
+
def obj(self):
|
| 1281 |
+
return Series([1.1, 2.2, 3.3, 4.4])
|
| 1282 |
+
|
| 1283 |
+
|
| 1284 |
+
@pytest.mark.parametrize(
|
| 1285 |
+
"val,exp_dtype",
|
| 1286 |
+
[
|
| 1287 |
+
(1, np.float32),
|
| 1288 |
+
pytest.param(
|
| 1289 |
+
1.1,
|
| 1290 |
+
np.float32,
|
| 1291 |
+
marks=pytest.mark.xfail(
|
| 1292 |
+
reason="np.float32(1.1) ends up as 1.100000023841858, so "
|
| 1293 |
+
"np_can_hold_element raises and we cast to float64",
|
| 1294 |
+
),
|
| 1295 |
+
),
|
| 1296 |
+
(1 + 1j, np.complex128),
|
| 1297 |
+
(True, object),
|
| 1298 |
+
(np.uint8(2), np.float32),
|
| 1299 |
+
(np.uint32(2), np.float32),
|
| 1300 |
+
# float32 cannot hold np.iinfo(np.uint32).max exactly
|
| 1301 |
+
# (closest it can hold is 4294967300.0 which off by 5.0), so
|
| 1302 |
+
# we cast to float64
|
| 1303 |
+
(np.uint32(np.iinfo(np.uint32).max), np.float64),
|
| 1304 |
+
(np.uint64(2), np.float32),
|
| 1305 |
+
(np.int64(2), np.float32),
|
| 1306 |
+
],
|
| 1307 |
+
)
|
| 1308 |
+
class TestCoercionFloat32(CoercionTest):
|
| 1309 |
+
@pytest.fixture
|
| 1310 |
+
def obj(self):
|
| 1311 |
+
return Series([1.1, 2.2, 3.3, 4.4], dtype=np.float32)
|
| 1312 |
+
|
| 1313 |
+
def test_slice_key(self, obj, key, expected, val, indexer_sli, is_inplace):
|
| 1314 |
+
super().test_slice_key(obj, key, expected, val, indexer_sli, is_inplace)
|
| 1315 |
+
|
| 1316 |
+
if type(val) is float:
|
| 1317 |
+
# the xfail would xpass bc test_slice_key short-circuits
|
| 1318 |
+
raise AssertionError("xfail not relevant for this test.")
|
| 1319 |
+
|
| 1320 |
+
|
| 1321 |
+
@pytest.mark.parametrize(
|
| 1322 |
+
"val,exp_dtype",
|
| 1323 |
+
[(Timestamp("2012-01-01"), "datetime64[ns]"), (1, object), ("x", object)],
|
| 1324 |
+
)
|
| 1325 |
+
class TestCoercionDatetime64(CoercionTest):
|
| 1326 |
+
# previously test_setitem_series_datetime64 in tests.indexing.test_coercion
|
| 1327 |
+
|
| 1328 |
+
@pytest.fixture
|
| 1329 |
+
def obj(self):
|
| 1330 |
+
return Series(date_range("2011-01-01", freq="D", periods=4))
|
| 1331 |
+
|
| 1332 |
+
|
| 1333 |
+
@pytest.mark.parametrize(
|
| 1334 |
+
"val,exp_dtype",
|
| 1335 |
+
[
|
| 1336 |
+
(Timestamp("2012-01-01", tz="US/Eastern"), "datetime64[ns, US/Eastern]"),
|
| 1337 |
+
# pre-2.0, a mis-matched tz would end up casting to object
|
| 1338 |
+
(Timestamp("2012-01-01", tz="US/Pacific"), "datetime64[ns, US/Eastern]"),
|
| 1339 |
+
(Timestamp("2012-01-01"), object),
|
| 1340 |
+
(1, object),
|
| 1341 |
+
],
|
| 1342 |
+
)
|
| 1343 |
+
class TestCoercionDatetime64TZ(CoercionTest):
|
| 1344 |
+
# previously test_setitem_series_datetime64tz in tests.indexing.test_coercion
|
| 1345 |
+
@pytest.fixture
|
| 1346 |
+
def obj(self):
|
| 1347 |
+
tz = "US/Eastern"
|
| 1348 |
+
return Series(date_range("2011-01-01", freq="D", periods=4, tz=tz))
|
| 1349 |
+
|
| 1350 |
+
|
| 1351 |
+
@pytest.mark.parametrize(
|
| 1352 |
+
"val,exp_dtype",
|
| 1353 |
+
[(Timedelta("12 day"), "timedelta64[ns]"), (1, object), ("x", object)],
|
| 1354 |
+
)
|
| 1355 |
+
class TestCoercionTimedelta64(CoercionTest):
|
| 1356 |
+
# previously test_setitem_series_timedelta64 in tests.indexing.test_coercion
|
| 1357 |
+
@pytest.fixture
|
| 1358 |
+
def obj(self):
|
| 1359 |
+
return Series(timedelta_range("1 day", periods=4))
|
| 1360 |
+
|
| 1361 |
+
|
| 1362 |
+
@pytest.mark.parametrize(
|
| 1363 |
+
"val", ["foo", Period("2016", freq="Y"), Interval(1, 2, closed="both")]
|
| 1364 |
+
)
|
| 1365 |
+
@pytest.mark.parametrize("exp_dtype", [object])
|
| 1366 |
+
class TestPeriodIntervalCoercion(CoercionTest):
|
| 1367 |
+
# GH#45768
|
| 1368 |
+
@pytest.fixture(
|
| 1369 |
+
params=[
|
| 1370 |
+
period_range("2016-01-01", periods=3, freq="D"),
|
| 1371 |
+
interval_range(1, 5),
|
| 1372 |
+
]
|
| 1373 |
+
)
|
| 1374 |
+
def obj(self, request):
|
| 1375 |
+
return Series(request.param)
|
| 1376 |
+
|
| 1377 |
+
|
| 1378 |
+
def test_20643():
|
| 1379 |
+
# closed by GH#45121
|
| 1380 |
+
orig = Series([0, 1, 2], index=["a", "b", "c"])
|
| 1381 |
+
|
| 1382 |
+
expected = Series([0, 2.7, 2], index=["a", "b", "c"])
|
| 1383 |
+
|
| 1384 |
+
ser = orig.copy()
|
| 1385 |
+
ser.at["b"] = 2.7
|
| 1386 |
+
tm.assert_series_equal(ser, expected)
|
| 1387 |
+
|
| 1388 |
+
ser = orig.copy()
|
| 1389 |
+
ser.loc["b"] = 2.7
|
| 1390 |
+
tm.assert_series_equal(ser, expected)
|
| 1391 |
+
|
| 1392 |
+
ser = orig.copy()
|
| 1393 |
+
ser["b"] = 2.7
|
| 1394 |
+
tm.assert_series_equal(ser, expected)
|
| 1395 |
+
|
| 1396 |
+
ser = orig.copy()
|
| 1397 |
+
ser.iat[1] = 2.7
|
| 1398 |
+
tm.assert_series_equal(ser, expected)
|
| 1399 |
+
|
| 1400 |
+
ser = orig.copy()
|
| 1401 |
+
ser.iloc[1] = 2.7
|
| 1402 |
+
tm.assert_series_equal(ser, expected)
|
| 1403 |
+
|
| 1404 |
+
orig_df = orig.to_frame("A")
|
| 1405 |
+
expected_df = expected.to_frame("A")
|
| 1406 |
+
|
| 1407 |
+
df = orig_df.copy()
|
| 1408 |
+
df.at["b", "A"] = 2.7
|
| 1409 |
+
tm.assert_frame_equal(df, expected_df)
|
| 1410 |
+
|
| 1411 |
+
df = orig_df.copy()
|
| 1412 |
+
df.loc["b", "A"] = 2.7
|
| 1413 |
+
tm.assert_frame_equal(df, expected_df)
|
| 1414 |
+
|
| 1415 |
+
df = orig_df.copy()
|
| 1416 |
+
df.iloc[1, 0] = 2.7
|
| 1417 |
+
tm.assert_frame_equal(df, expected_df)
|
| 1418 |
+
|
| 1419 |
+
df = orig_df.copy()
|
| 1420 |
+
df.iat[1, 0] = 2.7
|
| 1421 |
+
tm.assert_frame_equal(df, expected_df)
|
| 1422 |
+
|
| 1423 |
+
|
| 1424 |
+
def test_20643_comment():
|
| 1425 |
+
# https://github.com/pandas-dev/pandas/issues/20643#issuecomment-431244590
|
| 1426 |
+
# fixed sometime prior to GH#45121
|
| 1427 |
+
orig = Series([0, 1, 2], index=["a", "b", "c"])
|
| 1428 |
+
expected = Series([np.nan, 1, 2], index=["a", "b", "c"])
|
| 1429 |
+
|
| 1430 |
+
ser = orig.copy()
|
| 1431 |
+
ser.iat[0] = None
|
| 1432 |
+
tm.assert_series_equal(ser, expected)
|
| 1433 |
+
|
| 1434 |
+
ser = orig.copy()
|
| 1435 |
+
ser.iloc[0] = None
|
| 1436 |
+
tm.assert_series_equal(ser, expected)
|
| 1437 |
+
|
| 1438 |
+
|
| 1439 |
+
def test_15413():
|
| 1440 |
+
# fixed by GH#45121
|
| 1441 |
+
ser = Series([1, 2, 3])
|
| 1442 |
+
|
| 1443 |
+
ser[ser == 2] += 0.5
|
| 1444 |
+
expected = Series([1, 2.5, 3])
|
| 1445 |
+
tm.assert_series_equal(ser, expected)
|
| 1446 |
+
|
| 1447 |
+
ser = Series([1, 2, 3])
|
| 1448 |
+
ser[1] += 0.5
|
| 1449 |
+
tm.assert_series_equal(ser, expected)
|
| 1450 |
+
|
| 1451 |
+
ser = Series([1, 2, 3])
|
| 1452 |
+
ser.loc[1] += 0.5
|
| 1453 |
+
tm.assert_series_equal(ser, expected)
|
| 1454 |
+
|
| 1455 |
+
ser = Series([1, 2, 3])
|
| 1456 |
+
ser.iloc[1] += 0.5
|
| 1457 |
+
tm.assert_series_equal(ser, expected)
|
| 1458 |
+
|
| 1459 |
+
ser = Series([1, 2, 3])
|
| 1460 |
+
ser.iat[1] += 0.5
|
| 1461 |
+
tm.assert_series_equal(ser, expected)
|
| 1462 |
+
|
| 1463 |
+
ser = Series([1, 2, 3])
|
| 1464 |
+
ser.at[1] += 0.5
|
| 1465 |
+
tm.assert_series_equal(ser, expected)
|
| 1466 |
+
|
| 1467 |
+
|
| 1468 |
+
def test_32878_int_itemsize():
|
| 1469 |
+
# Fixed by GH#45121
|
| 1470 |
+
arr = np.arange(5).astype("i4")
|
| 1471 |
+
ser = Series(arr)
|
| 1472 |
+
val = np.int64(np.iinfo(np.int64).max)
|
| 1473 |
+
ser[0] = val
|
| 1474 |
+
expected = Series([val, 1, 2, 3, 4], dtype=np.int64)
|
| 1475 |
+
tm.assert_series_equal(ser, expected)
|
| 1476 |
+
|
| 1477 |
+
|
| 1478 |
+
def test_32878_complex_itemsize():
|
| 1479 |
+
arr = np.arange(5).astype("c8")
|
| 1480 |
+
ser = Series(arr)
|
| 1481 |
+
val = np.finfo(np.float64).max
|
| 1482 |
+
val = val.astype("c16")
|
| 1483 |
+
|
| 1484 |
+
# GH#32878 used to coerce val to inf+0.000000e+00j
|
| 1485 |
+
ser[0] = val
|
| 1486 |
+
assert ser[0] == val
|
| 1487 |
+
expected = Series([val, 1, 2, 3, 4], dtype="c16")
|
| 1488 |
+
tm.assert_series_equal(ser, expected)
|
| 1489 |
+
|
| 1490 |
+
|
| 1491 |
+
def test_37692(indexer_al):
|
| 1492 |
+
# GH#37692
|
| 1493 |
+
ser = Series([1, 2, 3], index=["a", "b", "c"])
|
| 1494 |
+
indexer_al(ser)["b"] = "test"
|
| 1495 |
+
expected = Series([1, "test", 3], index=["a", "b", "c"], dtype=object)
|
| 1496 |
+
tm.assert_series_equal(ser, expected)
|
| 1497 |
+
|
| 1498 |
+
|
| 1499 |
+
def test_setitem_bool_int_float_consistency(indexer_sli):
|
| 1500 |
+
# GH#21513
|
| 1501 |
+
# bool-with-int and bool-with-float both upcast to object
|
| 1502 |
+
# int-with-float and float-with-int are both non-casting so long
|
| 1503 |
+
# as the setitem can be done losslessly
|
| 1504 |
+
for dtype in [np.float64, np.int64]:
|
| 1505 |
+
ser = Series(0, index=range(3), dtype=dtype)
|
| 1506 |
+
indexer_sli(ser)[0] = True
|
| 1507 |
+
assert ser.dtype == object
|
| 1508 |
+
|
| 1509 |
+
ser = Series(0, index=range(3), dtype=bool)
|
| 1510 |
+
ser[0] = dtype(1)
|
| 1511 |
+
assert ser.dtype == object
|
| 1512 |
+
|
| 1513 |
+
# 1.0 can be held losslessly, so no casting
|
| 1514 |
+
ser = Series(0, index=range(3), dtype=np.int64)
|
| 1515 |
+
indexer_sli(ser)[0] = np.float64(1.0)
|
| 1516 |
+
assert ser.dtype == np.int64
|
| 1517 |
+
|
| 1518 |
+
# 1 can be held losslessly, so no casting
|
| 1519 |
+
ser = Series(0, index=range(3), dtype=np.float64)
|
| 1520 |
+
indexer_sli(ser)[0] = np.int64(1)
|
| 1521 |
+
|
| 1522 |
+
|
| 1523 |
+
def test_setitem_positional_with_casting():
|
| 1524 |
+
# GH#45070 case where in __setitem__ we get a KeyError, then when
|
| 1525 |
+
# we fallback we *also* get a ValueError if we try to set inplace.
|
| 1526 |
+
ser = Series([1, 2, 3], index=["a", "b", "c"])
|
| 1527 |
+
|
| 1528 |
+
ser[0] = "X"
|
| 1529 |
+
expected = Series(["X", 2, 3], index=["a", "b", "c"], dtype=object)
|
| 1530 |
+
tm.assert_series_equal(ser, expected)
|
| 1531 |
+
|
| 1532 |
+
|
| 1533 |
+
def test_setitem_positional_float_into_int_coerces():
|
| 1534 |
+
# Case where we hit a KeyError and then trying to set in-place incorrectly
|
| 1535 |
+
# casts a float to an int
|
| 1536 |
+
ser = Series([1, 2, 3], index=["a", "b", "c"])
|
| 1537 |
+
ser[0] = 1.5
|
| 1538 |
+
expected = Series([1.5, 2, 3], index=["a", "b", "c"])
|
| 1539 |
+
tm.assert_series_equal(ser, expected)
|
| 1540 |
+
|
| 1541 |
+
|
| 1542 |
+
def test_setitem_int_not_positional():
|
| 1543 |
+
# GH#42215 deprecated falling back to positional on __setitem__ with an
|
| 1544 |
+
# int not contained in the index; enforced in 2.0
|
| 1545 |
+
ser = Series([1, 2, 3, 4], index=[1.1, 2.1, 3.0, 4.1])
|
| 1546 |
+
assert not ser.index._should_fallback_to_positional
|
| 1547 |
+
# assert not ser.index.astype(object)._should_fallback_to_positional
|
| 1548 |
+
|
| 1549 |
+
# 3.0 is in our index, so post-enforcement behavior is unchanged
|
| 1550 |
+
ser[3] = 10
|
| 1551 |
+
expected = Series([1, 2, 10, 4], index=ser.index)
|
| 1552 |
+
tm.assert_series_equal(ser, expected)
|
| 1553 |
+
|
| 1554 |
+
# pre-enforcement `ser[5] = 5` raised IndexError
|
| 1555 |
+
ser[5] = 5
|
| 1556 |
+
expected = Series([1, 2, 10, 4, 5], index=[1.1, 2.1, 3.0, 4.1, 5.0])
|
| 1557 |
+
tm.assert_series_equal(ser, expected)
|
| 1558 |
+
|
| 1559 |
+
ii = IntervalIndex.from_breaks(range(10))[::2]
|
| 1560 |
+
ser2 = Series(range(len(ii)), index=ii)
|
| 1561 |
+
exp_index = ii.astype(object).append(Index([4]))
|
| 1562 |
+
expected2 = Series([0, 1, 2, 3, 4, 9], index=exp_index)
|
| 1563 |
+
# pre-enforcement `ser2[4] = 9` interpreted 4 as positional
|
| 1564 |
+
ser2[4] = 9
|
| 1565 |
+
tm.assert_series_equal(ser2, expected2)
|
| 1566 |
+
|
| 1567 |
+
mi = MultiIndex.from_product([ser.index, ["A", "B"]])
|
| 1568 |
+
ser3 = Series(range(len(mi)), index=mi)
|
| 1569 |
+
expected3 = ser3.copy()
|
| 1570 |
+
expected3.loc[4] = 99
|
| 1571 |
+
# pre-enforcement `ser3[4] = 99` interpreted 4 as positional
|
| 1572 |
+
ser3[4] = 99
|
| 1573 |
+
tm.assert_series_equal(ser3, expected3)
|
| 1574 |
+
|
| 1575 |
+
|
| 1576 |
+
def test_setitem_with_bool_indexer():
|
| 1577 |
+
# GH#42530
|
| 1578 |
+
|
| 1579 |
+
df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
|
| 1580 |
+
result = df.pop("b")
|
| 1581 |
+
result[[True, False, False]] = 9
|
| 1582 |
+
expected = Series(data=[9, 5, 6], name="b")
|
| 1583 |
+
tm.assert_series_equal(result, expected)
|
| 1584 |
+
|
| 1585 |
+
df.loc[[True, False, False], "a"] = 10
|
| 1586 |
+
expected = DataFrame({"a": [10, 2, 3]})
|
| 1587 |
+
tm.assert_frame_equal(df, expected)
|
| 1588 |
+
|
| 1589 |
+
|
| 1590 |
+
@pytest.mark.parametrize("size", range(2, 6))
|
| 1591 |
+
@pytest.mark.parametrize(
|
| 1592 |
+
"mask", [[True, False, False, False, False], [True, False], [False]]
|
| 1593 |
+
)
|
| 1594 |
+
@pytest.mark.parametrize(
|
| 1595 |
+
"item", [2.0, np.nan, np.finfo(float).max, np.finfo(float).min]
|
| 1596 |
+
)
|
| 1597 |
+
# Test numpy arrays, lists and tuples as the input to be
|
| 1598 |
+
# broadcast
|
| 1599 |
+
@pytest.mark.parametrize(
|
| 1600 |
+
"box", [lambda x: np.array([x]), lambda x: [x], lambda x: (x,)]
|
| 1601 |
+
)
|
| 1602 |
+
def test_setitem_bool_indexer_dont_broadcast_length1_values(size, mask, item, box):
|
| 1603 |
+
# GH#44265
|
| 1604 |
+
# see also tests.series.indexing.test_where.test_broadcast
|
| 1605 |
+
|
| 1606 |
+
selection = np.resize(mask, size)
|
| 1607 |
+
|
| 1608 |
+
data = np.arange(size, dtype=float)
|
| 1609 |
+
|
| 1610 |
+
ser = Series(data)
|
| 1611 |
+
|
| 1612 |
+
if selection.sum() != 1:
|
| 1613 |
+
msg = (
|
| 1614 |
+
"cannot set using a list-like indexer with a different "
|
| 1615 |
+
"length than the value"
|
| 1616 |
+
)
|
| 1617 |
+
with pytest.raises(ValueError, match=msg):
|
| 1618 |
+
# GH#44265
|
| 1619 |
+
ser[selection] = box(item)
|
| 1620 |
+
else:
|
| 1621 |
+
# In this corner case setting is equivalent to setting with the unboxed
|
| 1622 |
+
# item
|
| 1623 |
+
ser[selection] = box(item)
|
| 1624 |
+
|
| 1625 |
+
expected = Series(np.arange(size, dtype=float))
|
| 1626 |
+
expected[selection] = item
|
| 1627 |
+
tm.assert_series_equal(ser, expected)
|
| 1628 |
+
|
| 1629 |
+
|
| 1630 |
+
def test_setitem_empty_mask_dont_upcast_dt64():
|
| 1631 |
+
dti = date_range("2016-01-01", periods=3)
|
| 1632 |
+
ser = Series(dti)
|
| 1633 |
+
orig = ser.copy()
|
| 1634 |
+
mask = np.zeros(3, dtype=bool)
|
| 1635 |
+
|
| 1636 |
+
ser[mask] = "foo"
|
| 1637 |
+
assert ser.dtype == dti.dtype # no-op -> dont upcast
|
| 1638 |
+
tm.assert_series_equal(ser, orig)
|
| 1639 |
+
|
| 1640 |
+
ser.mask(mask, "foo", inplace=True)
|
| 1641 |
+
assert ser.dtype == dti.dtype # no-op -> dont upcast
|
| 1642 |
+
tm.assert_series_equal(ser, orig)
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/indexing/test_where.py
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas.core.dtypes.common import is_integer
|
| 5 |
+
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from pandas import (
|
| 8 |
+
Series,
|
| 9 |
+
Timestamp,
|
| 10 |
+
date_range,
|
| 11 |
+
isna,
|
| 12 |
+
)
|
| 13 |
+
import pandas._testing as tm
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_where_unsafe_int(any_signed_int_numpy_dtype):
|
| 17 |
+
s = Series(np.arange(10), dtype=any_signed_int_numpy_dtype)
|
| 18 |
+
mask = s < 5
|
| 19 |
+
|
| 20 |
+
s[mask] = range(2, 7)
|
| 21 |
+
expected = Series(
|
| 22 |
+
list(range(2, 7)) + list(range(5, 10)),
|
| 23 |
+
dtype=any_signed_int_numpy_dtype,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
tm.assert_series_equal(s, expected)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_where_unsafe_float(float_numpy_dtype):
|
| 30 |
+
s = Series(np.arange(10), dtype=float_numpy_dtype)
|
| 31 |
+
mask = s < 5
|
| 32 |
+
|
| 33 |
+
s[mask] = range(2, 7)
|
| 34 |
+
data = list(range(2, 7)) + list(range(5, 10))
|
| 35 |
+
expected = Series(data, dtype=float_numpy_dtype)
|
| 36 |
+
|
| 37 |
+
tm.assert_series_equal(s, expected)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@pytest.mark.parametrize(
|
| 41 |
+
"dtype,expected_dtype",
|
| 42 |
+
[
|
| 43 |
+
(np.int8, np.float64),
|
| 44 |
+
(np.int16, np.float64),
|
| 45 |
+
(np.int32, np.float64),
|
| 46 |
+
(np.int64, np.float64),
|
| 47 |
+
(np.float32, np.float32),
|
| 48 |
+
(np.float64, np.float64),
|
| 49 |
+
],
|
| 50 |
+
)
|
| 51 |
+
def test_where_unsafe_upcast(dtype, expected_dtype):
|
| 52 |
+
# see gh-9743
|
| 53 |
+
s = Series(np.arange(10), dtype=dtype)
|
| 54 |
+
values = [2.5, 3.5, 4.5, 5.5, 6.5]
|
| 55 |
+
mask = s < 5
|
| 56 |
+
expected = Series(values + list(range(5, 10)), dtype=expected_dtype)
|
| 57 |
+
s[mask] = values
|
| 58 |
+
tm.assert_series_equal(s, expected)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def test_where_unsafe():
|
| 62 |
+
# see gh-9731
|
| 63 |
+
s = Series(np.arange(10), dtype="int64")
|
| 64 |
+
values = [2.5, 3.5, 4.5, 5.5]
|
| 65 |
+
|
| 66 |
+
mask = s > 5
|
| 67 |
+
expected = Series(list(range(6)) + values, dtype="float64")
|
| 68 |
+
|
| 69 |
+
s[mask] = values
|
| 70 |
+
tm.assert_series_equal(s, expected)
|
| 71 |
+
|
| 72 |
+
# see gh-3235
|
| 73 |
+
s = Series(np.arange(10), dtype="int64")
|
| 74 |
+
mask = s < 5
|
| 75 |
+
s[mask] = range(2, 7)
|
| 76 |
+
expected = Series(list(range(2, 7)) + list(range(5, 10)), dtype="int64")
|
| 77 |
+
tm.assert_series_equal(s, expected)
|
| 78 |
+
assert s.dtype == expected.dtype
|
| 79 |
+
|
| 80 |
+
s = Series(np.arange(10), dtype="int64")
|
| 81 |
+
mask = s > 5
|
| 82 |
+
s[mask] = [0] * 4
|
| 83 |
+
expected = Series([0, 1, 2, 3, 4, 5] + [0] * 4, dtype="int64")
|
| 84 |
+
tm.assert_series_equal(s, expected)
|
| 85 |
+
|
| 86 |
+
s = Series(np.arange(10))
|
| 87 |
+
mask = s > 5
|
| 88 |
+
|
| 89 |
+
msg = "cannot set using a list-like indexer with a different length than the value"
|
| 90 |
+
with pytest.raises(ValueError, match=msg):
|
| 91 |
+
s[mask] = [5, 4, 3, 2, 1]
|
| 92 |
+
|
| 93 |
+
with pytest.raises(ValueError, match=msg):
|
| 94 |
+
s[mask] = [0] * 5
|
| 95 |
+
|
| 96 |
+
# dtype changes
|
| 97 |
+
s = Series([1, 2, 3, 4])
|
| 98 |
+
result = s.where(s > 2, np.nan)
|
| 99 |
+
expected = Series([np.nan, np.nan, 3, 4])
|
| 100 |
+
tm.assert_series_equal(result, expected)
|
| 101 |
+
|
| 102 |
+
# GH 4667
|
| 103 |
+
# setting with None changes dtype
|
| 104 |
+
s = Series(range(10)).astype(float)
|
| 105 |
+
s[8] = None
|
| 106 |
+
result = s[8]
|
| 107 |
+
assert isna(result)
|
| 108 |
+
|
| 109 |
+
s = Series(range(10)).astype(float)
|
| 110 |
+
s[s > 8] = None
|
| 111 |
+
result = s[isna(s)]
|
| 112 |
+
expected = Series(np.nan, index=[9])
|
| 113 |
+
tm.assert_series_equal(result, expected)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def test_where():
|
| 117 |
+
s = Series(np.random.randn(5))
|
| 118 |
+
cond = s > 0
|
| 119 |
+
|
| 120 |
+
rs = s.where(cond).dropna()
|
| 121 |
+
rs2 = s[cond]
|
| 122 |
+
tm.assert_series_equal(rs, rs2)
|
| 123 |
+
|
| 124 |
+
rs = s.where(cond, -s)
|
| 125 |
+
tm.assert_series_equal(rs, s.abs())
|
| 126 |
+
|
| 127 |
+
rs = s.where(cond)
|
| 128 |
+
assert s.shape == rs.shape
|
| 129 |
+
assert rs is not s
|
| 130 |
+
|
| 131 |
+
# test alignment
|
| 132 |
+
cond = Series([True, False, False, True, False], index=s.index)
|
| 133 |
+
s2 = -(s.abs())
|
| 134 |
+
|
| 135 |
+
expected = s2[cond].reindex(s2.index[:3]).reindex(s2.index)
|
| 136 |
+
rs = s2.where(cond[:3])
|
| 137 |
+
tm.assert_series_equal(rs, expected)
|
| 138 |
+
|
| 139 |
+
expected = s2.abs()
|
| 140 |
+
expected.iloc[0] = s2[0]
|
| 141 |
+
rs = s2.where(cond[:3], -s2)
|
| 142 |
+
tm.assert_series_equal(rs, expected)
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def test_where_error():
|
| 146 |
+
s = Series(np.random.randn(5))
|
| 147 |
+
cond = s > 0
|
| 148 |
+
|
| 149 |
+
msg = "Array conditional must be same shape as self"
|
| 150 |
+
with pytest.raises(ValueError, match=msg):
|
| 151 |
+
s.where(1)
|
| 152 |
+
with pytest.raises(ValueError, match=msg):
|
| 153 |
+
s.where(cond[:3].values, -s)
|
| 154 |
+
|
| 155 |
+
# GH 2745
|
| 156 |
+
s = Series([1, 2])
|
| 157 |
+
s[[True, False]] = [0, 1]
|
| 158 |
+
expected = Series([0, 2])
|
| 159 |
+
tm.assert_series_equal(s, expected)
|
| 160 |
+
|
| 161 |
+
# failures
|
| 162 |
+
msg = "cannot set using a list-like indexer with a different length than the value"
|
| 163 |
+
with pytest.raises(ValueError, match=msg):
|
| 164 |
+
s[[True, False]] = [0, 2, 3]
|
| 165 |
+
|
| 166 |
+
with pytest.raises(ValueError, match=msg):
|
| 167 |
+
s[[True, False]] = []
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
@pytest.mark.parametrize("klass", [list, tuple, np.array, Series])
|
| 171 |
+
def test_where_array_like(klass):
|
| 172 |
+
# see gh-15414
|
| 173 |
+
s = Series([1, 2, 3])
|
| 174 |
+
cond = [False, True, True]
|
| 175 |
+
expected = Series([np.nan, 2, 3])
|
| 176 |
+
|
| 177 |
+
result = s.where(klass(cond))
|
| 178 |
+
tm.assert_series_equal(result, expected)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
@pytest.mark.parametrize(
|
| 182 |
+
"cond",
|
| 183 |
+
[
|
| 184 |
+
[1, 0, 1],
|
| 185 |
+
Series([2, 5, 7]),
|
| 186 |
+
["True", "False", "True"],
|
| 187 |
+
[Timestamp("2017-01-01"), pd.NaT, Timestamp("2017-01-02")],
|
| 188 |
+
],
|
| 189 |
+
)
|
| 190 |
+
def test_where_invalid_input(cond):
|
| 191 |
+
# see gh-15414: only boolean arrays accepted
|
| 192 |
+
s = Series([1, 2, 3])
|
| 193 |
+
msg = "Boolean array expected for the condition"
|
| 194 |
+
|
| 195 |
+
with pytest.raises(ValueError, match=msg):
|
| 196 |
+
s.where(cond)
|
| 197 |
+
|
| 198 |
+
msg = "Array conditional must be same shape as self"
|
| 199 |
+
with pytest.raises(ValueError, match=msg):
|
| 200 |
+
s.where([True])
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def test_where_ndframe_align():
|
| 204 |
+
msg = "Array conditional must be same shape as self"
|
| 205 |
+
s = Series([1, 2, 3])
|
| 206 |
+
|
| 207 |
+
cond = [True]
|
| 208 |
+
with pytest.raises(ValueError, match=msg):
|
| 209 |
+
s.where(cond)
|
| 210 |
+
|
| 211 |
+
expected = Series([1, np.nan, np.nan])
|
| 212 |
+
|
| 213 |
+
out = s.where(Series(cond))
|
| 214 |
+
tm.assert_series_equal(out, expected)
|
| 215 |
+
|
| 216 |
+
cond = np.array([False, True, False, True])
|
| 217 |
+
with pytest.raises(ValueError, match=msg):
|
| 218 |
+
s.where(cond)
|
| 219 |
+
|
| 220 |
+
expected = Series([np.nan, 2, np.nan])
|
| 221 |
+
|
| 222 |
+
out = s.where(Series(cond))
|
| 223 |
+
tm.assert_series_equal(out, expected)
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
def test_where_setitem_invalid():
|
| 227 |
+
# GH 2702
|
| 228 |
+
# make sure correct exceptions are raised on invalid list assignment
|
| 229 |
+
|
| 230 |
+
msg = (
|
| 231 |
+
lambda x: f"cannot set using a {x} indexer with a "
|
| 232 |
+
"different length than the value"
|
| 233 |
+
)
|
| 234 |
+
# slice
|
| 235 |
+
s = Series(list("abc"))
|
| 236 |
+
|
| 237 |
+
with pytest.raises(ValueError, match=msg("slice")):
|
| 238 |
+
s[0:3] = list(range(27))
|
| 239 |
+
|
| 240 |
+
s[0:3] = list(range(3))
|
| 241 |
+
expected = Series([0, 1, 2])
|
| 242 |
+
tm.assert_series_equal(s.astype(np.int64), expected)
|
| 243 |
+
|
| 244 |
+
# slice with step
|
| 245 |
+
s = Series(list("abcdef"))
|
| 246 |
+
|
| 247 |
+
with pytest.raises(ValueError, match=msg("slice")):
|
| 248 |
+
s[0:4:2] = list(range(27))
|
| 249 |
+
|
| 250 |
+
s = Series(list("abcdef"))
|
| 251 |
+
s[0:4:2] = list(range(2))
|
| 252 |
+
expected = Series([0, "b", 1, "d", "e", "f"])
|
| 253 |
+
tm.assert_series_equal(s, expected)
|
| 254 |
+
|
| 255 |
+
# neg slices
|
| 256 |
+
s = Series(list("abcdef"))
|
| 257 |
+
|
| 258 |
+
with pytest.raises(ValueError, match=msg("slice")):
|
| 259 |
+
s[:-1] = list(range(27))
|
| 260 |
+
|
| 261 |
+
s[-3:-1] = list(range(2))
|
| 262 |
+
expected = Series(["a", "b", "c", 0, 1, "f"])
|
| 263 |
+
tm.assert_series_equal(s, expected)
|
| 264 |
+
|
| 265 |
+
# list
|
| 266 |
+
s = Series(list("abc"))
|
| 267 |
+
|
| 268 |
+
with pytest.raises(ValueError, match=msg("list-like")):
|
| 269 |
+
s[[0, 1, 2]] = list(range(27))
|
| 270 |
+
|
| 271 |
+
s = Series(list("abc"))
|
| 272 |
+
|
| 273 |
+
with pytest.raises(ValueError, match=msg("list-like")):
|
| 274 |
+
s[[0, 1, 2]] = list(range(2))
|
| 275 |
+
|
| 276 |
+
# scalar
|
| 277 |
+
s = Series(list("abc"))
|
| 278 |
+
s[0] = list(range(10))
|
| 279 |
+
expected = Series([list(range(10)), "b", "c"])
|
| 280 |
+
tm.assert_series_equal(s, expected)
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
@pytest.mark.parametrize("size", range(2, 6))
|
| 284 |
+
@pytest.mark.parametrize(
|
| 285 |
+
"mask", [[True, False, False, False, False], [True, False], [False]]
|
| 286 |
+
)
|
| 287 |
+
@pytest.mark.parametrize(
|
| 288 |
+
"item", [2.0, np.nan, np.finfo(float).max, np.finfo(float).min]
|
| 289 |
+
)
|
| 290 |
+
# Test numpy arrays, lists and tuples as the input to be
|
| 291 |
+
# broadcast
|
| 292 |
+
@pytest.mark.parametrize(
|
| 293 |
+
"box", [lambda x: np.array([x]), lambda x: [x], lambda x: (x,)]
|
| 294 |
+
)
|
| 295 |
+
def test_broadcast(size, mask, item, box):
|
| 296 |
+
# GH#8801, GH#4195
|
| 297 |
+
selection = np.resize(mask, size)
|
| 298 |
+
|
| 299 |
+
data = np.arange(size, dtype=float)
|
| 300 |
+
|
| 301 |
+
# Construct the expected series by taking the source
|
| 302 |
+
# data or item based on the selection
|
| 303 |
+
expected = Series(
|
| 304 |
+
[item if use_item else data[i] for i, use_item in enumerate(selection)]
|
| 305 |
+
)
|
| 306 |
+
|
| 307 |
+
s = Series(data)
|
| 308 |
+
|
| 309 |
+
s[selection] = item
|
| 310 |
+
tm.assert_series_equal(s, expected)
|
| 311 |
+
|
| 312 |
+
s = Series(data)
|
| 313 |
+
result = s.where(~selection, box(item))
|
| 314 |
+
tm.assert_series_equal(result, expected)
|
| 315 |
+
|
| 316 |
+
s = Series(data)
|
| 317 |
+
result = s.mask(selection, box(item))
|
| 318 |
+
tm.assert_series_equal(result, expected)
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
def test_where_inplace():
|
| 322 |
+
s = Series(np.random.randn(5))
|
| 323 |
+
cond = s > 0
|
| 324 |
+
|
| 325 |
+
rs = s.copy()
|
| 326 |
+
|
| 327 |
+
rs.where(cond, inplace=True)
|
| 328 |
+
tm.assert_series_equal(rs.dropna(), s[cond])
|
| 329 |
+
tm.assert_series_equal(rs, s.where(cond))
|
| 330 |
+
|
| 331 |
+
rs = s.copy()
|
| 332 |
+
rs.where(cond, -s, inplace=True)
|
| 333 |
+
tm.assert_series_equal(rs, s.where(cond, -s))
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
def test_where_dups():
|
| 337 |
+
# GH 4550
|
| 338 |
+
# where crashes with dups in index
|
| 339 |
+
s1 = Series(list(range(3)))
|
| 340 |
+
s2 = Series(list(range(3)))
|
| 341 |
+
comb = pd.concat([s1, s2])
|
| 342 |
+
result = comb.where(comb < 2)
|
| 343 |
+
expected = Series([0, 1, np.nan, 0, 1, np.nan], index=[0, 1, 2, 0, 1, 2])
|
| 344 |
+
tm.assert_series_equal(result, expected)
|
| 345 |
+
|
| 346 |
+
# GH 4548
|
| 347 |
+
# inplace updating not working with dups
|
| 348 |
+
comb[comb < 1] = 5
|
| 349 |
+
expected = Series([5, 1, 2, 5, 1, 2], index=[0, 1, 2, 0, 1, 2])
|
| 350 |
+
tm.assert_series_equal(comb, expected)
|
| 351 |
+
|
| 352 |
+
comb[comb < 2] += 10
|
| 353 |
+
expected = Series([5, 11, 2, 5, 11, 2], index=[0, 1, 2, 0, 1, 2])
|
| 354 |
+
tm.assert_series_equal(comb, expected)
|
| 355 |
+
|
| 356 |
+
|
| 357 |
+
def test_where_numeric_with_string():
|
| 358 |
+
# GH 9280
|
| 359 |
+
s = Series([1, 2, 3])
|
| 360 |
+
w = s.where(s > 1, "X")
|
| 361 |
+
|
| 362 |
+
assert not is_integer(w[0])
|
| 363 |
+
assert is_integer(w[1])
|
| 364 |
+
assert is_integer(w[2])
|
| 365 |
+
assert isinstance(w[0], str)
|
| 366 |
+
assert w.dtype == "object"
|
| 367 |
+
|
| 368 |
+
w = s.where(s > 1, ["X", "Y", "Z"])
|
| 369 |
+
assert not is_integer(w[0])
|
| 370 |
+
assert is_integer(w[1])
|
| 371 |
+
assert is_integer(w[2])
|
| 372 |
+
assert isinstance(w[0], str)
|
| 373 |
+
assert w.dtype == "object"
|
| 374 |
+
|
| 375 |
+
w = s.where(s > 1, np.array(["X", "Y", "Z"]))
|
| 376 |
+
assert not is_integer(w[0])
|
| 377 |
+
assert is_integer(w[1])
|
| 378 |
+
assert is_integer(w[2])
|
| 379 |
+
assert isinstance(w[0], str)
|
| 380 |
+
assert w.dtype == "object"
|
| 381 |
+
|
| 382 |
+
|
| 383 |
+
@pytest.mark.parametrize("dtype", ["timedelta64[ns]", "datetime64[ns]"])
|
| 384 |
+
def test_where_datetimelike_coerce(dtype):
|
| 385 |
+
ser = Series([1, 2], dtype=dtype)
|
| 386 |
+
expected = Series([10, 10])
|
| 387 |
+
mask = np.array([False, False])
|
| 388 |
+
|
| 389 |
+
rs = ser.where(mask, [10, 10])
|
| 390 |
+
tm.assert_series_equal(rs, expected)
|
| 391 |
+
|
| 392 |
+
rs = ser.where(mask, 10)
|
| 393 |
+
tm.assert_series_equal(rs, expected)
|
| 394 |
+
|
| 395 |
+
rs = ser.where(mask, 10.0)
|
| 396 |
+
tm.assert_series_equal(rs, expected)
|
| 397 |
+
|
| 398 |
+
rs = ser.where(mask, [10.0, 10.0])
|
| 399 |
+
tm.assert_series_equal(rs, expected)
|
| 400 |
+
|
| 401 |
+
rs = ser.where(mask, [10.0, np.nan])
|
| 402 |
+
expected = Series([10, None], dtype="object")
|
| 403 |
+
tm.assert_series_equal(rs, expected)
|
| 404 |
+
|
| 405 |
+
|
| 406 |
+
def test_where_datetimetz():
|
| 407 |
+
# GH 15701
|
| 408 |
+
timestamps = ["2016-12-31 12:00:04+00:00", "2016-12-31 12:00:04.010000+00:00"]
|
| 409 |
+
ser = Series([Timestamp(t) for t in timestamps], dtype="datetime64[ns, UTC]")
|
| 410 |
+
rs = ser.where(Series([False, True]))
|
| 411 |
+
expected = Series([pd.NaT, ser[1]], dtype="datetime64[ns, UTC]")
|
| 412 |
+
tm.assert_series_equal(rs, expected)
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
def test_where_sparse():
|
| 416 |
+
# GH#17198 make sure we dont get an AttributeError for sp_index
|
| 417 |
+
ser = Series(pd.arrays.SparseArray([1, 2]))
|
| 418 |
+
result = ser.where(ser >= 2, 0)
|
| 419 |
+
expected = Series(pd.arrays.SparseArray([0, 2]))
|
| 420 |
+
tm.assert_series_equal(result, expected)
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
def test_where_empty_series_and_empty_cond_having_non_bool_dtypes():
|
| 424 |
+
# https://github.com/pandas-dev/pandas/issues/34592
|
| 425 |
+
ser = Series([], dtype=float)
|
| 426 |
+
result = ser.where([])
|
| 427 |
+
tm.assert_series_equal(result, ser)
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
def test_where_categorical(frame_or_series):
|
| 431 |
+
# https://github.com/pandas-dev/pandas/issues/18888
|
| 432 |
+
exp = frame_or_series(
|
| 433 |
+
pd.Categorical(["A", "A", "B", "B", np.nan], categories=["A", "B", "C"]),
|
| 434 |
+
dtype="category",
|
| 435 |
+
)
|
| 436 |
+
df = frame_or_series(["A", "A", "B", "B", "C"], dtype="category")
|
| 437 |
+
res = df.where(df != "C")
|
| 438 |
+
tm.assert_equal(exp, res)
|
| 439 |
+
|
| 440 |
+
|
| 441 |
+
def test_where_datetimelike_categorical(tz_naive_fixture):
|
| 442 |
+
# GH#37682
|
| 443 |
+
tz = tz_naive_fixture
|
| 444 |
+
|
| 445 |
+
dr = date_range("2001-01-01", periods=3, tz=tz)._with_freq(None)
|
| 446 |
+
lvals = pd.DatetimeIndex([dr[0], dr[1], pd.NaT])
|
| 447 |
+
rvals = pd.Categorical([dr[0], pd.NaT, dr[2]])
|
| 448 |
+
|
| 449 |
+
mask = np.array([True, True, False])
|
| 450 |
+
|
| 451 |
+
# DatetimeIndex.where
|
| 452 |
+
res = lvals.where(mask, rvals)
|
| 453 |
+
tm.assert_index_equal(res, dr)
|
| 454 |
+
|
| 455 |
+
# DatetimeArray.where
|
| 456 |
+
res = lvals._data._where(mask, rvals)
|
| 457 |
+
tm.assert_datetime_array_equal(res, dr._data)
|
| 458 |
+
|
| 459 |
+
# Series.where
|
| 460 |
+
res = Series(lvals).where(mask, rvals)
|
| 461 |
+
tm.assert_series_equal(res, Series(dr))
|
| 462 |
+
|
| 463 |
+
# DataFrame.where
|
| 464 |
+
res = pd.DataFrame(lvals).where(mask[:, None], pd.DataFrame(rvals))
|
| 465 |
+
|
| 466 |
+
tm.assert_frame_equal(res, pd.DataFrame(dr))
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (413 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_add_prefix_suffix.cpython-310.pyc
ADDED
|
Binary file (1.99 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_align.cpython-310.pyc
ADDED
|
Binary file (5.65 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_argsort.cpython-310.pyc
ADDED
|
Binary file (2.58 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_asof.cpython-310.pyc
ADDED
|
Binary file (5.55 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_astype.cpython-310.pyc
ADDED
|
Binary file (20.8 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_autocorr.cpython-310.pyc
ADDED
|
Binary file (824 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_between.cpython-310.pyc
ADDED
|
Binary file (2.64 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_clip.cpython-310.pyc
ADDED
|
Binary file (4.21 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_combine.cpython-310.pyc
ADDED
|
Binary file (1.4 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_combine_first.cpython-310.pyc
ADDED
|
Binary file (3.69 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_compare.cpython-310.pyc
ADDED
|
Binary file (3.89 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_convert_dtypes.cpython-310.pyc
ADDED
|
Binary file (5.36 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_copy.cpython-310.pyc
ADDED
|
Binary file (2.06 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_count.cpython-310.pyc
ADDED
|
Binary file (1.16 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_describe.cpython-310.pyc
ADDED
|
Binary file (5.44 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_diff.cpython-310.pyc
ADDED
|
Binary file (2.6 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_drop.cpython-310.pyc
ADDED
|
Binary file (3.24 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_drop_duplicates.cpython-310.pyc
ADDED
|
Binary file (6.05 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_dropna.cpython-310.pyc
ADDED
|
Binary file (3.28 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_dtypes.cpython-310.pyc
ADDED
|
Binary file (587 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_duplicated.cpython-310.pyc
ADDED
|
Binary file (1.94 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_equals.cpython-310.pyc
ADDED
|
Binary file (3.64 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_explode.cpython-310.pyc
ADDED
|
Binary file (4.8 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_fillna.cpython-310.pyc
ADDED
|
Binary file (22 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_get_numeric_data.cpython-310.pyc
ADDED
|
Binary file (1.15 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_head_tail.cpython-310.pyc
ADDED
|
Binary file (511 Bytes). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_infer_objects.cpython-310.pyc
ADDED
|
Binary file (1.94 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_interpolate.cpython-310.pyc
ADDED
|
Binary file (25.3 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_is_monotonic.cpython-310.pyc
ADDED
|
Binary file (1.14 kB). View file
|
|
|
videochat2/lib/python3.10/site-packages/pandas/tests/series/methods/__pycache__/test_isin.cpython-310.pyc
ADDED
|
Binary file (7.33 kB). View file
|
|
|