Spaces:
Configuration error
Configuration error
Upload folder using huggingface_hub (part 12)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .venv/lib/python3.13/site-packages/pandas/tests/groupby/test_timegrouper.py +984 -0
- .venv/lib/python3.13/site-packages/pandas/tests/groupby/transform/__init__.py +0 -0
- .venv/lib/python3.13/site-packages/pandas/tests/groupby/transform/test_numba.py +331 -0
- .venv/lib/python3.13/site-packages/pandas/tests/groupby/transform/test_transform.py +1600 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/__init__.py +0 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/__init__.py +0 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_constructors.py +71 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_formats.py +154 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_indexing.py +104 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_pickle.py +11 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_reshape.py +95 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_setops.py +266 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_where.py +13 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/__init__.py +0 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_append.py +64 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_astype.py +95 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_category.py +406 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_constructors.py +149 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_equals.py +96 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_fillna.py +54 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_formats.py +114 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_indexing.py +418 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_map.py +144 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_reindex.py +78 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_setops.py +18 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/conftest.py +40 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/__init__.py +0 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_drop_duplicates.py +101 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_equals.py +185 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_indexing.py +45 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_is_monotonic.py +46 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_nat.py +42 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_sort_values.py +311 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_value_counts.py +103 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/__init__.py +0 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/__init__.py +0 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_asof.py +46 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_astype.py +341 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_delete.py +141 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_factorize.py +125 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_fillna.py +62 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_insert.py +279 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_isocalendar.py +28 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_map.py +47 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_normalize.py +99 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_repeat.py +83 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_resolution.py +35 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_round.py +221 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_shift.py +181 -0
- .venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_snap.py +54 -0
.venv/lib/python3.13/site-packages/pandas/tests/groupby/test_timegrouper.py
ADDED
|
@@ -0,0 +1,984 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
test with the TimeGrouper / grouping with datetimes
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from datetime import (
|
| 6 |
+
datetime,
|
| 7 |
+
timedelta,
|
| 8 |
+
timezone,
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
import pytest
|
| 13 |
+
|
| 14 |
+
import pandas.util._test_decorators as td
|
| 15 |
+
|
| 16 |
+
import pandas as pd
|
| 17 |
+
from pandas import (
|
| 18 |
+
DataFrame,
|
| 19 |
+
DatetimeIndex,
|
| 20 |
+
Index,
|
| 21 |
+
MultiIndex,
|
| 22 |
+
Series,
|
| 23 |
+
Timestamp,
|
| 24 |
+
date_range,
|
| 25 |
+
offsets,
|
| 26 |
+
)
|
| 27 |
+
import pandas._testing as tm
|
| 28 |
+
from pandas.core.groupby.grouper import Grouper
|
| 29 |
+
from pandas.core.groupby.ops import BinGrouper
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@pytest.fixture
|
| 33 |
+
def frame_for_truncated_bingrouper():
|
| 34 |
+
"""
|
| 35 |
+
DataFrame used by groupby_with_truncated_bingrouper, made into
|
| 36 |
+
a separate fixture for easier reuse in
|
| 37 |
+
test_groupby_apply_timegrouper_with_nat_apply_squeeze
|
| 38 |
+
"""
|
| 39 |
+
df = DataFrame(
|
| 40 |
+
{
|
| 41 |
+
"Quantity": [18, 3, 5, 1, 9, 3],
|
| 42 |
+
"Date": [
|
| 43 |
+
Timestamp(2013, 9, 1, 13, 0),
|
| 44 |
+
Timestamp(2013, 9, 1, 13, 5),
|
| 45 |
+
Timestamp(2013, 10, 1, 20, 0),
|
| 46 |
+
Timestamp(2013, 10, 3, 10, 0),
|
| 47 |
+
pd.NaT,
|
| 48 |
+
Timestamp(2013, 9, 2, 14, 0),
|
| 49 |
+
],
|
| 50 |
+
}
|
| 51 |
+
)
|
| 52 |
+
return df
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@pytest.fixture
|
| 56 |
+
def groupby_with_truncated_bingrouper(frame_for_truncated_bingrouper):
|
| 57 |
+
"""
|
| 58 |
+
GroupBy object such that gb._grouper is a BinGrouper and
|
| 59 |
+
len(gb._grouper.result_index) < len(gb._grouper.group_keys_seq)
|
| 60 |
+
|
| 61 |
+
Aggregations on this groupby should have
|
| 62 |
+
|
| 63 |
+
dti = date_range("2013-09-01", "2013-10-01", freq="5D", name="Date")
|
| 64 |
+
|
| 65 |
+
As either the index or an index level.
|
| 66 |
+
"""
|
| 67 |
+
df = frame_for_truncated_bingrouper
|
| 68 |
+
|
| 69 |
+
tdg = Grouper(key="Date", freq="5D")
|
| 70 |
+
gb = df.groupby(tdg)
|
| 71 |
+
|
| 72 |
+
# check we're testing the case we're interested in
|
| 73 |
+
assert len(gb._grouper.result_index) != len(gb._grouper.codes)
|
| 74 |
+
|
| 75 |
+
return gb
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class TestGroupBy:
|
| 79 |
+
def test_groupby_with_timegrouper(self, using_infer_string):
|
| 80 |
+
# GH 4161
|
| 81 |
+
# TimeGrouper requires a sorted index
|
| 82 |
+
# also verifies that the resultant index has the correct name
|
| 83 |
+
df_original = DataFrame(
|
| 84 |
+
{
|
| 85 |
+
"Buyer": "Carl Carl Carl Carl Joe Carl".split(),
|
| 86 |
+
"Quantity": [18, 3, 5, 1, 9, 3],
|
| 87 |
+
"Date": [
|
| 88 |
+
datetime(2013, 9, 1, 13, 0),
|
| 89 |
+
datetime(2013, 9, 1, 13, 5),
|
| 90 |
+
datetime(2013, 10, 1, 20, 0),
|
| 91 |
+
datetime(2013, 10, 3, 10, 0),
|
| 92 |
+
datetime(2013, 12, 2, 12, 0),
|
| 93 |
+
datetime(2013, 9, 2, 14, 0),
|
| 94 |
+
],
|
| 95 |
+
}
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# GH 6908 change target column's order
|
| 99 |
+
df_reordered = df_original.sort_values(by="Quantity")
|
| 100 |
+
|
| 101 |
+
for df in [df_original, df_reordered]:
|
| 102 |
+
df = df.set_index(["Date"])
|
| 103 |
+
|
| 104 |
+
exp_dti = date_range(
|
| 105 |
+
"20130901",
|
| 106 |
+
"20131205",
|
| 107 |
+
freq="5D",
|
| 108 |
+
name="Date",
|
| 109 |
+
inclusive="left",
|
| 110 |
+
unit=df.index.unit,
|
| 111 |
+
)
|
| 112 |
+
expected = DataFrame(
|
| 113 |
+
{"Buyer": "" if using_infer_string else 0, "Quantity": 0},
|
| 114 |
+
index=exp_dti,
|
| 115 |
+
)
|
| 116 |
+
# Cast to object/str to avoid implicit cast when setting
|
| 117 |
+
# entry to "CarlCarlCarl"
|
| 118 |
+
expected = expected.astype({"Buyer": object})
|
| 119 |
+
if using_infer_string:
|
| 120 |
+
expected = expected.astype({"Buyer": "str"})
|
| 121 |
+
expected.iloc[0, 0] = "CarlCarlCarl"
|
| 122 |
+
expected.iloc[6, 0] = "CarlCarl"
|
| 123 |
+
expected.iloc[18, 0] = "Joe"
|
| 124 |
+
expected.iloc[[0, 6, 18], 1] = np.array([24, 6, 9], dtype="int64")
|
| 125 |
+
|
| 126 |
+
result1 = df.resample("5D").sum()
|
| 127 |
+
tm.assert_frame_equal(result1, expected)
|
| 128 |
+
|
| 129 |
+
df_sorted = df.sort_index()
|
| 130 |
+
result2 = df_sorted.groupby(Grouper(freq="5D")).sum()
|
| 131 |
+
tm.assert_frame_equal(result2, expected)
|
| 132 |
+
|
| 133 |
+
result3 = df.groupby(Grouper(freq="5D")).sum()
|
| 134 |
+
tm.assert_frame_equal(result3, expected)
|
| 135 |
+
|
| 136 |
+
@pytest.mark.parametrize("should_sort", [True, False])
|
| 137 |
+
def test_groupby_with_timegrouper_methods(self, should_sort):
|
| 138 |
+
# GH 3881
|
| 139 |
+
# make sure API of timegrouper conforms
|
| 140 |
+
|
| 141 |
+
df = DataFrame(
|
| 142 |
+
{
|
| 143 |
+
"Branch": "A A A A A B".split(),
|
| 144 |
+
"Buyer": "Carl Mark Carl Joe Joe Carl".split(),
|
| 145 |
+
"Quantity": [1, 3, 5, 8, 9, 3],
|
| 146 |
+
"Date": [
|
| 147 |
+
datetime(2013, 1, 1, 13, 0),
|
| 148 |
+
datetime(2013, 1, 1, 13, 5),
|
| 149 |
+
datetime(2013, 10, 1, 20, 0),
|
| 150 |
+
datetime(2013, 10, 2, 10, 0),
|
| 151 |
+
datetime(2013, 12, 2, 12, 0),
|
| 152 |
+
datetime(2013, 12, 2, 14, 0),
|
| 153 |
+
],
|
| 154 |
+
}
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
if should_sort:
|
| 158 |
+
df = df.sort_values(by="Quantity", ascending=False)
|
| 159 |
+
|
| 160 |
+
df = df.set_index("Date", drop=False)
|
| 161 |
+
g = df.groupby(Grouper(freq="6ME"))
|
| 162 |
+
assert g.group_keys
|
| 163 |
+
|
| 164 |
+
assert isinstance(g._grouper, BinGrouper)
|
| 165 |
+
groups = g.groups
|
| 166 |
+
assert isinstance(groups, dict)
|
| 167 |
+
assert len(groups) == 3
|
| 168 |
+
|
| 169 |
+
def test_timegrouper_with_reg_groups(self):
|
| 170 |
+
# GH 3794
|
| 171 |
+
# allow combination of timegrouper/reg groups
|
| 172 |
+
|
| 173 |
+
df_original = DataFrame(
|
| 174 |
+
{
|
| 175 |
+
"Branch": "A A A A A A A B".split(),
|
| 176 |
+
"Buyer": "Carl Mark Carl Carl Joe Joe Joe Carl".split(),
|
| 177 |
+
"Quantity": [1, 3, 5, 1, 8, 1, 9, 3],
|
| 178 |
+
"Date": [
|
| 179 |
+
datetime(2013, 1, 1, 13, 0),
|
| 180 |
+
datetime(2013, 1, 1, 13, 5),
|
| 181 |
+
datetime(2013, 10, 1, 20, 0),
|
| 182 |
+
datetime(2013, 10, 2, 10, 0),
|
| 183 |
+
datetime(2013, 10, 1, 20, 0),
|
| 184 |
+
datetime(2013, 10, 2, 10, 0),
|
| 185 |
+
datetime(2013, 12, 2, 12, 0),
|
| 186 |
+
datetime(2013, 12, 2, 14, 0),
|
| 187 |
+
],
|
| 188 |
+
}
|
| 189 |
+
).set_index("Date")
|
| 190 |
+
|
| 191 |
+
df_sorted = df_original.sort_values(by="Quantity", ascending=False)
|
| 192 |
+
|
| 193 |
+
for df in [df_original, df_sorted]:
|
| 194 |
+
expected = DataFrame(
|
| 195 |
+
{
|
| 196 |
+
"Buyer": "Carl Joe Mark".split(),
|
| 197 |
+
"Quantity": [10, 18, 3],
|
| 198 |
+
"Date": [
|
| 199 |
+
datetime(2013, 12, 31, 0, 0),
|
| 200 |
+
datetime(2013, 12, 31, 0, 0),
|
| 201 |
+
datetime(2013, 12, 31, 0, 0),
|
| 202 |
+
],
|
| 203 |
+
}
|
| 204 |
+
).set_index(["Date", "Buyer"])
|
| 205 |
+
|
| 206 |
+
msg = "The default value of numeric_only"
|
| 207 |
+
result = df.groupby([Grouper(freq="YE"), "Buyer"]).sum(numeric_only=True)
|
| 208 |
+
tm.assert_frame_equal(result, expected)
|
| 209 |
+
|
| 210 |
+
expected = DataFrame(
|
| 211 |
+
{
|
| 212 |
+
"Buyer": "Carl Mark Carl Joe".split(),
|
| 213 |
+
"Quantity": [1, 3, 9, 18],
|
| 214 |
+
"Date": [
|
| 215 |
+
datetime(2013, 1, 1, 0, 0),
|
| 216 |
+
datetime(2013, 1, 1, 0, 0),
|
| 217 |
+
datetime(2013, 7, 1, 0, 0),
|
| 218 |
+
datetime(2013, 7, 1, 0, 0),
|
| 219 |
+
],
|
| 220 |
+
}
|
| 221 |
+
).set_index(["Date", "Buyer"])
|
| 222 |
+
result = df.groupby([Grouper(freq="6MS"), "Buyer"]).sum(numeric_only=True)
|
| 223 |
+
tm.assert_frame_equal(result, expected)
|
| 224 |
+
|
| 225 |
+
df_original = DataFrame(
|
| 226 |
+
{
|
| 227 |
+
"Branch": "A A A A A A A B".split(),
|
| 228 |
+
"Buyer": "Carl Mark Carl Carl Joe Joe Joe Carl".split(),
|
| 229 |
+
"Quantity": [1, 3, 5, 1, 8, 1, 9, 3],
|
| 230 |
+
"Date": [
|
| 231 |
+
datetime(2013, 10, 1, 13, 0),
|
| 232 |
+
datetime(2013, 10, 1, 13, 5),
|
| 233 |
+
datetime(2013, 10, 1, 20, 0),
|
| 234 |
+
datetime(2013, 10, 2, 10, 0),
|
| 235 |
+
datetime(2013, 10, 1, 20, 0),
|
| 236 |
+
datetime(2013, 10, 2, 10, 0),
|
| 237 |
+
datetime(2013, 10, 2, 12, 0),
|
| 238 |
+
datetime(2013, 10, 2, 14, 0),
|
| 239 |
+
],
|
| 240 |
+
}
|
| 241 |
+
).set_index("Date")
|
| 242 |
+
|
| 243 |
+
df_sorted = df_original.sort_values(by="Quantity", ascending=False)
|
| 244 |
+
for df in [df_original, df_sorted]:
|
| 245 |
+
expected = DataFrame(
|
| 246 |
+
{
|
| 247 |
+
"Buyer": "Carl Joe Mark Carl Joe".split(),
|
| 248 |
+
"Quantity": [6, 8, 3, 4, 10],
|
| 249 |
+
"Date": [
|
| 250 |
+
datetime(2013, 10, 1, 0, 0),
|
| 251 |
+
datetime(2013, 10, 1, 0, 0),
|
| 252 |
+
datetime(2013, 10, 1, 0, 0),
|
| 253 |
+
datetime(2013, 10, 2, 0, 0),
|
| 254 |
+
datetime(2013, 10, 2, 0, 0),
|
| 255 |
+
],
|
| 256 |
+
}
|
| 257 |
+
).set_index(["Date", "Buyer"])
|
| 258 |
+
|
| 259 |
+
result = df.groupby([Grouper(freq="1D"), "Buyer"]).sum(numeric_only=True)
|
| 260 |
+
tm.assert_frame_equal(result, expected)
|
| 261 |
+
|
| 262 |
+
result = df.groupby([Grouper(freq="1ME"), "Buyer"]).sum(numeric_only=True)
|
| 263 |
+
expected = DataFrame(
|
| 264 |
+
{
|
| 265 |
+
"Buyer": "Carl Joe Mark".split(),
|
| 266 |
+
"Quantity": [10, 18, 3],
|
| 267 |
+
"Date": [
|
| 268 |
+
datetime(2013, 10, 31, 0, 0),
|
| 269 |
+
datetime(2013, 10, 31, 0, 0),
|
| 270 |
+
datetime(2013, 10, 31, 0, 0),
|
| 271 |
+
],
|
| 272 |
+
}
|
| 273 |
+
).set_index(["Date", "Buyer"])
|
| 274 |
+
tm.assert_frame_equal(result, expected)
|
| 275 |
+
|
| 276 |
+
# passing the name
|
| 277 |
+
df = df.reset_index()
|
| 278 |
+
result = df.groupby([Grouper(freq="1ME", key="Date"), "Buyer"]).sum(
|
| 279 |
+
numeric_only=True
|
| 280 |
+
)
|
| 281 |
+
tm.assert_frame_equal(result, expected)
|
| 282 |
+
|
| 283 |
+
with pytest.raises(KeyError, match="'The grouper name foo is not found'"):
|
| 284 |
+
df.groupby([Grouper(freq="1ME", key="foo"), "Buyer"]).sum()
|
| 285 |
+
|
| 286 |
+
# passing the level
|
| 287 |
+
df = df.set_index("Date")
|
| 288 |
+
result = df.groupby([Grouper(freq="1ME", level="Date"), "Buyer"]).sum(
|
| 289 |
+
numeric_only=True
|
| 290 |
+
)
|
| 291 |
+
tm.assert_frame_equal(result, expected)
|
| 292 |
+
result = df.groupby([Grouper(freq="1ME", level=0), "Buyer"]).sum(
|
| 293 |
+
numeric_only=True
|
| 294 |
+
)
|
| 295 |
+
tm.assert_frame_equal(result, expected)
|
| 296 |
+
|
| 297 |
+
with pytest.raises(ValueError, match="The level foo is not valid"):
|
| 298 |
+
df.groupby([Grouper(freq="1ME", level="foo"), "Buyer"]).sum()
|
| 299 |
+
|
| 300 |
+
# multi names
|
| 301 |
+
df = df.copy()
|
| 302 |
+
df["Date"] = df.index + offsets.MonthEnd(2)
|
| 303 |
+
result = df.groupby([Grouper(freq="1ME", key="Date"), "Buyer"]).sum(
|
| 304 |
+
numeric_only=True
|
| 305 |
+
)
|
| 306 |
+
expected = DataFrame(
|
| 307 |
+
{
|
| 308 |
+
"Buyer": "Carl Joe Mark".split(),
|
| 309 |
+
"Quantity": [10, 18, 3],
|
| 310 |
+
"Date": [
|
| 311 |
+
datetime(2013, 11, 30, 0, 0),
|
| 312 |
+
datetime(2013, 11, 30, 0, 0),
|
| 313 |
+
datetime(2013, 11, 30, 0, 0),
|
| 314 |
+
],
|
| 315 |
+
}
|
| 316 |
+
).set_index(["Date", "Buyer"])
|
| 317 |
+
tm.assert_frame_equal(result, expected)
|
| 318 |
+
|
| 319 |
+
# error as we have both a level and a name!
|
| 320 |
+
msg = "The Grouper cannot specify both a key and a level!"
|
| 321 |
+
with pytest.raises(ValueError, match=msg):
|
| 322 |
+
df.groupby(
|
| 323 |
+
[Grouper(freq="1ME", key="Date", level="Date"), "Buyer"]
|
| 324 |
+
).sum()
|
| 325 |
+
|
| 326 |
+
# single groupers
|
| 327 |
+
expected = DataFrame(
|
| 328 |
+
[[31]],
|
| 329 |
+
columns=["Quantity"],
|
| 330 |
+
index=DatetimeIndex(
|
| 331 |
+
[datetime(2013, 10, 31, 0, 0)], freq=offsets.MonthEnd(), name="Date"
|
| 332 |
+
),
|
| 333 |
+
)
|
| 334 |
+
result = df.groupby(Grouper(freq="1ME")).sum(numeric_only=True)
|
| 335 |
+
tm.assert_frame_equal(result, expected)
|
| 336 |
+
|
| 337 |
+
result = df.groupby([Grouper(freq="1ME")]).sum(numeric_only=True)
|
| 338 |
+
tm.assert_frame_equal(result, expected)
|
| 339 |
+
|
| 340 |
+
expected.index = expected.index.shift(1)
|
| 341 |
+
assert expected.index.freq == offsets.MonthEnd()
|
| 342 |
+
result = df.groupby(Grouper(freq="1ME", key="Date")).sum(numeric_only=True)
|
| 343 |
+
tm.assert_frame_equal(result, expected)
|
| 344 |
+
|
| 345 |
+
result = df.groupby([Grouper(freq="1ME", key="Date")]).sum(
|
| 346 |
+
numeric_only=True
|
| 347 |
+
)
|
| 348 |
+
tm.assert_frame_equal(result, expected)
|
| 349 |
+
|
| 350 |
+
@pytest.mark.parametrize("freq", ["D", "ME", "YE", "QE-APR"])
|
| 351 |
+
def test_timegrouper_with_reg_groups_freq(self, freq):
|
| 352 |
+
# GH 6764 multiple grouping with/without sort
|
| 353 |
+
df = DataFrame(
|
| 354 |
+
{
|
| 355 |
+
"date": pd.to_datetime(
|
| 356 |
+
[
|
| 357 |
+
"20121002",
|
| 358 |
+
"20121007",
|
| 359 |
+
"20130130",
|
| 360 |
+
"20130202",
|
| 361 |
+
"20130305",
|
| 362 |
+
"20121002",
|
| 363 |
+
"20121207",
|
| 364 |
+
"20130130",
|
| 365 |
+
"20130202",
|
| 366 |
+
"20130305",
|
| 367 |
+
"20130202",
|
| 368 |
+
"20130305",
|
| 369 |
+
]
|
| 370 |
+
),
|
| 371 |
+
"user_id": [1, 1, 1, 1, 1, 3, 3, 3, 5, 5, 5, 5],
|
| 372 |
+
"whole_cost": [
|
| 373 |
+
1790,
|
| 374 |
+
364,
|
| 375 |
+
280,
|
| 376 |
+
259,
|
| 377 |
+
201,
|
| 378 |
+
623,
|
| 379 |
+
90,
|
| 380 |
+
312,
|
| 381 |
+
359,
|
| 382 |
+
301,
|
| 383 |
+
359,
|
| 384 |
+
801,
|
| 385 |
+
],
|
| 386 |
+
"cost1": [12, 15, 10, 24, 39, 1, 0, 90, 45, 34, 1, 12],
|
| 387 |
+
}
|
| 388 |
+
).set_index("date")
|
| 389 |
+
|
| 390 |
+
expected = (
|
| 391 |
+
df.groupby("user_id")["whole_cost"]
|
| 392 |
+
.resample(freq)
|
| 393 |
+
.sum(min_count=1) # XXX
|
| 394 |
+
.dropna()
|
| 395 |
+
.reorder_levels(["date", "user_id"])
|
| 396 |
+
.sort_index()
|
| 397 |
+
.astype("int64")
|
| 398 |
+
)
|
| 399 |
+
expected.name = "whole_cost"
|
| 400 |
+
|
| 401 |
+
result1 = (
|
| 402 |
+
df.sort_index().groupby([Grouper(freq=freq), "user_id"])["whole_cost"].sum()
|
| 403 |
+
)
|
| 404 |
+
tm.assert_series_equal(result1, expected)
|
| 405 |
+
|
| 406 |
+
result2 = df.groupby([Grouper(freq=freq), "user_id"])["whole_cost"].sum()
|
| 407 |
+
tm.assert_series_equal(result2, expected)
|
| 408 |
+
|
| 409 |
+
def test_timegrouper_get_group(self):
|
| 410 |
+
# GH 6914
|
| 411 |
+
|
| 412 |
+
df_original = DataFrame(
|
| 413 |
+
{
|
| 414 |
+
"Buyer": "Carl Joe Joe Carl Joe Carl".split(),
|
| 415 |
+
"Quantity": [18, 3, 5, 1, 9, 3],
|
| 416 |
+
"Date": [
|
| 417 |
+
datetime(2013, 9, 1, 13, 0),
|
| 418 |
+
datetime(2013, 9, 1, 13, 5),
|
| 419 |
+
datetime(2013, 10, 1, 20, 0),
|
| 420 |
+
datetime(2013, 10, 3, 10, 0),
|
| 421 |
+
datetime(2013, 12, 2, 12, 0),
|
| 422 |
+
datetime(2013, 9, 2, 14, 0),
|
| 423 |
+
],
|
| 424 |
+
}
|
| 425 |
+
)
|
| 426 |
+
df_reordered = df_original.sort_values(by="Quantity")
|
| 427 |
+
|
| 428 |
+
# single grouping
|
| 429 |
+
expected_list = [
|
| 430 |
+
df_original.iloc[[0, 1, 5]],
|
| 431 |
+
df_original.iloc[[2, 3]],
|
| 432 |
+
df_original.iloc[[4]],
|
| 433 |
+
]
|
| 434 |
+
dt_list = ["2013-09-30", "2013-10-31", "2013-12-31"]
|
| 435 |
+
|
| 436 |
+
for df in [df_original, df_reordered]:
|
| 437 |
+
grouped = df.groupby(Grouper(freq="ME", key="Date"))
|
| 438 |
+
for t, expected in zip(dt_list, expected_list, strict=True):
|
| 439 |
+
dt = Timestamp(t)
|
| 440 |
+
result = grouped.get_group(dt)
|
| 441 |
+
tm.assert_frame_equal(result, expected)
|
| 442 |
+
|
| 443 |
+
# multiple grouping
|
| 444 |
+
expected_list = [
|
| 445 |
+
df_original.iloc[[1]],
|
| 446 |
+
df_original.iloc[[3]],
|
| 447 |
+
df_original.iloc[[4]],
|
| 448 |
+
]
|
| 449 |
+
g_list = [("Joe", "2013-09-30"), ("Carl", "2013-10-31"), ("Joe", "2013-12-31")]
|
| 450 |
+
|
| 451 |
+
for df in [df_original, df_reordered]:
|
| 452 |
+
grouped = df.groupby(["Buyer", Grouper(freq="ME", key="Date")])
|
| 453 |
+
for (b, t), expected in zip(g_list, expected_list, strict=True):
|
| 454 |
+
dt = Timestamp(t)
|
| 455 |
+
result = grouped.get_group((b, dt))
|
| 456 |
+
tm.assert_frame_equal(result, expected)
|
| 457 |
+
|
| 458 |
+
# with index
|
| 459 |
+
df_original = df_original.set_index("Date")
|
| 460 |
+
df_reordered = df_original.sort_values(by="Quantity")
|
| 461 |
+
|
| 462 |
+
expected_list = [
|
| 463 |
+
df_original.iloc[[0, 1, 5]],
|
| 464 |
+
df_original.iloc[[2, 3]],
|
| 465 |
+
df_original.iloc[[4]],
|
| 466 |
+
]
|
| 467 |
+
|
| 468 |
+
for df in [df_original, df_reordered]:
|
| 469 |
+
grouped = df.groupby(Grouper(freq="ME"))
|
| 470 |
+
for t, expected in zip(dt_list, expected_list, strict=True):
|
| 471 |
+
dt = Timestamp(t)
|
| 472 |
+
result = grouped.get_group(dt)
|
| 473 |
+
tm.assert_frame_equal(result, expected)
|
| 474 |
+
|
| 475 |
+
def test_timegrouper_apply_return_type_series(self):
|
| 476 |
+
# Using `apply` with the `TimeGrouper` should give the
|
| 477 |
+
# same return type as an `apply` with a `Grouper`.
|
| 478 |
+
# Issue #11742
|
| 479 |
+
df = DataFrame({"date": ["10/10/2000", "11/10/2000"], "value": [10, 13]})
|
| 480 |
+
df_dt = df.copy()
|
| 481 |
+
df_dt["date"] = pd.to_datetime(df_dt["date"])
|
| 482 |
+
|
| 483 |
+
def sumfunc_series(x):
|
| 484 |
+
return Series([x["value"].sum()], ("sum",))
|
| 485 |
+
|
| 486 |
+
expected = df.groupby(Grouper(key="date")).apply(sumfunc_series)
|
| 487 |
+
result = df_dt.groupby(Grouper(freq="ME", key="date")).apply(sumfunc_series)
|
| 488 |
+
tm.assert_frame_equal(
|
| 489 |
+
result.reset_index(drop=True), expected.reset_index(drop=True)
|
| 490 |
+
)
|
| 491 |
+
|
| 492 |
+
def test_timegrouper_apply_return_type_value(self):
|
| 493 |
+
# Using `apply` with the `TimeGrouper` should give the
|
| 494 |
+
# same return type as an `apply` with a `Grouper`.
|
| 495 |
+
# Issue #11742
|
| 496 |
+
df = DataFrame({"date": ["10/10/2000", "11/10/2000"], "value": [10, 13]})
|
| 497 |
+
df_dt = df.copy()
|
| 498 |
+
df_dt["date"] = pd.to_datetime(df_dt["date"])
|
| 499 |
+
|
| 500 |
+
def sumfunc_value(x):
|
| 501 |
+
return x.value.sum()
|
| 502 |
+
|
| 503 |
+
expected = df.groupby(Grouper(key="date")).apply(sumfunc_value)
|
| 504 |
+
result = df_dt.groupby(Grouper(freq="ME", key="date")).apply(sumfunc_value)
|
| 505 |
+
tm.assert_series_equal(
|
| 506 |
+
result.reset_index(drop=True), expected.reset_index(drop=True)
|
| 507 |
+
)
|
| 508 |
+
|
| 509 |
+
def test_groupby_groups_datetimeindex(self):
|
| 510 |
+
# GH#1430
|
| 511 |
+
periods = 1000
|
| 512 |
+
ind = date_range(start="2012/1/1", freq="5min", periods=periods)
|
| 513 |
+
df = DataFrame(
|
| 514 |
+
{"high": np.arange(periods), "low": np.arange(periods)}, index=ind
|
| 515 |
+
)
|
| 516 |
+
grouped = df.groupby(lambda x: datetime(x.year, x.month, x.day))
|
| 517 |
+
|
| 518 |
+
# it works!
|
| 519 |
+
groups = grouped.groups
|
| 520 |
+
assert isinstance(next(iter(groups.keys())), datetime)
|
| 521 |
+
|
| 522 |
+
def test_groupby_groups_datetimeindex2(self):
|
| 523 |
+
# GH#11442
|
| 524 |
+
index = date_range("2015/01/01", periods=5, name="date")
|
| 525 |
+
df = DataFrame({"A": [5, 6, 7, 8, 9], "B": [1, 2, 3, 4, 5]}, index=index)
|
| 526 |
+
result = df.groupby(level="date").groups
|
| 527 |
+
dates = ["2015-01-05", "2015-01-04", "2015-01-03", "2015-01-02", "2015-01-01"]
|
| 528 |
+
expected = {
|
| 529 |
+
Timestamp(date): DatetimeIndex([date], name="date") for date in dates
|
| 530 |
+
}
|
| 531 |
+
tm.assert_dict_equal(result, expected)
|
| 532 |
+
|
| 533 |
+
grouped = df.groupby(level="date")
|
| 534 |
+
for date in dates:
|
| 535 |
+
result = grouped.get_group(date)
|
| 536 |
+
data = [[df.loc[date, "A"], df.loc[date, "B"]]]
|
| 537 |
+
expected_index = DatetimeIndex(
|
| 538 |
+
[date], name="date", freq="D", dtype=index.dtype
|
| 539 |
+
)
|
| 540 |
+
expected = DataFrame(data, columns=list("AB"), index=expected_index)
|
| 541 |
+
tm.assert_frame_equal(result, expected)
|
| 542 |
+
|
| 543 |
+
def test_groupby_groups_datetimeindex_tz(self):
|
| 544 |
+
# GH 3950
|
| 545 |
+
dates = [
|
| 546 |
+
"2011-07-19 07:00:00",
|
| 547 |
+
"2011-07-19 08:00:00",
|
| 548 |
+
"2011-07-19 09:00:00",
|
| 549 |
+
"2011-07-19 07:00:00",
|
| 550 |
+
"2011-07-19 08:00:00",
|
| 551 |
+
"2011-07-19 09:00:00",
|
| 552 |
+
]
|
| 553 |
+
df = DataFrame(
|
| 554 |
+
{
|
| 555 |
+
"label": ["a", "a", "a", "b", "b", "b"],
|
| 556 |
+
"datetime": dates,
|
| 557 |
+
"value1": np.arange(6, dtype="int64"),
|
| 558 |
+
"value2": [1, 2] * 3,
|
| 559 |
+
}
|
| 560 |
+
)
|
| 561 |
+
df["datetime"] = df["datetime"].apply(lambda d: Timestamp(d, tz="US/Pacific"))
|
| 562 |
+
|
| 563 |
+
exp_idx1 = DatetimeIndex(
|
| 564 |
+
[
|
| 565 |
+
"2011-07-19 07:00:00",
|
| 566 |
+
"2011-07-19 07:00:00",
|
| 567 |
+
"2011-07-19 08:00:00",
|
| 568 |
+
"2011-07-19 08:00:00",
|
| 569 |
+
"2011-07-19 09:00:00",
|
| 570 |
+
"2011-07-19 09:00:00",
|
| 571 |
+
],
|
| 572 |
+
tz="US/Pacific",
|
| 573 |
+
name="datetime",
|
| 574 |
+
)
|
| 575 |
+
exp_idx2 = Index(["a", "b"] * 3, name="label")
|
| 576 |
+
exp_idx = MultiIndex.from_arrays([exp_idx1, exp_idx2])
|
| 577 |
+
expected = DataFrame(
|
| 578 |
+
{"value1": [0, 3, 1, 4, 2, 5], "value2": [1, 2, 2, 1, 1, 2]},
|
| 579 |
+
index=exp_idx,
|
| 580 |
+
columns=["value1", "value2"],
|
| 581 |
+
)
|
| 582 |
+
|
| 583 |
+
result = df.groupby(["datetime", "label"]).sum()
|
| 584 |
+
tm.assert_frame_equal(result, expected)
|
| 585 |
+
|
| 586 |
+
# by level
|
| 587 |
+
didx = DatetimeIndex(dates, tz="Asia/Tokyo")
|
| 588 |
+
df = DataFrame(
|
| 589 |
+
{"value1": np.arange(6, dtype="int64"), "value2": [1, 2, 3, 1, 2, 3]},
|
| 590 |
+
index=didx,
|
| 591 |
+
)
|
| 592 |
+
|
| 593 |
+
exp_idx = DatetimeIndex(
|
| 594 |
+
["2011-07-19 07:00:00", "2011-07-19 08:00:00", "2011-07-19 09:00:00"],
|
| 595 |
+
tz="Asia/Tokyo",
|
| 596 |
+
)
|
| 597 |
+
expected = DataFrame(
|
| 598 |
+
{"value1": [3, 5, 7], "value2": [2, 4, 6]},
|
| 599 |
+
index=exp_idx,
|
| 600 |
+
columns=["value1", "value2"],
|
| 601 |
+
)
|
| 602 |
+
|
| 603 |
+
result = df.groupby(level=0).sum()
|
| 604 |
+
tm.assert_frame_equal(result, expected)
|
| 605 |
+
|
| 606 |
+
def test_frame_datetime64_handling_groupby(self):
|
| 607 |
+
# it works!
|
| 608 |
+
df = DataFrame(
|
| 609 |
+
[(3, np.datetime64("2012-07-03")), (3, np.datetime64("2012-07-04"))],
|
| 610 |
+
columns=["a", "date"],
|
| 611 |
+
)
|
| 612 |
+
result = df.groupby("a").first()
|
| 613 |
+
assert result["date"][3] == Timestamp("2012-07-03")
|
| 614 |
+
|
| 615 |
+
def test_groupby_multi_timezone(self):
|
| 616 |
+
# combining multiple / different timezones yields UTC
|
| 617 |
+
df = DataFrame(
|
| 618 |
+
{
|
| 619 |
+
"value": range(5),
|
| 620 |
+
"date": [
|
| 621 |
+
"2000-01-28 16:47:00",
|
| 622 |
+
"2000-01-29 16:48:00",
|
| 623 |
+
"2000-01-30 16:49:00",
|
| 624 |
+
"2000-01-31 16:50:00",
|
| 625 |
+
"2000-01-01 16:50:00",
|
| 626 |
+
],
|
| 627 |
+
"tz": [
|
| 628 |
+
"America/Chicago",
|
| 629 |
+
"America/Chicago",
|
| 630 |
+
"America/Los_Angeles",
|
| 631 |
+
"America/Chicago",
|
| 632 |
+
"America/New_York",
|
| 633 |
+
],
|
| 634 |
+
}
|
| 635 |
+
)
|
| 636 |
+
|
| 637 |
+
result = df.groupby("tz", group_keys=False).date.apply(
|
| 638 |
+
lambda x: pd.to_datetime(x).dt.tz_localize(x.name)
|
| 639 |
+
)
|
| 640 |
+
|
| 641 |
+
expected = Series(
|
| 642 |
+
[
|
| 643 |
+
Timestamp("2000-01-28 16:47:00-0600", tz="America/Chicago"),
|
| 644 |
+
Timestamp("2000-01-29 16:48:00-0600", tz="America/Chicago"),
|
| 645 |
+
Timestamp("2000-01-30 16:49:00-0800", tz="America/Los_Angeles"),
|
| 646 |
+
Timestamp("2000-01-31 16:50:00-0600", tz="America/Chicago"),
|
| 647 |
+
Timestamp("2000-01-01 16:50:00-0500", tz="America/New_York"),
|
| 648 |
+
],
|
| 649 |
+
name="date",
|
| 650 |
+
dtype=object,
|
| 651 |
+
)
|
| 652 |
+
tm.assert_series_equal(result, expected)
|
| 653 |
+
|
| 654 |
+
tz = "America/Chicago"
|
| 655 |
+
res_values = df.groupby("tz").date.get_group(tz)
|
| 656 |
+
result = pd.to_datetime(res_values).dt.tz_localize(tz)
|
| 657 |
+
exp_values = Series(
|
| 658 |
+
["2000-01-28 16:47:00", "2000-01-29 16:48:00", "2000-01-31 16:50:00"],
|
| 659 |
+
index=[0, 1, 3],
|
| 660 |
+
name="date",
|
| 661 |
+
)
|
| 662 |
+
expected = pd.to_datetime(exp_values).dt.tz_localize(tz)
|
| 663 |
+
tm.assert_series_equal(result, expected)
|
| 664 |
+
|
| 665 |
+
def test_groupby_groups_periods(self):
|
| 666 |
+
dates = [
|
| 667 |
+
"2011-07-19 07:00:00",
|
| 668 |
+
"2011-07-19 08:00:00",
|
| 669 |
+
"2011-07-19 09:00:00",
|
| 670 |
+
"2011-07-19 07:00:00",
|
| 671 |
+
"2011-07-19 08:00:00",
|
| 672 |
+
"2011-07-19 09:00:00",
|
| 673 |
+
]
|
| 674 |
+
df = DataFrame(
|
| 675 |
+
{
|
| 676 |
+
"label": ["a", "a", "a", "b", "b", "b"],
|
| 677 |
+
"period": [pd.Period(d, freq="h") for d in dates],
|
| 678 |
+
"value1": np.arange(6, dtype="int64"),
|
| 679 |
+
"value2": [1, 2] * 3,
|
| 680 |
+
}
|
| 681 |
+
)
|
| 682 |
+
|
| 683 |
+
exp_idx1 = pd.PeriodIndex(
|
| 684 |
+
[
|
| 685 |
+
"2011-07-19 07:00:00",
|
| 686 |
+
"2011-07-19 07:00:00",
|
| 687 |
+
"2011-07-19 08:00:00",
|
| 688 |
+
"2011-07-19 08:00:00",
|
| 689 |
+
"2011-07-19 09:00:00",
|
| 690 |
+
"2011-07-19 09:00:00",
|
| 691 |
+
],
|
| 692 |
+
freq="h",
|
| 693 |
+
name="period",
|
| 694 |
+
)
|
| 695 |
+
exp_idx2 = Index(["a", "b"] * 3, name="label")
|
| 696 |
+
exp_idx = MultiIndex.from_arrays([exp_idx1, exp_idx2])
|
| 697 |
+
expected = DataFrame(
|
| 698 |
+
{"value1": [0, 3, 1, 4, 2, 5], "value2": [1, 2, 2, 1, 1, 2]},
|
| 699 |
+
index=exp_idx,
|
| 700 |
+
columns=["value1", "value2"],
|
| 701 |
+
)
|
| 702 |
+
|
| 703 |
+
result = df.groupby(["period", "label"]).sum()
|
| 704 |
+
tm.assert_frame_equal(result, expected)
|
| 705 |
+
|
| 706 |
+
# by level
|
| 707 |
+
didx = pd.PeriodIndex(dates, freq="h")
|
| 708 |
+
df = DataFrame(
|
| 709 |
+
{"value1": np.arange(6, dtype="int64"), "value2": [1, 2, 3, 1, 2, 3]},
|
| 710 |
+
index=didx,
|
| 711 |
+
)
|
| 712 |
+
|
| 713 |
+
exp_idx = pd.PeriodIndex(
|
| 714 |
+
["2011-07-19 07:00:00", "2011-07-19 08:00:00", "2011-07-19 09:00:00"],
|
| 715 |
+
freq="h",
|
| 716 |
+
)
|
| 717 |
+
expected = DataFrame(
|
| 718 |
+
{"value1": [3, 5, 7], "value2": [2, 4, 6]},
|
| 719 |
+
index=exp_idx,
|
| 720 |
+
columns=["value1", "value2"],
|
| 721 |
+
)
|
| 722 |
+
|
| 723 |
+
result = df.groupby(level=0).sum()
|
| 724 |
+
tm.assert_frame_equal(result, expected)
|
| 725 |
+
|
| 726 |
+
def test_groupby_first_datetime64(self):
|
| 727 |
+
df = DataFrame([(1, 1351036800000000000), (2, 1351036800000000000)])
|
| 728 |
+
df[1] = df[1].astype("M8[ns]")
|
| 729 |
+
|
| 730 |
+
assert issubclass(df[1].dtype.type, np.datetime64)
|
| 731 |
+
|
| 732 |
+
result = df.groupby(level=0).first()
|
| 733 |
+
got_dt = result[1].dtype
|
| 734 |
+
assert issubclass(got_dt.type, np.datetime64)
|
| 735 |
+
|
| 736 |
+
result = df[1].groupby(level=0).first()
|
| 737 |
+
got_dt = result.dtype
|
| 738 |
+
assert issubclass(got_dt.type, np.datetime64)
|
| 739 |
+
|
| 740 |
+
def test_groupby_max_datetime64(self):
|
| 741 |
+
# GH 5869
|
| 742 |
+
# datetimelike dtype conversion from int
|
| 743 |
+
df = DataFrame({"A": Timestamp("20130101").as_unit("s"), "B": np.arange(5)})
|
| 744 |
+
# TODO: can we retain second reso in .apply here?
|
| 745 |
+
expected = df.groupby("A")["A"].apply(lambda x: x.max()).astype("M8[s]")
|
| 746 |
+
result = df.groupby("A")["A"].max()
|
| 747 |
+
tm.assert_series_equal(result, expected)
|
| 748 |
+
|
| 749 |
+
def test_groupby_datetime64_32_bit(self):
|
| 750 |
+
# GH 6410 / numpy 4328
|
| 751 |
+
# 32-bit under 1.9-dev indexing issue
|
| 752 |
+
|
| 753 |
+
df = DataFrame({"A": range(2), "B": [Timestamp("2000-01-1")] * 2})
|
| 754 |
+
result = df.groupby("A")["B"].transform("min")
|
| 755 |
+
expected = Series([Timestamp("2000-01-1")] * 2, name="B")
|
| 756 |
+
tm.assert_series_equal(result, expected)
|
| 757 |
+
|
| 758 |
+
def test_groupby_with_timezone_selection(self):
|
| 759 |
+
# GH 11616
|
| 760 |
+
# Test that column selection returns output in correct timezone.
|
| 761 |
+
|
| 762 |
+
df = DataFrame(
|
| 763 |
+
{
|
| 764 |
+
"factor": np.random.default_rng(2).integers(0, 3, size=60),
|
| 765 |
+
"time": date_range("01/01/2000 00:00", periods=60, freq="s", tz="UTC"),
|
| 766 |
+
}
|
| 767 |
+
)
|
| 768 |
+
df1 = df.groupby("factor").max()["time"]
|
| 769 |
+
df2 = df.groupby("factor")["time"].max()
|
| 770 |
+
tm.assert_series_equal(df1, df2)
|
| 771 |
+
|
| 772 |
+
def test_timezone_info(self):
|
| 773 |
+
# see gh-11682: Timezone info lost when broadcasting
|
| 774 |
+
# scalar datetime to DataFrame
|
| 775 |
+
utc = timezone.utc
|
| 776 |
+
df = DataFrame({"a": [1], "b": [datetime.now(utc)]})
|
| 777 |
+
assert df["b"][0].tzinfo == utc
|
| 778 |
+
df = DataFrame({"a": [1, 2, 3]})
|
| 779 |
+
df["b"] = datetime.now(utc)
|
| 780 |
+
assert df["b"][0].tzinfo == utc
|
| 781 |
+
|
| 782 |
+
def test_datetime_count(self):
|
| 783 |
+
df = DataFrame(
|
| 784 |
+
{"a": [1, 2, 3] * 2, "dates": date_range("now", periods=6, freq="min")}
|
| 785 |
+
)
|
| 786 |
+
result = df.groupby("a").dates.count()
|
| 787 |
+
expected = Series([2, 2, 2], index=Index([1, 2, 3], name="a"), name="dates")
|
| 788 |
+
tm.assert_series_equal(result, expected)
|
| 789 |
+
|
| 790 |
+
def test_first_last_max_min_on_time_data(self):
|
| 791 |
+
# GH 10295
|
| 792 |
+
# Verify that NaT is not in the result of max, min, first and last on
|
| 793 |
+
# Dataframe with datetime or timedelta values.
|
| 794 |
+
df_test = DataFrame(
|
| 795 |
+
{
|
| 796 |
+
"dt": [
|
| 797 |
+
np.nan,
|
| 798 |
+
"2015-07-24 10:10",
|
| 799 |
+
"2015-07-25 11:11",
|
| 800 |
+
"2015-07-23 12:12",
|
| 801 |
+
np.nan,
|
| 802 |
+
],
|
| 803 |
+
"td": [
|
| 804 |
+
np.nan,
|
| 805 |
+
timedelta(days=1),
|
| 806 |
+
timedelta(days=2),
|
| 807 |
+
timedelta(days=3),
|
| 808 |
+
np.nan,
|
| 809 |
+
],
|
| 810 |
+
}
|
| 811 |
+
)
|
| 812 |
+
df_test.dt = pd.to_datetime(df_test.dt)
|
| 813 |
+
df_test["group"] = "A"
|
| 814 |
+
df_ref = df_test[df_test.dt.notna()]
|
| 815 |
+
|
| 816 |
+
grouped_test = df_test.groupby("group")
|
| 817 |
+
grouped_ref = df_ref.groupby("group")
|
| 818 |
+
|
| 819 |
+
tm.assert_frame_equal(grouped_ref.max(), grouped_test.max())
|
| 820 |
+
tm.assert_frame_equal(grouped_ref.min(), grouped_test.min())
|
| 821 |
+
tm.assert_frame_equal(grouped_ref.first(), grouped_test.first())
|
| 822 |
+
tm.assert_frame_equal(grouped_ref.last(), grouped_test.last())
|
| 823 |
+
|
| 824 |
+
def test_nunique_with_timegrouper_and_nat(self):
|
| 825 |
+
# GH 17575
|
| 826 |
+
test = DataFrame(
|
| 827 |
+
{
|
| 828 |
+
"time": [
|
| 829 |
+
Timestamp("2016-06-28 09:35:35"),
|
| 830 |
+
pd.NaT,
|
| 831 |
+
Timestamp("2016-06-28 16:46:28"),
|
| 832 |
+
],
|
| 833 |
+
"data": ["1", "2", "3"],
|
| 834 |
+
}
|
| 835 |
+
)
|
| 836 |
+
|
| 837 |
+
grouper = Grouper(key="time", freq="h")
|
| 838 |
+
result = test.groupby(grouper)["data"].nunique()
|
| 839 |
+
expected = test[test.time.notnull()].groupby(grouper)["data"].nunique()
|
| 840 |
+
expected.index = expected.index._with_freq(None)
|
| 841 |
+
tm.assert_series_equal(result, expected)
|
| 842 |
+
|
| 843 |
+
def test_scalar_call_versus_list_call(self):
|
| 844 |
+
# Issue: 17530
|
| 845 |
+
data_frame = {
|
| 846 |
+
"location": ["shanghai", "beijing", "shanghai"],
|
| 847 |
+
"time": Series(
|
| 848 |
+
["2017-08-09 13:32:23", "2017-08-11 23:23:15", "2017-08-11 22:23:15"],
|
| 849 |
+
dtype="datetime64[ns]",
|
| 850 |
+
),
|
| 851 |
+
"value": [1, 2, 3],
|
| 852 |
+
}
|
| 853 |
+
data_frame = DataFrame(data_frame).set_index("time")
|
| 854 |
+
grouper = Grouper(freq="D")
|
| 855 |
+
|
| 856 |
+
grouped = data_frame.groupby(grouper)
|
| 857 |
+
result = grouped.count()
|
| 858 |
+
grouped = data_frame.groupby([grouper])
|
| 859 |
+
expected = grouped.count()
|
| 860 |
+
|
| 861 |
+
tm.assert_frame_equal(result, expected)
|
| 862 |
+
|
| 863 |
+
def test_grouper_period_index(self):
|
| 864 |
+
# GH 32108
|
| 865 |
+
periods = 2
|
| 866 |
+
index = pd.period_range(
|
| 867 |
+
start="2018-01", periods=periods, freq="M", name="Month"
|
| 868 |
+
)
|
| 869 |
+
period_series = Series(range(periods), index=index)
|
| 870 |
+
result = period_series.groupby(period_series.index.month).sum()
|
| 871 |
+
|
| 872 |
+
expected = Series(
|
| 873 |
+
range(periods), index=Index(range(1, periods + 1), name=index.name)
|
| 874 |
+
)
|
| 875 |
+
tm.assert_series_equal(result, expected)
|
| 876 |
+
|
| 877 |
+
def test_groupby_apply_timegrouper_with_nat_dict_returns(
|
| 878 |
+
self, groupby_with_truncated_bingrouper
|
| 879 |
+
):
|
| 880 |
+
# GH#43500 case where gb._grouper.result_index and gb._grouper.group_keys_seq
|
| 881 |
+
# have different lengths that goes through the `isinstance(values[0], dict)`
|
| 882 |
+
# path
|
| 883 |
+
gb = groupby_with_truncated_bingrouper
|
| 884 |
+
|
| 885 |
+
res = gb["Quantity"].apply(lambda x: {"foo": len(x)})
|
| 886 |
+
|
| 887 |
+
df = gb.obj
|
| 888 |
+
unit = df["Date"]._values.unit
|
| 889 |
+
dti = date_range("2013-09-01", "2013-10-01", freq="5D", name="Date", unit=unit)
|
| 890 |
+
mi = MultiIndex.from_arrays([dti, ["foo"] * len(dti)])
|
| 891 |
+
expected = Series([3, 0, 0, 0, 0, 0, 2], index=mi, name="Quantity")
|
| 892 |
+
tm.assert_series_equal(res, expected)
|
| 893 |
+
|
| 894 |
+
def test_groupby_apply_timegrouper_with_nat_scalar_returns(
|
| 895 |
+
self, groupby_with_truncated_bingrouper
|
| 896 |
+
):
|
| 897 |
+
# GH#43500 Previously raised ValueError bc used index with incorrect
|
| 898 |
+
# length in wrap_applied_result
|
| 899 |
+
gb = groupby_with_truncated_bingrouper
|
| 900 |
+
|
| 901 |
+
res = gb["Quantity"].apply(lambda x: x.iloc[0] if len(x) else np.nan)
|
| 902 |
+
|
| 903 |
+
df = gb.obj
|
| 904 |
+
unit = df["Date"]._values.unit
|
| 905 |
+
dti = date_range("2013-09-01", "2013-10-01", freq="5D", name="Date", unit=unit)
|
| 906 |
+
expected = Series(
|
| 907 |
+
[18, np.nan, np.nan, np.nan, np.nan, np.nan, 5],
|
| 908 |
+
index=dti._with_freq(None),
|
| 909 |
+
name="Quantity",
|
| 910 |
+
)
|
| 911 |
+
|
| 912 |
+
tm.assert_series_equal(res, expected)
|
| 913 |
+
|
| 914 |
+
def test_groupby_apply_timegrouper_with_nat_apply_squeeze(
|
| 915 |
+
self, frame_for_truncated_bingrouper
|
| 916 |
+
):
|
| 917 |
+
df = frame_for_truncated_bingrouper
|
| 918 |
+
|
| 919 |
+
# We need to create a GroupBy object with only one non-NaT group,
|
| 920 |
+
# so use a huge freq so that all non-NaT dates will be grouped together
|
| 921 |
+
tdg = Grouper(key="Date", freq="100YE")
|
| 922 |
+
gb = df.groupby(tdg)
|
| 923 |
+
|
| 924 |
+
# check that we will go through the singular_series path
|
| 925 |
+
# in _wrap_applied_output_series
|
| 926 |
+
assert gb.ngroups == 1
|
| 927 |
+
assert gb._selected_obj.index.nlevels == 1
|
| 928 |
+
|
| 929 |
+
# function that returns a Series
|
| 930 |
+
res = gb.apply(lambda x: x["Quantity"] * 2)
|
| 931 |
+
|
| 932 |
+
dti = Index([Timestamp("2013-12-31")], dtype=df["Date"].dtype, name="Date")
|
| 933 |
+
expected = DataFrame(
|
| 934 |
+
[[36, 6, 6, 10, 2]],
|
| 935 |
+
index=dti,
|
| 936 |
+
columns=Index([0, 1, 5, 2, 3], name="Quantity"),
|
| 937 |
+
)
|
| 938 |
+
tm.assert_frame_equal(res, expected)
|
| 939 |
+
|
| 940 |
+
@pytest.mark.single_cpu
|
| 941 |
+
def test_groupby_agg_numba_timegrouper_with_nat(
|
| 942 |
+
self, groupby_with_truncated_bingrouper
|
| 943 |
+
):
|
| 944 |
+
pytest.importorskip("numba")
|
| 945 |
+
|
| 946 |
+
# See discussion in GH#43487
|
| 947 |
+
gb = groupby_with_truncated_bingrouper
|
| 948 |
+
|
| 949 |
+
result = gb["Quantity"].aggregate(
|
| 950 |
+
lambda values, index: np.nanmean(values), engine="numba"
|
| 951 |
+
)
|
| 952 |
+
|
| 953 |
+
expected = gb["Quantity"].aggregate("mean")
|
| 954 |
+
tm.assert_series_equal(result, expected)
|
| 955 |
+
|
| 956 |
+
result_df = gb[["Quantity"]].aggregate(
|
| 957 |
+
lambda values, index: np.nanmean(values), engine="numba"
|
| 958 |
+
)
|
| 959 |
+
expected_df = gb[["Quantity"]].aggregate("mean")
|
| 960 |
+
tm.assert_frame_equal(result_df, expected_df)
|
| 961 |
+
|
| 962 |
+
@td.skip_if_no("pyarrow")
|
| 963 |
+
def test_pyarrow_index_retention(self):
|
| 964 |
+
# https://github.com/pandas-dev/pandas/issues/63518
|
| 965 |
+
df = DataFrame(
|
| 966 |
+
{
|
| 967 |
+
"a": [1, 2, 3],
|
| 968 |
+
},
|
| 969 |
+
index=Index(
|
| 970 |
+
[
|
| 971 |
+
Timestamp("2013-01-01"),
|
| 972 |
+
Timestamp("2013-01-01"),
|
| 973 |
+
Timestamp("2013-01-02"),
|
| 974 |
+
],
|
| 975 |
+
dtype="timestamp[ns, America/Denver][pyarrow]",
|
| 976 |
+
),
|
| 977 |
+
)
|
| 978 |
+
gb = df.groupby(Grouper(freq="D"))
|
| 979 |
+
result = gb._grouper.result_index
|
| 980 |
+
expected = Index(
|
| 981 |
+
[Timestamp("2013-01-01"), Timestamp("2013-01-02")],
|
| 982 |
+
dtype="timestamp[ns, America/Denver][pyarrow]",
|
| 983 |
+
)
|
| 984 |
+
tm.assert_index_equal(result, expected)
|
.venv/lib/python3.13/site-packages/pandas/tests/groupby/transform/__init__.py
ADDED
|
File without changes
|
.venv/lib/python3.13/site-packages/pandas/tests/groupby/transform/test_numba.py
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas.compat import is_platform_arm
|
| 5 |
+
from pandas.errors import NumbaUtilError
|
| 6 |
+
|
| 7 |
+
from pandas import (
|
| 8 |
+
DataFrame,
|
| 9 |
+
Series,
|
| 10 |
+
option_context,
|
| 11 |
+
)
|
| 12 |
+
import pandas._testing as tm
|
| 13 |
+
from pandas.util.version import Version
|
| 14 |
+
|
| 15 |
+
pytestmark = [pytest.mark.single_cpu]
|
| 16 |
+
|
| 17 |
+
numba = pytest.importorskip("numba")
|
| 18 |
+
pytestmark.append(
|
| 19 |
+
pytest.mark.skipif(
|
| 20 |
+
Version(numba.__version__) == Version("0.61") and is_platform_arm(),
|
| 21 |
+
reason=f"Segfaults on ARM platforms with numba {numba.__version__}",
|
| 22 |
+
)
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_correct_function_signature():
|
| 27 |
+
pytest.importorskip("numba")
|
| 28 |
+
|
| 29 |
+
def incorrect_function(x):
|
| 30 |
+
return x + 1
|
| 31 |
+
|
| 32 |
+
data = DataFrame(
|
| 33 |
+
{"key": ["a", "a", "b", "b", "a"], "data": [1.0, 2.0, 3.0, 4.0, 5.0]},
|
| 34 |
+
columns=["key", "data"],
|
| 35 |
+
)
|
| 36 |
+
with pytest.raises(NumbaUtilError, match="The first 2"):
|
| 37 |
+
data.groupby("key").transform(incorrect_function, engine="numba")
|
| 38 |
+
|
| 39 |
+
with pytest.raises(NumbaUtilError, match="The first 2"):
|
| 40 |
+
data.groupby("key")["data"].transform(incorrect_function, engine="numba")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def test_check_nopython_kwargs():
|
| 44 |
+
pytest.importorskip("numba")
|
| 45 |
+
|
| 46 |
+
def incorrect_function(values, index, *, a):
|
| 47 |
+
return values + a
|
| 48 |
+
|
| 49 |
+
def correct_function(values, index, a):
|
| 50 |
+
return values + a
|
| 51 |
+
|
| 52 |
+
data = DataFrame(
|
| 53 |
+
{"key": ["a", "a", "b", "b", "a"], "data": [1.0, 2.0, 3.0, 4.0, 5.0]},
|
| 54 |
+
columns=["key", "data"],
|
| 55 |
+
)
|
| 56 |
+
# py signature binding
|
| 57 |
+
with pytest.raises(
|
| 58 |
+
TypeError, match="missing a required (keyword-only argument|argument): 'a'"
|
| 59 |
+
):
|
| 60 |
+
data.groupby("key").transform(incorrect_function, engine="numba", b=1)
|
| 61 |
+
with pytest.raises(TypeError, match="missing a required argument: 'a'"):
|
| 62 |
+
data.groupby("key").transform(correct_function, engine="numba", b=1)
|
| 63 |
+
|
| 64 |
+
with pytest.raises(
|
| 65 |
+
TypeError, match="missing a required (keyword-only argument|argument): 'a'"
|
| 66 |
+
):
|
| 67 |
+
data.groupby("key")["data"].transform(incorrect_function, engine="numba", b=1)
|
| 68 |
+
with pytest.raises(TypeError, match="missing a required argument: 'a'"):
|
| 69 |
+
data.groupby("key")["data"].transform(correct_function, engine="numba", b=1)
|
| 70 |
+
|
| 71 |
+
# numba signature check after binding
|
| 72 |
+
with pytest.raises(NumbaUtilError, match="numba does not support"):
|
| 73 |
+
data.groupby("key").transform(incorrect_function, engine="numba", a=1)
|
| 74 |
+
actual = data.groupby("key").transform(correct_function, engine="numba", a=1)
|
| 75 |
+
tm.assert_frame_equal(data[["data"]] + 1, actual)
|
| 76 |
+
|
| 77 |
+
with pytest.raises(NumbaUtilError, match="numba does not support"):
|
| 78 |
+
data.groupby("key")["data"].transform(incorrect_function, engine="numba", a=1)
|
| 79 |
+
actual = data.groupby("key")["data"].transform(
|
| 80 |
+
correct_function, engine="numba", a=1
|
| 81 |
+
)
|
| 82 |
+
tm.assert_series_equal(data["data"] + 1, actual)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
@pytest.mark.filterwarnings("ignore")
|
| 86 |
+
# Filter warnings when parallel=True and the function can't be parallelized by Numba
|
| 87 |
+
@pytest.mark.parametrize("jit", [True, False])
|
| 88 |
+
def test_numba_vs_cython(jit, frame_or_series, nogil, parallel, nopython, as_index):
|
| 89 |
+
pytest.importorskip("numba")
|
| 90 |
+
|
| 91 |
+
def func(values, index):
|
| 92 |
+
return values + 1
|
| 93 |
+
|
| 94 |
+
if jit:
|
| 95 |
+
# Test accepted jitted functions
|
| 96 |
+
import numba
|
| 97 |
+
|
| 98 |
+
func = numba.jit(func)
|
| 99 |
+
|
| 100 |
+
data = DataFrame(
|
| 101 |
+
{0: ["a", "a", "b", "b", "a"], 1: [1.0, 2.0, 3.0, 4.0, 5.0]}, columns=[0, 1]
|
| 102 |
+
)
|
| 103 |
+
engine_kwargs = {"nogil": nogil, "parallel": parallel, "nopython": nopython}
|
| 104 |
+
grouped = data.groupby(0, as_index=as_index)
|
| 105 |
+
if frame_or_series is Series:
|
| 106 |
+
grouped = grouped[1]
|
| 107 |
+
|
| 108 |
+
result = grouped.transform(func, engine="numba", engine_kwargs=engine_kwargs)
|
| 109 |
+
expected = grouped.transform(lambda x: x + 1, engine="cython")
|
| 110 |
+
|
| 111 |
+
tm.assert_equal(result, expected)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
@pytest.mark.filterwarnings("ignore")
|
| 115 |
+
# Filter warnings when parallel=True and the function can't be parallelized by Numba
|
| 116 |
+
@pytest.mark.parametrize("jit", [True, False])
|
| 117 |
+
def test_cache(jit, frame_or_series, nogil, parallel, nopython):
|
| 118 |
+
# Test that the functions are cached correctly if we switch functions
|
| 119 |
+
pytest.importorskip("numba")
|
| 120 |
+
|
| 121 |
+
def func_1(values, index):
|
| 122 |
+
return values + 1
|
| 123 |
+
|
| 124 |
+
def func_2(values, index):
|
| 125 |
+
return values * 5
|
| 126 |
+
|
| 127 |
+
if jit:
|
| 128 |
+
import numba
|
| 129 |
+
|
| 130 |
+
func_1 = numba.jit(func_1)
|
| 131 |
+
func_2 = numba.jit(func_2)
|
| 132 |
+
|
| 133 |
+
data = DataFrame(
|
| 134 |
+
{0: ["a", "a", "b", "b", "a"], 1: [1.0, 2.0, 3.0, 4.0, 5.0]}, columns=[0, 1]
|
| 135 |
+
)
|
| 136 |
+
engine_kwargs = {"nogil": nogil, "parallel": parallel, "nopython": nopython}
|
| 137 |
+
grouped = data.groupby(0)
|
| 138 |
+
if frame_or_series is Series:
|
| 139 |
+
grouped = grouped[1]
|
| 140 |
+
|
| 141 |
+
result = grouped.transform(func_1, engine="numba", engine_kwargs=engine_kwargs)
|
| 142 |
+
expected = grouped.transform(lambda x: x + 1, engine="cython")
|
| 143 |
+
tm.assert_equal(result, expected)
|
| 144 |
+
|
| 145 |
+
result = grouped.transform(func_2, engine="numba", engine_kwargs=engine_kwargs)
|
| 146 |
+
expected = grouped.transform(lambda x: x * 5, engine="cython")
|
| 147 |
+
tm.assert_equal(result, expected)
|
| 148 |
+
|
| 149 |
+
# Retest func_1 which should use the cache
|
| 150 |
+
result = grouped.transform(func_1, engine="numba", engine_kwargs=engine_kwargs)
|
| 151 |
+
expected = grouped.transform(lambda x: x + 1, engine="cython")
|
| 152 |
+
tm.assert_equal(result, expected)
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def test_use_global_config():
|
| 156 |
+
pytest.importorskip("numba")
|
| 157 |
+
|
| 158 |
+
def func_1(values, index):
|
| 159 |
+
return values + 1
|
| 160 |
+
|
| 161 |
+
data = DataFrame(
|
| 162 |
+
{0: ["a", "a", "b", "b", "a"], 1: [1.0, 2.0, 3.0, 4.0, 5.0]}, columns=[0, 1]
|
| 163 |
+
)
|
| 164 |
+
grouped = data.groupby(0)
|
| 165 |
+
expected = grouped.transform(func_1, engine="numba")
|
| 166 |
+
with option_context("compute.use_numba", True):
|
| 167 |
+
result = grouped.transform(func_1, engine=None)
|
| 168 |
+
tm.assert_frame_equal(expected, result)
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
# TODO: Test more than just reductions (e.g. actually test transformations once we have
|
| 172 |
+
@pytest.mark.parametrize(
|
| 173 |
+
"agg_func", [["min", "max"], "min", {"B": ["min", "max"], "C": "sum"}]
|
| 174 |
+
)
|
| 175 |
+
def test_string_cython_vs_numba(agg_func, numba_supported_reductions):
|
| 176 |
+
pytest.importorskip("numba")
|
| 177 |
+
agg_func, kwargs = numba_supported_reductions
|
| 178 |
+
data = DataFrame(
|
| 179 |
+
{0: ["a", "a", "b", "b", "a"], 1: [1.0, 2.0, 3.0, 4.0, 5.0]}, columns=[0, 1]
|
| 180 |
+
)
|
| 181 |
+
grouped = data.groupby(0)
|
| 182 |
+
|
| 183 |
+
result = grouped.transform(agg_func, engine="numba", **kwargs)
|
| 184 |
+
expected = grouped.transform(agg_func, engine="cython", **kwargs)
|
| 185 |
+
tm.assert_frame_equal(result, expected)
|
| 186 |
+
|
| 187 |
+
result = grouped[1].transform(agg_func, engine="numba", **kwargs)
|
| 188 |
+
expected = grouped[1].transform(agg_func, engine="cython", **kwargs)
|
| 189 |
+
tm.assert_series_equal(result, expected)
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
def test_args_not_cached():
|
| 193 |
+
# GH 41647
|
| 194 |
+
pytest.importorskip("numba")
|
| 195 |
+
|
| 196 |
+
def sum_last(values, index, n):
|
| 197 |
+
return values[-n:].sum()
|
| 198 |
+
|
| 199 |
+
df = DataFrame({"id": [0, 0, 1, 1], "x": [1, 1, 1, 1]})
|
| 200 |
+
grouped_x = df.groupby("id")["x"]
|
| 201 |
+
result = grouped_x.transform(sum_last, 1, engine="numba")
|
| 202 |
+
expected = Series([1.0] * 4, name="x")
|
| 203 |
+
tm.assert_series_equal(result, expected)
|
| 204 |
+
|
| 205 |
+
result = grouped_x.transform(sum_last, 2, engine="numba")
|
| 206 |
+
expected = Series([2.0] * 4, name="x")
|
| 207 |
+
tm.assert_series_equal(result, expected)
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def test_index_data_correctly_passed():
|
| 211 |
+
# GH 43133
|
| 212 |
+
pytest.importorskip("numba")
|
| 213 |
+
|
| 214 |
+
def f(values, index):
|
| 215 |
+
return index - 1
|
| 216 |
+
|
| 217 |
+
df = DataFrame({"group": ["A", "A", "B"], "v": [4, 5, 6]}, index=[-1, -2, -3])
|
| 218 |
+
result = df.groupby("group").transform(f, engine="numba")
|
| 219 |
+
expected = DataFrame([-2.0, -3.0, -4.0], columns=["v"], index=[-1, -2, -3])
|
| 220 |
+
tm.assert_frame_equal(result, expected)
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def test_index_order_consistency_preserved():
|
| 224 |
+
# GH 57069
|
| 225 |
+
pytest.importorskip("numba")
|
| 226 |
+
|
| 227 |
+
def f(values, index):
|
| 228 |
+
return values
|
| 229 |
+
|
| 230 |
+
df = DataFrame(
|
| 231 |
+
{"vals": [0.0, 1.0, 2.0, 3.0], "group": [0, 1, 0, 1]}, index=range(3, -1, -1)
|
| 232 |
+
)
|
| 233 |
+
result = df.groupby("group")["vals"].transform(f, engine="numba")
|
| 234 |
+
expected = Series([0.0, 1.0, 2.0, 3.0], index=range(3, -1, -1), name="vals")
|
| 235 |
+
tm.assert_series_equal(result, expected)
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
def test_engine_kwargs_not_cached():
|
| 239 |
+
# If the user passes a different set of engine_kwargs don't return the same
|
| 240 |
+
# jitted function
|
| 241 |
+
pytest.importorskip("numba")
|
| 242 |
+
nogil = True
|
| 243 |
+
parallel = False
|
| 244 |
+
nopython = True
|
| 245 |
+
|
| 246 |
+
def func_kwargs(values, index):
|
| 247 |
+
return nogil + parallel + nopython
|
| 248 |
+
|
| 249 |
+
engine_kwargs = {"nopython": nopython, "nogil": nogil, "parallel": parallel}
|
| 250 |
+
df = DataFrame({"value": [0, 0, 0]})
|
| 251 |
+
result = df.groupby(level=0).transform(
|
| 252 |
+
func_kwargs, engine="numba", engine_kwargs=engine_kwargs
|
| 253 |
+
)
|
| 254 |
+
expected = DataFrame({"value": [2.0, 2.0, 2.0]})
|
| 255 |
+
tm.assert_frame_equal(result, expected)
|
| 256 |
+
|
| 257 |
+
nogil = False
|
| 258 |
+
engine_kwargs = {"nopython": nopython, "nogil": nogil, "parallel": parallel}
|
| 259 |
+
result = df.groupby(level=0).transform(
|
| 260 |
+
func_kwargs, engine="numba", engine_kwargs=engine_kwargs
|
| 261 |
+
)
|
| 262 |
+
expected = DataFrame({"value": [1.0, 1.0, 1.0]})
|
| 263 |
+
tm.assert_frame_equal(result, expected)
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
@pytest.mark.filterwarnings("ignore")
|
| 267 |
+
def test_multiindex_one_key(nogil, parallel, nopython):
|
| 268 |
+
pytest.importorskip("numba")
|
| 269 |
+
|
| 270 |
+
def numba_func(values, index):
|
| 271 |
+
return 1
|
| 272 |
+
|
| 273 |
+
df = DataFrame([{"A": 1, "B": 2, "C": 3}]).set_index(["A", "B"])
|
| 274 |
+
engine_kwargs = {"nopython": nopython, "nogil": nogil, "parallel": parallel}
|
| 275 |
+
result = df.groupby("A").transform(
|
| 276 |
+
numba_func, engine="numba", engine_kwargs=engine_kwargs
|
| 277 |
+
)
|
| 278 |
+
expected = DataFrame([{"A": 1, "B": 2, "C": 1.0}]).set_index(["A", "B"])
|
| 279 |
+
tm.assert_frame_equal(result, expected)
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
def test_multiindex_multi_key_not_supported(nogil, parallel, nopython):
|
| 283 |
+
pytest.importorskip("numba")
|
| 284 |
+
|
| 285 |
+
def numba_func(values, index):
|
| 286 |
+
return 1
|
| 287 |
+
|
| 288 |
+
df = DataFrame([{"A": 1, "B": 2, "C": 3}]).set_index(["A", "B"])
|
| 289 |
+
engine_kwargs = {"nopython": nopython, "nogil": nogil, "parallel": parallel}
|
| 290 |
+
with pytest.raises(NotImplementedError, match="more than 1 grouping labels"):
|
| 291 |
+
df.groupby(["A", "B"]).transform(
|
| 292 |
+
numba_func, engine="numba", engine_kwargs=engine_kwargs
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def test_multilabel_numba_vs_cython(numba_supported_reductions):
|
| 297 |
+
pytest.importorskip("numba")
|
| 298 |
+
reduction, kwargs = numba_supported_reductions
|
| 299 |
+
df = DataFrame(
|
| 300 |
+
{
|
| 301 |
+
"A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
|
| 302 |
+
"B": ["one", "one", "two", "three", "two", "two", "one", "three"],
|
| 303 |
+
"C": np.random.default_rng(2).standard_normal(8),
|
| 304 |
+
"D": np.random.default_rng(2).standard_normal(8),
|
| 305 |
+
}
|
| 306 |
+
)
|
| 307 |
+
gb = df.groupby(["A", "B"])
|
| 308 |
+
res_agg = gb.transform(reduction, engine="numba", **kwargs)
|
| 309 |
+
expected_agg = gb.transform(reduction, engine="cython", **kwargs)
|
| 310 |
+
tm.assert_frame_equal(res_agg, expected_agg)
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
def test_multilabel_udf_numba_vs_cython():
|
| 314 |
+
pytest.importorskip("numba")
|
| 315 |
+
df = DataFrame(
|
| 316 |
+
{
|
| 317 |
+
"A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
|
| 318 |
+
"B": ["one", "one", "two", "three", "two", "two", "one", "three"],
|
| 319 |
+
"C": np.random.default_rng(2).standard_normal(8),
|
| 320 |
+
"D": np.random.default_rng(2).standard_normal(8),
|
| 321 |
+
}
|
| 322 |
+
)
|
| 323 |
+
gb = df.groupby(["A", "B"])
|
| 324 |
+
result = gb.transform(
|
| 325 |
+
lambda values, index: (values - values.min()) / (values.max() - values.min()),
|
| 326 |
+
engine="numba",
|
| 327 |
+
)
|
| 328 |
+
expected = gb.transform(
|
| 329 |
+
lambda x: (x - x.min()) / (x.max() - x.min()), engine="cython"
|
| 330 |
+
)
|
| 331 |
+
tm.assert_frame_equal(result, expected)
|
.venv/lib/python3.13/site-packages/pandas/tests/groupby/transform/test_transform.py
ADDED
|
@@ -0,0 +1,1600 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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core.dtypes.common import ensure_platform_int
|
| 10 |
+
|
| 11 |
+
import pandas as pd
|
| 12 |
+
from pandas import (
|
| 13 |
+
Categorical,
|
| 14 |
+
DataFrame,
|
| 15 |
+
Index,
|
| 16 |
+
MultiIndex,
|
| 17 |
+
Series,
|
| 18 |
+
Timestamp,
|
| 19 |
+
concat,
|
| 20 |
+
date_range,
|
| 21 |
+
)
|
| 22 |
+
import pandas._testing as tm
|
| 23 |
+
from pandas.tests.groupby import get_groupby_method_args
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def assert_fp_equal(a, b):
|
| 27 |
+
assert (np.abs(a - b) < 1e-12).all()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_transform():
|
| 31 |
+
data = Series(np.arange(9) // 3, index=np.arange(9))
|
| 32 |
+
|
| 33 |
+
index = np.arange(9)
|
| 34 |
+
np.random.default_rng(2).shuffle(index)
|
| 35 |
+
data = data.reindex(index)
|
| 36 |
+
|
| 37 |
+
grouped = data.groupby(lambda x: x // 3)
|
| 38 |
+
|
| 39 |
+
transformed = grouped.transform(lambda x: x * x.sum())
|
| 40 |
+
assert transformed[7] == 12
|
| 41 |
+
|
| 42 |
+
# GH 8046
|
| 43 |
+
# make sure that we preserve the input order
|
| 44 |
+
|
| 45 |
+
df = DataFrame(
|
| 46 |
+
np.arange(6, dtype="int64").reshape(3, 2), columns=["a", "b"], index=[0, 2, 1]
|
| 47 |
+
)
|
| 48 |
+
key = [0, 0, 1]
|
| 49 |
+
expected = (
|
| 50 |
+
df.sort_index()
|
| 51 |
+
.groupby(key)
|
| 52 |
+
.transform(lambda x: x - x.mean())
|
| 53 |
+
.groupby(key)
|
| 54 |
+
.mean()
|
| 55 |
+
)
|
| 56 |
+
result = df.groupby(key).transform(lambda x: x - x.mean()).groupby(key).mean()
|
| 57 |
+
tm.assert_frame_equal(result, expected)
|
| 58 |
+
|
| 59 |
+
def demean(arr):
|
| 60 |
+
return arr - arr.mean(axis=0)
|
| 61 |
+
|
| 62 |
+
people = DataFrame(
|
| 63 |
+
np.random.default_rng(2).standard_normal((5, 5)),
|
| 64 |
+
columns=["a", "b", "c", "d", "e"],
|
| 65 |
+
index=["Joe", "Steve", "Wes", "Jim", "Travis"],
|
| 66 |
+
)
|
| 67 |
+
key = ["one", "two", "one", "two", "one"]
|
| 68 |
+
result = people.groupby(key).transform(demean).groupby(key).mean()
|
| 69 |
+
expected = people.groupby(key, group_keys=False).apply(demean).groupby(key).mean()
|
| 70 |
+
tm.assert_frame_equal(result, expected)
|
| 71 |
+
|
| 72 |
+
# GH 8430
|
| 73 |
+
df = DataFrame(
|
| 74 |
+
np.random.default_rng(2).standard_normal((50, 4)),
|
| 75 |
+
columns=Index(list("ABCD"), dtype=object),
|
| 76 |
+
index=date_range("2000-01-01", periods=50, freq="B"),
|
| 77 |
+
)
|
| 78 |
+
g = df.groupby(pd.Grouper(freq="ME"))
|
| 79 |
+
g.transform(lambda x: x - 1)
|
| 80 |
+
|
| 81 |
+
# GH 9700
|
| 82 |
+
df = DataFrame({"a": range(5, 10), "b": range(5)})
|
| 83 |
+
result = df.groupby("a").transform(max)
|
| 84 |
+
expected = DataFrame({"b": range(5)})
|
| 85 |
+
tm.assert_frame_equal(result, expected)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def test_transform_fast():
|
| 89 |
+
df = DataFrame(
|
| 90 |
+
{
|
| 91 |
+
"id": np.arange(10) / 3,
|
| 92 |
+
"val": np.random.default_rng(2).standard_normal(10),
|
| 93 |
+
}
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
grp = df.groupby("id")["val"]
|
| 97 |
+
|
| 98 |
+
values = np.repeat(grp.mean().values, ensure_platform_int(grp.count().values))
|
| 99 |
+
expected = Series(values, index=df.index, name="val")
|
| 100 |
+
|
| 101 |
+
result = grp.transform(np.mean)
|
| 102 |
+
tm.assert_series_equal(result, expected)
|
| 103 |
+
|
| 104 |
+
result = grp.transform("mean")
|
| 105 |
+
tm.assert_series_equal(result, expected)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def test_transform_fast2():
|
| 109 |
+
# GH 12737
|
| 110 |
+
df = DataFrame(
|
| 111 |
+
{
|
| 112 |
+
"grouping": [0, 1, 1, 3],
|
| 113 |
+
"f": [1.1, 2.1, 3.1, 4.5],
|
| 114 |
+
"d": date_range("2014-1-1", "2014-1-4", unit="ns"),
|
| 115 |
+
"i": [1, 2, 3, 4],
|
| 116 |
+
},
|
| 117 |
+
columns=["grouping", "f", "i", "d"],
|
| 118 |
+
)
|
| 119 |
+
result = df.groupby("grouping").transform("first")
|
| 120 |
+
|
| 121 |
+
dates = Index(
|
| 122 |
+
[
|
| 123 |
+
Timestamp("2014-1-1"),
|
| 124 |
+
Timestamp("2014-1-2"),
|
| 125 |
+
Timestamp("2014-1-2"),
|
| 126 |
+
Timestamp("2014-1-4"),
|
| 127 |
+
],
|
| 128 |
+
dtype="M8[ns]",
|
| 129 |
+
)
|
| 130 |
+
expected = DataFrame(
|
| 131 |
+
{"f": [1.1, 2.1, 2.1, 4.5], "d": dates, "i": [1, 2, 2, 4]},
|
| 132 |
+
columns=["f", "i", "d"],
|
| 133 |
+
)
|
| 134 |
+
tm.assert_frame_equal(result, expected)
|
| 135 |
+
|
| 136 |
+
# selection
|
| 137 |
+
result = df.groupby("grouping")[["f", "i"]].transform("first")
|
| 138 |
+
expected = expected[["f", "i"]]
|
| 139 |
+
tm.assert_frame_equal(result, expected)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def test_transform_fast3():
|
| 143 |
+
# dup columns
|
| 144 |
+
df = DataFrame([[1, 2, 3], [4, 5, 6]], columns=["g", "a", "a"])
|
| 145 |
+
result = df.groupby("g").transform("first")
|
| 146 |
+
expected = df.drop("g", axis=1)
|
| 147 |
+
tm.assert_frame_equal(result, expected)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def test_transform_broadcast(tsframe, ts):
|
| 151 |
+
grouped = ts.groupby(lambda x: x.month)
|
| 152 |
+
result = grouped.transform(np.mean)
|
| 153 |
+
|
| 154 |
+
tm.assert_index_equal(result.index, ts.index)
|
| 155 |
+
for _, gp in grouped:
|
| 156 |
+
assert_fp_equal(result.reindex(gp.index), gp.mean())
|
| 157 |
+
|
| 158 |
+
grouped = tsframe.groupby(lambda x: x.month)
|
| 159 |
+
result = grouped.transform(np.mean)
|
| 160 |
+
tm.assert_index_equal(result.index, tsframe.index)
|
| 161 |
+
for _, gp in grouped:
|
| 162 |
+
agged = gp.mean(axis=0)
|
| 163 |
+
res = result.reindex(gp.index)
|
| 164 |
+
for col in tsframe:
|
| 165 |
+
assert_fp_equal(res[col], agged[col])
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def test_transform_axis_ts(tsframe):
|
| 169 |
+
# make sure that we are setting the axes correctly
|
| 170 |
+
# in the presence of a non-monotonic indexer
|
| 171 |
+
# GH12713
|
| 172 |
+
|
| 173 |
+
base = tsframe.iloc[0:5]
|
| 174 |
+
r = len(base.index)
|
| 175 |
+
c = len(base.columns)
|
| 176 |
+
tso = DataFrame(
|
| 177 |
+
np.random.default_rng(2).standard_normal((r, c)),
|
| 178 |
+
index=base.index,
|
| 179 |
+
columns=base.columns,
|
| 180 |
+
dtype="float64",
|
| 181 |
+
)
|
| 182 |
+
# monotonic
|
| 183 |
+
ts = tso
|
| 184 |
+
grouped = ts.groupby(lambda x: x.weekday(), group_keys=False)
|
| 185 |
+
result = ts - grouped.transform("mean")
|
| 186 |
+
expected = grouped.apply(lambda x: x - x.mean(axis=0))
|
| 187 |
+
tm.assert_frame_equal(result, expected)
|
| 188 |
+
|
| 189 |
+
# non-monotonic
|
| 190 |
+
ts = tso.iloc[[1, 0, *list(range(2, len(base)))]]
|
| 191 |
+
grouped = ts.groupby(lambda x: x.weekday(), group_keys=False)
|
| 192 |
+
result = ts - grouped.transform("mean")
|
| 193 |
+
expected = grouped.apply(lambda x: x - x.mean(axis=0))
|
| 194 |
+
tm.assert_frame_equal(result, expected)
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
def test_transform_dtype():
|
| 198 |
+
# GH 9807
|
| 199 |
+
# Check transform dtype output is preserved
|
| 200 |
+
df = DataFrame([[1, 3], [2, 3]])
|
| 201 |
+
result = df.groupby(1).transform("mean")
|
| 202 |
+
expected = DataFrame([[1.5], [1.5]])
|
| 203 |
+
tm.assert_frame_equal(result, expected)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def test_transform_bug():
|
| 207 |
+
# GH 5712
|
| 208 |
+
# transforming on a datetime column
|
| 209 |
+
df = DataFrame({"A": Timestamp("20130101"), "B": np.arange(5)})
|
| 210 |
+
result = df.groupby("A")["B"].transform(lambda x: x.rank(ascending=False))
|
| 211 |
+
expected = Series(np.arange(5, 0, step=-1), name="B", dtype="float64")
|
| 212 |
+
tm.assert_series_equal(result, expected)
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
def test_transform_numeric_to_boolean():
|
| 216 |
+
# GH 16875
|
| 217 |
+
# inconsistency in transforming boolean values
|
| 218 |
+
expected = Series([True, True], name="A")
|
| 219 |
+
|
| 220 |
+
df = DataFrame({"A": [1.1, 2.2], "B": [1, 2]})
|
| 221 |
+
result = df.groupby("B").A.transform(lambda x: True)
|
| 222 |
+
tm.assert_series_equal(result, expected)
|
| 223 |
+
|
| 224 |
+
df = DataFrame({"A": [1, 2], "B": [1, 2]})
|
| 225 |
+
result = df.groupby("B").A.transform(lambda x: True)
|
| 226 |
+
tm.assert_series_equal(result, expected)
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
def test_transform_datetime_to_timedelta():
|
| 230 |
+
# GH 15429
|
| 231 |
+
# transforming a datetime to timedelta
|
| 232 |
+
df = DataFrame({"A": Timestamp("20130101"), "B": np.arange(5)})
|
| 233 |
+
expected = Series(
|
| 234 |
+
Timestamp("20130101") - Timestamp("20130101"), index=range(5), name="A"
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
# this does date math without changing result type in transform
|
| 238 |
+
base_time = df["A"][0]
|
| 239 |
+
result = (
|
| 240 |
+
df.groupby("A")["A"].transform(lambda x: x.max() - x.min() + base_time)
|
| 241 |
+
- base_time
|
| 242 |
+
)
|
| 243 |
+
tm.assert_series_equal(result, expected)
|
| 244 |
+
|
| 245 |
+
# this does date math and causes the transform to return timedelta
|
| 246 |
+
result = df.groupby("A")["A"].transform(lambda x: x.max() - x.min())
|
| 247 |
+
tm.assert_series_equal(result, expected)
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
def test_transform_datetime_to_numeric():
|
| 251 |
+
# GH 10972
|
| 252 |
+
# convert dt to float
|
| 253 |
+
df = DataFrame({"a": 1, "b": date_range("2015-01-01", periods=2, freq="D")})
|
| 254 |
+
result = df.groupby("a").b.transform(
|
| 255 |
+
lambda x: x.dt.dayofweek - x.dt.dayofweek.mean()
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
expected = Series([-0.5, 0.5], name="b")
|
| 259 |
+
tm.assert_series_equal(result, expected)
|
| 260 |
+
|
| 261 |
+
# convert dt to int
|
| 262 |
+
df = DataFrame({"a": 1, "b": date_range("2015-01-01", periods=2, freq="D")})
|
| 263 |
+
result = df.groupby("a").b.transform(
|
| 264 |
+
lambda x: x.dt.dayofweek - x.dt.dayofweek.min()
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
expected = Series([0, 1], dtype=np.int32, name="b")
|
| 268 |
+
tm.assert_series_equal(result, expected)
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
def test_transform_casting():
|
| 272 |
+
# 13046
|
| 273 |
+
times = [
|
| 274 |
+
"13:43:27",
|
| 275 |
+
"14:26:19",
|
| 276 |
+
"14:29:01",
|
| 277 |
+
"18:39:34",
|
| 278 |
+
"18:40:18",
|
| 279 |
+
"18:44:30",
|
| 280 |
+
"18:46:00",
|
| 281 |
+
"18:52:15",
|
| 282 |
+
"18:59:59",
|
| 283 |
+
"19:17:48",
|
| 284 |
+
"19:21:38",
|
| 285 |
+
]
|
| 286 |
+
df = DataFrame(
|
| 287 |
+
{
|
| 288 |
+
"A": [f"B-{i}" for i in range(11)],
|
| 289 |
+
"ID3": np.take(
|
| 290 |
+
["a", "b", "c", "d", "e"], [0, 1, 2, 1, 3, 1, 1, 1, 4, 1, 1]
|
| 291 |
+
),
|
| 292 |
+
"DATETIME": pd.to_datetime([f"2014-10-08 {time}" for time in times]),
|
| 293 |
+
},
|
| 294 |
+
index=pd.RangeIndex(11, name="idx"),
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
result = df.groupby("ID3")["DATETIME"].transform(lambda x: x.diff())
|
| 298 |
+
assert lib.is_np_dtype(result.dtype, "m")
|
| 299 |
+
|
| 300 |
+
result = df[["ID3", "DATETIME"]].groupby("ID3").transform(lambda x: x.diff())
|
| 301 |
+
assert lib.is_np_dtype(result.DATETIME.dtype, "m")
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
def test_transform_multiple(ts):
|
| 305 |
+
grouped = ts.groupby([lambda x: x.year, lambda x: x.month])
|
| 306 |
+
grouped.transform(lambda x: x * 2)
|
| 307 |
+
grouped.transform(np.mean)
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
def test_dispatch_transform(tsframe):
|
| 311 |
+
df = tsframe[::5].reindex(tsframe.index)
|
| 312 |
+
|
| 313 |
+
grouped = df.groupby(lambda x: x.month)
|
| 314 |
+
|
| 315 |
+
filled = grouped.ffill()
|
| 316 |
+
fillit = lambda x: x.ffill()
|
| 317 |
+
expected = df.groupby(lambda x: x.month).transform(fillit)
|
| 318 |
+
tm.assert_frame_equal(filled, expected)
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
def test_transform_transformation_func(transformation_func):
|
| 322 |
+
# GH 30918
|
| 323 |
+
df = DataFrame(
|
| 324 |
+
{
|
| 325 |
+
"A": ["foo", "foo", "foo", "foo", "bar", "bar", "baz"],
|
| 326 |
+
"B": [1, 2, np.nan, 3, 3, np.nan, 4],
|
| 327 |
+
},
|
| 328 |
+
index=date_range("2020-01-01", "2020-01-07"),
|
| 329 |
+
)
|
| 330 |
+
if transformation_func == "cumcount":
|
| 331 |
+
test_op = lambda x: x.transform("cumcount")
|
| 332 |
+
mock_op = lambda x: Series(range(len(x)), x.index)
|
| 333 |
+
elif transformation_func == "ngroup":
|
| 334 |
+
test_op = lambda x: x.transform("ngroup")
|
| 335 |
+
counter = -1
|
| 336 |
+
|
| 337 |
+
def mock_op(x):
|
| 338 |
+
nonlocal counter
|
| 339 |
+
counter += 1
|
| 340 |
+
return Series(counter, index=x.index)
|
| 341 |
+
|
| 342 |
+
else:
|
| 343 |
+
test_op = lambda x: x.transform(transformation_func)
|
| 344 |
+
mock_op = lambda x: getattr(x, transformation_func)()
|
| 345 |
+
|
| 346 |
+
result = test_op(df.groupby("A"))
|
| 347 |
+
|
| 348 |
+
# pass the group in same order as iterating `for ... in df.groupby(...)`
|
| 349 |
+
# but reorder to match df's index since this is a transform
|
| 350 |
+
groups = [df[["B"]].iloc[4:6], df[["B"]].iloc[6:], df[["B"]].iloc[:4]]
|
| 351 |
+
expected = concat([mock_op(g) for g in groups]).sort_index()
|
| 352 |
+
# sort_index does not preserve the freq
|
| 353 |
+
expected = expected.set_axis(df.index)
|
| 354 |
+
|
| 355 |
+
if transformation_func in ("cumcount", "ngroup"):
|
| 356 |
+
tm.assert_series_equal(result, expected)
|
| 357 |
+
else:
|
| 358 |
+
tm.assert_frame_equal(result, expected)
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
def test_transform_select_columns(df):
|
| 362 |
+
f = lambda x: x.mean()
|
| 363 |
+
result = df.groupby("A")[["C", "D"]].transform(f)
|
| 364 |
+
|
| 365 |
+
selection = df[["C", "D"]]
|
| 366 |
+
expected = selection.groupby(df["A"]).transform(f)
|
| 367 |
+
|
| 368 |
+
tm.assert_frame_equal(result, expected)
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
def test_transform_nuisance_raises(df, using_infer_string):
|
| 372 |
+
# case that goes through _transform_item_by_item
|
| 373 |
+
|
| 374 |
+
df.columns = ["A", "B", "B", "D"]
|
| 375 |
+
|
| 376 |
+
# this also tests orderings in transform between
|
| 377 |
+
# series/frame to make sure it's consistent
|
| 378 |
+
grouped = df.groupby("A")
|
| 379 |
+
|
| 380 |
+
gbc = grouped["B"]
|
| 381 |
+
msg = "Could not convert"
|
| 382 |
+
if using_infer_string:
|
| 383 |
+
msg = "Cannot perform reduction 'mean' with string dtype"
|
| 384 |
+
with pytest.raises(TypeError, match=msg):
|
| 385 |
+
gbc.transform(lambda x: np.mean(x))
|
| 386 |
+
|
| 387 |
+
with pytest.raises(TypeError, match=msg):
|
| 388 |
+
df.groupby("A").transform(lambda x: np.mean(x))
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
def test_transform_function_aliases(df):
|
| 392 |
+
result = df.groupby("A").transform("mean", numeric_only=True)
|
| 393 |
+
expected = df.groupby("A")[["C", "D"]].transform(np.mean)
|
| 394 |
+
tm.assert_frame_equal(result, expected)
|
| 395 |
+
|
| 396 |
+
result = df.groupby("A")["C"].transform("mean")
|
| 397 |
+
expected = df.groupby("A")["C"].transform(np.mean)
|
| 398 |
+
tm.assert_series_equal(result, expected)
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
def test_series_fast_transform_date():
|
| 402 |
+
# GH 13191
|
| 403 |
+
df = DataFrame(
|
| 404 |
+
{
|
| 405 |
+
"grouping": [np.nan, 1, 1, 3],
|
| 406 |
+
"d": date_range("2014-1-1", "2014-1-4", unit="ns"),
|
| 407 |
+
}
|
| 408 |
+
)
|
| 409 |
+
result = df.groupby("grouping")["d"].transform("first")
|
| 410 |
+
dates = [
|
| 411 |
+
pd.NaT,
|
| 412 |
+
Timestamp("2014-1-2"),
|
| 413 |
+
Timestamp("2014-1-2"),
|
| 414 |
+
Timestamp("2014-1-4"),
|
| 415 |
+
]
|
| 416 |
+
expected = Series(dates, name="d", dtype="M8[ns]")
|
| 417 |
+
tm.assert_series_equal(result, expected)
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
@pytest.mark.parametrize("func", [lambda x: np.nansum(x), sum])
|
| 421 |
+
def test_transform_length(func):
|
| 422 |
+
# GH 9697
|
| 423 |
+
df = DataFrame({"col1": [1, 1, 2, 2], "col2": [1, 2, 3, np.nan]})
|
| 424 |
+
if func is sum:
|
| 425 |
+
expected = Series([3.0, 3.0, np.nan, np.nan])
|
| 426 |
+
else:
|
| 427 |
+
expected = Series([3.0] * 4)
|
| 428 |
+
|
| 429 |
+
results = [
|
| 430 |
+
df.groupby("col1").transform(func)["col2"],
|
| 431 |
+
df.groupby("col1")["col2"].transform(func),
|
| 432 |
+
]
|
| 433 |
+
for result in results:
|
| 434 |
+
tm.assert_series_equal(result, expected, check_names=False)
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
def test_transform_coercion():
|
| 438 |
+
# 14457
|
| 439 |
+
# when we are transforming be sure to not coerce
|
| 440 |
+
# via assignment
|
| 441 |
+
df = DataFrame({"A": ["a", "a", "b", "b"], "B": [0, 1, 3, 4]})
|
| 442 |
+
g = df.groupby("A")
|
| 443 |
+
|
| 444 |
+
expected = g.transform(np.mean)
|
| 445 |
+
result = g.transform(lambda x: np.mean(x, axis=0))
|
| 446 |
+
tm.assert_frame_equal(result, expected)
|
| 447 |
+
|
| 448 |
+
|
| 449 |
+
def test_groupby_transform_with_int(using_infer_string):
|
| 450 |
+
# GH 3740, make sure that we might upcast on item-by-item transform
|
| 451 |
+
|
| 452 |
+
# floats
|
| 453 |
+
df = DataFrame(
|
| 454 |
+
{
|
| 455 |
+
"A": [1, 1, 1, 2, 2, 2],
|
| 456 |
+
"B": Series(1, dtype="float64"),
|
| 457 |
+
"C": Series([1, 2, 3, 1, 2, 3], dtype="float64"),
|
| 458 |
+
"D": "foo",
|
| 459 |
+
}
|
| 460 |
+
)
|
| 461 |
+
with np.errstate(all="ignore"):
|
| 462 |
+
result = df.groupby("A")[["B", "C"]].transform(
|
| 463 |
+
lambda x: (x - x.mean()) / x.std()
|
| 464 |
+
)
|
| 465 |
+
expected = DataFrame(
|
| 466 |
+
{"B": np.nan, "C": Series([-1, 0, 1, -1, 0, 1], dtype="float64")}
|
| 467 |
+
)
|
| 468 |
+
tm.assert_frame_equal(result, expected)
|
| 469 |
+
|
| 470 |
+
# int case
|
| 471 |
+
df = DataFrame(
|
| 472 |
+
{
|
| 473 |
+
"A": [1, 1, 1, 2, 2, 2],
|
| 474 |
+
"B": 1,
|
| 475 |
+
"C": [1, 2, 3, 1, 2, 3],
|
| 476 |
+
"D": "foo",
|
| 477 |
+
}
|
| 478 |
+
)
|
| 479 |
+
msg = "Could not convert"
|
| 480 |
+
if using_infer_string:
|
| 481 |
+
msg = "Cannot perform reduction 'mean' with string dtype"
|
| 482 |
+
with np.errstate(all="ignore"):
|
| 483 |
+
with pytest.raises(TypeError, match=msg):
|
| 484 |
+
df.groupby("A").transform(lambda x: (x - x.mean()) / x.std())
|
| 485 |
+
result = df.groupby("A")[["B", "C"]].transform(
|
| 486 |
+
lambda x: (x - x.mean()) / x.std()
|
| 487 |
+
)
|
| 488 |
+
expected = DataFrame({"B": np.nan, "C": [-1.0, 0.0, 1.0, -1.0, 0.0, 1.0]})
|
| 489 |
+
tm.assert_frame_equal(result, expected)
|
| 490 |
+
|
| 491 |
+
# int that needs float conversion
|
| 492 |
+
s = Series([2, 3, 4, 10, 5, -1])
|
| 493 |
+
df = DataFrame({"A": [1, 1, 1, 2, 2, 2], "B": 1, "C": s, "D": "foo"})
|
| 494 |
+
with np.errstate(all="ignore"):
|
| 495 |
+
with pytest.raises(TypeError, match=msg):
|
| 496 |
+
df.groupby("A").transform(lambda x: (x - x.mean()) / x.std())
|
| 497 |
+
result = df.groupby("A")[["B", "C"]].transform(
|
| 498 |
+
lambda x: (x - x.mean()) / x.std()
|
| 499 |
+
)
|
| 500 |
+
|
| 501 |
+
s1 = s.iloc[0:3]
|
| 502 |
+
s1 = (s1 - s1.mean()) / s1.std()
|
| 503 |
+
s2 = s.iloc[3:6]
|
| 504 |
+
s2 = (s2 - s2.mean()) / s2.std()
|
| 505 |
+
expected = DataFrame({"B": np.nan, "C": concat([s1, s2])})
|
| 506 |
+
tm.assert_frame_equal(result, expected)
|
| 507 |
+
|
| 508 |
+
# int doesn't get downcasted
|
| 509 |
+
result = df.groupby("A")[["B", "C"]].transform(lambda x: x * 2 / 2)
|
| 510 |
+
expected = DataFrame({"B": 1.0, "C": [2.0, 3.0, 4.0, 10.0, 5.0, -1.0]})
|
| 511 |
+
tm.assert_frame_equal(result, expected)
|
| 512 |
+
|
| 513 |
+
|
| 514 |
+
def test_groupby_transform_with_nan_group():
|
| 515 |
+
# GH 9941
|
| 516 |
+
df = DataFrame({"a": range(10), "b": [1, 1, 2, 3, np.nan, 4, 4, 5, 5, 5]})
|
| 517 |
+
result = df.groupby(df.b)["a"].transform(max)
|
| 518 |
+
expected = Series([1.0, 1.0, 2.0, 3.0, np.nan, 6.0, 6.0, 9.0, 9.0, 9.0], name="a")
|
| 519 |
+
tm.assert_series_equal(result, expected)
|
| 520 |
+
|
| 521 |
+
|
| 522 |
+
def test_transform_mixed_type():
|
| 523 |
+
index = MultiIndex.from_arrays([[0, 0, 0, 1, 1, 1], [1, 2, 3, 1, 2, 3]])
|
| 524 |
+
df = DataFrame(
|
| 525 |
+
{
|
| 526 |
+
"d": [1.0, 1.0, 1.0, 2.0, 2.0, 2.0],
|
| 527 |
+
"c": np.tile(["a", "b", "c"], 2),
|
| 528 |
+
"v": np.arange(1.0, 7.0),
|
| 529 |
+
},
|
| 530 |
+
index=index,
|
| 531 |
+
)
|
| 532 |
+
|
| 533 |
+
def f(group):
|
| 534 |
+
group["g"] = group["d"] * 2
|
| 535 |
+
return group[:1]
|
| 536 |
+
|
| 537 |
+
grouped = df.groupby("c")
|
| 538 |
+
result = grouped.apply(f)
|
| 539 |
+
|
| 540 |
+
assert result["d"].dtype == np.float64
|
| 541 |
+
|
| 542 |
+
# this is by definition a mutating operation!
|
| 543 |
+
for key, group in grouped:
|
| 544 |
+
res = f(group.drop(columns="c"))
|
| 545 |
+
tm.assert_frame_equal(res, result.loc[key])
|
| 546 |
+
|
| 547 |
+
|
| 548 |
+
@pytest.mark.parametrize(
|
| 549 |
+
"op, args, targop",
|
| 550 |
+
[
|
| 551 |
+
("cumprod", (), lambda x: x.cumprod()),
|
| 552 |
+
("cumsum", (), lambda x: x.cumsum()),
|
| 553 |
+
("shift", (-1,), lambda x: x.shift(-1)),
|
| 554 |
+
("shift", (1,), lambda x: x.shift()),
|
| 555 |
+
],
|
| 556 |
+
)
|
| 557 |
+
def test_cython_transform_series(op, args, targop):
|
| 558 |
+
# GH 4095
|
| 559 |
+
s = Series(np.random.default_rng(2).standard_normal(1000))
|
| 560 |
+
s_missing = s.copy()
|
| 561 |
+
s_missing.iloc[2:10] = np.nan
|
| 562 |
+
labels = np.random.default_rng(2).integers(0, 50, size=1000).astype(float)
|
| 563 |
+
|
| 564 |
+
# series
|
| 565 |
+
for data in [s, s_missing]:
|
| 566 |
+
# print(data.head())
|
| 567 |
+
expected = data.groupby(labels).transform(targop)
|
| 568 |
+
|
| 569 |
+
tm.assert_series_equal(expected, data.groupby(labels).transform(op, *args))
|
| 570 |
+
tm.assert_series_equal(expected, getattr(data.groupby(labels), op)(*args))
|
| 571 |
+
|
| 572 |
+
|
| 573 |
+
@pytest.mark.parametrize("op", ["cumprod", "cumsum"])
|
| 574 |
+
@pytest.mark.parametrize(
|
| 575 |
+
"input, exp",
|
| 576 |
+
[
|
| 577 |
+
# When everything is NaN
|
| 578 |
+
({"key": ["b"] * 10, "value": np.nan}, Series([np.nan] * 10, name="value")),
|
| 579 |
+
# When there is a single NaN
|
| 580 |
+
(
|
| 581 |
+
{"key": ["b"] * 10 + ["a"] * 2, "value": [3] * 3 + [np.nan] + [3] * 8},
|
| 582 |
+
{
|
| 583 |
+
("cumprod", False): [3.0, 9.0, 27.0] + [np.nan] * 7 + [3.0, 9.0],
|
| 584 |
+
("cumprod", True): [
|
| 585 |
+
3.0,
|
| 586 |
+
9.0,
|
| 587 |
+
27.0,
|
| 588 |
+
np.nan,
|
| 589 |
+
81.0,
|
| 590 |
+
243.0,
|
| 591 |
+
729.0,
|
| 592 |
+
2187.0,
|
| 593 |
+
6561.0,
|
| 594 |
+
19683.0,
|
| 595 |
+
3.0,
|
| 596 |
+
9.0,
|
| 597 |
+
],
|
| 598 |
+
("cumsum", False): [3.0, 6.0, 9.0] + [np.nan] * 7 + [3.0, 6.0],
|
| 599 |
+
("cumsum", True): [
|
| 600 |
+
3.0,
|
| 601 |
+
6.0,
|
| 602 |
+
9.0,
|
| 603 |
+
np.nan,
|
| 604 |
+
12.0,
|
| 605 |
+
15.0,
|
| 606 |
+
18.0,
|
| 607 |
+
21.0,
|
| 608 |
+
24.0,
|
| 609 |
+
27.0,
|
| 610 |
+
3.0,
|
| 611 |
+
6.0,
|
| 612 |
+
],
|
| 613 |
+
},
|
| 614 |
+
),
|
| 615 |
+
],
|
| 616 |
+
)
|
| 617 |
+
def test_groupby_cum_skipna(op, skipna, input, exp):
|
| 618 |
+
df = DataFrame(input)
|
| 619 |
+
result = df.groupby("key")["value"].transform(op, skipna=skipna)
|
| 620 |
+
if isinstance(exp, dict):
|
| 621 |
+
expected = exp[(op, skipna)]
|
| 622 |
+
else:
|
| 623 |
+
expected = exp
|
| 624 |
+
expected = Series(expected, name="value")
|
| 625 |
+
tm.assert_series_equal(expected, result)
|
| 626 |
+
|
| 627 |
+
|
| 628 |
+
@pytest.fixture
|
| 629 |
+
def frame():
|
| 630 |
+
floating = Series(np.random.default_rng(2).standard_normal(10))
|
| 631 |
+
floating_missing = floating.copy()
|
| 632 |
+
floating_missing.iloc[2:7] = np.nan
|
| 633 |
+
strings = list("abcde") * 2
|
| 634 |
+
strings_missing = strings[:]
|
| 635 |
+
strings_missing[5] = np.nan
|
| 636 |
+
|
| 637 |
+
df = DataFrame(
|
| 638 |
+
{
|
| 639 |
+
"float": floating,
|
| 640 |
+
"float_missing": floating_missing,
|
| 641 |
+
"int": [1, 1, 1, 1, 2] * 2,
|
| 642 |
+
"datetime": date_range("1990-1-1", periods=10),
|
| 643 |
+
"timedelta": pd.timedelta_range(1, freq="s", periods=10),
|
| 644 |
+
"string": strings,
|
| 645 |
+
"string_missing": strings_missing,
|
| 646 |
+
"cat": Categorical(strings),
|
| 647 |
+
},
|
| 648 |
+
)
|
| 649 |
+
return df
|
| 650 |
+
|
| 651 |
+
|
| 652 |
+
@pytest.fixture
|
| 653 |
+
def frame_mi(frame):
|
| 654 |
+
frame.index = MultiIndex.from_product([range(5), range(2)])
|
| 655 |
+
return frame
|
| 656 |
+
|
| 657 |
+
|
| 658 |
+
@pytest.mark.slow
|
| 659 |
+
@pytest.mark.parametrize(
|
| 660 |
+
"op, args, targop",
|
| 661 |
+
[
|
| 662 |
+
("cumprod", (), lambda x: x.cumprod()),
|
| 663 |
+
("cumsum", (), lambda x: x.cumsum()),
|
| 664 |
+
("shift", (-1,), lambda x: x.shift(-1)),
|
| 665 |
+
("shift", (1,), lambda x: x.shift()),
|
| 666 |
+
],
|
| 667 |
+
)
|
| 668 |
+
@pytest.mark.parametrize("df_fix", ["frame", "frame_mi"])
|
| 669 |
+
@pytest.mark.parametrize(
|
| 670 |
+
"gb_target",
|
| 671 |
+
[
|
| 672 |
+
{"by": np.random.default_rng(2).integers(0, 50, size=10).astype(float)},
|
| 673 |
+
{"level": 0},
|
| 674 |
+
{"by": "string"},
|
| 675 |
+
pytest.param({"by": "string_missing"}, marks=pytest.mark.xfail),
|
| 676 |
+
{"by": ["int", "string"]},
|
| 677 |
+
],
|
| 678 |
+
)
|
| 679 |
+
def test_cython_transform_frame(request, op, args, targop, df_fix, gb_target):
|
| 680 |
+
df = request.getfixturevalue(df_fix)
|
| 681 |
+
gb = df.groupby(group_keys=False, **gb_target)
|
| 682 |
+
|
| 683 |
+
if op != "shift" and "int" not in gb_target:
|
| 684 |
+
# numeric apply fastpath promotes dtype so have
|
| 685 |
+
# to apply separately and concat
|
| 686 |
+
i = gb[["int"]].apply(targop)
|
| 687 |
+
f = gb[["float", "float_missing"]].apply(targop)
|
| 688 |
+
expected = concat([f, i], axis=1)
|
| 689 |
+
else:
|
| 690 |
+
expected = gb.apply(targop)
|
| 691 |
+
|
| 692 |
+
expected = expected.sort_index(axis=1)
|
| 693 |
+
if op == "shift":
|
| 694 |
+
expected["string_missing"] = expected["string_missing"].fillna(np.nan)
|
| 695 |
+
by = gb_target.get("by")
|
| 696 |
+
if not isinstance(by, (str, list)) or (by != "string" and "string" not in by):
|
| 697 |
+
expected["string"] = expected["string"].fillna(np.nan)
|
| 698 |
+
|
| 699 |
+
result = gb[expected.columns].transform(op, *args).sort_index(axis=1)
|
| 700 |
+
tm.assert_frame_equal(result, expected)
|
| 701 |
+
result = getattr(gb[expected.columns], op)(*args).sort_index(axis=1)
|
| 702 |
+
tm.assert_frame_equal(result, expected)
|
| 703 |
+
|
| 704 |
+
|
| 705 |
+
@pytest.mark.slow
|
| 706 |
+
@pytest.mark.parametrize(
|
| 707 |
+
"op, args, targop",
|
| 708 |
+
[
|
| 709 |
+
("cumprod", (), lambda x: x.cumprod()),
|
| 710 |
+
("cumsum", (), lambda x: x.cumsum()),
|
| 711 |
+
("shift", (-1,), lambda x: x.shift(-1)),
|
| 712 |
+
("shift", (1,), lambda x: x.shift()),
|
| 713 |
+
],
|
| 714 |
+
)
|
| 715 |
+
@pytest.mark.parametrize("df_fix", ["frame", "frame_mi"])
|
| 716 |
+
@pytest.mark.parametrize(
|
| 717 |
+
"gb_target",
|
| 718 |
+
[
|
| 719 |
+
{"by": np.random.default_rng(2).integers(0, 50, size=10).astype(float)},
|
| 720 |
+
{"level": 0},
|
| 721 |
+
{"by": "string"},
|
| 722 |
+
# TODO: create xfail condition given other params
|
| 723 |
+
# {"by": 'string_missing'},
|
| 724 |
+
{"by": ["int", "string"]},
|
| 725 |
+
],
|
| 726 |
+
)
|
| 727 |
+
@pytest.mark.parametrize(
|
| 728 |
+
"column",
|
| 729 |
+
[
|
| 730 |
+
"float",
|
| 731 |
+
"float_missing",
|
| 732 |
+
"int",
|
| 733 |
+
"datetime",
|
| 734 |
+
"timedelta",
|
| 735 |
+
"string",
|
| 736 |
+
"string_missing",
|
| 737 |
+
],
|
| 738 |
+
)
|
| 739 |
+
def test_cython_transform_frame_column(
|
| 740 |
+
request, op, args, targop, df_fix, gb_target, column
|
| 741 |
+
):
|
| 742 |
+
df = request.getfixturevalue(df_fix)
|
| 743 |
+
gb = df.groupby(group_keys=False, **gb_target)
|
| 744 |
+
c = column
|
| 745 |
+
if (
|
| 746 |
+
c not in ["float", "int", "float_missing"]
|
| 747 |
+
and op != "shift"
|
| 748 |
+
and not (c == "timedelta" and op == "cumsum")
|
| 749 |
+
):
|
| 750 |
+
msg = "|".join(
|
| 751 |
+
[
|
| 752 |
+
"does not support .* operations",
|
| 753 |
+
"does not support operation",
|
| 754 |
+
".* is not supported for object dtype",
|
| 755 |
+
"is not implemented for this dtype",
|
| 756 |
+
".* is not supported for str dtype",
|
| 757 |
+
]
|
| 758 |
+
)
|
| 759 |
+
with pytest.raises(TypeError, match=msg):
|
| 760 |
+
gb[c].transform(op)
|
| 761 |
+
with pytest.raises(TypeError, match=msg):
|
| 762 |
+
getattr(gb[c], op)()
|
| 763 |
+
else:
|
| 764 |
+
expected = gb[c].apply(targop)
|
| 765 |
+
expected.name = c
|
| 766 |
+
if c in ["string_missing", "string"]:
|
| 767 |
+
expected = expected.fillna(np.nan)
|
| 768 |
+
|
| 769 |
+
res = gb[c].transform(op, *args)
|
| 770 |
+
tm.assert_series_equal(expected, res)
|
| 771 |
+
res2 = getattr(gb[c], op)(*args)
|
| 772 |
+
tm.assert_series_equal(expected, res2)
|
| 773 |
+
|
| 774 |
+
|
| 775 |
+
@pytest.mark.parametrize(
|
| 776 |
+
"cols,expected",
|
| 777 |
+
[
|
| 778 |
+
("a", Series([1, 1, 1], name="a")),
|
| 779 |
+
(
|
| 780 |
+
["a", "c"],
|
| 781 |
+
DataFrame({"a": [1, 1, 1], "c": [1, 1, 1]}),
|
| 782 |
+
),
|
| 783 |
+
],
|
| 784 |
+
)
|
| 785 |
+
@pytest.mark.parametrize("agg_func", ["count", "rank", "size"])
|
| 786 |
+
def test_transform_numeric_ret(cols, expected, agg_func):
|
| 787 |
+
# GH#19200 and GH#27469
|
| 788 |
+
df = DataFrame(
|
| 789 |
+
{"a": date_range("2018-01-01", periods=3), "b": range(3), "c": range(7, 10)}
|
| 790 |
+
)
|
| 791 |
+
result = df.groupby("b")[cols].transform(agg_func)
|
| 792 |
+
|
| 793 |
+
if agg_func == "rank":
|
| 794 |
+
expected = expected.astype("float")
|
| 795 |
+
elif agg_func == "size" and cols == ["a", "c"]:
|
| 796 |
+
# transform("size") returns a Series
|
| 797 |
+
expected = expected["a"].rename(None)
|
| 798 |
+
tm.assert_equal(result, expected)
|
| 799 |
+
|
| 800 |
+
|
| 801 |
+
def test_transform_ffill():
|
| 802 |
+
# GH 24211
|
| 803 |
+
data = [["a", 0.0], ["a", float("nan")], ["b", 1.0], ["b", float("nan")]]
|
| 804 |
+
df = DataFrame(data, columns=["key", "values"])
|
| 805 |
+
result = df.groupby("key").transform("ffill")
|
| 806 |
+
expected = DataFrame({"values": [0.0, 0.0, 1.0, 1.0]})
|
| 807 |
+
tm.assert_frame_equal(result, expected)
|
| 808 |
+
result = df.groupby("key")["values"].transform("ffill")
|
| 809 |
+
expected = Series([0.0, 0.0, 1.0, 1.0], name="values")
|
| 810 |
+
tm.assert_series_equal(result, expected)
|
| 811 |
+
|
| 812 |
+
|
| 813 |
+
@pytest.mark.parametrize("mix_groupings", [True, False])
|
| 814 |
+
@pytest.mark.parametrize("as_series", [True, False])
|
| 815 |
+
@pytest.mark.parametrize("val1,val2", [("foo", "bar"), (1, 2), (1.0, 2.0)])
|
| 816 |
+
@pytest.mark.parametrize(
|
| 817 |
+
"fill_method,limit,exp_vals",
|
| 818 |
+
[
|
| 819 |
+
(
|
| 820 |
+
"ffill",
|
| 821 |
+
None,
|
| 822 |
+
[np.nan, np.nan, "val1", "val1", "val1", "val2", "val2", "val2"],
|
| 823 |
+
),
|
| 824 |
+
("ffill", 1, [np.nan, np.nan, "val1", "val1", np.nan, "val2", "val2", np.nan]),
|
| 825 |
+
(
|
| 826 |
+
"bfill",
|
| 827 |
+
None,
|
| 828 |
+
["val1", "val1", "val1", "val2", "val2", "val2", np.nan, np.nan],
|
| 829 |
+
),
|
| 830 |
+
("bfill", 1, [np.nan, "val1", "val1", np.nan, "val2", "val2", np.nan, np.nan]),
|
| 831 |
+
],
|
| 832 |
+
)
|
| 833 |
+
def test_group_fill_methods(
|
| 834 |
+
mix_groupings, as_series, val1, val2, fill_method, limit, exp_vals
|
| 835 |
+
):
|
| 836 |
+
vals = [np.nan, np.nan, val1, np.nan, np.nan, val2, np.nan, np.nan]
|
| 837 |
+
_exp_vals = list(exp_vals)
|
| 838 |
+
# Overwrite placeholder values
|
| 839 |
+
for index, exp_val in enumerate(_exp_vals):
|
| 840 |
+
if exp_val == "val1":
|
| 841 |
+
_exp_vals[index] = val1
|
| 842 |
+
elif exp_val == "val2":
|
| 843 |
+
_exp_vals[index] = val2
|
| 844 |
+
|
| 845 |
+
# Need to modify values and expectations depending on the
|
| 846 |
+
# Series / DataFrame that we ultimately want to generate
|
| 847 |
+
if mix_groupings: # ['a', 'b', 'a, 'b', ...]
|
| 848 |
+
keys = ["a", "b"] * len(vals)
|
| 849 |
+
|
| 850 |
+
def interweave(list_obj):
|
| 851 |
+
temp = []
|
| 852 |
+
for x in list_obj:
|
| 853 |
+
temp.extend([x, x])
|
| 854 |
+
|
| 855 |
+
return temp
|
| 856 |
+
|
| 857 |
+
_exp_vals = interweave(_exp_vals)
|
| 858 |
+
vals = interweave(vals)
|
| 859 |
+
else: # ['a', 'a', 'a', ... 'b', 'b', 'b']
|
| 860 |
+
keys = ["a"] * len(vals) + ["b"] * len(vals)
|
| 861 |
+
_exp_vals = _exp_vals * 2
|
| 862 |
+
vals = vals * 2
|
| 863 |
+
|
| 864 |
+
df = DataFrame({"key": keys, "val": vals})
|
| 865 |
+
if as_series:
|
| 866 |
+
result = getattr(df.groupby("key")["val"], fill_method)(limit=limit)
|
| 867 |
+
exp = Series(_exp_vals, name="val")
|
| 868 |
+
tm.assert_series_equal(result, exp)
|
| 869 |
+
else:
|
| 870 |
+
result = getattr(df.groupby("key"), fill_method)(limit=limit)
|
| 871 |
+
exp = DataFrame({"val": _exp_vals})
|
| 872 |
+
tm.assert_frame_equal(result, exp)
|
| 873 |
+
|
| 874 |
+
|
| 875 |
+
@pytest.mark.parametrize("fill_method", ["ffill", "bfill"])
|
| 876 |
+
def test_pad_stable_sorting(fill_method):
|
| 877 |
+
# GH 21207
|
| 878 |
+
x = [0] * 20
|
| 879 |
+
y = [np.nan] * 10 + [1] * 10
|
| 880 |
+
|
| 881 |
+
if fill_method == "bfill":
|
| 882 |
+
y = y[::-1]
|
| 883 |
+
|
| 884 |
+
df = DataFrame({"x": x, "y": y})
|
| 885 |
+
expected = df.drop("x", axis=1)
|
| 886 |
+
|
| 887 |
+
result = getattr(df.groupby("x"), fill_method)()
|
| 888 |
+
|
| 889 |
+
tm.assert_frame_equal(result, expected)
|
| 890 |
+
|
| 891 |
+
|
| 892 |
+
@pytest.mark.parametrize(
|
| 893 |
+
"freq",
|
| 894 |
+
[
|
| 895 |
+
None,
|
| 896 |
+
pytest.param(
|
| 897 |
+
"D",
|
| 898 |
+
marks=pytest.mark.xfail(
|
| 899 |
+
reason="GH#23918 before method uses freq in vectorized approach"
|
| 900 |
+
),
|
| 901 |
+
),
|
| 902 |
+
],
|
| 903 |
+
)
|
| 904 |
+
@pytest.mark.parametrize("periods", [1, -1])
|
| 905 |
+
def test_pct_change(frame_or_series, freq, periods):
|
| 906 |
+
# GH 21200, 21621, 30463
|
| 907 |
+
vals = [3, np.nan, np.nan, np.nan, 1, 2, 4, 10, np.nan, 4]
|
| 908 |
+
keys = ["a", "b"]
|
| 909 |
+
key_v = np.repeat(keys, len(vals))
|
| 910 |
+
df = DataFrame({"key": key_v, "vals": vals * 2})
|
| 911 |
+
|
| 912 |
+
df_g = df
|
| 913 |
+
grp = df_g.groupby(df.key)
|
| 914 |
+
|
| 915 |
+
expected = grp["vals"].obj / grp["vals"].shift(periods) - 1
|
| 916 |
+
|
| 917 |
+
gb = df.groupby("key")
|
| 918 |
+
|
| 919 |
+
if frame_or_series is Series:
|
| 920 |
+
gb = gb["vals"]
|
| 921 |
+
else:
|
| 922 |
+
expected = expected.to_frame("vals")
|
| 923 |
+
|
| 924 |
+
result = gb.pct_change(periods=periods, freq=freq)
|
| 925 |
+
tm.assert_equal(result, expected)
|
| 926 |
+
|
| 927 |
+
|
| 928 |
+
@pytest.mark.parametrize(
|
| 929 |
+
"func, expected_status",
|
| 930 |
+
[
|
| 931 |
+
("ffill", ["shrt", "shrt", "lng", np.nan, "shrt", "ntrl", "ntrl"]),
|
| 932 |
+
("bfill", ["shrt", "lng", "lng", "shrt", "shrt", "ntrl", np.nan]),
|
| 933 |
+
],
|
| 934 |
+
)
|
| 935 |
+
def test_ffill_bfill_non_unique_multilevel(func, expected_status):
|
| 936 |
+
# GH 19437
|
| 937 |
+
date = pd.to_datetime(
|
| 938 |
+
[
|
| 939 |
+
"2018-01-01",
|
| 940 |
+
"2018-01-01",
|
| 941 |
+
"2018-01-01",
|
| 942 |
+
"2018-01-01",
|
| 943 |
+
"2018-01-02",
|
| 944 |
+
"2018-01-01",
|
| 945 |
+
"2018-01-02",
|
| 946 |
+
]
|
| 947 |
+
)
|
| 948 |
+
symbol = ["MSFT", "MSFT", "MSFT", "AAPL", "AAPL", "TSLA", "TSLA"]
|
| 949 |
+
status = ["shrt", np.nan, "lng", np.nan, "shrt", "ntrl", np.nan]
|
| 950 |
+
|
| 951 |
+
df = DataFrame({"date": date, "symbol": symbol, "status": status})
|
| 952 |
+
df = df.set_index(["date", "symbol"])
|
| 953 |
+
result = getattr(df.groupby("symbol")["status"], func)()
|
| 954 |
+
|
| 955 |
+
index = MultiIndex.from_tuples(
|
| 956 |
+
tuples=list(zip(*[date, symbol], strict=True)), names=["date", "symbol"]
|
| 957 |
+
)
|
| 958 |
+
expected = Series(expected_status, index=index, name="status")
|
| 959 |
+
|
| 960 |
+
tm.assert_series_equal(result, expected)
|
| 961 |
+
|
| 962 |
+
|
| 963 |
+
@pytest.mark.parametrize("func", [np.any, np.all])
|
| 964 |
+
def test_any_all_np_func(func):
|
| 965 |
+
# GH 20653
|
| 966 |
+
df = DataFrame(
|
| 967 |
+
[["foo", True], [np.nan, True], ["foo", True]], columns=["key", "val"]
|
| 968 |
+
)
|
| 969 |
+
|
| 970 |
+
exp = Series([True, np.nan, True], name="val")
|
| 971 |
+
|
| 972 |
+
res = df.groupby("key")["val"].transform(func)
|
| 973 |
+
tm.assert_series_equal(res, exp)
|
| 974 |
+
|
| 975 |
+
|
| 976 |
+
def test_groupby_transform_rename():
|
| 977 |
+
# https://github.com/pandas-dev/pandas/issues/23461
|
| 978 |
+
def demean_rename(x):
|
| 979 |
+
result = x - x.mean()
|
| 980 |
+
|
| 981 |
+
if isinstance(x, Series):
|
| 982 |
+
return result
|
| 983 |
+
|
| 984 |
+
result = result.rename(columns={c: f"{c}_demeaned" for c in result.columns})
|
| 985 |
+
|
| 986 |
+
return result
|
| 987 |
+
|
| 988 |
+
df = DataFrame({"group": list("ababa"), "value": [1, 1, 1, 2, 2]})
|
| 989 |
+
expected = DataFrame({"value": [-1.0 / 3, -0.5, -1.0 / 3, 0.5, 2.0 / 3]})
|
| 990 |
+
|
| 991 |
+
result = df.groupby("group").transform(demean_rename)
|
| 992 |
+
tm.assert_frame_equal(result, expected)
|
| 993 |
+
result_single = df.groupby("group").value.transform(demean_rename)
|
| 994 |
+
tm.assert_series_equal(result_single, expected["value"])
|
| 995 |
+
|
| 996 |
+
|
| 997 |
+
@pytest.mark.parametrize("func", [min, max, np.min, np.max, "first", "last"])
|
| 998 |
+
def test_groupby_transform_timezone_column(func):
|
| 999 |
+
# GH 24198
|
| 1000 |
+
ts = pd.to_datetime("now", utc=True).tz_convert("Asia/Singapore")
|
| 1001 |
+
result = DataFrame({"end_time": [ts], "id": [1]})
|
| 1002 |
+
result["max_end_time"] = result.groupby("id").end_time.transform(func)
|
| 1003 |
+
expected = DataFrame([[ts, 1, ts]], columns=["end_time", "id", "max_end_time"])
|
| 1004 |
+
tm.assert_frame_equal(result, expected)
|
| 1005 |
+
|
| 1006 |
+
|
| 1007 |
+
@pytest.mark.parametrize(
|
| 1008 |
+
"func, values",
|
| 1009 |
+
[
|
| 1010 |
+
("idxmin", ["1/1/2011"] * 2 + ["1/3/2011"] * 7 + ["1/10/2011"]),
|
| 1011 |
+
("idxmax", ["1/2/2011"] * 2 + ["1/9/2011"] * 7 + ["1/10/2011"]),
|
| 1012 |
+
],
|
| 1013 |
+
)
|
| 1014 |
+
def test_groupby_transform_with_datetimes(func, values):
|
| 1015 |
+
# GH 15306
|
| 1016 |
+
dates = date_range("1/1/2011", periods=10, freq="D", unit="ns")
|
| 1017 |
+
|
| 1018 |
+
stocks = DataFrame({"price": np.arange(10.0)}, index=dates)
|
| 1019 |
+
stocks["week_id"] = dates.isocalendar().week
|
| 1020 |
+
|
| 1021 |
+
result = stocks.groupby(stocks["week_id"])["price"].transform(func)
|
| 1022 |
+
|
| 1023 |
+
expected = Series(
|
| 1024 |
+
data=pd.to_datetime(values).as_unit("ns"), index=dates, name="price"
|
| 1025 |
+
)
|
| 1026 |
+
|
| 1027 |
+
tm.assert_series_equal(result, expected)
|
| 1028 |
+
|
| 1029 |
+
|
| 1030 |
+
def test_groupby_transform_dtype():
|
| 1031 |
+
# GH 22243
|
| 1032 |
+
df = DataFrame({"a": [1], "val": [1.35]})
|
| 1033 |
+
|
| 1034 |
+
result = df["val"].transform(lambda x: x.map(lambda y: f"+{y}"))
|
| 1035 |
+
expected1 = Series(["+1.35"], name="val")
|
| 1036 |
+
tm.assert_series_equal(result, expected1)
|
| 1037 |
+
|
| 1038 |
+
result = df.groupby("a")["val"].transform(lambda x: x.map(lambda y: f"+{y}"))
|
| 1039 |
+
tm.assert_series_equal(result, expected1)
|
| 1040 |
+
|
| 1041 |
+
result = df.groupby("a")["val"].transform(lambda x: x.map(lambda y: f"+({y})"))
|
| 1042 |
+
expected2 = Series(["+(1.35)"], name="val")
|
| 1043 |
+
tm.assert_series_equal(result, expected2)
|
| 1044 |
+
|
| 1045 |
+
df["val"] = df["val"].astype(object)
|
| 1046 |
+
result = df.groupby("a")["val"].transform(lambda x: x.map(lambda y: f"+{y}"))
|
| 1047 |
+
tm.assert_series_equal(result, expected1)
|
| 1048 |
+
|
| 1049 |
+
|
| 1050 |
+
def test_transform_absent_categories(all_numeric_accumulations):
|
| 1051 |
+
# GH 16771
|
| 1052 |
+
# cython transforms with more groups than rows
|
| 1053 |
+
x_vals = [1]
|
| 1054 |
+
x_cats = range(2)
|
| 1055 |
+
y = [1]
|
| 1056 |
+
df = DataFrame({"x": Categorical(x_vals, x_cats), "y": y})
|
| 1057 |
+
result = getattr(df.y.groupby(df.x, observed=False), all_numeric_accumulations)()
|
| 1058 |
+
expected = df.y
|
| 1059 |
+
tm.assert_series_equal(result, expected)
|
| 1060 |
+
|
| 1061 |
+
|
| 1062 |
+
@pytest.mark.parametrize("func", ["ffill", "bfill", "shift"])
|
| 1063 |
+
@pytest.mark.parametrize("key, val", [("level", 0), ("by", Series([0]))])
|
| 1064 |
+
def test_ffill_not_in_axis(func, key, val):
|
| 1065 |
+
# GH 21521
|
| 1066 |
+
df = DataFrame([[np.nan]])
|
| 1067 |
+
result = getattr(df.groupby(**{key: val}), func)()
|
| 1068 |
+
expected = df
|
| 1069 |
+
|
| 1070 |
+
tm.assert_frame_equal(result, expected)
|
| 1071 |
+
|
| 1072 |
+
|
| 1073 |
+
def test_transform_invalid_name_raises():
|
| 1074 |
+
# GH#27486
|
| 1075 |
+
df = DataFrame({"a": [0, 1, 1, 2]})
|
| 1076 |
+
g = df.groupby(["a", "b", "b", "c"])
|
| 1077 |
+
with pytest.raises(ValueError, match="not a valid function name"):
|
| 1078 |
+
g.transform("some_arbitrary_name")
|
| 1079 |
+
|
| 1080 |
+
# method exists on the object, but is not a valid transformation/agg
|
| 1081 |
+
assert hasattr(g, "aggregate") # make sure the method exists
|
| 1082 |
+
with pytest.raises(ValueError, match="not a valid function name"):
|
| 1083 |
+
g.transform("aggregate")
|
| 1084 |
+
|
| 1085 |
+
# Test SeriesGroupBy
|
| 1086 |
+
g = df["a"].groupby(["a", "b", "b", "c"])
|
| 1087 |
+
with pytest.raises(ValueError, match="not a valid function name"):
|
| 1088 |
+
g.transform("some_arbitrary_name")
|
| 1089 |
+
|
| 1090 |
+
|
| 1091 |
+
def test_transform_agg_by_name(request, reduction_func, frame_or_series):
|
| 1092 |
+
func = reduction_func
|
| 1093 |
+
|
| 1094 |
+
obj = DataFrame(
|
| 1095 |
+
{"a": [0, 0, 0, 0, 1, 1, 1, 1], "b": range(8)},
|
| 1096 |
+
index=["A", "B", "C", "D", "E", "F", "G", "H"],
|
| 1097 |
+
)
|
| 1098 |
+
if frame_or_series is Series:
|
| 1099 |
+
obj = obj["a"]
|
| 1100 |
+
|
| 1101 |
+
g = obj.groupby(np.repeat([0, 1], 4))
|
| 1102 |
+
|
| 1103 |
+
if func == "corrwith" and isinstance(obj, Series): # GH#32293
|
| 1104 |
+
# TODO: implement SeriesGroupBy.corrwith
|
| 1105 |
+
assert not hasattr(g, func)
|
| 1106 |
+
return
|
| 1107 |
+
|
| 1108 |
+
args = get_groupby_method_args(reduction_func, obj)
|
| 1109 |
+
if func == "corrwith":
|
| 1110 |
+
warn = Pandas4Warning
|
| 1111 |
+
msg = "DataFrameGroupBy.corrwith is deprecated"
|
| 1112 |
+
else:
|
| 1113 |
+
warn = None
|
| 1114 |
+
msg = ""
|
| 1115 |
+
with tm.assert_produces_warning(warn, match=msg):
|
| 1116 |
+
result = g.transform(func, *args)
|
| 1117 |
+
|
| 1118 |
+
# this is the *definition* of a transformation
|
| 1119 |
+
tm.assert_index_equal(result.index, obj.index)
|
| 1120 |
+
|
| 1121 |
+
if func not in ("ngroup", "size") and obj.ndim == 2:
|
| 1122 |
+
# size/ngroup return a Series, unlike other transforms
|
| 1123 |
+
tm.assert_index_equal(result.columns, obj.columns)
|
| 1124 |
+
|
| 1125 |
+
# verify that values were broadcasted across each group
|
| 1126 |
+
assert len(set(DataFrame(result).iloc[-4:, -1])) == 1
|
| 1127 |
+
|
| 1128 |
+
|
| 1129 |
+
def test_transform_lambda_with_datetimetz():
|
| 1130 |
+
# GH 27496
|
| 1131 |
+
df = DataFrame(
|
| 1132 |
+
{
|
| 1133 |
+
"time": [
|
| 1134 |
+
Timestamp("2010-07-15 03:14:45"),
|
| 1135 |
+
Timestamp("2010-11-19 18:47:06"),
|
| 1136 |
+
],
|
| 1137 |
+
"timezone": ["Etc/GMT+4", "US/Eastern"],
|
| 1138 |
+
}
|
| 1139 |
+
)
|
| 1140 |
+
result = df.groupby(["timezone"])["time"].transform(
|
| 1141 |
+
lambda x: x.dt.tz_localize(x.name)
|
| 1142 |
+
)
|
| 1143 |
+
expected = Series(
|
| 1144 |
+
[
|
| 1145 |
+
Timestamp("2010-07-15 03:14:45", tz="Etc/GMT+4"),
|
| 1146 |
+
Timestamp("2010-11-19 18:47:06", tz="US/Eastern"),
|
| 1147 |
+
],
|
| 1148 |
+
name="time",
|
| 1149 |
+
)
|
| 1150 |
+
tm.assert_series_equal(result, expected)
|
| 1151 |
+
|
| 1152 |
+
|
| 1153 |
+
def test_transform_fastpath_raises():
|
| 1154 |
+
# GH#29631 case where fastpath defined in groupby.generic _choose_path
|
| 1155 |
+
# raises, but slow_path does not
|
| 1156 |
+
|
| 1157 |
+
df = DataFrame({"A": [1, 1, 2, 2], "B": [1, -1, 1, 2]})
|
| 1158 |
+
gb = df.groupby("A")
|
| 1159 |
+
|
| 1160 |
+
def func(grp):
|
| 1161 |
+
# we want a function such that func(frame) fails but func.apply(frame)
|
| 1162 |
+
# works
|
| 1163 |
+
if grp.ndim == 2:
|
| 1164 |
+
# Ensure that fast_path fails
|
| 1165 |
+
raise NotImplementedError("Don't cross the streams")
|
| 1166 |
+
return grp * 2
|
| 1167 |
+
|
| 1168 |
+
# Check that the fastpath raises, see _transform_general
|
| 1169 |
+
obj = gb._obj_with_exclusions
|
| 1170 |
+
gen = gb._grouper.get_iterator(obj)
|
| 1171 |
+
fast_path, slow_path = gb._define_paths(func)
|
| 1172 |
+
_, group = next(gen)
|
| 1173 |
+
|
| 1174 |
+
with pytest.raises(NotImplementedError, match="Don't cross the streams"):
|
| 1175 |
+
fast_path(group)
|
| 1176 |
+
|
| 1177 |
+
result = gb.transform(func)
|
| 1178 |
+
|
| 1179 |
+
expected = DataFrame([2, -2, 2, 4], columns=["B"])
|
| 1180 |
+
tm.assert_frame_equal(result, expected)
|
| 1181 |
+
|
| 1182 |
+
|
| 1183 |
+
def test_transform_lambda_indexing():
|
| 1184 |
+
# GH 7883
|
| 1185 |
+
df = DataFrame(
|
| 1186 |
+
{
|
| 1187 |
+
"A": ["foo", "bar", "foo", "bar", "foo", "flux", "foo", "flux"],
|
| 1188 |
+
"B": ["one", "one", "two", "three", "two", "six", "five", "three"],
|
| 1189 |
+
"C": range(8),
|
| 1190 |
+
"D": range(8),
|
| 1191 |
+
"E": range(8),
|
| 1192 |
+
}
|
| 1193 |
+
)
|
| 1194 |
+
df = df.set_index(["A", "B"])
|
| 1195 |
+
df = df.sort_index()
|
| 1196 |
+
result = df.groupby(level="A").transform(lambda x: x.iloc[-1])
|
| 1197 |
+
expected = DataFrame(
|
| 1198 |
+
{
|
| 1199 |
+
"C": [3, 3, 7, 7, 4, 4, 4, 4],
|
| 1200 |
+
"D": [3, 3, 7, 7, 4, 4, 4, 4],
|
| 1201 |
+
"E": [3, 3, 7, 7, 4, 4, 4, 4],
|
| 1202 |
+
},
|
| 1203 |
+
index=MultiIndex.from_tuples(
|
| 1204 |
+
[
|
| 1205 |
+
("bar", "one"),
|
| 1206 |
+
("bar", "three"),
|
| 1207 |
+
("flux", "six"),
|
| 1208 |
+
("flux", "three"),
|
| 1209 |
+
("foo", "five"),
|
| 1210 |
+
("foo", "one"),
|
| 1211 |
+
("foo", "two"),
|
| 1212 |
+
("foo", "two"),
|
| 1213 |
+
],
|
| 1214 |
+
names=["A", "B"],
|
| 1215 |
+
),
|
| 1216 |
+
)
|
| 1217 |
+
tm.assert_frame_equal(result, expected)
|
| 1218 |
+
|
| 1219 |
+
|
| 1220 |
+
def test_categorical_and_not_categorical_key(observed):
|
| 1221 |
+
# Checks that groupby-transform, when grouping by both a categorical
|
| 1222 |
+
# and a non-categorical key, doesn't try to expand the output to include
|
| 1223 |
+
# non-observed categories but instead matches the input shape.
|
| 1224 |
+
# GH 32494
|
| 1225 |
+
df_with_categorical = DataFrame(
|
| 1226 |
+
{
|
| 1227 |
+
"A": Categorical(["a", "b", "a"], categories=["a", "b", "c"]),
|
| 1228 |
+
"B": [1, 2, 3],
|
| 1229 |
+
"C": ["a", "b", "a"],
|
| 1230 |
+
}
|
| 1231 |
+
)
|
| 1232 |
+
df_without_categorical = DataFrame(
|
| 1233 |
+
{"A": ["a", "b", "a"], "B": [1, 2, 3], "C": ["a", "b", "a"]}
|
| 1234 |
+
)
|
| 1235 |
+
|
| 1236 |
+
# DataFrame case
|
| 1237 |
+
result = df_with_categorical.groupby(["A", "C"], observed=observed).transform("sum")
|
| 1238 |
+
expected = df_without_categorical.groupby(["A", "C"]).transform("sum")
|
| 1239 |
+
tm.assert_frame_equal(result, expected)
|
| 1240 |
+
expected_explicit = DataFrame({"B": [4, 2, 4]})
|
| 1241 |
+
tm.assert_frame_equal(result, expected_explicit)
|
| 1242 |
+
|
| 1243 |
+
# Series case
|
| 1244 |
+
gb = df_with_categorical.groupby(["A", "C"], observed=observed)
|
| 1245 |
+
gbp = gb["B"]
|
| 1246 |
+
result = gbp.transform("sum")
|
| 1247 |
+
expected = df_without_categorical.groupby(["A", "C"])["B"].transform("sum")
|
| 1248 |
+
tm.assert_series_equal(result, expected)
|
| 1249 |
+
expected_explicit = Series([4, 2, 4], name="B")
|
| 1250 |
+
tm.assert_series_equal(result, expected_explicit)
|
| 1251 |
+
|
| 1252 |
+
|
| 1253 |
+
def test_string_rank_grouping():
|
| 1254 |
+
# GH 19354
|
| 1255 |
+
df = DataFrame({"A": [1, 1, 2], "B": [1, 2, 3]})
|
| 1256 |
+
result = df.groupby("A").transform("rank")
|
| 1257 |
+
expected = DataFrame({"B": [1.0, 2.0, 1.0]})
|
| 1258 |
+
tm.assert_frame_equal(result, expected)
|
| 1259 |
+
|
| 1260 |
+
|
| 1261 |
+
def test_transform_cumcount():
|
| 1262 |
+
# GH 27472
|
| 1263 |
+
df = DataFrame({"a": [0, 0, 0, 1, 1, 1], "b": range(6)})
|
| 1264 |
+
grp = df.groupby(np.repeat([0, 1], 3))
|
| 1265 |
+
|
| 1266 |
+
result = grp.cumcount()
|
| 1267 |
+
expected = Series([0, 1, 2, 0, 1, 2])
|
| 1268 |
+
tm.assert_series_equal(result, expected)
|
| 1269 |
+
|
| 1270 |
+
result = grp.transform("cumcount")
|
| 1271 |
+
tm.assert_series_equal(result, expected)
|
| 1272 |
+
|
| 1273 |
+
|
| 1274 |
+
@pytest.mark.parametrize("keys", [["A1"], ["A1", "A2"]])
|
| 1275 |
+
def test_null_group_lambda_self(sort, dropna, keys):
|
| 1276 |
+
# GH 17093
|
| 1277 |
+
size = 50
|
| 1278 |
+
nulls1 = np.random.default_rng(2).choice([False, True], size)
|
| 1279 |
+
nulls2 = np.random.default_rng(2).choice([False, True], size)
|
| 1280 |
+
# Whether a group contains a null value or not
|
| 1281 |
+
nulls_grouper = nulls1 if len(keys) == 1 else nulls1 | nulls2
|
| 1282 |
+
|
| 1283 |
+
a1 = np.random.default_rng(2).integers(0, 5, size=size).astype(float)
|
| 1284 |
+
a1[nulls1] = np.nan
|
| 1285 |
+
a2 = np.random.default_rng(2).integers(0, 5, size=size).astype(float)
|
| 1286 |
+
a2[nulls2] = np.nan
|
| 1287 |
+
values = np.random.default_rng(2).integers(0, 5, size=a1.shape)
|
| 1288 |
+
df = DataFrame({"A1": a1, "A2": a2, "B": values})
|
| 1289 |
+
|
| 1290 |
+
expected_values = values
|
| 1291 |
+
if dropna and nulls_grouper.any():
|
| 1292 |
+
expected_values = expected_values.astype(float)
|
| 1293 |
+
expected_values[nulls_grouper] = np.nan
|
| 1294 |
+
expected = DataFrame(expected_values, columns=["B"])
|
| 1295 |
+
|
| 1296 |
+
gb = df.groupby(keys, dropna=dropna, sort=sort)
|
| 1297 |
+
result = gb[["B"]].transform(lambda x: x)
|
| 1298 |
+
tm.assert_frame_equal(result, expected)
|
| 1299 |
+
|
| 1300 |
+
|
| 1301 |
+
def test_null_group_str_reducer(request, dropna, reduction_func):
|
| 1302 |
+
# GH 17093
|
| 1303 |
+
if reduction_func == "corrwith":
|
| 1304 |
+
msg = "incorrectly raises"
|
| 1305 |
+
request.applymarker(pytest.mark.xfail(reason=msg))
|
| 1306 |
+
|
| 1307 |
+
index = [1, 2, 3, 4] # test transform preserves non-standard index
|
| 1308 |
+
df = DataFrame({"A": [1, 1, np.nan, np.nan], "B": [1, 2, 2, 3]}, index=index)
|
| 1309 |
+
gb = df.groupby("A", dropna=dropna)
|
| 1310 |
+
|
| 1311 |
+
args = get_groupby_method_args(reduction_func, df)
|
| 1312 |
+
|
| 1313 |
+
# Manually handle reducers that don't fit the generic pattern
|
| 1314 |
+
# Set expected with dropna=False, then replace if necessary
|
| 1315 |
+
if reduction_func == "first":
|
| 1316 |
+
expected = DataFrame({"B": [1, 1, 2, 2]}, index=index)
|
| 1317 |
+
elif reduction_func == "last":
|
| 1318 |
+
expected = DataFrame({"B": [2, 2, 3, 3]}, index=index)
|
| 1319 |
+
elif reduction_func == "nth":
|
| 1320 |
+
expected = DataFrame({"B": [1, 1, 2, 2]}, index=index)
|
| 1321 |
+
elif reduction_func == "size":
|
| 1322 |
+
expected = Series([2, 2, 2, 2], index=index)
|
| 1323 |
+
elif reduction_func == "corrwith":
|
| 1324 |
+
expected = DataFrame({"B": [1.0, 1.0, 1.0, 1.0]}, index=index)
|
| 1325 |
+
else:
|
| 1326 |
+
expected_gb = df.groupby("A", dropna=False)
|
| 1327 |
+
buffer = []
|
| 1328 |
+
for idx, group in expected_gb:
|
| 1329 |
+
res = getattr(group["B"], reduction_func)()
|
| 1330 |
+
buffer.append(Series(res, index=group.index))
|
| 1331 |
+
expected = concat(buffer).to_frame("B")
|
| 1332 |
+
if dropna:
|
| 1333 |
+
dtype = object if reduction_func in ("any", "all") else float
|
| 1334 |
+
expected = expected.astype(dtype)
|
| 1335 |
+
if expected.ndim == 2:
|
| 1336 |
+
expected.iloc[[2, 3], 0] = np.nan
|
| 1337 |
+
else:
|
| 1338 |
+
expected.iloc[[2, 3]] = np.nan
|
| 1339 |
+
|
| 1340 |
+
result = gb.transform(reduction_func, *args)
|
| 1341 |
+
tm.assert_equal(result, expected)
|
| 1342 |
+
|
| 1343 |
+
|
| 1344 |
+
def test_null_group_str_transformer(dropna, transformation_func):
|
| 1345 |
+
# GH 17093
|
| 1346 |
+
df = DataFrame({"A": [1, 1, np.nan], "B": [1, 2, 2]}, index=[1, 2, 3])
|
| 1347 |
+
args = get_groupby_method_args(transformation_func, df)
|
| 1348 |
+
gb = df.groupby("A", dropna=dropna)
|
| 1349 |
+
|
| 1350 |
+
buffer = []
|
| 1351 |
+
for k, (idx, group) in enumerate(gb):
|
| 1352 |
+
if transformation_func == "cumcount":
|
| 1353 |
+
# DataFrame has no cumcount method
|
| 1354 |
+
res = DataFrame({"B": range(len(group))}, index=group.index)
|
| 1355 |
+
elif transformation_func == "ngroup":
|
| 1356 |
+
res = DataFrame(len(group) * [k], index=group.index, columns=["B"])
|
| 1357 |
+
else:
|
| 1358 |
+
res = getattr(group[["B"]], transformation_func)(*args)
|
| 1359 |
+
buffer.append(res)
|
| 1360 |
+
if dropna:
|
| 1361 |
+
dtype = object if transformation_func in ("any", "all") else None
|
| 1362 |
+
buffer.append(DataFrame([[np.nan]], index=[3], dtype=dtype, columns=["B"]))
|
| 1363 |
+
expected = concat(buffer)
|
| 1364 |
+
|
| 1365 |
+
if transformation_func in ("cumcount", "ngroup"):
|
| 1366 |
+
# ngroup/cumcount always returns a Series as it counts the groups, not values
|
| 1367 |
+
expected = expected["B"].rename(None)
|
| 1368 |
+
|
| 1369 |
+
result = gb.transform(transformation_func, *args)
|
| 1370 |
+
tm.assert_equal(result, expected)
|
| 1371 |
+
|
| 1372 |
+
|
| 1373 |
+
def test_null_group_str_reducer_series(request, dropna, reduction_func):
|
| 1374 |
+
# GH 17093
|
| 1375 |
+
index = [1, 2, 3, 4] # test transform preserves non-standard index
|
| 1376 |
+
ser = Series([1, 2, 2, 3], index=index)
|
| 1377 |
+
gb = ser.groupby([1, 1, np.nan, np.nan], dropna=dropna)
|
| 1378 |
+
|
| 1379 |
+
if reduction_func == "corrwith":
|
| 1380 |
+
# corrwith not implemented for SeriesGroupBy
|
| 1381 |
+
assert not hasattr(gb, reduction_func)
|
| 1382 |
+
return
|
| 1383 |
+
|
| 1384 |
+
args = get_groupby_method_args(reduction_func, ser)
|
| 1385 |
+
|
| 1386 |
+
# Manually handle reducers that don't fit the generic pattern
|
| 1387 |
+
# Set expected with dropna=False, then replace if necessary
|
| 1388 |
+
if reduction_func == "first":
|
| 1389 |
+
expected = Series([1, 1, 2, 2], index=index)
|
| 1390 |
+
elif reduction_func == "last":
|
| 1391 |
+
expected = Series([2, 2, 3, 3], index=index)
|
| 1392 |
+
elif reduction_func == "nth":
|
| 1393 |
+
expected = Series([1, 1, 2, 2], index=index)
|
| 1394 |
+
elif reduction_func == "size":
|
| 1395 |
+
expected = Series([2, 2, 2, 2], index=index)
|
| 1396 |
+
elif reduction_func == "corrwith":
|
| 1397 |
+
expected = Series([1, 1, 2, 2], index=index)
|
| 1398 |
+
else:
|
| 1399 |
+
expected_gb = ser.groupby([1, 1, np.nan, np.nan], dropna=False)
|
| 1400 |
+
buffer = []
|
| 1401 |
+
for idx, group in expected_gb:
|
| 1402 |
+
res = getattr(group, reduction_func)()
|
| 1403 |
+
buffer.append(Series(res, index=group.index))
|
| 1404 |
+
expected = concat(buffer)
|
| 1405 |
+
if dropna:
|
| 1406 |
+
dtype = object if reduction_func in ("any", "all") else float
|
| 1407 |
+
expected = expected.astype(dtype)
|
| 1408 |
+
expected.iloc[[2, 3]] = np.nan
|
| 1409 |
+
|
| 1410 |
+
result = gb.transform(reduction_func, *args)
|
| 1411 |
+
tm.assert_series_equal(result, expected)
|
| 1412 |
+
|
| 1413 |
+
|
| 1414 |
+
def test_null_group_str_transformer_series(dropna, transformation_func):
|
| 1415 |
+
# GH 17093
|
| 1416 |
+
ser = Series([1, 2, 2], index=[1, 2, 3])
|
| 1417 |
+
args = get_groupby_method_args(transformation_func, ser)
|
| 1418 |
+
gb = ser.groupby([1, 1, np.nan], dropna=dropna)
|
| 1419 |
+
|
| 1420 |
+
buffer = []
|
| 1421 |
+
for k, (idx, group) in enumerate(gb):
|
| 1422 |
+
if transformation_func == "cumcount":
|
| 1423 |
+
# Series has no cumcount method
|
| 1424 |
+
res = Series(range(len(group)), index=group.index)
|
| 1425 |
+
elif transformation_func == "ngroup":
|
| 1426 |
+
res = Series(k, index=group.index)
|
| 1427 |
+
else:
|
| 1428 |
+
res = getattr(group, transformation_func)(*args)
|
| 1429 |
+
buffer.append(res)
|
| 1430 |
+
if dropna:
|
| 1431 |
+
dtype = object if transformation_func in ("any", "all") else None
|
| 1432 |
+
buffer.append(Series([np.nan], index=[3], dtype=dtype))
|
| 1433 |
+
expected = concat(buffer)
|
| 1434 |
+
result = gb.transform(transformation_func, *args)
|
| 1435 |
+
|
| 1436 |
+
tm.assert_equal(result, expected)
|
| 1437 |
+
|
| 1438 |
+
|
| 1439 |
+
@pytest.mark.parametrize(
|
| 1440 |
+
"func, expected_values",
|
| 1441 |
+
[
|
| 1442 |
+
(Series.sort_values, [5, 4, 3, 2, 1]),
|
| 1443 |
+
(lambda x: x.head(1), [5.0, np.nan, 3, 2, np.nan]),
|
| 1444 |
+
],
|
| 1445 |
+
)
|
| 1446 |
+
@pytest.mark.parametrize("keys", [["a1"], ["a1", "a2"]])
|
| 1447 |
+
@pytest.mark.parametrize("keys_in_index", [True, False])
|
| 1448 |
+
def test_transform_aligns(func, frame_or_series, expected_values, keys, keys_in_index):
|
| 1449 |
+
# GH#45648 - transform should align with the input's index
|
| 1450 |
+
df = DataFrame({"a1": [1, 1, 3, 2, 2], "b": [5, 4, 3, 2, 1]})
|
| 1451 |
+
if "a2" in keys:
|
| 1452 |
+
df["a2"] = df["a1"]
|
| 1453 |
+
if keys_in_index:
|
| 1454 |
+
df = df.set_index(keys, append=True)
|
| 1455 |
+
|
| 1456 |
+
gb = df.groupby(keys)
|
| 1457 |
+
if frame_or_series is Series:
|
| 1458 |
+
gb = gb["b"]
|
| 1459 |
+
|
| 1460 |
+
result = gb.transform(func)
|
| 1461 |
+
expected = DataFrame({"b": expected_values}, index=df.index)
|
| 1462 |
+
if frame_or_series is Series:
|
| 1463 |
+
expected = expected["b"]
|
| 1464 |
+
tm.assert_equal(result, expected)
|
| 1465 |
+
|
| 1466 |
+
|
| 1467 |
+
@pytest.mark.parametrize("keys", ["A", ["A", "B"]])
|
| 1468 |
+
def test_as_index_no_change(keys, df, groupby_func):
|
| 1469 |
+
# GH#49834 - as_index should have no impact on DataFrameGroupBy.transform
|
| 1470 |
+
if keys == "A":
|
| 1471 |
+
# Column B is string dtype; will fail on some ops
|
| 1472 |
+
df = df.drop(columns="B")
|
| 1473 |
+
args = get_groupby_method_args(groupby_func, df)
|
| 1474 |
+
gb_as_index_true = df.groupby(keys, as_index=True)
|
| 1475 |
+
gb_as_index_false = df.groupby(keys, as_index=False)
|
| 1476 |
+
if groupby_func == "corrwith":
|
| 1477 |
+
warn = Pandas4Warning
|
| 1478 |
+
msg = "DataFrameGroupBy.corrwith is deprecated"
|
| 1479 |
+
else:
|
| 1480 |
+
warn = None
|
| 1481 |
+
msg = ""
|
| 1482 |
+
with tm.assert_produces_warning(warn, match=msg):
|
| 1483 |
+
result = gb_as_index_true.transform(groupby_func, *args)
|
| 1484 |
+
with tm.assert_produces_warning(warn, match=msg):
|
| 1485 |
+
expected = gb_as_index_false.transform(groupby_func, *args)
|
| 1486 |
+
tm.assert_equal(result, expected)
|
| 1487 |
+
|
| 1488 |
+
|
| 1489 |
+
@pytest.mark.parametrize("how", ["idxmax", "idxmin"])
|
| 1490 |
+
@pytest.mark.parametrize("numeric_only", [True, False])
|
| 1491 |
+
def test_idxmin_idxmax_transform_args(how, skipna, numeric_only):
|
| 1492 |
+
# GH#55268 - ensure *args are passed through when calling transform
|
| 1493 |
+
df = DataFrame({"a": [1, 1, 1, 2], "b": [3.0, 4.0, np.nan, 6.0], "c": list("abcd")})
|
| 1494 |
+
gb = df.groupby("a")
|
| 1495 |
+
if skipna:
|
| 1496 |
+
result = gb.transform(how, skipna, numeric_only)
|
| 1497 |
+
expected = gb.transform(how, skipna=skipna, numeric_only=numeric_only)
|
| 1498 |
+
tm.assert_frame_equal(result, expected)
|
| 1499 |
+
else:
|
| 1500 |
+
msg = f"{how} with skipna=False encountered an NA value"
|
| 1501 |
+
with pytest.raises(ValueError, match=msg):
|
| 1502 |
+
gb.transform(how, skipna, numeric_only)
|
| 1503 |
+
|
| 1504 |
+
|
| 1505 |
+
def test_transform_sum_one_column_no_matching_labels():
|
| 1506 |
+
df = DataFrame({"X": [1.0]})
|
| 1507 |
+
series = Series(["Y"])
|
| 1508 |
+
result = df.groupby(series, as_index=False).transform("sum")
|
| 1509 |
+
expected = DataFrame({"X": [1.0]})
|
| 1510 |
+
tm.assert_frame_equal(result, expected)
|
| 1511 |
+
|
| 1512 |
+
|
| 1513 |
+
def test_transform_sum_no_matching_labels():
|
| 1514 |
+
df = DataFrame({"X": [1.0, -93204, 4935]})
|
| 1515 |
+
series = Series(["A", "B", "C"])
|
| 1516 |
+
|
| 1517 |
+
result = df.groupby(series, as_index=False).transform("sum")
|
| 1518 |
+
expected = DataFrame({"X": [1.0, -93204, 4935]})
|
| 1519 |
+
tm.assert_frame_equal(result, expected)
|
| 1520 |
+
|
| 1521 |
+
|
| 1522 |
+
def test_transform_sum_one_column_with_matching_labels():
|
| 1523 |
+
df = DataFrame({"X": [1.0, -93204, 4935]})
|
| 1524 |
+
series = Series(["A", "B", "A"])
|
| 1525 |
+
|
| 1526 |
+
result = df.groupby(series, as_index=False).transform("sum")
|
| 1527 |
+
expected = DataFrame({"X": [4936.0, -93204, 4936.0]})
|
| 1528 |
+
tm.assert_frame_equal(result, expected)
|
| 1529 |
+
|
| 1530 |
+
|
| 1531 |
+
def test_transform_sum_one_column_with_missing_labels():
|
| 1532 |
+
df = DataFrame({"X": [1.0, -93204, 4935]})
|
| 1533 |
+
series = Series(["A", "C"])
|
| 1534 |
+
|
| 1535 |
+
result = df.groupby(series, as_index=False).transform("sum")
|
| 1536 |
+
expected = DataFrame({"X": [1.0, -93204, np.nan]})
|
| 1537 |
+
tm.assert_frame_equal(result, expected)
|
| 1538 |
+
|
| 1539 |
+
|
| 1540 |
+
def test_transform_sum_one_column_with_matching_labels_and_missing_labels():
|
| 1541 |
+
df = DataFrame({"X": [1.0, -93204, 4935]})
|
| 1542 |
+
series = Series(["A", "A"])
|
| 1543 |
+
|
| 1544 |
+
result = df.groupby(series, as_index=False).transform("sum")
|
| 1545 |
+
expected = DataFrame({"X": [-93203.0, -93203.0, np.nan]})
|
| 1546 |
+
tm.assert_frame_equal(result, expected)
|
| 1547 |
+
|
| 1548 |
+
|
| 1549 |
+
@pytest.mark.parametrize("dtype", ["int32", "float32"])
|
| 1550 |
+
def test_min_one_unobserved_category_no_type_coercion(dtype):
|
| 1551 |
+
# GH#58084
|
| 1552 |
+
df = DataFrame({"A": Categorical([1, 1, 2], categories=[1, 2, 3]), "B": [3, 4, 5]})
|
| 1553 |
+
df["B"] = df["B"].astype(dtype)
|
| 1554 |
+
gb = df.groupby("A", observed=False)
|
| 1555 |
+
result = gb.transform("min")
|
| 1556 |
+
|
| 1557 |
+
expected = DataFrame({"B": [3, 3, 5]}, dtype=dtype)
|
| 1558 |
+
tm.assert_frame_equal(expected, result)
|
| 1559 |
+
|
| 1560 |
+
|
| 1561 |
+
def test_min_all_empty_data_no_type_coercion():
|
| 1562 |
+
# GH#58084
|
| 1563 |
+
df = DataFrame(
|
| 1564 |
+
{
|
| 1565 |
+
"X": Categorical(
|
| 1566 |
+
[],
|
| 1567 |
+
categories=[1, "randomcat", 100],
|
| 1568 |
+
),
|
| 1569 |
+
"Y": [],
|
| 1570 |
+
}
|
| 1571 |
+
)
|
| 1572 |
+
df["Y"] = df["Y"].astype("int32")
|
| 1573 |
+
|
| 1574 |
+
gb = df.groupby("X", observed=False)
|
| 1575 |
+
result = gb.transform("min")
|
| 1576 |
+
|
| 1577 |
+
expected = DataFrame({"Y": []}, dtype="int32")
|
| 1578 |
+
tm.assert_frame_equal(expected, result)
|
| 1579 |
+
|
| 1580 |
+
|
| 1581 |
+
def test_min_one_dim_no_type_coercion():
|
| 1582 |
+
# GH#58084
|
| 1583 |
+
df = DataFrame({"Y": [9435, -5465765, 5055, 0, 954960]})
|
| 1584 |
+
df["Y"] = df["Y"].astype("int32")
|
| 1585 |
+
categories = Categorical([1, 2, 2, 5, 1], categories=[1, 2, 3, 4, 5])
|
| 1586 |
+
|
| 1587 |
+
gb = df.groupby(categories, observed=False)
|
| 1588 |
+
result = gb.transform("min")
|
| 1589 |
+
|
| 1590 |
+
expected = DataFrame({"Y": [9435, -5465765, -5465765, 0, 9435]}, dtype="int32")
|
| 1591 |
+
tm.assert_frame_equal(expected, result)
|
| 1592 |
+
|
| 1593 |
+
|
| 1594 |
+
def test_nan_in_cumsum_group_label():
|
| 1595 |
+
# GH#58811
|
| 1596 |
+
df = DataFrame({"A": [1, None], "B": [2, 3]}, dtype="Int16")
|
| 1597 |
+
gb = df.groupby("A")["B"]
|
| 1598 |
+
result = gb.cumsum()
|
| 1599 |
+
expected = Series([2, None], dtype="Int16", name="B")
|
| 1600 |
+
tm.assert_series_equal(expected, result)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/__init__.py
ADDED
|
File without changes
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/__init__.py
ADDED
|
File without changes
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_constructors.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from pandas import (
|
| 6 |
+
Index,
|
| 7 |
+
MultiIndex,
|
| 8 |
+
Series,
|
| 9 |
+
)
|
| 10 |
+
import pandas._testing as tm
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class TestIndexConstructor:
|
| 14 |
+
# Tests for the Index constructor, specifically for cases that do
|
| 15 |
+
# not return a subclass
|
| 16 |
+
|
| 17 |
+
@pytest.mark.parametrize("value", [1, np.int64(1)])
|
| 18 |
+
def test_constructor_corner(self, value):
|
| 19 |
+
# corner case
|
| 20 |
+
msg = (
|
| 21 |
+
r"Index\(\.\.\.\) must be called with a collection of some "
|
| 22 |
+
f"kind, {value} was passed"
|
| 23 |
+
)
|
| 24 |
+
with pytest.raises(TypeError, match=msg):
|
| 25 |
+
Index(value)
|
| 26 |
+
|
| 27 |
+
@pytest.mark.parametrize("index_vals", [[("A", 1), "B"], ["B", ("A", 1)]])
|
| 28 |
+
def test_construction_list_mixed_tuples(self, index_vals):
|
| 29 |
+
# see gh-10697: if we are constructing from a mixed list of tuples,
|
| 30 |
+
# make sure that we are independent of the sorting order.
|
| 31 |
+
index = Index(index_vals)
|
| 32 |
+
assert isinstance(index, Index)
|
| 33 |
+
assert not isinstance(index, MultiIndex)
|
| 34 |
+
|
| 35 |
+
def test_constructor_cast(self):
|
| 36 |
+
msg = "could not convert string to float"
|
| 37 |
+
with pytest.raises(ValueError, match=msg):
|
| 38 |
+
Index(["a", "b", "c"], dtype=float)
|
| 39 |
+
|
| 40 |
+
@pytest.mark.parametrize("tuple_list", [[()], [(), ()]])
|
| 41 |
+
def test_construct_empty_tuples(self, tuple_list):
|
| 42 |
+
# GH #45608
|
| 43 |
+
result = Index(tuple_list)
|
| 44 |
+
expected = MultiIndex.from_tuples(tuple_list)
|
| 45 |
+
|
| 46 |
+
tm.assert_index_equal(result, expected)
|
| 47 |
+
|
| 48 |
+
def test_index_string_inference(self):
|
| 49 |
+
# GH#54430
|
| 50 |
+
expected = Index(["a", "b"], dtype=pd.StringDtype(na_value=np.nan))
|
| 51 |
+
with pd.option_context("future.infer_string", True):
|
| 52 |
+
ser = Index(["a", "b"])
|
| 53 |
+
tm.assert_index_equal(ser, expected)
|
| 54 |
+
|
| 55 |
+
expected = Index(["a", 1], dtype="object")
|
| 56 |
+
with pd.option_context("future.infer_string", True):
|
| 57 |
+
ser = Index(["a", 1])
|
| 58 |
+
tm.assert_index_equal(ser, expected)
|
| 59 |
+
|
| 60 |
+
@pytest.mark.parametrize("klass", [Series, Index])
|
| 61 |
+
def test_inference_on_pandas_objects(self, klass):
|
| 62 |
+
# GH#56012
|
| 63 |
+
obj = klass([pd.Timestamp("2019-12-31")], dtype=object)
|
| 64 |
+
result = Index(obj)
|
| 65 |
+
assert result.dtype == np.object_
|
| 66 |
+
|
| 67 |
+
def test_constructor_not_read_only(self):
|
| 68 |
+
# GH#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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas._config.config as cf
|
| 5 |
+
|
| 6 |
+
from pandas import Index
|
| 7 |
+
import pandas._testing as tm
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TestIndexRendering:
|
| 11 |
+
def test_repr_is_valid_construction_code(self):
|
| 12 |
+
# for the case of Index, where the repr is traditional rather than
|
| 13 |
+
# stylized
|
| 14 |
+
idx = Index(["a", "b"])
|
| 15 |
+
res = eval(repr(idx))
|
| 16 |
+
tm.assert_index_equal(res, idx)
|
| 17 |
+
|
| 18 |
+
@pytest.mark.parametrize(
|
| 19 |
+
"index,expected",
|
| 20 |
+
[
|
| 21 |
+
# ASCII
|
| 22 |
+
# short
|
| 23 |
+
(
|
| 24 |
+
Index(["a", "bb", "ccc"]),
|
| 25 |
+
"""Index(['a', 'bb', 'ccc'], dtype='object')""",
|
| 26 |
+
),
|
| 27 |
+
# multiple lines
|
| 28 |
+
(
|
| 29 |
+
Index(["a", "bb", "ccc"] * 10),
|
| 30 |
+
"Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', "
|
| 31 |
+
"'bb', 'ccc', 'a', 'bb', 'ccc',\n"
|
| 32 |
+
" 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', "
|
| 33 |
+
"'bb', 'ccc', 'a', 'bb', 'ccc',\n"
|
| 34 |
+
" 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],\n"
|
| 35 |
+
" dtype='object')",
|
| 36 |
+
),
|
| 37 |
+
# truncated
|
| 38 |
+
(
|
| 39 |
+
Index(["a", "bb", "ccc"] * 100),
|
| 40 |
+
"Index(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',\n"
|
| 41 |
+
" ...\n"
|
| 42 |
+
" 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],\n"
|
| 43 |
+
" dtype='object', length=300)",
|
| 44 |
+
),
|
| 45 |
+
# Non-ASCII
|
| 46 |
+
# short
|
| 47 |
+
(
|
| 48 |
+
Index(["あ", "いい", "ううう"]),
|
| 49 |
+
"""Index(['あ', 'いい', 'ううう'], dtype='object')""",
|
| 50 |
+
),
|
| 51 |
+
# multiple lines
|
| 52 |
+
(
|
| 53 |
+
Index(["あ", "いい", "ううう"] * 10),
|
| 54 |
+
(
|
| 55 |
+
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
|
| 56 |
+
"'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"
|
| 57 |
+
" 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
|
| 58 |
+
"'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',\n"
|
| 59 |
+
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
|
| 60 |
+
"'ううう'],\n"
|
| 61 |
+
" dtype='object')"
|
| 62 |
+
),
|
| 63 |
+
),
|
| 64 |
+
# truncated
|
| 65 |
+
(
|
| 66 |
+
Index(["あ", "いい", "ううう"] * 100),
|
| 67 |
+
(
|
| 68 |
+
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', "
|
| 69 |
+
"'あ', 'いい', 'ううう', 'あ',\n"
|
| 70 |
+
" ...\n"
|
| 71 |
+
" 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', "
|
| 72 |
+
"'ううう', 'あ', 'いい', 'ううう'],\n"
|
| 73 |
+
" dtype='object', length=300)"
|
| 74 |
+
),
|
| 75 |
+
),
|
| 76 |
+
],
|
| 77 |
+
)
|
| 78 |
+
def test_string_index_repr(self, index, expected, using_infer_string):
|
| 79 |
+
result = repr(index)
|
| 80 |
+
if using_infer_string:
|
| 81 |
+
expected = expected.replace("dtype='object'", "dtype='str'")
|
| 82 |
+
|
| 83 |
+
assert result == expected
|
| 84 |
+
|
| 85 |
+
@pytest.mark.parametrize(
|
| 86 |
+
"index,expected",
|
| 87 |
+
[
|
| 88 |
+
# short
|
| 89 |
+
(
|
| 90 |
+
Index(["あ", "いい", "ううう"]),
|
| 91 |
+
("Index(['あ', 'いい', 'ううう'], dtype='object')"),
|
| 92 |
+
),
|
| 93 |
+
# multiple lines
|
| 94 |
+
(
|
| 95 |
+
Index(["あ", "いい", "ううう"] * 10),
|
| 96 |
+
(
|
| 97 |
+
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', "
|
| 98 |
+
"'ううう', 'あ', 'いい', 'ううう',\n"
|
| 99 |
+
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
|
| 100 |
+
"'ううう', 'あ', 'いい', 'ううう',\n"
|
| 101 |
+
" 'あ', 'いい', 'ううう', 'あ', 'いい', "
|
| 102 |
+
"'ううう', 'あ', 'いい', 'ううう',\n"
|
| 103 |
+
" 'あ', 'いい', 'ううう'],\n"
|
| 104 |
+
" dtype='object')"
|
| 105 |
+
""
|
| 106 |
+
),
|
| 107 |
+
),
|
| 108 |
+
# truncated
|
| 109 |
+
(
|
| 110 |
+
Index(["あ", "いい", "ううう"] * 100),
|
| 111 |
+
(
|
| 112 |
+
"Index(['あ', 'いい', 'ううう', 'あ', 'いい', "
|
| 113 |
+
"'ううう', 'あ', 'いい', 'ううう',\n"
|
| 114 |
+
" 'あ',\n"
|
| 115 |
+
" ...\n"
|
| 116 |
+
" 'ううう', 'あ', 'いい', 'ううう', 'あ', "
|
| 117 |
+
"'いい', 'ううう', 'あ', 'いい',\n"
|
| 118 |
+
" 'ううう'],\n"
|
| 119 |
+
" dtype='object', length=300)"
|
| 120 |
+
),
|
| 121 |
+
),
|
| 122 |
+
],
|
| 123 |
+
)
|
| 124 |
+
def test_string_index_repr_with_unicode_option(
|
| 125 |
+
self, index, expected, using_infer_string
|
| 126 |
+
):
|
| 127 |
+
# Enable Unicode option -----------------------------------------
|
| 128 |
+
with cf.option_context("display.unicode.east_asian_width", True):
|
| 129 |
+
result = repr(index)
|
| 130 |
+
|
| 131 |
+
if using_infer_string:
|
| 132 |
+
expected = expected.replace("dtype='object'", "dtype='str'")
|
| 133 |
+
assert result == expected
|
| 134 |
+
|
| 135 |
+
def test_repr_summary(self):
|
| 136 |
+
with cf.option_context("display.max_seq_items", 10):
|
| 137 |
+
result = repr(Index(np.arange(1000)))
|
| 138 |
+
assert len(result) < 200
|
| 139 |
+
assert "..." in result
|
| 140 |
+
|
| 141 |
+
def test_summary_bug(self):
|
| 142 |
+
# GH#3869
|
| 143 |
+
ind = Index(["{other}%s", "~:{range}:0"], name="A")
|
| 144 |
+
result = ind._summary()
|
| 145 |
+
# shouldn't be formatted accidentally.
|
| 146 |
+
assert "~:{range}:0" in result
|
| 147 |
+
assert "{other}%s" in result
|
| 148 |
+
|
| 149 |
+
def test_index_repr_bool_nan(self):
|
| 150 |
+
# GH32146
|
| 151 |
+
arr = Index([True, False, np.nan], dtype=object)
|
| 152 |
+
exp2 = repr(arr)
|
| 153 |
+
out2 = "Index([True, False, nan], dtype='object')"
|
| 154 |
+
assert out2 == exp2
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_indexing.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas._libs import index as libindex
|
| 5 |
+
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from pandas import (
|
| 8 |
+
Index,
|
| 9 |
+
NaT,
|
| 10 |
+
)
|
| 11 |
+
import pandas._testing as tm
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class TestGetSliceBounds:
|
| 15 |
+
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
|
| 16 |
+
def test_get_slice_bounds_within(self, side, expected):
|
| 17 |
+
index = Index(list("abcdef"))
|
| 18 |
+
result = index.get_slice_bound("e", side=side)
|
| 19 |
+
assert result == expected
|
| 20 |
+
|
| 21 |
+
@pytest.mark.parametrize("side", ["left", "right"])
|
| 22 |
+
@pytest.mark.parametrize(
|
| 23 |
+
"data, bound, expected", [(list("abcdef"), "x", 6), (list("bcdefg"), "a", 0)]
|
| 24 |
+
)
|
| 25 |
+
def test_get_slice_bounds_outside(self, side, expected, data, bound):
|
| 26 |
+
index = Index(data)
|
| 27 |
+
result = index.get_slice_bound(bound, side=side)
|
| 28 |
+
assert result == expected
|
| 29 |
+
|
| 30 |
+
def test_get_slice_bounds_invalid_side(self):
|
| 31 |
+
with pytest.raises(ValueError, match="Invalid value for side kwarg"):
|
| 32 |
+
Index([]).get_slice_bound("a", side="middle")
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class TestGetIndexerNonUnique:
|
| 36 |
+
def test_get_indexer_non_unique_dtype_mismatch(self):
|
| 37 |
+
# GH#25459
|
| 38 |
+
indexes, missing = Index(["A", "B"]).get_indexer_non_unique(Index([0]))
|
| 39 |
+
tm.assert_numpy_array_equal(np.array([-1], dtype=np.intp), indexes)
|
| 40 |
+
tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), missing)
|
| 41 |
+
|
| 42 |
+
@pytest.mark.parametrize(
|
| 43 |
+
"idx_values,idx_non_unique",
|
| 44 |
+
[
|
| 45 |
+
([np.nan, 100, 200, 100], [np.nan, 100]),
|
| 46 |
+
([np.nan, 100.0, 200.0, 100.0], [np.nan, 100.0]),
|
| 47 |
+
],
|
| 48 |
+
)
|
| 49 |
+
def test_get_indexer_non_unique_int_index(self, idx_values, idx_non_unique):
|
| 50 |
+
indexes, missing = Index(idx_values).get_indexer_non_unique(Index([np.nan]))
|
| 51 |
+
tm.assert_numpy_array_equal(np.array([0], dtype=np.intp), indexes)
|
| 52 |
+
tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing)
|
| 53 |
+
|
| 54 |
+
indexes, missing = Index(idx_values).get_indexer_non_unique(
|
| 55 |
+
Index(idx_non_unique)
|
| 56 |
+
)
|
| 57 |
+
tm.assert_numpy_array_equal(np.array([0, 1, 3], dtype=np.intp), indexes)
|
| 58 |
+
tm.assert_numpy_array_equal(np.array([], dtype=np.intp), missing)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class TestGetLoc:
|
| 62 |
+
@pytest.mark.slow # to_flat_index takes a while
|
| 63 |
+
def test_get_loc_tuple_monotonic_above_size_cutoff(self, monkeypatch):
|
| 64 |
+
# Go through the libindex path for which using
|
| 65 |
+
# _bin_search vs ndarray.searchsorted makes a difference
|
| 66 |
+
|
| 67 |
+
with monkeypatch.context():
|
| 68 |
+
monkeypatch.setattr(libindex, "_SIZE_CUTOFF", 100)
|
| 69 |
+
lev = list("ABCD")
|
| 70 |
+
dti = pd.date_range("2016-01-01", periods=10)
|
| 71 |
+
|
| 72 |
+
mi = pd.MultiIndex.from_product([lev, range(5), dti])
|
| 73 |
+
oidx = mi.to_flat_index()
|
| 74 |
+
|
| 75 |
+
loc = len(oidx) // 2
|
| 76 |
+
tup = oidx[loc]
|
| 77 |
+
|
| 78 |
+
res = oidx.get_loc(tup)
|
| 79 |
+
assert res == loc
|
| 80 |
+
|
| 81 |
+
def test_get_loc_nan_object_dtype_nonmonotonic_nonunique(self):
|
| 82 |
+
# case that goes through _maybe_get_bool_indexer
|
| 83 |
+
idx = Index(["foo", np.nan, None, "foo", 1.0, None], dtype=object)
|
| 84 |
+
|
| 85 |
+
# we dont raise KeyError on nan
|
| 86 |
+
res = idx.get_loc(np.nan)
|
| 87 |
+
assert res == 1
|
| 88 |
+
|
| 89 |
+
# we only match on None, not on np.nan
|
| 90 |
+
res = idx.get_loc(None)
|
| 91 |
+
expected = np.array([False, False, True, False, False, True])
|
| 92 |
+
tm.assert_numpy_array_equal(res, expected)
|
| 93 |
+
|
| 94 |
+
# we don't match at all on mismatched NA
|
| 95 |
+
with pytest.raises(KeyError, match="NaT"):
|
| 96 |
+
idx.get_loc(NaT)
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def test_getitem_boolean_ea_indexer():
|
| 100 |
+
# GH#45806
|
| 101 |
+
ser = pd.Series([True, False, pd.NA], dtype="boolean")
|
| 102 |
+
result = ser.index[ser]
|
| 103 |
+
expected = Index([0])
|
| 104 |
+
tm.assert_index_equal(result, expected)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_pickle.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pandas import Index
|
| 2 |
+
import pandas._testing as tm
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def test_pickle_preserves_object_dtype(temp_file):
|
| 6 |
+
# GH#43188, GH#43155 don't infer numeric dtype
|
| 7 |
+
index = Index([1, 2, 3], dtype=object)
|
| 8 |
+
|
| 9 |
+
result = tm.round_trip_pickle(index, temp_file)
|
| 10 |
+
assert result.dtype == object
|
| 11 |
+
tm.assert_index_equal(index, result)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_reshape.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tests for ndarray-like method on the base Index class
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pytest
|
| 7 |
+
|
| 8 |
+
from pandas import Index
|
| 9 |
+
import pandas._testing as tm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestReshape:
|
| 13 |
+
def test_repeat(self):
|
| 14 |
+
repeats = 2
|
| 15 |
+
index = Index([1, 2, 3])
|
| 16 |
+
expected = Index([1, 1, 2, 2, 3, 3])
|
| 17 |
+
|
| 18 |
+
result = index.repeat(repeats)
|
| 19 |
+
tm.assert_index_equal(result, expected)
|
| 20 |
+
|
| 21 |
+
def test_insert(self):
|
| 22 |
+
# GH 7256
|
| 23 |
+
# validate neg/pos inserts
|
| 24 |
+
result = Index(["b", "c", "d"])
|
| 25 |
+
|
| 26 |
+
# test 0th element
|
| 27 |
+
tm.assert_index_equal(Index(["a", "b", "c", "d"]), result.insert(0, "a"))
|
| 28 |
+
|
| 29 |
+
# test Nth element that follows Python list behavior
|
| 30 |
+
tm.assert_index_equal(Index(["b", "c", "e", "d"]), result.insert(-1, "e"))
|
| 31 |
+
|
| 32 |
+
# test loc +/- neq (0, -1)
|
| 33 |
+
tm.assert_index_equal(result.insert(1, "z"), result.insert(-2, "z"))
|
| 34 |
+
|
| 35 |
+
# test empty
|
| 36 |
+
null_index = Index([])
|
| 37 |
+
tm.assert_index_equal(Index(["a"]), null_index.insert(0, "a"))
|
| 38 |
+
|
| 39 |
+
def test_insert_missing(self, nulls_fixture, using_infer_string):
|
| 40 |
+
# GH#22295
|
| 41 |
+
# test there is no mangling of NA values
|
| 42 |
+
expected = Index(["a", nulls_fixture, "b", "c"], dtype=object)
|
| 43 |
+
result = Index(list("abc"), dtype=object).insert(
|
| 44 |
+
1, Index([nulls_fixture], dtype=object)
|
| 45 |
+
)
|
| 46 |
+
tm.assert_index_equal(result, expected)
|
| 47 |
+
|
| 48 |
+
@pytest.mark.parametrize(
|
| 49 |
+
"val", [(1, 2), np.datetime64("2019-12-31"), np.timedelta64(1, "D")]
|
| 50 |
+
)
|
| 51 |
+
@pytest.mark.parametrize("loc", [-1, 2])
|
| 52 |
+
def test_insert_datetime_into_object(self, loc, val):
|
| 53 |
+
# GH#44509
|
| 54 |
+
idx = Index(["1", "2", "3"])
|
| 55 |
+
result = idx.insert(loc, val)
|
| 56 |
+
expected = Index(["1", "2", val, "3"])
|
| 57 |
+
tm.assert_index_equal(result, expected)
|
| 58 |
+
assert type(expected[2]) is type(val)
|
| 59 |
+
|
| 60 |
+
def test_insert_none_into_string_numpy(self, string_dtype_no_object):
|
| 61 |
+
# GH#55365
|
| 62 |
+
index = Index(["a", "b", "c"], dtype=string_dtype_no_object)
|
| 63 |
+
result = index.insert(-1, None)
|
| 64 |
+
expected = Index(["a", "b", None, "c"], dtype=string_dtype_no_object)
|
| 65 |
+
tm.assert_index_equal(result, expected)
|
| 66 |
+
|
| 67 |
+
@pytest.mark.parametrize(
|
| 68 |
+
"pos,expected",
|
| 69 |
+
[
|
| 70 |
+
(0, Index(["b", "c", "d"], name="index")),
|
| 71 |
+
(-1, Index(["a", "b", "c"], name="index")),
|
| 72 |
+
],
|
| 73 |
+
)
|
| 74 |
+
def test_delete(self, pos, expected):
|
| 75 |
+
index = Index(["a", "b", "c", "d"], name="index")
|
| 76 |
+
result = index.delete(pos)
|
| 77 |
+
tm.assert_index_equal(result, expected)
|
| 78 |
+
assert result.name == expected.name
|
| 79 |
+
|
| 80 |
+
def test_delete_raises(self):
|
| 81 |
+
index = Index(["a", "b", "c", "d"], name="index")
|
| 82 |
+
msg = "index 5 is out of bounds for axis 0 with size 4"
|
| 83 |
+
with pytest.raises(IndexError, match=msg):
|
| 84 |
+
index.delete(5)
|
| 85 |
+
|
| 86 |
+
def test_append_multiple(self):
|
| 87 |
+
index = Index(["a", "b", "c", "d", "e", "f"])
|
| 88 |
+
|
| 89 |
+
foos = [index[:2], index[2:4], index[4:]]
|
| 90 |
+
result = foos[0].append(foos[1:])
|
| 91 |
+
tm.assert_index_equal(result, index)
|
| 92 |
+
|
| 93 |
+
# empty
|
| 94 |
+
result = index.append([])
|
| 95 |
+
tm.assert_index_equal(result, index)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_setops.py
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from pandas import (
|
| 8 |
+
Index,
|
| 9 |
+
Series,
|
| 10 |
+
)
|
| 11 |
+
import pandas._testing as tm
|
| 12 |
+
from pandas.core.algorithms import safe_sort
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def equal_contents(arr1, arr2) -> bool:
|
| 16 |
+
"""
|
| 17 |
+
Checks if the set of unique elements of arr1 and arr2 are equivalent.
|
| 18 |
+
"""
|
| 19 |
+
return frozenset(arr1) == frozenset(arr2)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class TestIndexSetOps:
|
| 23 |
+
@pytest.mark.parametrize(
|
| 24 |
+
"method", ["union", "intersection", "difference", "symmetric_difference"]
|
| 25 |
+
)
|
| 26 |
+
def test_setops_sort_validation(self, method):
|
| 27 |
+
idx1 = Index(["a", "b"])
|
| 28 |
+
idx2 = Index(["b", "c"])
|
| 29 |
+
|
| 30 |
+
with pytest.raises(ValueError, match="The 'sort' keyword only takes"):
|
| 31 |
+
getattr(idx1, method)(idx2, sort=2)
|
| 32 |
+
|
| 33 |
+
# sort=True is supported as of GH#??
|
| 34 |
+
getattr(idx1, method)(idx2, sort=True)
|
| 35 |
+
|
| 36 |
+
def test_setops_preserve_object_dtype(self):
|
| 37 |
+
idx = Index([1, 2, 3], dtype=object)
|
| 38 |
+
result = idx.intersection(idx[1:])
|
| 39 |
+
expected = idx[1:]
|
| 40 |
+
tm.assert_index_equal(result, expected)
|
| 41 |
+
|
| 42 |
+
# if other is not monotonic increasing, intersection goes through
|
| 43 |
+
# a different route
|
| 44 |
+
result = idx.intersection(idx[1:][::-1])
|
| 45 |
+
tm.assert_index_equal(result, expected)
|
| 46 |
+
|
| 47 |
+
result = idx._union(idx[1:], sort=None)
|
| 48 |
+
expected = idx
|
| 49 |
+
tm.assert_numpy_array_equal(result, expected.values)
|
| 50 |
+
|
| 51 |
+
result = idx.union(idx[1:], sort=None)
|
| 52 |
+
tm.assert_index_equal(result, expected)
|
| 53 |
+
|
| 54 |
+
# if other is not monotonic increasing, _union goes through
|
| 55 |
+
# a different route
|
| 56 |
+
result = idx._union(idx[1:][::-1], sort=None)
|
| 57 |
+
tm.assert_numpy_array_equal(result, expected.values)
|
| 58 |
+
|
| 59 |
+
result = idx.union(idx[1:][::-1], sort=None)
|
| 60 |
+
tm.assert_index_equal(result, expected)
|
| 61 |
+
|
| 62 |
+
def test_union_base(self):
|
| 63 |
+
index = Index([0, "a", 1, "b", 2, "c"])
|
| 64 |
+
first = index[3:]
|
| 65 |
+
second = index[:5]
|
| 66 |
+
|
| 67 |
+
result = first.union(second)
|
| 68 |
+
|
| 69 |
+
expected = Index([0, 1, 2, "a", "b", "c"])
|
| 70 |
+
tm.assert_index_equal(result, expected)
|
| 71 |
+
|
| 72 |
+
@pytest.mark.parametrize("klass", [np.array, Series, list])
|
| 73 |
+
def test_union_different_type_base(self, klass):
|
| 74 |
+
# GH 10149
|
| 75 |
+
index = Index([0, "a", 1, "b", 2, "c"])
|
| 76 |
+
first = index[3:]
|
| 77 |
+
second = index[:5]
|
| 78 |
+
|
| 79 |
+
result = first.union(klass(second.values))
|
| 80 |
+
|
| 81 |
+
assert equal_contents(result, index)
|
| 82 |
+
|
| 83 |
+
def test_union_sort_other_incomparable(self):
|
| 84 |
+
# https://github.com/pandas-dev/pandas/issues/24959
|
| 85 |
+
idx = Index([1, pd.Timestamp("2000")])
|
| 86 |
+
# default (sort=None)
|
| 87 |
+
with tm.assert_produces_warning(RuntimeWarning, match="not supported between"):
|
| 88 |
+
result = idx.union(idx[:1])
|
| 89 |
+
|
| 90 |
+
tm.assert_index_equal(result, idx)
|
| 91 |
+
|
| 92 |
+
# sort=None
|
| 93 |
+
with tm.assert_produces_warning(RuntimeWarning, match="not supported between"):
|
| 94 |
+
result = idx.union(idx[:1], sort=None)
|
| 95 |
+
tm.assert_index_equal(result, idx)
|
| 96 |
+
|
| 97 |
+
# sort=False
|
| 98 |
+
result = idx.union(idx[:1], sort=False)
|
| 99 |
+
tm.assert_index_equal(result, idx)
|
| 100 |
+
|
| 101 |
+
def test_union_sort_other_incomparable_true(self):
|
| 102 |
+
idx = Index([1, pd.Timestamp("2000")])
|
| 103 |
+
with pytest.raises(TypeError, match=".*"):
|
| 104 |
+
idx.union(idx[:1], sort=True)
|
| 105 |
+
|
| 106 |
+
def test_intersection_equal_sort_true(self):
|
| 107 |
+
idx = Index(["c", "a", "b"])
|
| 108 |
+
sorted_ = Index(["a", "b", "c"])
|
| 109 |
+
tm.assert_index_equal(idx.intersection(idx, sort=True), sorted_)
|
| 110 |
+
|
| 111 |
+
def test_intersection_base(self, sort):
|
| 112 |
+
# (same results for py2 and py3 but sortedness not tested elsewhere)
|
| 113 |
+
index = Index([0, "a", 1, "b", 2, "c"])
|
| 114 |
+
first = index[:5]
|
| 115 |
+
second = index[:3]
|
| 116 |
+
|
| 117 |
+
expected = Index([0, 1, "a"]) if sort is None else Index([0, "a", 1])
|
| 118 |
+
result = first.intersection(second, sort=sort)
|
| 119 |
+
tm.assert_index_equal(result, expected)
|
| 120 |
+
|
| 121 |
+
@pytest.mark.parametrize("klass", [np.array, Series, list])
|
| 122 |
+
def test_intersection_different_type_base(self, klass, sort):
|
| 123 |
+
# GH 10149
|
| 124 |
+
index = Index([0, "a", 1, "b", 2, "c"])
|
| 125 |
+
first = index[:5]
|
| 126 |
+
second = index[:3]
|
| 127 |
+
|
| 128 |
+
result = first.intersection(klass(second.values), sort=sort)
|
| 129 |
+
assert equal_contents(result, second)
|
| 130 |
+
|
| 131 |
+
def test_intersection_nosort(self):
|
| 132 |
+
result = Index(["c", "b", "a"]).intersection(["b", "a"])
|
| 133 |
+
expected = Index(["b", "a"])
|
| 134 |
+
tm.assert_index_equal(result, expected)
|
| 135 |
+
|
| 136 |
+
def test_intersection_equal_sort(self):
|
| 137 |
+
idx = Index(["c", "a", "b"])
|
| 138 |
+
tm.assert_index_equal(idx.intersection(idx, sort=False), idx)
|
| 139 |
+
tm.assert_index_equal(idx.intersection(idx, sort=None), idx)
|
| 140 |
+
|
| 141 |
+
def test_intersection_str_dates(self, sort):
|
| 142 |
+
dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)]
|
| 143 |
+
|
| 144 |
+
i1 = Index(dt_dates, dtype=object)
|
| 145 |
+
i2 = Index(["aa"], dtype=object)
|
| 146 |
+
result = i2.intersection(i1, sort=sort)
|
| 147 |
+
|
| 148 |
+
assert len(result) == 0
|
| 149 |
+
|
| 150 |
+
@pytest.mark.parametrize(
|
| 151 |
+
"index2,expected_arr",
|
| 152 |
+
[(["B", "D"], ["B"]), (["B", "D", "A"], ["A", "B"])],
|
| 153 |
+
)
|
| 154 |
+
def test_intersection_non_monotonic_non_unique(self, index2, expected_arr, sort):
|
| 155 |
+
# non-monotonic non-unique
|
| 156 |
+
index1 = Index(["A", "B", "A", "C"])
|
| 157 |
+
expected = Index(expected_arr)
|
| 158 |
+
result = index1.intersection(Index(index2), sort=sort)
|
| 159 |
+
if sort is None:
|
| 160 |
+
expected = expected.sort_values()
|
| 161 |
+
tm.assert_index_equal(result, expected)
|
| 162 |
+
|
| 163 |
+
def test_difference_base(self, sort):
|
| 164 |
+
# (same results for py2 and py3 but sortedness not tested elsewhere)
|
| 165 |
+
index = Index([0, "a", 1, "b", 2, "c"])
|
| 166 |
+
first = index[:4]
|
| 167 |
+
second = index[3:]
|
| 168 |
+
|
| 169 |
+
result = first.difference(second, sort)
|
| 170 |
+
expected = Index([0, "a", 1])
|
| 171 |
+
if sort is None:
|
| 172 |
+
expected = Index(safe_sort(expected))
|
| 173 |
+
tm.assert_index_equal(result, expected)
|
| 174 |
+
|
| 175 |
+
def test_symmetric_difference(self):
|
| 176 |
+
# (same results for py2 and py3 but sortedness not tested elsewhere)
|
| 177 |
+
index = Index([0, "a", 1, "b", 2, "c"])
|
| 178 |
+
first = index[:4]
|
| 179 |
+
second = index[3:]
|
| 180 |
+
|
| 181 |
+
result = first.symmetric_difference(second)
|
| 182 |
+
expected = Index([0, 1, 2, "a", "c"])
|
| 183 |
+
tm.assert_index_equal(result, expected)
|
| 184 |
+
|
| 185 |
+
@pytest.mark.parametrize(
|
| 186 |
+
"method,expected,sort",
|
| 187 |
+
[
|
| 188 |
+
(
|
| 189 |
+
"intersection",
|
| 190 |
+
np.array(
|
| 191 |
+
[(1, "A"), (2, "A"), (1, "B"), (2, "B")],
|
| 192 |
+
dtype=[("num", int), ("let", "S1")],
|
| 193 |
+
),
|
| 194 |
+
False,
|
| 195 |
+
),
|
| 196 |
+
(
|
| 197 |
+
"intersection",
|
| 198 |
+
np.array(
|
| 199 |
+
[(1, "A"), (1, "B"), (2, "A"), (2, "B")],
|
| 200 |
+
dtype=[("num", int), ("let", "S1")],
|
| 201 |
+
),
|
| 202 |
+
None,
|
| 203 |
+
),
|
| 204 |
+
(
|
| 205 |
+
"union",
|
| 206 |
+
np.array(
|
| 207 |
+
[(1, "A"), (1, "B"), (1, "C"), (2, "A"), (2, "B"), (2, "C")],
|
| 208 |
+
dtype=[("num", int), ("let", "S1")],
|
| 209 |
+
),
|
| 210 |
+
None,
|
| 211 |
+
),
|
| 212 |
+
],
|
| 213 |
+
)
|
| 214 |
+
def test_tuple_union_bug(self, method, expected, sort):
|
| 215 |
+
index1 = Index(
|
| 216 |
+
np.array(
|
| 217 |
+
[(1, "A"), (2, "A"), (1, "B"), (2, "B")],
|
| 218 |
+
dtype=[("num", int), ("let", "S1")],
|
| 219 |
+
)
|
| 220 |
+
)
|
| 221 |
+
index2 = Index(
|
| 222 |
+
np.array(
|
| 223 |
+
[(1, "A"), (2, "A"), (1, "B"), (2, "B"), (1, "C"), (2, "C")],
|
| 224 |
+
dtype=[("num", int), ("let", "S1")],
|
| 225 |
+
)
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
result = getattr(index1, method)(index2, sort=sort)
|
| 229 |
+
assert result.ndim == 1
|
| 230 |
+
|
| 231 |
+
expected = Index(expected)
|
| 232 |
+
tm.assert_index_equal(result, expected)
|
| 233 |
+
|
| 234 |
+
@pytest.mark.parametrize("first_list", [["b", "a"], []])
|
| 235 |
+
@pytest.mark.parametrize("second_list", [["a", "b"], []])
|
| 236 |
+
@pytest.mark.parametrize(
|
| 237 |
+
"first_name, second_name, expected_name",
|
| 238 |
+
[("A", "B", None), (None, "B", None), ("A", None, None)],
|
| 239 |
+
)
|
| 240 |
+
def test_union_name_preservation(
|
| 241 |
+
self, first_list, second_list, first_name, second_name, expected_name, sort
|
| 242 |
+
):
|
| 243 |
+
first = Index(first_list, name=first_name)
|
| 244 |
+
second = Index(second_list, name=second_name)
|
| 245 |
+
union = first.union(second, sort=sort)
|
| 246 |
+
|
| 247 |
+
vals = set(first_list).union(second_list)
|
| 248 |
+
|
| 249 |
+
if sort is None and len(first_list) > 0 and len(second_list) > 0:
|
| 250 |
+
expected = Index(sorted(vals), name=expected_name)
|
| 251 |
+
tm.assert_index_equal(union, expected)
|
| 252 |
+
else:
|
| 253 |
+
expected = Index(vals, name=expected_name)
|
| 254 |
+
tm.assert_index_equal(union.sort_values(), expected.sort_values())
|
| 255 |
+
|
| 256 |
+
@pytest.mark.parametrize(
|
| 257 |
+
"diff_type, expected",
|
| 258 |
+
[["difference", [1, "B"]], ["symmetric_difference", [1, 2, "B", "C"]]],
|
| 259 |
+
)
|
| 260 |
+
def test_difference_object_type(self, diff_type, expected):
|
| 261 |
+
# GH 13432
|
| 262 |
+
idx1 = Index([0, 1, "A", "B"])
|
| 263 |
+
idx2 = Index([0, 2, "A", "C"])
|
| 264 |
+
result = getattr(idx1, diff_type)(idx2)
|
| 265 |
+
expected = Index(expected)
|
| 266 |
+
tm.assert_index_equal(result, expected)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/base_class/test_where.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from pandas import Index
|
| 4 |
+
import pandas._testing as tm
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestWhere:
|
| 8 |
+
def test_where_intlike_str_doesnt_cast_ints(self):
|
| 9 |
+
idx = Index(range(3))
|
| 10 |
+
mask = np.array([True, False, True])
|
| 11 |
+
res = idx.where(mask, "2")
|
| 12 |
+
expected = Index([0, "2", 2])
|
| 13 |
+
tm.assert_index_equal(res, expected)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/__init__.py
ADDED
|
File without changes
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_append.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from pandas import (
|
| 4 |
+
CategoricalIndex,
|
| 5 |
+
Index,
|
| 6 |
+
)
|
| 7 |
+
import pandas._testing as tm
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TestAppend:
|
| 11 |
+
@pytest.fixture
|
| 12 |
+
def ci(self):
|
| 13 |
+
categories = list("cab")
|
| 14 |
+
return CategoricalIndex(list("aabbca"), categories=categories, ordered=False)
|
| 15 |
+
|
| 16 |
+
def test_append(self, ci):
|
| 17 |
+
# append cats with the same categories
|
| 18 |
+
result = ci[:3].append(ci[3:])
|
| 19 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 20 |
+
|
| 21 |
+
foos = [ci[:1], ci[1:3], ci[3:]]
|
| 22 |
+
result = foos[0].append(foos[1:])
|
| 23 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 24 |
+
|
| 25 |
+
def test_append_empty(self, ci):
|
| 26 |
+
# empty
|
| 27 |
+
result = ci.append([])
|
| 28 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 29 |
+
|
| 30 |
+
def test_append_mismatched_categories(self, ci):
|
| 31 |
+
# appending with different categories or reordered is not ok
|
| 32 |
+
msg = "all inputs must be Index"
|
| 33 |
+
with pytest.raises(TypeError, match=msg):
|
| 34 |
+
ci.append(ci.values.set_categories(list("abcd")))
|
| 35 |
+
with pytest.raises(TypeError, match=msg):
|
| 36 |
+
ci.append(ci.values.reorder_categories(list("abc")))
|
| 37 |
+
|
| 38 |
+
def test_append_category_objects(self, ci):
|
| 39 |
+
# GH#41626 pre-3.0 this used to cast the object-dtype index to
|
| 40 |
+
# ci.dtype
|
| 41 |
+
# with objects
|
| 42 |
+
result = ci.append(Index(["c", "a"]))
|
| 43 |
+
expected = Index(list("aabbcaca"))
|
| 44 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 45 |
+
|
| 46 |
+
def test_append_non_categories(self, ci):
|
| 47 |
+
# invalid objects -> cast to object via concat_compat
|
| 48 |
+
result = ci.append(Index(["a", "d"]))
|
| 49 |
+
expected = Index(["a", "a", "b", "b", "c", "a", "a", "d"])
|
| 50 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 51 |
+
|
| 52 |
+
def test_append_object(self, ci):
|
| 53 |
+
# GH#14298 - if base object is not categorical -> coerce to object
|
| 54 |
+
result = Index(["c", "a"]).append(ci)
|
| 55 |
+
expected = Index(list("caaabbca"))
|
| 56 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 57 |
+
|
| 58 |
+
def test_append_to_another(self):
|
| 59 |
+
# hits Index._concat
|
| 60 |
+
fst = Index(["a", "b"])
|
| 61 |
+
snd = CategoricalIndex(["d", "e"])
|
| 62 |
+
result = fst.append(snd)
|
| 63 |
+
expected = Index(["a", "b", "d", "e"])
|
| 64 |
+
tm.assert_index_equal(result, expected)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_astype.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import date
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from pandas.errors import Pandas4Warning
|
| 7 |
+
|
| 8 |
+
from pandas import (
|
| 9 |
+
Categorical,
|
| 10 |
+
CategoricalDtype,
|
| 11 |
+
CategoricalIndex,
|
| 12 |
+
Index,
|
| 13 |
+
IntervalIndex,
|
| 14 |
+
)
|
| 15 |
+
import pandas._testing as tm
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class TestAstype:
|
| 19 |
+
def test_astype(self):
|
| 20 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
| 21 |
+
|
| 22 |
+
result = ci.astype(object)
|
| 23 |
+
tm.assert_index_equal(result, Index(np.array(ci), dtype=object))
|
| 24 |
+
|
| 25 |
+
# this IS equal, but not the same class
|
| 26 |
+
assert result.equals(ci)
|
| 27 |
+
assert isinstance(result, Index)
|
| 28 |
+
assert not isinstance(result, CategoricalIndex)
|
| 29 |
+
|
| 30 |
+
# interval
|
| 31 |
+
ii = IntervalIndex.from_arrays(left=[-0.001, 2.0], right=[2, 4], closed="right")
|
| 32 |
+
|
| 33 |
+
ci = CategoricalIndex(
|
| 34 |
+
Categorical.from_codes([0, 1, -1], categories=ii, ordered=True)
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
result = ci.astype("interval")
|
| 38 |
+
expected = ii.take([0, 1, -1], allow_fill=True, fill_value=np.nan)
|
| 39 |
+
tm.assert_index_equal(result, expected)
|
| 40 |
+
|
| 41 |
+
result = IntervalIndex(result.values)
|
| 42 |
+
tm.assert_index_equal(result, expected)
|
| 43 |
+
|
| 44 |
+
@pytest.mark.parametrize("name", [None, "foo"])
|
| 45 |
+
@pytest.mark.parametrize("dtype_ordered", [True, False])
|
| 46 |
+
@pytest.mark.parametrize("index_ordered", [True, False])
|
| 47 |
+
def test_astype_category(self, name, dtype_ordered, index_ordered):
|
| 48 |
+
# GH#18630
|
| 49 |
+
index = CategoricalIndex(
|
| 50 |
+
list("aabbca"), categories=list("cab"), ordered=index_ordered
|
| 51 |
+
)
|
| 52 |
+
if name:
|
| 53 |
+
index = index.rename(name)
|
| 54 |
+
|
| 55 |
+
# standard categories
|
| 56 |
+
dtype = CategoricalDtype(ordered=dtype_ordered)
|
| 57 |
+
result = index.astype(dtype)
|
| 58 |
+
expected = CategoricalIndex(
|
| 59 |
+
index.tolist(),
|
| 60 |
+
name=name,
|
| 61 |
+
categories=index.categories,
|
| 62 |
+
ordered=dtype_ordered,
|
| 63 |
+
)
|
| 64 |
+
tm.assert_index_equal(result, expected)
|
| 65 |
+
|
| 66 |
+
# non-standard categories
|
| 67 |
+
dtype = CategoricalDtype(index.unique().tolist()[:-1], dtype_ordered)
|
| 68 |
+
msg = "Constructing a Categorical with a dtype and values containing"
|
| 69 |
+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
|
| 70 |
+
result = index.astype(dtype)
|
| 71 |
+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
|
| 72 |
+
expected = CategoricalIndex(index.tolist(), name=name, dtype=dtype)
|
| 73 |
+
tm.assert_index_equal(result, expected)
|
| 74 |
+
|
| 75 |
+
if dtype_ordered is False:
|
| 76 |
+
# dtype='category' can't specify ordered, so only test once
|
| 77 |
+
result = index.astype("category")
|
| 78 |
+
expected = index
|
| 79 |
+
tm.assert_index_equal(result, expected)
|
| 80 |
+
|
| 81 |
+
@pytest.mark.parametrize("box", [True, False])
|
| 82 |
+
def test_categorical_date_roundtrip(self, box):
|
| 83 |
+
# astype to categorical and back should preserve date objects
|
| 84 |
+
v = date.today()
|
| 85 |
+
|
| 86 |
+
obj = Index([v, v])
|
| 87 |
+
assert obj.dtype == object
|
| 88 |
+
if box:
|
| 89 |
+
obj = obj.array
|
| 90 |
+
|
| 91 |
+
cat = obj.astype("category")
|
| 92 |
+
|
| 93 |
+
rtrip = cat.astype(object)
|
| 94 |
+
assert rtrip.dtype == object
|
| 95 |
+
assert type(rtrip[0]) is date
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_category.py
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas._libs import index as libindex
|
| 5 |
+
from pandas._libs.arrays import NDArrayBacked
|
| 6 |
+
from pandas.errors import Pandas4Warning
|
| 7 |
+
|
| 8 |
+
import pandas as pd
|
| 9 |
+
from pandas import (
|
| 10 |
+
Categorical,
|
| 11 |
+
CategoricalDtype,
|
| 12 |
+
)
|
| 13 |
+
import pandas._testing as tm
|
| 14 |
+
from pandas.core.indexes.api import (
|
| 15 |
+
CategoricalIndex,
|
| 16 |
+
Index,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class TestCategoricalIndex:
|
| 21 |
+
@pytest.fixture
|
| 22 |
+
def simple_index(self) -> CategoricalIndex:
|
| 23 |
+
"""
|
| 24 |
+
Fixture that provides a CategoricalIndex.
|
| 25 |
+
"""
|
| 26 |
+
return CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
| 27 |
+
|
| 28 |
+
def test_can_hold_identifiers(self):
|
| 29 |
+
idx = CategoricalIndex(list("aabbca"), categories=None, ordered=False)
|
| 30 |
+
key = idx[0]
|
| 31 |
+
assert idx._can_hold_identifiers_and_holds_name(key) is True
|
| 32 |
+
|
| 33 |
+
def test_insert(self, simple_index):
|
| 34 |
+
ci = simple_index
|
| 35 |
+
categories = ci.categories
|
| 36 |
+
|
| 37 |
+
# test 0th element
|
| 38 |
+
result = ci.insert(0, "a")
|
| 39 |
+
expected = CategoricalIndex(list("aaabbca"), categories=categories)
|
| 40 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 41 |
+
|
| 42 |
+
# test Nth element that follows Python list behavior
|
| 43 |
+
result = ci.insert(-1, "a")
|
| 44 |
+
expected = CategoricalIndex(list("aabbcaa"), categories=categories)
|
| 45 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 46 |
+
|
| 47 |
+
# test empty
|
| 48 |
+
result = CategoricalIndex([], categories=categories).insert(0, "a")
|
| 49 |
+
expected = CategoricalIndex(["a"], categories=categories)
|
| 50 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 51 |
+
|
| 52 |
+
# invalid -> cast to object
|
| 53 |
+
expected = ci.astype(object).insert(0, "d")
|
| 54 |
+
result = ci.insert(0, "d").astype(object)
|
| 55 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 56 |
+
|
| 57 |
+
# GH 18295 (test missing)
|
| 58 |
+
expected = CategoricalIndex(["a", np.nan, "a", "b", "c", "b"])
|
| 59 |
+
for na in (np.nan, pd.NaT, None):
|
| 60 |
+
result = CategoricalIndex(list("aabcb")).insert(1, na)
|
| 61 |
+
tm.assert_index_equal(result, expected)
|
| 62 |
+
|
| 63 |
+
def test_insert_na_mismatched_dtype(self):
|
| 64 |
+
ci = CategoricalIndex([0, 1, 1])
|
| 65 |
+
result = ci.insert(0, pd.NaT)
|
| 66 |
+
expected = Index([pd.NaT, 0, 1, 1], dtype=object)
|
| 67 |
+
tm.assert_index_equal(result, expected)
|
| 68 |
+
|
| 69 |
+
def test_delete(self, simple_index):
|
| 70 |
+
ci = simple_index
|
| 71 |
+
categories = ci.categories
|
| 72 |
+
|
| 73 |
+
result = ci.delete(0)
|
| 74 |
+
expected = CategoricalIndex(list("abbca"), categories=categories)
|
| 75 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 76 |
+
|
| 77 |
+
result = ci.delete(-1)
|
| 78 |
+
expected = CategoricalIndex(list("aabbc"), categories=categories)
|
| 79 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 80 |
+
|
| 81 |
+
with tm.external_error_raised((IndexError, ValueError)):
|
| 82 |
+
# Either depending on NumPy version
|
| 83 |
+
ci.delete(10)
|
| 84 |
+
|
| 85 |
+
@pytest.mark.parametrize(
|
| 86 |
+
"data, non_lexsorted_data",
|
| 87 |
+
[[[1, 2, 3], [9, 0, 1, 2, 3]], [list("abc"), list("fabcd")]],
|
| 88 |
+
)
|
| 89 |
+
def test_is_monotonic(self, data, non_lexsorted_data):
|
| 90 |
+
c = CategoricalIndex(data)
|
| 91 |
+
assert c.is_monotonic_increasing is True
|
| 92 |
+
assert c.is_monotonic_decreasing is False
|
| 93 |
+
|
| 94 |
+
c = CategoricalIndex(data, ordered=True)
|
| 95 |
+
assert c.is_monotonic_increasing is True
|
| 96 |
+
assert c.is_monotonic_decreasing is False
|
| 97 |
+
|
| 98 |
+
c = CategoricalIndex(data, categories=reversed(data))
|
| 99 |
+
assert c.is_monotonic_increasing is False
|
| 100 |
+
assert c.is_monotonic_decreasing is True
|
| 101 |
+
|
| 102 |
+
c = CategoricalIndex(data, categories=reversed(data), ordered=True)
|
| 103 |
+
assert c.is_monotonic_increasing is False
|
| 104 |
+
assert c.is_monotonic_decreasing is True
|
| 105 |
+
|
| 106 |
+
# test when data is neither monotonic increasing nor decreasing
|
| 107 |
+
reordered_data = [data[0], data[2], data[1]]
|
| 108 |
+
c = CategoricalIndex(reordered_data, categories=reversed(data))
|
| 109 |
+
assert c.is_monotonic_increasing is False
|
| 110 |
+
assert c.is_monotonic_decreasing is False
|
| 111 |
+
|
| 112 |
+
# non lexsorted categories
|
| 113 |
+
categories = non_lexsorted_data
|
| 114 |
+
|
| 115 |
+
c = CategoricalIndex(categories[:2], categories=categories)
|
| 116 |
+
assert c.is_monotonic_increasing is True
|
| 117 |
+
assert c.is_monotonic_decreasing is False
|
| 118 |
+
|
| 119 |
+
c = CategoricalIndex(categories[1:3], categories=categories)
|
| 120 |
+
assert c.is_monotonic_increasing is True
|
| 121 |
+
assert c.is_monotonic_decreasing is False
|
| 122 |
+
|
| 123 |
+
def test_has_duplicates(self):
|
| 124 |
+
idx = CategoricalIndex([0, 0, 0], name="foo")
|
| 125 |
+
assert idx.is_unique is False
|
| 126 |
+
assert idx.has_duplicates is True
|
| 127 |
+
|
| 128 |
+
idx = CategoricalIndex([None, None], categories=[2, 3], name="foo")
|
| 129 |
+
assert idx.is_unique is False
|
| 130 |
+
assert idx.has_duplicates is True
|
| 131 |
+
|
| 132 |
+
idx = CategoricalIndex([None, 1, 2, 3], categories=[1, 2, 3], name="foo")
|
| 133 |
+
assert idx.is_unique is True
|
| 134 |
+
assert idx.has_duplicates is False
|
| 135 |
+
|
| 136 |
+
@pytest.mark.parametrize(
|
| 137 |
+
"data, categories, expected",
|
| 138 |
+
[
|
| 139 |
+
(
|
| 140 |
+
[1, 1, 1],
|
| 141 |
+
[1, 2, 3],
|
| 142 |
+
{
|
| 143 |
+
"first": np.array([False, True, True]),
|
| 144 |
+
"last": np.array([True, True, False]),
|
| 145 |
+
False: np.array([True, True, True]),
|
| 146 |
+
},
|
| 147 |
+
),
|
| 148 |
+
(
|
| 149 |
+
[None, None, None],
|
| 150 |
+
list("abc"),
|
| 151 |
+
{
|
| 152 |
+
"first": np.array([False, True, True]),
|
| 153 |
+
"last": np.array([True, True, False]),
|
| 154 |
+
False: np.array([True, True, True]),
|
| 155 |
+
},
|
| 156 |
+
),
|
| 157 |
+
(
|
| 158 |
+
[None, "a", "b"],
|
| 159 |
+
list("abc"),
|
| 160 |
+
{
|
| 161 |
+
"first": np.zeros(shape=(3), dtype=np.bool_),
|
| 162 |
+
"last": np.zeros(shape=(3), dtype=np.bool_),
|
| 163 |
+
False: np.zeros(shape=(3), dtype=np.bool_),
|
| 164 |
+
},
|
| 165 |
+
),
|
| 166 |
+
(
|
| 167 |
+
list("abb"),
|
| 168 |
+
list("abc"),
|
| 169 |
+
{
|
| 170 |
+
"first": np.array([False, False, True]),
|
| 171 |
+
"last": np.array([False, True, False]),
|
| 172 |
+
False: np.array([False, True, True]),
|
| 173 |
+
},
|
| 174 |
+
),
|
| 175 |
+
],
|
| 176 |
+
)
|
| 177 |
+
def test_drop_duplicates(self, data, categories, expected):
|
| 178 |
+
idx = CategoricalIndex(data, categories=categories, name="foo")
|
| 179 |
+
for keep, e in expected.items():
|
| 180 |
+
tm.assert_numpy_array_equal(idx.duplicated(keep=keep), e)
|
| 181 |
+
e = idx[~e]
|
| 182 |
+
result = idx.drop_duplicates(keep=keep)
|
| 183 |
+
tm.assert_index_equal(result, e)
|
| 184 |
+
|
| 185 |
+
@pytest.mark.parametrize(
|
| 186 |
+
"data, categories, expected_data",
|
| 187 |
+
[
|
| 188 |
+
([1, 1, 1], [1, 2, 3], [1]),
|
| 189 |
+
([1, 1, 1], list("abc"), [np.nan]),
|
| 190 |
+
([1, 2, "a"], [1, 2, 3], [1, 2, np.nan]),
|
| 191 |
+
([2, "a", "b"], list("abc"), [np.nan, "a", "b"]),
|
| 192 |
+
],
|
| 193 |
+
)
|
| 194 |
+
def test_unique(self, data, categories, expected_data, ordered):
|
| 195 |
+
dtype = CategoricalDtype(categories, ordered=ordered)
|
| 196 |
+
|
| 197 |
+
msg = "Constructing a Categorical with a dtype and values containing"
|
| 198 |
+
warn = None if expected_data == [1] else Pandas4Warning
|
| 199 |
+
with tm.assert_produces_warning(warn, match=msg):
|
| 200 |
+
idx = CategoricalIndex(data, dtype=dtype)
|
| 201 |
+
expected = CategoricalIndex(expected_data, dtype=dtype)
|
| 202 |
+
tm.assert_index_equal(idx.unique(), expected)
|
| 203 |
+
|
| 204 |
+
def test_repr_roundtrip(self):
|
| 205 |
+
ci = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
|
| 206 |
+
str(ci)
|
| 207 |
+
tm.assert_index_equal(eval(repr(ci)), ci, exact=True)
|
| 208 |
+
|
| 209 |
+
# formatting
|
| 210 |
+
str(ci)
|
| 211 |
+
|
| 212 |
+
# long format
|
| 213 |
+
# this is not reprable
|
| 214 |
+
ci = CategoricalIndex(np.random.default_rng(2).integers(0, 5, size=100))
|
| 215 |
+
str(ci)
|
| 216 |
+
|
| 217 |
+
def test_isin(self):
|
| 218 |
+
ci = CategoricalIndex([*list("aabca"), np.nan], categories=["c", "a", "b"])
|
| 219 |
+
tm.assert_numpy_array_equal(
|
| 220 |
+
ci.isin(["c"]), np.array([False, False, False, True, False, False])
|
| 221 |
+
)
|
| 222 |
+
tm.assert_numpy_array_equal(
|
| 223 |
+
ci.isin(["c", "a", "b"]), np.array([True] * 5 + [False])
|
| 224 |
+
)
|
| 225 |
+
tm.assert_numpy_array_equal(
|
| 226 |
+
ci.isin(["c", "a", "b", np.nan]), np.array([True] * 6)
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
# mismatched categorical -> coerced to ndarray so doesn't matter
|
| 230 |
+
result = ci.isin(ci.set_categories(list("abcdefghi")))
|
| 231 |
+
expected = np.array([True] * 6)
|
| 232 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 233 |
+
|
| 234 |
+
result = ci.isin(ci.set_categories(list("defghi")))
|
| 235 |
+
expected = np.array([False] * 5 + [True])
|
| 236 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 237 |
+
|
| 238 |
+
def test_isin_overlapping_intervals(self):
|
| 239 |
+
# GH 34974
|
| 240 |
+
idx = pd.IntervalIndex([pd.Interval(0, 2), pd.Interval(0, 1)])
|
| 241 |
+
result = CategoricalIndex(idx).isin(idx)
|
| 242 |
+
expected = np.array([True, True])
|
| 243 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 244 |
+
|
| 245 |
+
def test_identical(self):
|
| 246 |
+
ci1 = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
|
| 247 |
+
ci2 = CategoricalIndex(["a", "b"], categories=["a", "b", "c"], ordered=True)
|
| 248 |
+
assert ci1.identical(ci1)
|
| 249 |
+
assert ci1.identical(ci1.copy())
|
| 250 |
+
assert not ci1.identical(ci2)
|
| 251 |
+
|
| 252 |
+
def test_ensure_copied_data(self):
|
| 253 |
+
# gh-12309: Check the "copy" argument of each
|
| 254 |
+
# Index.__new__ is honored.
|
| 255 |
+
#
|
| 256 |
+
# Must be tested separately from other indexes because
|
| 257 |
+
# self.values is not an ndarray.
|
| 258 |
+
index = CategoricalIndex(list("ab") * 5)
|
| 259 |
+
|
| 260 |
+
result = CategoricalIndex(index.values, copy=True)
|
| 261 |
+
tm.assert_index_equal(index, result)
|
| 262 |
+
assert not np.shares_memory(result._data._codes, index._data._codes)
|
| 263 |
+
|
| 264 |
+
result = CategoricalIndex(index.values, copy=False)
|
| 265 |
+
assert result._data._codes is index._data._codes
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
class TestCategoricalIndex2:
|
| 269 |
+
def test_view_i8(self):
|
| 270 |
+
# GH#25464
|
| 271 |
+
ci = CategoricalIndex(list("ab") * 50)
|
| 272 |
+
msg = "When changing to a larger dtype, its size must be a divisor"
|
| 273 |
+
with pytest.raises(ValueError, match=msg):
|
| 274 |
+
ci.view("i8")
|
| 275 |
+
with pytest.raises(ValueError, match=msg):
|
| 276 |
+
ci._data.view("i8")
|
| 277 |
+
|
| 278 |
+
ci = ci[:-4] # length divisible by 8
|
| 279 |
+
|
| 280 |
+
res = ci.view("i8")
|
| 281 |
+
expected = ci._data.codes.view("i8")
|
| 282 |
+
tm.assert_numpy_array_equal(res, expected)
|
| 283 |
+
|
| 284 |
+
cat = ci._data
|
| 285 |
+
tm.assert_numpy_array_equal(cat.view("i8"), expected)
|
| 286 |
+
|
| 287 |
+
@pytest.mark.parametrize(
|
| 288 |
+
"dtype, engine_type",
|
| 289 |
+
[
|
| 290 |
+
(np.int8, libindex.Int8Engine),
|
| 291 |
+
(np.int16, libindex.Int16Engine),
|
| 292 |
+
(np.int32, libindex.Int32Engine),
|
| 293 |
+
(np.int64, libindex.Int64Engine),
|
| 294 |
+
],
|
| 295 |
+
)
|
| 296 |
+
def test_engine_type(self, dtype, engine_type):
|
| 297 |
+
if dtype != np.int64:
|
| 298 |
+
# num. of uniques required to push CategoricalIndex.codes to a
|
| 299 |
+
# dtype (128 categories required for .codes dtype to be int16 etc.)
|
| 300 |
+
num_uniques = {np.int8: 1, np.int16: 128, np.int32: 32768}[dtype]
|
| 301 |
+
ci = CategoricalIndex(range(num_uniques))
|
| 302 |
+
else:
|
| 303 |
+
# having 2**32 - 2**31 categories would be very memory-intensive,
|
| 304 |
+
# so we cheat a bit with the dtype
|
| 305 |
+
ci = CategoricalIndex(range(32768)) # == 2**16 - 2**(16 - 1)
|
| 306 |
+
arr = ci.values._ndarray.astype("int64")
|
| 307 |
+
NDArrayBacked.__init__(ci._data, arr, ci.dtype)
|
| 308 |
+
assert np.issubdtype(ci.codes.dtype, dtype)
|
| 309 |
+
assert isinstance(ci._engine, engine_type)
|
| 310 |
+
|
| 311 |
+
@pytest.mark.parametrize(
|
| 312 |
+
"func,op_name",
|
| 313 |
+
[
|
| 314 |
+
(lambda idx: idx - idx, "__sub__"),
|
| 315 |
+
(lambda idx: idx + idx, "__add__"),
|
| 316 |
+
(lambda idx: idx - ["a", "b"], "__sub__"),
|
| 317 |
+
(lambda idx: idx + ["a", "b"], "__add__"), # noqa: RUF005
|
| 318 |
+
(lambda idx: ["a", "b"] - idx, "__rsub__"),
|
| 319 |
+
(lambda idx: ["a", "b"] + idx, "__radd__"), # noqa: RUF005
|
| 320 |
+
],
|
| 321 |
+
)
|
| 322 |
+
def test_disallow_addsub_ops(self, func, op_name):
|
| 323 |
+
# GH 10039
|
| 324 |
+
# set ops (+/-) raise TypeError
|
| 325 |
+
idx = Index(Categorical(["a", "b"]))
|
| 326 |
+
cat_or_list = "'(Categorical|list)' and '(Categorical|list)'"
|
| 327 |
+
msg = "|".join(
|
| 328 |
+
[
|
| 329 |
+
rf"unsupported operand type\(s\) for [\+-]: {cat_or_list}",
|
| 330 |
+
"Object with dtype category cannot perform the numpy op (add|subtract)",
|
| 331 |
+
"operation 'r?(add|sub)' not supported for dtype 'str' "
|
| 332 |
+
"with dtype 'category'",
|
| 333 |
+
]
|
| 334 |
+
)
|
| 335 |
+
with pytest.raises(TypeError, match=msg):
|
| 336 |
+
func(idx)
|
| 337 |
+
|
| 338 |
+
def test_method_delegation(self):
|
| 339 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
|
| 340 |
+
result = ci.set_categories(list("cab"))
|
| 341 |
+
tm.assert_index_equal(
|
| 342 |
+
result, CategoricalIndex(list("aabbca"), categories=list("cab"))
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
|
| 346 |
+
result = ci.rename_categories(list("efg"))
|
| 347 |
+
tm.assert_index_equal(
|
| 348 |
+
result, CategoricalIndex(list("ffggef"), categories=list("efg"))
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
# GH18862 (let rename_categories take callables)
|
| 352 |
+
result = ci.rename_categories(lambda x: x.upper())
|
| 353 |
+
tm.assert_index_equal(
|
| 354 |
+
result, CategoricalIndex(list("AABBCA"), categories=list("CAB"))
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
|
| 358 |
+
result = ci.add_categories(["d"])
|
| 359 |
+
tm.assert_index_equal(
|
| 360 |
+
result, CategoricalIndex(list("aabbca"), categories=list("cabd"))
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"))
|
| 364 |
+
result = ci.remove_categories(["c"])
|
| 365 |
+
tm.assert_index_equal(
|
| 366 |
+
result,
|
| 367 |
+
CategoricalIndex([*list("aabb"), np.nan, "a"], categories=list("ab")),
|
| 368 |
+
)
|
| 369 |
+
|
| 370 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
|
| 371 |
+
result = ci.as_unordered()
|
| 372 |
+
tm.assert_index_equal(result, ci)
|
| 373 |
+
|
| 374 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"))
|
| 375 |
+
result = ci.as_ordered()
|
| 376 |
+
tm.assert_index_equal(
|
| 377 |
+
result,
|
| 378 |
+
CategoricalIndex(list("aabbca"), categories=list("cabdef"), ordered=True),
|
| 379 |
+
)
|
| 380 |
+
|
| 381 |
+
# invalid
|
| 382 |
+
msg = "cannot use inplace with CategoricalIndex"
|
| 383 |
+
with pytest.raises(ValueError, match=msg):
|
| 384 |
+
ci.set_categories(list("cab"), inplace=True)
|
| 385 |
+
|
| 386 |
+
def test_remove_maintains_order(self):
|
| 387 |
+
ci = CategoricalIndex(list("abcdda"), categories=list("abcd"))
|
| 388 |
+
result = ci.reorder_categories(["d", "c", "b", "a"], ordered=True)
|
| 389 |
+
tm.assert_index_equal(
|
| 390 |
+
result,
|
| 391 |
+
CategoricalIndex(list("abcdda"), categories=list("dcba"), ordered=True),
|
| 392 |
+
)
|
| 393 |
+
result = result.remove_categories(["c"])
|
| 394 |
+
tm.assert_index_equal(
|
| 395 |
+
result,
|
| 396 |
+
CategoricalIndex(
|
| 397 |
+
["a", "b", np.nan, "d", "d", "a"], categories=list("dba"), ordered=True
|
| 398 |
+
),
|
| 399 |
+
)
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
def test_contains_rangeindex_categories_no_engine():
|
| 403 |
+
ci = CategoricalIndex(range(3))
|
| 404 |
+
assert 2 in ci
|
| 405 |
+
assert 5 not in ci
|
| 406 |
+
assert "_engine" not in ci._cache
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_constructors.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas.errors import Pandas4Warning
|
| 5 |
+
|
| 6 |
+
from pandas import (
|
| 7 |
+
Categorical,
|
| 8 |
+
CategoricalDtype,
|
| 9 |
+
CategoricalIndex,
|
| 10 |
+
Index,
|
| 11 |
+
)
|
| 12 |
+
import pandas._testing as tm
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class TestCategoricalIndexConstructors:
|
| 16 |
+
def test_construction_disallows_scalar(self):
|
| 17 |
+
msg = "must be called with a collection of some kind"
|
| 18 |
+
with pytest.raises(TypeError, match=msg):
|
| 19 |
+
CategoricalIndex(data=1, categories=list("abcd"), ordered=False)
|
| 20 |
+
with pytest.raises(TypeError, match=msg):
|
| 21 |
+
CategoricalIndex(categories=list("abcd"), ordered=False)
|
| 22 |
+
|
| 23 |
+
def test_construction(self):
|
| 24 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("abcd"), ordered=False)
|
| 25 |
+
categories = ci.categories
|
| 26 |
+
|
| 27 |
+
result = Index(ci)
|
| 28 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 29 |
+
assert not result.ordered
|
| 30 |
+
|
| 31 |
+
result = Index(ci.values)
|
| 32 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 33 |
+
assert not result.ordered
|
| 34 |
+
|
| 35 |
+
# empty
|
| 36 |
+
result = CategoricalIndex([], categories=categories)
|
| 37 |
+
tm.assert_index_equal(result.categories, Index(categories))
|
| 38 |
+
tm.assert_numpy_array_equal(result.codes, np.array([], dtype="int8"))
|
| 39 |
+
assert not result.ordered
|
| 40 |
+
|
| 41 |
+
# passing categories
|
| 42 |
+
result = CategoricalIndex(list("aabbca"), categories=categories)
|
| 43 |
+
tm.assert_index_equal(result.categories, Index(categories))
|
| 44 |
+
tm.assert_numpy_array_equal(
|
| 45 |
+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
c = Categorical(list("aabbca"))
|
| 49 |
+
result = CategoricalIndex(c)
|
| 50 |
+
tm.assert_index_equal(result.categories, Index(list("abc")))
|
| 51 |
+
tm.assert_numpy_array_equal(
|
| 52 |
+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
|
| 53 |
+
)
|
| 54 |
+
assert not result.ordered
|
| 55 |
+
|
| 56 |
+
result = CategoricalIndex(c, categories=categories)
|
| 57 |
+
tm.assert_index_equal(result.categories, Index(categories))
|
| 58 |
+
tm.assert_numpy_array_equal(
|
| 59 |
+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
|
| 60 |
+
)
|
| 61 |
+
assert not result.ordered
|
| 62 |
+
|
| 63 |
+
ci = CategoricalIndex(c, categories=list("abcd"))
|
| 64 |
+
result = CategoricalIndex(ci)
|
| 65 |
+
tm.assert_index_equal(result.categories, Index(categories))
|
| 66 |
+
tm.assert_numpy_array_equal(
|
| 67 |
+
result.codes, np.array([0, 0, 1, 1, 2, 0], dtype="int8")
|
| 68 |
+
)
|
| 69 |
+
assert not result.ordered
|
| 70 |
+
|
| 71 |
+
msg = "Constructing a Categorical with a dtype and values containing"
|
| 72 |
+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
|
| 73 |
+
result = CategoricalIndex(ci, categories=list("ab"))
|
| 74 |
+
tm.assert_index_equal(result.categories, Index(list("ab")))
|
| 75 |
+
tm.assert_numpy_array_equal(
|
| 76 |
+
result.codes, np.array([0, 0, 1, 1, -1, 0], dtype="int8")
|
| 77 |
+
)
|
| 78 |
+
assert not result.ordered
|
| 79 |
+
|
| 80 |
+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
|
| 81 |
+
result = CategoricalIndex(ci, categories=list("ab"), ordered=True)
|
| 82 |
+
tm.assert_index_equal(result.categories, Index(list("ab")))
|
| 83 |
+
tm.assert_numpy_array_equal(
|
| 84 |
+
result.codes, np.array([0, 0, 1, 1, -1, 0], dtype="int8")
|
| 85 |
+
)
|
| 86 |
+
assert result.ordered
|
| 87 |
+
|
| 88 |
+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
|
| 89 |
+
result = CategoricalIndex(ci, categories=list("ab"), ordered=True)
|
| 90 |
+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
|
| 91 |
+
expected = CategoricalIndex(
|
| 92 |
+
ci, categories=list("ab"), ordered=True, dtype="category"
|
| 93 |
+
)
|
| 94 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 95 |
+
|
| 96 |
+
# turn me to an Index
|
| 97 |
+
result = Index(np.array(ci))
|
| 98 |
+
assert isinstance(result, Index)
|
| 99 |
+
assert not isinstance(result, CategoricalIndex)
|
| 100 |
+
|
| 101 |
+
def test_construction_with_dtype(self):
|
| 102 |
+
# specify dtype
|
| 103 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("abc"), ordered=False)
|
| 104 |
+
|
| 105 |
+
result = Index(np.array(ci), dtype="category")
|
| 106 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 107 |
+
|
| 108 |
+
result = Index(np.array(ci).tolist(), dtype="category")
|
| 109 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 110 |
+
|
| 111 |
+
# these are generally only equal when the categories are reordered
|
| 112 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
| 113 |
+
|
| 114 |
+
result = Index(np.array(ci), dtype="category").reorder_categories(ci.categories)
|
| 115 |
+
tm.assert_index_equal(result, ci, exact=True)
|
| 116 |
+
|
| 117 |
+
# make sure indexes are handled
|
| 118 |
+
idx = Index(range(3))
|
| 119 |
+
expected = CategoricalIndex([0, 1, 2], categories=idx, ordered=True)
|
| 120 |
+
result = CategoricalIndex(idx, categories=idx, ordered=True)
|
| 121 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 122 |
+
|
| 123 |
+
def test_construction_empty_with_bool_categories(self):
|
| 124 |
+
# see GH#22702
|
| 125 |
+
cat = CategoricalIndex([], categories=[True, False])
|
| 126 |
+
categories = sorted(cat.categories.tolist())
|
| 127 |
+
assert categories == [False, True]
|
| 128 |
+
|
| 129 |
+
def test_construction_with_categorical_dtype(self):
|
| 130 |
+
# construction with CategoricalDtype
|
| 131 |
+
# GH#18109
|
| 132 |
+
data, cats, ordered = "a a b b".split(), "c b a".split(), True
|
| 133 |
+
dtype = CategoricalDtype(categories=cats, ordered=ordered)
|
| 134 |
+
|
| 135 |
+
result = CategoricalIndex(data, dtype=dtype)
|
| 136 |
+
expected = CategoricalIndex(data, categories=cats, ordered=ordered)
|
| 137 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 138 |
+
|
| 139 |
+
# GH#19032
|
| 140 |
+
result = Index(data, dtype=dtype)
|
| 141 |
+
tm.assert_index_equal(result, expected, exact=True)
|
| 142 |
+
|
| 143 |
+
# error when combining categories/ordered and dtype kwargs
|
| 144 |
+
msg = "Cannot specify `categories` or `ordered` together with `dtype`."
|
| 145 |
+
with pytest.raises(ValueError, match=msg):
|
| 146 |
+
CategoricalIndex(data, categories=cats, dtype=dtype)
|
| 147 |
+
|
| 148 |
+
with pytest.raises(ValueError, match=msg):
|
| 149 |
+
CategoricalIndex(data, ordered=ordered, dtype=dtype)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_equals.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
Categorical,
|
| 6 |
+
CategoricalIndex,
|
| 7 |
+
Index,
|
| 8 |
+
MultiIndex,
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestEquals:
|
| 13 |
+
def test_equals_categorical(self):
|
| 14 |
+
ci1 = CategoricalIndex(["a", "b"], categories=["a", "b"], ordered=True)
|
| 15 |
+
ci2 = CategoricalIndex(["a", "b"], categories=["a", "b", "c"], ordered=True)
|
| 16 |
+
|
| 17 |
+
assert ci1.equals(ci1)
|
| 18 |
+
assert not ci1.equals(ci2)
|
| 19 |
+
assert ci1.equals(ci1.astype(object))
|
| 20 |
+
assert ci1.astype(object).equals(ci1)
|
| 21 |
+
|
| 22 |
+
assert (ci1 == ci1).all()
|
| 23 |
+
assert not (ci1 != ci1).all()
|
| 24 |
+
assert not (ci1 > ci1).all()
|
| 25 |
+
assert not (ci1 < ci1).all()
|
| 26 |
+
assert (ci1 <= ci1).all()
|
| 27 |
+
assert (ci1 >= ci1).all()
|
| 28 |
+
|
| 29 |
+
assert not (ci1 == 1).all()
|
| 30 |
+
assert (ci1 == Index(["a", "b"])).all()
|
| 31 |
+
assert (ci1 == ci1.values).all()
|
| 32 |
+
|
| 33 |
+
# invalid comparisons
|
| 34 |
+
with pytest.raises(ValueError, match="Lengths must match"):
|
| 35 |
+
ci1 == Index(["a", "b", "c"])
|
| 36 |
+
|
| 37 |
+
msg = "Categoricals can only be compared if 'categories' are the same"
|
| 38 |
+
with pytest.raises(TypeError, match=msg):
|
| 39 |
+
ci1 == ci2
|
| 40 |
+
with pytest.raises(TypeError, match=msg):
|
| 41 |
+
ci1 == Categorical(ci1.values, ordered=False)
|
| 42 |
+
with pytest.raises(TypeError, match=msg):
|
| 43 |
+
ci1 == Categorical(ci1.values, categories=list("abc"))
|
| 44 |
+
|
| 45 |
+
# tests
|
| 46 |
+
# make sure that we are testing for category inclusion properly
|
| 47 |
+
ci = CategoricalIndex(list("aabca"), categories=["c", "a", "b"])
|
| 48 |
+
assert not ci.equals(list("aabca"))
|
| 49 |
+
# Same categories, but different order
|
| 50 |
+
# Unordered
|
| 51 |
+
assert ci.equals(CategoricalIndex(list("aabca")))
|
| 52 |
+
# Ordered
|
| 53 |
+
assert not ci.equals(CategoricalIndex(list("aabca"), ordered=True))
|
| 54 |
+
assert ci.equals(ci.copy())
|
| 55 |
+
|
| 56 |
+
ci = CategoricalIndex([*list("aabca"), np.nan], categories=["c", "a", "b"])
|
| 57 |
+
assert not ci.equals(list("aabca"))
|
| 58 |
+
assert not ci.equals(CategoricalIndex(list("aabca")))
|
| 59 |
+
assert ci.equals(ci.copy())
|
| 60 |
+
|
| 61 |
+
ci = CategoricalIndex([*list("aabca"), np.nan], categories=["c", "a", "b"])
|
| 62 |
+
assert not ci.equals([*list("aabca"), np.nan])
|
| 63 |
+
assert ci.equals(CategoricalIndex([*list("aabca"), np.nan]))
|
| 64 |
+
assert not ci.equals(CategoricalIndex([*list("aabca"), np.nan], ordered=True))
|
| 65 |
+
assert ci.equals(ci.copy())
|
| 66 |
+
|
| 67 |
+
def test_equals_categorical_unordered(self):
|
| 68 |
+
# https://github.com/pandas-dev/pandas/issues/16603
|
| 69 |
+
a = CategoricalIndex(["A"], categories=["A", "B"])
|
| 70 |
+
b = CategoricalIndex(["A"], categories=["B", "A"])
|
| 71 |
+
c = CategoricalIndex([None], categories=["B", "A"])
|
| 72 |
+
assert a.equals(b)
|
| 73 |
+
assert not a.equals(c)
|
| 74 |
+
assert not b.equals(c)
|
| 75 |
+
|
| 76 |
+
def test_equals_non_category(self):
|
| 77 |
+
# GH#37667 Case where other contains a value not among ci's
|
| 78 |
+
# categories ("D") and also contains np.nan
|
| 79 |
+
ci = CategoricalIndex(["A", "B", np.nan, np.nan])
|
| 80 |
+
other = Index(["A", "B", "D", np.nan])
|
| 81 |
+
|
| 82 |
+
assert not ci.equals(other)
|
| 83 |
+
|
| 84 |
+
def test_equals_multiindex(self):
|
| 85 |
+
# dont raise NotImplementedError when calling is_dtype_compat
|
| 86 |
+
|
| 87 |
+
mi = MultiIndex.from_arrays([["A", "B", "C", "D"], range(4)])
|
| 88 |
+
ci = mi.to_flat_index().astype("category")
|
| 89 |
+
|
| 90 |
+
assert not ci.equals(mi)
|
| 91 |
+
|
| 92 |
+
def test_equals_string_dtype(self, any_string_dtype):
|
| 93 |
+
# GH#55364
|
| 94 |
+
idx = CategoricalIndex(list("abc"), name="B")
|
| 95 |
+
other = Index(["a", "b", "c"], name="B", dtype=any_string_dtype)
|
| 96 |
+
assert idx.equals(other)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_fillna.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import CategoricalIndex
|
| 5 |
+
import pandas._testing as tm
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class TestFillNA:
|
| 9 |
+
def test_fillna_categorical(self):
|
| 10 |
+
# GH#11343
|
| 11 |
+
idx = CategoricalIndex([1.0, np.nan, 3.0, 1.0], name="x")
|
| 12 |
+
# fill by value in categories
|
| 13 |
+
exp = CategoricalIndex([1.0, 1.0, 3.0, 1.0], name="x")
|
| 14 |
+
tm.assert_index_equal(idx.fillna(1.0), exp)
|
| 15 |
+
|
| 16 |
+
cat = idx._data
|
| 17 |
+
|
| 18 |
+
# fill by value not in categories raises TypeError on EA, casts on CI
|
| 19 |
+
msg = "Cannot setitem on a Categorical with a new category"
|
| 20 |
+
with pytest.raises(TypeError, match=msg):
|
| 21 |
+
cat.fillna(2.0)
|
| 22 |
+
|
| 23 |
+
result = idx.fillna(2.0)
|
| 24 |
+
expected = idx.astype(object).fillna(2.0)
|
| 25 |
+
tm.assert_index_equal(result, expected)
|
| 26 |
+
|
| 27 |
+
def test_fillna_copies_with_no_nas(self):
|
| 28 |
+
# Nothing to fill, should still get a copy for the Categorical method,
|
| 29 |
+
# but OK to get a view on CategoricalIndex method
|
| 30 |
+
ci = CategoricalIndex([0, 1, 1])
|
| 31 |
+
result = ci.fillna(0)
|
| 32 |
+
assert result is not ci
|
| 33 |
+
assert tm.shares_memory(result, ci)
|
| 34 |
+
|
| 35 |
+
# But at the EA level we always get a copy.
|
| 36 |
+
cat = ci._data
|
| 37 |
+
result = cat.fillna(0)
|
| 38 |
+
assert result._ndarray is not cat._ndarray
|
| 39 |
+
assert result._ndarray.base is None
|
| 40 |
+
assert not tm.shares_memory(result, cat)
|
| 41 |
+
|
| 42 |
+
def test_fillna_validates_with_no_nas(self):
|
| 43 |
+
# We validate the fill value even if fillna is a no-op
|
| 44 |
+
ci = CategoricalIndex([2, 3, 3])
|
| 45 |
+
cat = ci._data
|
| 46 |
+
|
| 47 |
+
msg = "Cannot setitem on a Categorical with a new category"
|
| 48 |
+
res = ci.fillna(False)
|
| 49 |
+
# nothing to fill, so we dont cast
|
| 50 |
+
tm.assert_index_equal(res, ci)
|
| 51 |
+
|
| 52 |
+
# Same check directly on the Categorical
|
| 53 |
+
with pytest.raises(TypeError, match=msg):
|
| 54 |
+
cat.fillna(False)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_formats.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tests for CategoricalIndex.__repr__ and related methods.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import pandas._config.config as cf
|
| 6 |
+
|
| 7 |
+
from pandas import CategoricalIndex
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class TestCategoricalIndexReprStringCategories:
|
| 11 |
+
def test_string_categorical_index_repr(self):
|
| 12 |
+
# short
|
| 13 |
+
idx = CategoricalIndex(["a", "bb", "ccc"])
|
| 14 |
+
expected = """CategoricalIndex(['a', 'bb', 'ccc'], categories=['a', 'bb', 'ccc'], ordered=False, dtype='category')""" # noqa: E501
|
| 15 |
+
assert repr(idx) == expected
|
| 16 |
+
|
| 17 |
+
def test_categorical_index_repr_multiline(self):
|
| 18 |
+
# multiple lines
|
| 19 |
+
idx = CategoricalIndex(["a", "bb", "ccc"] * 10)
|
| 20 |
+
expected = """CategoricalIndex(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',
|
| 21 |
+
'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb',
|
| 22 |
+
'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],
|
| 23 |
+
categories=['a', 'bb', 'ccc'], ordered=False, dtype='category')""" # noqa: E501
|
| 24 |
+
assert repr(idx) == expected
|
| 25 |
+
|
| 26 |
+
def test_categorical_index_repr_truncated(self):
|
| 27 |
+
# truncated
|
| 28 |
+
idx = CategoricalIndex(["a", "bb", "ccc"] * 100)
|
| 29 |
+
expected = """CategoricalIndex(['a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a',
|
| 30 |
+
...
|
| 31 |
+
'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc', 'a', 'bb', 'ccc'],
|
| 32 |
+
categories=['a', 'bb', 'ccc'], ordered=False, dtype='category', length=300)""" # noqa: E501
|
| 33 |
+
assert repr(idx) == expected
|
| 34 |
+
|
| 35 |
+
def test_categorical_index_repr_many_categories(self):
|
| 36 |
+
# larger categories
|
| 37 |
+
idx = CategoricalIndex(list("abcdefghijklmmo"))
|
| 38 |
+
expected = """CategoricalIndex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
|
| 39 |
+
'm', 'm', 'o'],
|
| 40 |
+
categories=['a', 'b', 'c', 'd', ..., 'k', 'l', 'm', 'o'], ordered=False, dtype='category')""" # noqa: E501
|
| 41 |
+
assert repr(idx) == expected
|
| 42 |
+
|
| 43 |
+
def test_categorical_index_repr_unicode(self):
|
| 44 |
+
# short
|
| 45 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"])
|
| 46 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう'], categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category')""" # noqa: E501
|
| 47 |
+
assert repr(idx) == expected
|
| 48 |
+
|
| 49 |
+
def test_categorical_index_repr_unicode_multiline(self):
|
| 50 |
+
# multiple lines
|
| 51 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"] * 10)
|
| 52 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ',
|
| 53 |
+
'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい',
|
| 54 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう'],
|
| 55 |
+
categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category')""" # noqa: E501
|
| 56 |
+
assert repr(idx) == expected
|
| 57 |
+
|
| 58 |
+
def test_categorical_index_repr_unicode_truncated(self):
|
| 59 |
+
# truncated
|
| 60 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"] * 100)
|
| 61 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ',
|
| 62 |
+
...
|
| 63 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう'],
|
| 64 |
+
categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category', length=300)""" # noqa: E501
|
| 65 |
+
assert repr(idx) == expected
|
| 66 |
+
|
| 67 |
+
def test_categorical_index_repr_unicode_many_categories(self):
|
| 68 |
+
# larger categories
|
| 69 |
+
idx = CategoricalIndex(list("あいうえおかきくけこさしすせそ"))
|
| 70 |
+
expected = """CategoricalIndex(['あ', 'い', 'う', 'え', 'お', 'か', 'き', 'く', 'け', 'こ', 'さ', 'し',
|
| 71 |
+
'す', 'せ', 'そ'],
|
| 72 |
+
categories=['あ', 'い', 'う', 'え', ..., 'し', 'す', 'せ', 'そ'], ordered=False, dtype='category')""" # noqa: E501
|
| 73 |
+
assert repr(idx) == expected
|
| 74 |
+
|
| 75 |
+
def test_categorical_index_repr_east_asian_width(self):
|
| 76 |
+
with cf.option_context("display.unicode.east_asian_width", True):
|
| 77 |
+
# short
|
| 78 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"])
|
| 79 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう'], categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category')""" # noqa: E501
|
| 80 |
+
assert repr(idx) == expected
|
| 81 |
+
|
| 82 |
+
def test_categorical_index_repr_east_asian_width_multiline(self):
|
| 83 |
+
with cf.option_context("display.unicode.east_asian_width", True):
|
| 84 |
+
# multiple lines
|
| 85 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"] * 10)
|
| 86 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい',
|
| 87 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',
|
| 88 |
+
'あ', 'いい', 'ううう', 'あ', 'いい', 'う��う', 'あ', 'いい',
|
| 89 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう'],
|
| 90 |
+
categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category')""" # noqa: E501
|
| 91 |
+
|
| 92 |
+
assert repr(idx) == expected
|
| 93 |
+
|
| 94 |
+
def test_categorical_index_repr_east_asian_width_truncated(self):
|
| 95 |
+
with cf.option_context("display.unicode.east_asian_width", True):
|
| 96 |
+
# truncated
|
| 97 |
+
idx = CategoricalIndex(["あ", "いい", "ううう"] * 100)
|
| 98 |
+
expected = """CategoricalIndex(['あ', 'いい', 'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい',
|
| 99 |
+
'ううう', 'あ',
|
| 100 |
+
...
|
| 101 |
+
'ううう', 'あ', 'いい', 'ううう', 'あ', 'いい', 'ううう',
|
| 102 |
+
'あ', 'いい', 'ううう'],
|
| 103 |
+
categories=['あ', 'いい', 'ううう'], ordered=False, dtype='category', length=300)""" # noqa: E501
|
| 104 |
+
|
| 105 |
+
assert repr(idx) == expected
|
| 106 |
+
|
| 107 |
+
def test_categorical_index_repr_east_asian_width_many_categories(self):
|
| 108 |
+
with cf.option_context("display.unicode.east_asian_width", True):
|
| 109 |
+
idx = CategoricalIndex(list("あいうえおかきくけこさしすせそ"))
|
| 110 |
+
expected = """CategoricalIndex(['あ', 'い', 'う', 'え', 'お', 'か', 'き', 'く', 'け', 'こ',
|
| 111 |
+
'さ', 'し', 'す', 'せ', 'そ'],
|
| 112 |
+
categories=['あ', 'い', 'う', 'え', ..., 'し', 'す', 'せ', 'そ'], ordered=False, dtype='category')""" # noqa: E501
|
| 113 |
+
|
| 114 |
+
assert repr(idx) == expected
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_indexing.py
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas.errors import InvalidIndexError
|
| 5 |
+
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from pandas import (
|
| 8 |
+
CategoricalIndex,
|
| 9 |
+
Index,
|
| 10 |
+
IntervalIndex,
|
| 11 |
+
Timestamp,
|
| 12 |
+
)
|
| 13 |
+
import pandas._testing as tm
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TestTake:
|
| 17 |
+
def test_take_fill_value(self):
|
| 18 |
+
# GH 12631
|
| 19 |
+
|
| 20 |
+
# numeric category
|
| 21 |
+
idx = CategoricalIndex([1, 2, 3], name="xxx")
|
| 22 |
+
result = idx.take(np.array([1, 0, -1]))
|
| 23 |
+
expected = CategoricalIndex([2, 1, 3], name="xxx")
|
| 24 |
+
tm.assert_index_equal(result, expected)
|
| 25 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
| 26 |
+
|
| 27 |
+
# fill_value
|
| 28 |
+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
| 29 |
+
expected = CategoricalIndex([2, 1, np.nan], categories=[1, 2, 3], name="xxx")
|
| 30 |
+
tm.assert_index_equal(result, expected)
|
| 31 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
| 32 |
+
|
| 33 |
+
# allow_fill=False
|
| 34 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
| 35 |
+
expected = CategoricalIndex([2, 1, 3], name="xxx")
|
| 36 |
+
tm.assert_index_equal(result, expected)
|
| 37 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
| 38 |
+
|
| 39 |
+
# object category
|
| 40 |
+
idx = CategoricalIndex(
|
| 41 |
+
list("CBA"), categories=list("ABC"), ordered=True, name="xxx"
|
| 42 |
+
)
|
| 43 |
+
result = idx.take(np.array([1, 0, -1]))
|
| 44 |
+
expected = CategoricalIndex(
|
| 45 |
+
list("BCA"), categories=list("ABC"), ordered=True, name="xxx"
|
| 46 |
+
)
|
| 47 |
+
tm.assert_index_equal(result, expected)
|
| 48 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
| 49 |
+
|
| 50 |
+
# fill_value
|
| 51 |
+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
| 52 |
+
expected = CategoricalIndex(
|
| 53 |
+
["B", "C", np.nan], categories=list("ABC"), ordered=True, name="xxx"
|
| 54 |
+
)
|
| 55 |
+
tm.assert_index_equal(result, expected)
|
| 56 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
| 57 |
+
|
| 58 |
+
# allow_fill=False
|
| 59 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
| 60 |
+
expected = CategoricalIndex(
|
| 61 |
+
list("BCA"), categories=list("ABC"), ordered=True, name="xxx"
|
| 62 |
+
)
|
| 63 |
+
tm.assert_index_equal(result, expected)
|
| 64 |
+
tm.assert_categorical_equal(result.values, expected.values)
|
| 65 |
+
|
| 66 |
+
msg = (
|
| 67 |
+
"When allow_fill=True and fill_value is not None, all indices must be >= -1"
|
| 68 |
+
)
|
| 69 |
+
with pytest.raises(ValueError, match=msg):
|
| 70 |
+
idx.take(np.array([1, 0, -2]), fill_value=True)
|
| 71 |
+
with pytest.raises(ValueError, match=msg):
|
| 72 |
+
idx.take(np.array([1, 0, -5]), fill_value=True)
|
| 73 |
+
|
| 74 |
+
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
|
| 75 |
+
with pytest.raises(IndexError, match=msg):
|
| 76 |
+
idx.take(np.array([1, -5]))
|
| 77 |
+
|
| 78 |
+
def test_take_fill_value_datetime(self):
|
| 79 |
+
# datetime category
|
| 80 |
+
idx = pd.DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"], name="xxx")
|
| 81 |
+
idx = CategoricalIndex(idx)
|
| 82 |
+
result = idx.take(np.array([1, 0, -1]))
|
| 83 |
+
expected = pd.DatetimeIndex(
|
| 84 |
+
["2011-02-01", "2011-01-01", "2011-03-01"], name="xxx"
|
| 85 |
+
)
|
| 86 |
+
expected = CategoricalIndex(expected)
|
| 87 |
+
tm.assert_index_equal(result, expected)
|
| 88 |
+
|
| 89 |
+
# fill_value
|
| 90 |
+
result = idx.take(np.array([1, 0, -1]), fill_value=True)
|
| 91 |
+
expected = pd.DatetimeIndex(["2011-02-01", "2011-01-01", "NaT"], name="xxx")
|
| 92 |
+
exp_cats = pd.DatetimeIndex(["2011-01-01", "2011-02-01", "2011-03-01"])
|
| 93 |
+
expected = CategoricalIndex(expected, categories=exp_cats)
|
| 94 |
+
tm.assert_index_equal(result, expected)
|
| 95 |
+
|
| 96 |
+
# allow_fill=False
|
| 97 |
+
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
|
| 98 |
+
expected = pd.DatetimeIndex(
|
| 99 |
+
["2011-02-01", "2011-01-01", "2011-03-01"], name="xxx"
|
| 100 |
+
)
|
| 101 |
+
expected = CategoricalIndex(expected)
|
| 102 |
+
tm.assert_index_equal(result, expected)
|
| 103 |
+
|
| 104 |
+
msg = (
|
| 105 |
+
"When allow_fill=True and fill_value is not None, all indices must be >= -1"
|
| 106 |
+
)
|
| 107 |
+
with pytest.raises(ValueError, match=msg):
|
| 108 |
+
idx.take(np.array([1, 0, -2]), fill_value=True)
|
| 109 |
+
with pytest.raises(ValueError, match=msg):
|
| 110 |
+
idx.take(np.array([1, 0, -5]), fill_value=True)
|
| 111 |
+
|
| 112 |
+
msg = "index -5 is out of bounds for (axis 0 with )?size 3"
|
| 113 |
+
with pytest.raises(IndexError, match=msg):
|
| 114 |
+
idx.take(np.array([1, -5]))
|
| 115 |
+
|
| 116 |
+
def test_take_invalid_kwargs(self):
|
| 117 |
+
idx = CategoricalIndex([1, 2, 3], name="foo")
|
| 118 |
+
indices = [1, 0, -1]
|
| 119 |
+
|
| 120 |
+
msg = r"take\(\) got an unexpected keyword argument 'foo'"
|
| 121 |
+
with pytest.raises(TypeError, match=msg):
|
| 122 |
+
idx.take(indices, foo=2)
|
| 123 |
+
|
| 124 |
+
msg = "the 'out' parameter is not supported"
|
| 125 |
+
with pytest.raises(ValueError, match=msg):
|
| 126 |
+
idx.take(indices, out=indices)
|
| 127 |
+
|
| 128 |
+
msg = "the 'mode' parameter is not supported"
|
| 129 |
+
with pytest.raises(ValueError, match=msg):
|
| 130 |
+
idx.take(indices, mode="clip")
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
class TestGetLoc:
|
| 134 |
+
def test_get_loc(self):
|
| 135 |
+
# GH 12531
|
| 136 |
+
cidx1 = CategoricalIndex(list("abcde"), categories=list("edabc"))
|
| 137 |
+
idx1 = Index(list("abcde"))
|
| 138 |
+
assert cidx1.get_loc("a") == idx1.get_loc("a")
|
| 139 |
+
assert cidx1.get_loc("e") == idx1.get_loc("e")
|
| 140 |
+
|
| 141 |
+
for i in [cidx1, idx1]:
|
| 142 |
+
with pytest.raises(KeyError, match="'NOT-EXIST'"):
|
| 143 |
+
i.get_loc("NOT-EXIST")
|
| 144 |
+
|
| 145 |
+
# non-unique
|
| 146 |
+
cidx2 = CategoricalIndex(list("aacded"), categories=list("edabc"))
|
| 147 |
+
idx2 = Index(list("aacded"))
|
| 148 |
+
|
| 149 |
+
# results in bool array
|
| 150 |
+
res = cidx2.get_loc("d")
|
| 151 |
+
tm.assert_numpy_array_equal(res, idx2.get_loc("d"))
|
| 152 |
+
tm.assert_numpy_array_equal(
|
| 153 |
+
res, np.array([False, False, False, True, False, True])
|
| 154 |
+
)
|
| 155 |
+
# unique element results in scalar
|
| 156 |
+
res = cidx2.get_loc("e")
|
| 157 |
+
assert res == idx2.get_loc("e")
|
| 158 |
+
assert res == 4
|
| 159 |
+
|
| 160 |
+
for i in [cidx2, idx2]:
|
| 161 |
+
with pytest.raises(KeyError, match="'NOT-EXIST'"):
|
| 162 |
+
i.get_loc("NOT-EXIST")
|
| 163 |
+
|
| 164 |
+
# non-unique, sliceable
|
| 165 |
+
cidx3 = CategoricalIndex(list("aabbb"), categories=list("abc"))
|
| 166 |
+
idx3 = Index(list("aabbb"))
|
| 167 |
+
|
| 168 |
+
# results in slice
|
| 169 |
+
res = cidx3.get_loc("a")
|
| 170 |
+
assert res == idx3.get_loc("a")
|
| 171 |
+
assert res == slice(0, 2, None)
|
| 172 |
+
|
| 173 |
+
res = cidx3.get_loc("b")
|
| 174 |
+
assert res == idx3.get_loc("b")
|
| 175 |
+
assert res == slice(2, 5, None)
|
| 176 |
+
|
| 177 |
+
for i in [cidx3, idx3]:
|
| 178 |
+
with pytest.raises(KeyError, match="'c'"):
|
| 179 |
+
i.get_loc("c")
|
| 180 |
+
|
| 181 |
+
def test_get_loc_unique(self):
|
| 182 |
+
cidx = CategoricalIndex(list("abc"))
|
| 183 |
+
result = cidx.get_loc("b")
|
| 184 |
+
assert result == 1
|
| 185 |
+
|
| 186 |
+
def test_get_loc_monotonic_nonunique(self):
|
| 187 |
+
cidx = CategoricalIndex(list("abbc"))
|
| 188 |
+
result = cidx.get_loc("b")
|
| 189 |
+
expected = slice(1, 3, None)
|
| 190 |
+
assert result == expected
|
| 191 |
+
|
| 192 |
+
def test_get_loc_nonmonotonic_nonunique(self):
|
| 193 |
+
cidx = CategoricalIndex(list("abcb"))
|
| 194 |
+
result = cidx.get_loc("b")
|
| 195 |
+
expected = np.array([False, True, False, True], dtype=bool)
|
| 196 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 197 |
+
|
| 198 |
+
def test_get_loc_nan(self):
|
| 199 |
+
# GH#41933
|
| 200 |
+
ci = CategoricalIndex(["A", "B", np.nan])
|
| 201 |
+
res = ci.get_loc(np.nan)
|
| 202 |
+
|
| 203 |
+
assert res == 2
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
class TestGetIndexer:
|
| 207 |
+
def test_get_indexer_base(self):
|
| 208 |
+
# Determined by cat ordering.
|
| 209 |
+
idx = CategoricalIndex(list("cab"), categories=list("cab"))
|
| 210 |
+
expected = np.arange(len(idx), dtype=np.intp)
|
| 211 |
+
|
| 212 |
+
actual = idx.get_indexer(idx)
|
| 213 |
+
tm.assert_numpy_array_equal(expected, actual)
|
| 214 |
+
|
| 215 |
+
with pytest.raises(ValueError, match="Invalid fill method"):
|
| 216 |
+
idx.get_indexer(idx, method="invalid")
|
| 217 |
+
|
| 218 |
+
def test_get_indexer_requires_unique(self):
|
| 219 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
| 220 |
+
oidx = Index(np.array(ci))
|
| 221 |
+
|
| 222 |
+
msg = "Reindexing only valid with uniquely valued Index objects"
|
| 223 |
+
|
| 224 |
+
for n in [1, 2, 5, len(ci)]:
|
| 225 |
+
finder = oidx[np.random.default_rng(2).integers(0, len(ci), size=n)]
|
| 226 |
+
|
| 227 |
+
with pytest.raises(InvalidIndexError, match=msg):
|
| 228 |
+
ci.get_indexer(finder)
|
| 229 |
+
|
| 230 |
+
# see gh-17323
|
| 231 |
+
#
|
| 232 |
+
# Even when indexer is equal to the
|
| 233 |
+
# members in the index, we should
|
| 234 |
+
# respect duplicates instead of taking
|
| 235 |
+
# the fast-track path.
|
| 236 |
+
for finder in [list("aabbca"), list("aababca")]:
|
| 237 |
+
with pytest.raises(InvalidIndexError, match=msg):
|
| 238 |
+
ci.get_indexer(finder)
|
| 239 |
+
|
| 240 |
+
def test_get_indexer_non_unique(self):
|
| 241 |
+
idx1 = CategoricalIndex(list("aabcde"), categories=list("edabc"))
|
| 242 |
+
idx2 = CategoricalIndex(list("abf"))
|
| 243 |
+
|
| 244 |
+
for indexer in [idx2, list("abf"), Index(list("abf"))]:
|
| 245 |
+
msg = "Reindexing only valid with uniquely valued Index objects"
|
| 246 |
+
with pytest.raises(InvalidIndexError, match=msg):
|
| 247 |
+
idx1.get_indexer(indexer)
|
| 248 |
+
|
| 249 |
+
r1, _ = idx1.get_indexer_non_unique(indexer)
|
| 250 |
+
expected = np.array([0, 1, 2, -1], dtype=np.intp)
|
| 251 |
+
tm.assert_almost_equal(r1, expected)
|
| 252 |
+
|
| 253 |
+
def test_get_indexer_method(self):
|
| 254 |
+
idx1 = CategoricalIndex(list("aabcde"), categories=list("edabc"))
|
| 255 |
+
idx2 = CategoricalIndex(list("abf"))
|
| 256 |
+
|
| 257 |
+
msg = "method pad not yet implemented for CategoricalIndex"
|
| 258 |
+
with pytest.raises(NotImplementedError, match=msg):
|
| 259 |
+
idx2.get_indexer(idx1, method="pad")
|
| 260 |
+
msg = "method backfill not yet implemented for CategoricalIndex"
|
| 261 |
+
with pytest.raises(NotImplementedError, match=msg):
|
| 262 |
+
idx2.get_indexer(idx1, method="backfill")
|
| 263 |
+
|
| 264 |
+
msg = "method nearest not yet implemented for CategoricalIndex"
|
| 265 |
+
with pytest.raises(NotImplementedError, match=msg):
|
| 266 |
+
idx2.get_indexer(idx1, method="nearest")
|
| 267 |
+
|
| 268 |
+
def test_get_indexer_array(self):
|
| 269 |
+
arr = np.array(
|
| 270 |
+
[Timestamp("1999-12-31 00:00:00"), Timestamp("2000-12-31 00:00:00")],
|
| 271 |
+
dtype=object,
|
| 272 |
+
)
|
| 273 |
+
cats = [Timestamp("1999-12-31 00:00:00"), Timestamp("2000-12-31 00:00:00")]
|
| 274 |
+
ci = CategoricalIndex(cats, categories=cats, ordered=False, dtype="category")
|
| 275 |
+
result = ci.get_indexer(arr)
|
| 276 |
+
expected = np.array([0, 1], dtype="intp")
|
| 277 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 278 |
+
|
| 279 |
+
def test_get_indexer_same_categories_same_order(self):
|
| 280 |
+
ci = CategoricalIndex(["a", "b"], categories=["a", "b"])
|
| 281 |
+
|
| 282 |
+
result = ci.get_indexer(CategoricalIndex(["b", "b"], categories=["a", "b"]))
|
| 283 |
+
expected = np.array([1, 1], dtype="intp")
|
| 284 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 285 |
+
|
| 286 |
+
def test_get_indexer_same_categories_different_order(self):
|
| 287 |
+
# https://github.com/pandas-dev/pandas/issues/19551
|
| 288 |
+
ci = CategoricalIndex(["a", "b"], categories=["a", "b"])
|
| 289 |
+
|
| 290 |
+
result = ci.get_indexer(CategoricalIndex(["b", "b"], categories=["b", "a"]))
|
| 291 |
+
expected = np.array([1, 1], dtype="intp")
|
| 292 |
+
tm.assert_numpy_array_equal(result, expected)
|
| 293 |
+
|
| 294 |
+
def test_get_indexer_nans_in_index_and_target(self):
|
| 295 |
+
# GH 45361
|
| 296 |
+
ci = CategoricalIndex([1, 2, np.nan, 3])
|
| 297 |
+
other1 = [2, 3, 4, np.nan]
|
| 298 |
+
res1 = ci.get_indexer(other1)
|
| 299 |
+
expected1 = np.array([1, 3, -1, 2], dtype=np.intp)
|
| 300 |
+
tm.assert_numpy_array_equal(res1, expected1)
|
| 301 |
+
other2 = [1, 4, 2, 3]
|
| 302 |
+
res2 = ci.get_indexer(other2)
|
| 303 |
+
expected2 = np.array([0, -1, 1, 3], dtype=np.intp)
|
| 304 |
+
tm.assert_numpy_array_equal(res2, expected2)
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
class TestWhere:
|
| 308 |
+
def test_where(self, listlike_box):
|
| 309 |
+
klass = listlike_box
|
| 310 |
+
|
| 311 |
+
i = CategoricalIndex(list("aabbca"), categories=list("cab"), ordered=False)
|
| 312 |
+
cond = [True] * len(i)
|
| 313 |
+
expected = i
|
| 314 |
+
result = i.where(klass(cond))
|
| 315 |
+
tm.assert_index_equal(result, expected)
|
| 316 |
+
|
| 317 |
+
cond = [False] + [True] * (len(i) - 1)
|
| 318 |
+
expected = CategoricalIndex([np.nan, *i[1:].tolist()], categories=i.categories)
|
| 319 |
+
result = i.where(klass(cond))
|
| 320 |
+
tm.assert_index_equal(result, expected)
|
| 321 |
+
|
| 322 |
+
def test_where_non_categories(self):
|
| 323 |
+
ci = CategoricalIndex(["a", "b", "c", "d"])
|
| 324 |
+
mask = np.array([True, False, True, False])
|
| 325 |
+
|
| 326 |
+
result = ci.where(mask, 2)
|
| 327 |
+
expected = Index(["a", 2, "c", 2], dtype=object)
|
| 328 |
+
tm.assert_index_equal(result, expected)
|
| 329 |
+
|
| 330 |
+
msg = "Cannot setitem on a Categorical with a new category"
|
| 331 |
+
with pytest.raises(TypeError, match=msg):
|
| 332 |
+
# Test the Categorical method directly
|
| 333 |
+
ci._data._where(mask, 2)
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
class TestContains:
|
| 337 |
+
def test_contains(self):
|
| 338 |
+
ci = CategoricalIndex(list("aabbca"), categories=list("cabdef"), ordered=False)
|
| 339 |
+
|
| 340 |
+
assert "a" in ci
|
| 341 |
+
assert "z" not in ci
|
| 342 |
+
assert "e" not in ci
|
| 343 |
+
assert np.nan not in ci
|
| 344 |
+
|
| 345 |
+
# assert codes NOT in index
|
| 346 |
+
assert 0 not in ci
|
| 347 |
+
assert 1 not in ci
|
| 348 |
+
|
| 349 |
+
def test_contains_nan(self):
|
| 350 |
+
ci = CategoricalIndex([*list("aabbca"), np.nan], categories=list("cabdef"))
|
| 351 |
+
assert np.nan in ci
|
| 352 |
+
|
| 353 |
+
@pytest.mark.parametrize("unwrap", [True, False])
|
| 354 |
+
def test_contains_na_dtype(self, unwrap):
|
| 355 |
+
dti = pd.date_range("2016-01-01", periods=100).insert(0, pd.NaT)
|
| 356 |
+
pi = dti.to_period("D")
|
| 357 |
+
tdi = dti - dti[-1]
|
| 358 |
+
ci = CategoricalIndex(dti)
|
| 359 |
+
|
| 360 |
+
obj = ci
|
| 361 |
+
if unwrap:
|
| 362 |
+
obj = ci._data
|
| 363 |
+
|
| 364 |
+
assert np.nan in obj
|
| 365 |
+
assert None in obj
|
| 366 |
+
assert pd.NaT in obj
|
| 367 |
+
assert np.datetime64("NaT", "ns") in obj
|
| 368 |
+
assert np.timedelta64("NaT", "ns") not in obj
|
| 369 |
+
|
| 370 |
+
obj2 = CategoricalIndex(tdi)
|
| 371 |
+
if unwrap:
|
| 372 |
+
obj2 = obj2._data
|
| 373 |
+
|
| 374 |
+
assert np.nan in obj2
|
| 375 |
+
assert None in obj2
|
| 376 |
+
assert pd.NaT in obj2
|
| 377 |
+
assert np.datetime64("NaT", "ns") not in obj2
|
| 378 |
+
assert np.timedelta64("NaT", "ns") in obj2
|
| 379 |
+
|
| 380 |
+
obj3 = CategoricalIndex(pi)
|
| 381 |
+
if unwrap:
|
| 382 |
+
obj3 = obj3._data
|
| 383 |
+
|
| 384 |
+
assert np.nan in obj3
|
| 385 |
+
assert None in obj3
|
| 386 |
+
assert pd.NaT in obj3
|
| 387 |
+
assert np.datetime64("NaT", "ns") not in obj3
|
| 388 |
+
assert np.timedelta64("NaT", "ns") not in obj3
|
| 389 |
+
|
| 390 |
+
@pytest.mark.parametrize(
|
| 391 |
+
"item, expected",
|
| 392 |
+
[
|
| 393 |
+
(pd.Interval(0, 1), True),
|
| 394 |
+
(1.5, True),
|
| 395 |
+
(pd.Interval(0.5, 1.5), False),
|
| 396 |
+
("a", False),
|
| 397 |
+
(Timestamp(1), False),
|
| 398 |
+
(pd.Timedelta(1), False),
|
| 399 |
+
],
|
| 400 |
+
ids=str,
|
| 401 |
+
)
|
| 402 |
+
def test_contains_interval(self, item, expected):
|
| 403 |
+
# GH 23705
|
| 404 |
+
ci = CategoricalIndex(IntervalIndex.from_breaks(range(3)))
|
| 405 |
+
result = item in ci
|
| 406 |
+
assert result is expected
|
| 407 |
+
|
| 408 |
+
def test_contains_list(self):
|
| 409 |
+
# GH#21729
|
| 410 |
+
idx = CategoricalIndex([1, 2, 3])
|
| 411 |
+
|
| 412 |
+
assert "a" not in idx
|
| 413 |
+
|
| 414 |
+
with pytest.raises(TypeError, match="unhashable type"):
|
| 415 |
+
["a"] in idx
|
| 416 |
+
|
| 417 |
+
with pytest.raises(TypeError, match="unhashable type"):
|
| 418 |
+
["a", "b"] in idx
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_map.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from pandas import (
|
| 6 |
+
CategoricalIndex,
|
| 7 |
+
Index,
|
| 8 |
+
Series,
|
| 9 |
+
)
|
| 10 |
+
import pandas._testing as tm
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@pytest.mark.parametrize(
|
| 14 |
+
"data, categories",
|
| 15 |
+
[
|
| 16 |
+
(list("abcbca"), list("cab")),
|
| 17 |
+
(pd.interval_range(0, 3).repeat(3), pd.interval_range(0, 3)),
|
| 18 |
+
],
|
| 19 |
+
ids=["string", "interval"],
|
| 20 |
+
)
|
| 21 |
+
def test_map_str(data, categories, ordered):
|
| 22 |
+
# GH 31202 - override base class since we want to maintain categorical/ordered
|
| 23 |
+
index = CategoricalIndex(data, categories=categories, ordered=ordered)
|
| 24 |
+
result = index.map(str)
|
| 25 |
+
expected = CategoricalIndex(
|
| 26 |
+
map(str, data), categories=map(str, categories), ordered=ordered
|
| 27 |
+
)
|
| 28 |
+
tm.assert_index_equal(result, expected)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def test_map():
|
| 32 |
+
ci = CategoricalIndex(list("ABABC"), categories=list("CBA"), ordered=True)
|
| 33 |
+
result = ci.map(lambda x: x.lower())
|
| 34 |
+
exp = CategoricalIndex(list("ababc"), categories=list("cba"), ordered=True)
|
| 35 |
+
tm.assert_index_equal(result, exp)
|
| 36 |
+
|
| 37 |
+
ci = CategoricalIndex(
|
| 38 |
+
list("ABABC"), categories=list("BAC"), ordered=False, name="XXX"
|
| 39 |
+
)
|
| 40 |
+
result = ci.map(lambda x: x.lower())
|
| 41 |
+
exp = CategoricalIndex(
|
| 42 |
+
list("ababc"), categories=list("bac"), ordered=False, name="XXX"
|
| 43 |
+
)
|
| 44 |
+
tm.assert_index_equal(result, exp)
|
| 45 |
+
|
| 46 |
+
# GH 12766: Return an index not an array
|
| 47 |
+
tm.assert_index_equal(
|
| 48 |
+
ci.map(lambda x: 1), Index(np.array([1] * 5, dtype=np.int64), name="XXX")
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# change categories dtype
|
| 52 |
+
ci = CategoricalIndex(list("ABABC"), categories=list("BAC"), ordered=False)
|
| 53 |
+
|
| 54 |
+
def f(x):
|
| 55 |
+
return {"A": 10, "B": 20, "C": 30}.get(x)
|
| 56 |
+
|
| 57 |
+
result = ci.map(f)
|
| 58 |
+
exp = CategoricalIndex([10, 20, 10, 20, 30], categories=[20, 10, 30], ordered=False)
|
| 59 |
+
tm.assert_index_equal(result, exp)
|
| 60 |
+
|
| 61 |
+
result = ci.map(Series([10, 20, 30], index=["A", "B", "C"]))
|
| 62 |
+
tm.assert_index_equal(result, exp)
|
| 63 |
+
|
| 64 |
+
result = ci.map({"A": 10, "B": 20, "C": 30})
|
| 65 |
+
tm.assert_index_equal(result, exp)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_map_with_categorical_series():
|
| 69 |
+
# GH 12756
|
| 70 |
+
a = Index([1, 2, 3, 4])
|
| 71 |
+
b = Series(["even", "odd", "even", "odd"], dtype="category")
|
| 72 |
+
c = Series(["even", "odd", "even", "odd"])
|
| 73 |
+
|
| 74 |
+
exp = CategoricalIndex(["odd", "even", "odd", np.nan])
|
| 75 |
+
tm.assert_index_equal(a.map(b), exp)
|
| 76 |
+
exp = Index(["odd", "even", "odd", np.nan])
|
| 77 |
+
tm.assert_index_equal(a.map(c), exp)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
@pytest.mark.parametrize(
|
| 81 |
+
("data", "f", "expected"),
|
| 82 |
+
(
|
| 83 |
+
([1, 1, np.nan], pd.isna, CategoricalIndex([False, False, np.nan])),
|
| 84 |
+
([1, 2, np.nan], pd.isna, Index([False, False, np.nan])),
|
| 85 |
+
([1, 1, np.nan], {1: False}, CategoricalIndex([False, False, np.nan])),
|
| 86 |
+
([1, 2, np.nan], {1: False, 2: False}, Index([False, False, np.nan])),
|
| 87 |
+
(
|
| 88 |
+
[1, 1, np.nan],
|
| 89 |
+
Series([False, False]),
|
| 90 |
+
CategoricalIndex([False, False, np.nan]),
|
| 91 |
+
),
|
| 92 |
+
(
|
| 93 |
+
[1, 2, np.nan],
|
| 94 |
+
Series([False, False, False]),
|
| 95 |
+
Index([False, False, np.nan]),
|
| 96 |
+
),
|
| 97 |
+
),
|
| 98 |
+
)
|
| 99 |
+
def test_map_with_nan_ignore(data, f, expected): # GH 24241
|
| 100 |
+
values = CategoricalIndex(data)
|
| 101 |
+
result = values.map(f, na_action="ignore")
|
| 102 |
+
tm.assert_index_equal(result, expected)
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
@pytest.mark.parametrize(
|
| 106 |
+
("data", "f", "expected"),
|
| 107 |
+
(
|
| 108 |
+
([1, 1, np.nan], pd.isna, Index([False, False, True])),
|
| 109 |
+
([1, 2, np.nan], pd.isna, Index([False, False, True])),
|
| 110 |
+
([1, 1, np.nan], {1: False}, CategoricalIndex([False, False, np.nan])),
|
| 111 |
+
([1, 2, np.nan], {1: False, 2: False}, Index([False, False, np.nan])),
|
| 112 |
+
(
|
| 113 |
+
[1, 1, np.nan],
|
| 114 |
+
Series([False, False]),
|
| 115 |
+
CategoricalIndex([False, False, np.nan]),
|
| 116 |
+
),
|
| 117 |
+
(
|
| 118 |
+
[1, 2, np.nan],
|
| 119 |
+
Series([False, False, False]),
|
| 120 |
+
Index([False, False, np.nan]),
|
| 121 |
+
),
|
| 122 |
+
),
|
| 123 |
+
)
|
| 124 |
+
def test_map_with_nan_none(data, f, expected): # GH 24241
|
| 125 |
+
values = CategoricalIndex(data)
|
| 126 |
+
result = values.map(f, na_action=None)
|
| 127 |
+
tm.assert_index_equal(result, expected)
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def test_map_with_dict_or_series():
|
| 131 |
+
orig_values = ["a", "B", 1, "a"]
|
| 132 |
+
new_values = ["one", 2, 3.0, "one"]
|
| 133 |
+
cur_index = CategoricalIndex(orig_values, name="XXX")
|
| 134 |
+
expected = CategoricalIndex(new_values, name="XXX", categories=[3.0, 2, "one"])
|
| 135 |
+
|
| 136 |
+
mapper = Series(new_values[:-1], index=orig_values[:-1])
|
| 137 |
+
result = cur_index.map(mapper)
|
| 138 |
+
# Order of categories in result can be different
|
| 139 |
+
tm.assert_index_equal(result, expected)
|
| 140 |
+
|
| 141 |
+
mapper = dict(zip(orig_values[:-1], new_values[:-1]))
|
| 142 |
+
result = cur_index.map(mapper)
|
| 143 |
+
# Order of categories in result can be different
|
| 144 |
+
tm.assert_index_equal(result, expected)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_reindex.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
Categorical,
|
| 6 |
+
CategoricalIndex,
|
| 7 |
+
Index,
|
| 8 |
+
Interval,
|
| 9 |
+
)
|
| 10 |
+
import pandas._testing as tm
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class TestReindex:
|
| 14 |
+
def test_reindex_list_non_unique(self):
|
| 15 |
+
# GH#11586
|
| 16 |
+
msg = "cannot reindex on an axis with duplicate labels"
|
| 17 |
+
ci = CategoricalIndex(["a", "b", "c", "a"])
|
| 18 |
+
with pytest.raises(ValueError, match=msg):
|
| 19 |
+
ci.reindex(["a", "c"])
|
| 20 |
+
|
| 21 |
+
def test_reindex_categorical_non_unique(self):
|
| 22 |
+
msg = "cannot reindex on an axis with duplicate labels"
|
| 23 |
+
ci = CategoricalIndex(["a", "b", "c", "a"])
|
| 24 |
+
with pytest.raises(ValueError, match=msg):
|
| 25 |
+
ci.reindex(Categorical(["a", "c"]))
|
| 26 |
+
|
| 27 |
+
def test_reindex_list_non_unique_unused_category(self):
|
| 28 |
+
msg = "cannot reindex on an axis with duplicate labels"
|
| 29 |
+
ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
|
| 30 |
+
with pytest.raises(ValueError, match=msg):
|
| 31 |
+
ci.reindex(["a", "c"])
|
| 32 |
+
|
| 33 |
+
def test_reindex_categorical_non_unique_unused_category(self):
|
| 34 |
+
msg = "cannot reindex on an axis with duplicate labels"
|
| 35 |
+
ci = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
|
| 36 |
+
with pytest.raises(ValueError, match=msg):
|
| 37 |
+
ci.reindex(Categorical(["a", "c"]))
|
| 38 |
+
|
| 39 |
+
def test_reindex_duplicate_target(self):
|
| 40 |
+
# See GH25459
|
| 41 |
+
cat = CategoricalIndex(["a", "b", "c"], categories=["a", "b", "c", "d"])
|
| 42 |
+
res, indexer = cat.reindex(["a", "c", "c"])
|
| 43 |
+
exp = Index(["a", "c", "c"])
|
| 44 |
+
tm.assert_index_equal(res, exp, exact=True)
|
| 45 |
+
tm.assert_numpy_array_equal(indexer, np.array([0, 2, 2], dtype=np.intp))
|
| 46 |
+
|
| 47 |
+
res, indexer = cat.reindex(
|
| 48 |
+
CategoricalIndex(["a", "c", "c"], categories=["a", "b", "c", "d"])
|
| 49 |
+
)
|
| 50 |
+
exp = CategoricalIndex(["a", "c", "c"], categories=["a", "b", "c", "d"])
|
| 51 |
+
tm.assert_index_equal(res, exp, exact=True)
|
| 52 |
+
tm.assert_numpy_array_equal(indexer, np.array([0, 2, 2], dtype=np.intp))
|
| 53 |
+
|
| 54 |
+
def test_reindex_empty_index(self):
|
| 55 |
+
# See GH16770
|
| 56 |
+
c = CategoricalIndex([])
|
| 57 |
+
res, indexer = c.reindex(["a", "b"])
|
| 58 |
+
tm.assert_index_equal(res, Index(["a", "b"]), exact=True)
|
| 59 |
+
tm.assert_numpy_array_equal(indexer, np.array([-1, -1], dtype=np.intp))
|
| 60 |
+
|
| 61 |
+
def test_reindex_categorical_added_category(self):
|
| 62 |
+
# GH 42424
|
| 63 |
+
ci = CategoricalIndex(
|
| 64 |
+
[Interval(0, 1, closed="right"), Interval(1, 2, closed="right")],
|
| 65 |
+
ordered=True,
|
| 66 |
+
)
|
| 67 |
+
ci_add = CategoricalIndex(
|
| 68 |
+
[
|
| 69 |
+
Interval(0, 1, closed="right"),
|
| 70 |
+
Interval(1, 2, closed="right"),
|
| 71 |
+
Interval(2, 3, closed="right"),
|
| 72 |
+
Interval(3, 4, closed="right"),
|
| 73 |
+
],
|
| 74 |
+
ordered=True,
|
| 75 |
+
)
|
| 76 |
+
result, _ = ci.reindex(ci_add)
|
| 77 |
+
expected = ci_add
|
| 78 |
+
tm.assert_index_equal(expected, result)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/categorical/test_setops.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
CategoricalIndex,
|
| 6 |
+
Index,
|
| 7 |
+
)
|
| 8 |
+
import pandas._testing as tm
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@pytest.mark.parametrize("na_value", [None, np.nan])
|
| 12 |
+
def test_difference_with_na(na_value):
|
| 13 |
+
# GH 57318
|
| 14 |
+
ci = CategoricalIndex(["a", "b", "c", None])
|
| 15 |
+
other = Index(["c", na_value])
|
| 16 |
+
result = ci.difference(other)
|
| 17 |
+
expected = CategoricalIndex(["a", "b"], categories=["a", "b", "c"])
|
| 18 |
+
tm.assert_index_equal(result, expected)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/conftest.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
Series,
|
| 6 |
+
array,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.fixture(params=[None, False])
|
| 11 |
+
def sort(request):
|
| 12 |
+
"""
|
| 13 |
+
Valid values for the 'sort' parameter used in the Index
|
| 14 |
+
setops methods (intersection, union, etc.)
|
| 15 |
+
|
| 16 |
+
Caution:
|
| 17 |
+
Don't confuse this one with the "sort" fixture used
|
| 18 |
+
for concat. That one has parameters [True, False].
|
| 19 |
+
|
| 20 |
+
We can't combine them as sort=True is not permitted
|
| 21 |
+
in the Index setops methods.
|
| 22 |
+
"""
|
| 23 |
+
return request.param
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@pytest.fixture(params=["D", "3D", "-3D", "h", "2h", "-2h", "min", "2min", "s", "-3s"])
|
| 27 |
+
def freq_sample(request):
|
| 28 |
+
"""
|
| 29 |
+
Valid values for 'freq' parameter used to create date_range and
|
| 30 |
+
timedelta_range..
|
| 31 |
+
"""
|
| 32 |
+
return request.param
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@pytest.fixture(params=[list, tuple, np.array, array, Series])
|
| 36 |
+
def listlike_box(request):
|
| 37 |
+
"""
|
| 38 |
+
Types that may be passed as the indexer to searchsorted.
|
| 39 |
+
"""
|
| 40 |
+
return request.param
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/__init__.py
ADDED
|
File without changes
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_drop_duplicates.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
PeriodIndex,
|
| 6 |
+
Series,
|
| 7 |
+
date_range,
|
| 8 |
+
period_range,
|
| 9 |
+
timedelta_range,
|
| 10 |
+
)
|
| 11 |
+
import pandas._testing as tm
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class DropDuplicates:
|
| 15 |
+
def test_drop_duplicates_metadata(self, idx):
|
| 16 |
+
# GH#10115
|
| 17 |
+
result = idx.drop_duplicates()
|
| 18 |
+
tm.assert_index_equal(idx, result)
|
| 19 |
+
assert idx.freq == result.freq
|
| 20 |
+
|
| 21 |
+
idx_dup = idx.append(idx)
|
| 22 |
+
result = idx_dup.drop_duplicates()
|
| 23 |
+
|
| 24 |
+
expected = idx
|
| 25 |
+
if not isinstance(idx, PeriodIndex):
|
| 26 |
+
# freq is reset except for PeriodIndex
|
| 27 |
+
assert idx_dup.freq is None
|
| 28 |
+
assert result.freq is None
|
| 29 |
+
expected = idx._with_freq(None)
|
| 30 |
+
else:
|
| 31 |
+
assert result.freq == expected.freq
|
| 32 |
+
|
| 33 |
+
tm.assert_index_equal(result, expected)
|
| 34 |
+
|
| 35 |
+
@pytest.mark.parametrize(
|
| 36 |
+
"keep, expected, index",
|
| 37 |
+
[
|
| 38 |
+
(
|
| 39 |
+
"first",
|
| 40 |
+
np.concatenate(([False] * 10, [True] * 5)),
|
| 41 |
+
np.arange(0, 10, dtype=np.int64),
|
| 42 |
+
),
|
| 43 |
+
(
|
| 44 |
+
"last",
|
| 45 |
+
np.concatenate(([True] * 5, [False] * 10)),
|
| 46 |
+
np.arange(5, 15, dtype=np.int64),
|
| 47 |
+
),
|
| 48 |
+
(
|
| 49 |
+
False,
|
| 50 |
+
np.concatenate(([True] * 5, [False] * 5, [True] * 5)),
|
| 51 |
+
np.arange(5, 10, dtype=np.int64),
|
| 52 |
+
),
|
| 53 |
+
],
|
| 54 |
+
)
|
| 55 |
+
def test_drop_duplicates(self, keep, expected, index, idx):
|
| 56 |
+
# to check Index/Series compat
|
| 57 |
+
idx = idx.append(idx[:5])
|
| 58 |
+
|
| 59 |
+
tm.assert_numpy_array_equal(idx.duplicated(keep=keep), expected)
|
| 60 |
+
expected = idx[~expected]
|
| 61 |
+
|
| 62 |
+
result = idx.drop_duplicates(keep=keep)
|
| 63 |
+
tm.assert_index_equal(result, expected)
|
| 64 |
+
|
| 65 |
+
result = Series(idx).drop_duplicates(keep=keep)
|
| 66 |
+
expected = Series(expected, index=index)
|
| 67 |
+
tm.assert_series_equal(result, expected)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class TestDropDuplicatesPeriodIndex(DropDuplicates):
|
| 71 |
+
@pytest.fixture(params=["D", "3D", "h", "2h", "min", "2min", "s", "3s"])
|
| 72 |
+
def freq(self, request):
|
| 73 |
+
"""
|
| 74 |
+
Fixture to test for different frequencies for PeriodIndex.
|
| 75 |
+
"""
|
| 76 |
+
return request.param
|
| 77 |
+
|
| 78 |
+
@pytest.fixture
|
| 79 |
+
def idx(self, freq):
|
| 80 |
+
"""
|
| 81 |
+
Fixture to get PeriodIndex for 10 periods for different frequencies.
|
| 82 |
+
"""
|
| 83 |
+
return period_range("2011-01-01", periods=10, freq=freq, name="idx")
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
class TestDropDuplicatesDatetimeIndex(DropDuplicates):
|
| 87 |
+
@pytest.fixture
|
| 88 |
+
def idx(self, freq_sample):
|
| 89 |
+
"""
|
| 90 |
+
Fixture to get DatetimeIndex for 10 periods for different frequencies.
|
| 91 |
+
"""
|
| 92 |
+
return date_range("2011-01-01", freq=freq_sample, periods=10, name="idx")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class TestDropDuplicatesTimedeltaIndex(DropDuplicates):
|
| 96 |
+
@pytest.fixture
|
| 97 |
+
def idx(self, freq_sample):
|
| 98 |
+
"""
|
| 99 |
+
Fixture to get TimedeltaIndex for 10 periods for different frequencies.
|
| 100 |
+
"""
|
| 101 |
+
return timedelta_range("1 day", periods=10, freq=freq_sample, name="idx")
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_equals.py
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Tests shared for DatetimeIndex/TimedeltaIndex/PeriodIndex
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from datetime import (
|
| 6 |
+
datetime,
|
| 7 |
+
timedelta,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
import pytest
|
| 12 |
+
|
| 13 |
+
import pandas as pd
|
| 14 |
+
from pandas import (
|
| 15 |
+
CategoricalIndex,
|
| 16 |
+
DatetimeIndex,
|
| 17 |
+
Index,
|
| 18 |
+
PeriodIndex,
|
| 19 |
+
TimedeltaIndex,
|
| 20 |
+
date_range,
|
| 21 |
+
period_range,
|
| 22 |
+
timedelta_range,
|
| 23 |
+
)
|
| 24 |
+
import pandas._testing as tm
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class EqualsTests:
|
| 28 |
+
def test_not_equals_numeric(self, index):
|
| 29 |
+
assert not index.equals(Index(index.asi8))
|
| 30 |
+
assert not index.equals(Index(index.asi8.astype("u8")))
|
| 31 |
+
assert not index.equals(Index(index.asi8).astype("f8"))
|
| 32 |
+
|
| 33 |
+
def test_equals(self, index):
|
| 34 |
+
assert index.equals(index)
|
| 35 |
+
assert index.equals(index.astype(object))
|
| 36 |
+
assert index.equals(CategoricalIndex(index))
|
| 37 |
+
assert index.equals(CategoricalIndex(index.astype(object)))
|
| 38 |
+
|
| 39 |
+
def test_not_equals_non_arraylike(self, index):
|
| 40 |
+
assert not index.equals(list(index))
|
| 41 |
+
|
| 42 |
+
def test_not_equals_strings(self, index):
|
| 43 |
+
other = Index([str(x) for x in index], dtype=object)
|
| 44 |
+
assert not index.equals(other)
|
| 45 |
+
assert not index.equals(CategoricalIndex(other))
|
| 46 |
+
|
| 47 |
+
def test_not_equals_misc_strs(self, index):
|
| 48 |
+
other = Index(list("abc"))
|
| 49 |
+
assert not index.equals(other)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class TestPeriodIndexEquals(EqualsTests):
|
| 53 |
+
@pytest.fixture
|
| 54 |
+
def index(self):
|
| 55 |
+
"""Fixture for creating a PeriodIndex for use in equality tests."""
|
| 56 |
+
return period_range("2013-01-01", periods=5, freq="D")
|
| 57 |
+
|
| 58 |
+
# TODO: de-duplicate with other test_equals2 methods
|
| 59 |
+
@pytest.mark.parametrize("freq", ["D", "M"])
|
| 60 |
+
def test_equals2(self, freq):
|
| 61 |
+
# GH#13107
|
| 62 |
+
idx = PeriodIndex(["2011-01-01", "2011-01-02", "NaT"], freq=freq)
|
| 63 |
+
assert idx.equals(idx)
|
| 64 |
+
assert idx.equals(idx.copy())
|
| 65 |
+
assert idx.equals(idx.astype(object))
|
| 66 |
+
assert idx.astype(object).equals(idx)
|
| 67 |
+
assert idx.astype(object).equals(idx.astype(object))
|
| 68 |
+
assert not idx.equals(list(idx))
|
| 69 |
+
assert not idx.equals(pd.Series(idx))
|
| 70 |
+
|
| 71 |
+
idx2 = PeriodIndex(["2011-01-01", "2011-01-02", "NaT"], freq="h")
|
| 72 |
+
assert not idx.equals(idx2)
|
| 73 |
+
assert not idx.equals(idx2.copy())
|
| 74 |
+
assert not idx.equals(idx2.astype(object))
|
| 75 |
+
assert not idx.astype(object).equals(idx2)
|
| 76 |
+
assert not idx.equals(list(idx2))
|
| 77 |
+
assert not idx.equals(pd.Series(idx2))
|
| 78 |
+
|
| 79 |
+
# same internal, different tz
|
| 80 |
+
idx3 = PeriodIndex._simple_new(
|
| 81 |
+
idx._values._simple_new(idx._values.asi8, dtype=pd.PeriodDtype("h"))
|
| 82 |
+
)
|
| 83 |
+
tm.assert_numpy_array_equal(idx.asi8, idx3.asi8)
|
| 84 |
+
assert not idx.equals(idx3)
|
| 85 |
+
assert not idx.equals(idx3.copy())
|
| 86 |
+
assert not idx.equals(idx3.astype(object))
|
| 87 |
+
assert not idx.astype(object).equals(idx3)
|
| 88 |
+
assert not idx.equals(list(idx3))
|
| 89 |
+
assert not idx.equals(pd.Series(idx3))
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
class TestDatetimeIndexEquals(EqualsTests):
|
| 93 |
+
@pytest.fixture
|
| 94 |
+
def index(self):
|
| 95 |
+
"""Fixture for creating a DatetimeIndex for use in equality tests."""
|
| 96 |
+
return date_range("2013-01-01", periods=5)
|
| 97 |
+
|
| 98 |
+
def test_equals2(self):
|
| 99 |
+
# GH#13107
|
| 100 |
+
idx = DatetimeIndex(["2011-01-01", "2011-01-02", "NaT"])
|
| 101 |
+
assert idx.equals(idx)
|
| 102 |
+
assert idx.equals(idx.copy())
|
| 103 |
+
assert idx.equals(idx.astype(object))
|
| 104 |
+
assert idx.astype(object).equals(idx)
|
| 105 |
+
assert idx.astype(object).equals(idx.astype(object))
|
| 106 |
+
assert not idx.equals(list(idx))
|
| 107 |
+
assert not idx.equals(pd.Series(idx))
|
| 108 |
+
|
| 109 |
+
idx2 = DatetimeIndex(["2011-01-01", "2011-01-02", "NaT"], tz="US/Pacific")
|
| 110 |
+
assert not idx.equals(idx2)
|
| 111 |
+
assert not idx.equals(idx2.copy())
|
| 112 |
+
assert not idx.equals(idx2.astype(object))
|
| 113 |
+
assert not idx.astype(object).equals(idx2)
|
| 114 |
+
assert not idx.equals(list(idx2))
|
| 115 |
+
assert not idx.equals(pd.Series(idx2))
|
| 116 |
+
|
| 117 |
+
# same internal, different tz
|
| 118 |
+
idx3 = DatetimeIndex(idx.asi8, tz="US/Pacific")
|
| 119 |
+
tm.assert_numpy_array_equal(idx.asi8, idx3.asi8)
|
| 120 |
+
assert not idx.equals(idx3)
|
| 121 |
+
assert not idx.equals(idx3.copy())
|
| 122 |
+
assert not idx.equals(idx3.astype(object))
|
| 123 |
+
assert not idx.astype(object).equals(idx3)
|
| 124 |
+
assert not idx.equals(list(idx3))
|
| 125 |
+
assert not idx.equals(pd.Series(idx3))
|
| 126 |
+
|
| 127 |
+
# check that we do not raise when comparing with OutOfBounds objects
|
| 128 |
+
oob = Index([datetime(2500, 1, 1)] * 3, dtype=object)
|
| 129 |
+
assert not idx.equals(oob)
|
| 130 |
+
assert not idx2.equals(oob)
|
| 131 |
+
assert not idx3.equals(oob)
|
| 132 |
+
|
| 133 |
+
# check that we do not raise when comparing with OutOfBounds dt64
|
| 134 |
+
oob2 = oob.map(np.datetime64)
|
| 135 |
+
assert not idx.equals(oob2)
|
| 136 |
+
assert not idx2.equals(oob2)
|
| 137 |
+
assert not idx3.equals(oob2)
|
| 138 |
+
|
| 139 |
+
@pytest.mark.parametrize("freq", ["B", "C"])
|
| 140 |
+
def test_not_equals_bday(self, freq):
|
| 141 |
+
rng = date_range("2009-01-01", "2010-01-01", freq=freq)
|
| 142 |
+
assert not rng.equals(list(rng))
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
class TestTimedeltaIndexEquals(EqualsTests):
|
| 146 |
+
@pytest.fixture
|
| 147 |
+
def index(self):
|
| 148 |
+
"""Fixture for creating a TimedeltaIndex for use in equality tests."""
|
| 149 |
+
return timedelta_range("1 day", periods=10)
|
| 150 |
+
|
| 151 |
+
def test_equals2(self):
|
| 152 |
+
# GH#13107
|
| 153 |
+
idx = TimedeltaIndex(["1 days", "2 days", "NaT"])
|
| 154 |
+
assert idx.equals(idx)
|
| 155 |
+
assert idx.equals(idx.copy())
|
| 156 |
+
assert idx.equals(idx.astype(object))
|
| 157 |
+
assert idx.astype(object).equals(idx)
|
| 158 |
+
assert idx.astype(object).equals(idx.astype(object))
|
| 159 |
+
assert not idx.equals(list(idx))
|
| 160 |
+
assert not idx.equals(pd.Series(idx))
|
| 161 |
+
|
| 162 |
+
idx2 = TimedeltaIndex(["2 days", "1 days", "NaT"])
|
| 163 |
+
assert not idx.equals(idx2)
|
| 164 |
+
assert not idx.equals(idx2.copy())
|
| 165 |
+
assert not idx.equals(idx2.astype(object))
|
| 166 |
+
assert not idx.astype(object).equals(idx2)
|
| 167 |
+
assert not idx.astype(object).equals(idx2.astype(object))
|
| 168 |
+
assert not idx.equals(list(idx2))
|
| 169 |
+
assert not idx.equals(pd.Series(idx2))
|
| 170 |
+
|
| 171 |
+
# Check that we dont raise OverflowError on comparisons outside the
|
| 172 |
+
# implementation range GH#28532
|
| 173 |
+
oob = Index([timedelta(days=10**6)] * 3, dtype=object)
|
| 174 |
+
assert not idx.equals(oob)
|
| 175 |
+
assert not idx2.equals(oob)
|
| 176 |
+
|
| 177 |
+
oob2 = Index([np.timedelta64(x) for x in oob], dtype=object)
|
| 178 |
+
assert (oob == oob2).all()
|
| 179 |
+
assert not idx.equals(oob2)
|
| 180 |
+
assert not idx2.equals(oob2)
|
| 181 |
+
|
| 182 |
+
oob3 = oob.map(np.timedelta64)
|
| 183 |
+
assert (oob3 == oob).all()
|
| 184 |
+
assert not idx.equals(oob3)
|
| 185 |
+
assert not idx2.equals(oob3)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_indexing.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from pandas import (
|
| 6 |
+
DatetimeIndex,
|
| 7 |
+
Index,
|
| 8 |
+
)
|
| 9 |
+
import pandas._testing as tm
|
| 10 |
+
|
| 11 |
+
dtlike_dtypes = [
|
| 12 |
+
np.dtype("timedelta64[ns]"),
|
| 13 |
+
np.dtype("datetime64[ns]"),
|
| 14 |
+
pd.DatetimeTZDtype("ns", "Asia/Tokyo"),
|
| 15 |
+
pd.PeriodDtype("ns"),
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@pytest.mark.parametrize("ldtype", dtlike_dtypes)
|
| 20 |
+
@pytest.mark.parametrize("rdtype", dtlike_dtypes)
|
| 21 |
+
def test_get_indexer_non_unique_wrong_dtype(ldtype, rdtype):
|
| 22 |
+
vals = np.tile(3600 * 10**9 * np.arange(3, dtype=np.int64), 2)
|
| 23 |
+
|
| 24 |
+
def construct(dtype):
|
| 25 |
+
if dtype is dtlike_dtypes[-1]:
|
| 26 |
+
# PeriodArray will try to cast ints to strings
|
| 27 |
+
return DatetimeIndex(vals).astype(dtype)
|
| 28 |
+
return Index(vals, dtype=dtype)
|
| 29 |
+
|
| 30 |
+
left = construct(ldtype)
|
| 31 |
+
right = construct(rdtype)
|
| 32 |
+
|
| 33 |
+
result = left.get_indexer_non_unique(right)
|
| 34 |
+
|
| 35 |
+
if ldtype is rdtype:
|
| 36 |
+
ex1 = np.array([0, 3, 1, 4, 2, 5] * 2, dtype=np.intp)
|
| 37 |
+
ex2 = np.array([], dtype=np.intp)
|
| 38 |
+
tm.assert_numpy_array_equal(result[0], ex1)
|
| 39 |
+
tm.assert_numpy_array_equal(result[1], ex2)
|
| 40 |
+
|
| 41 |
+
else:
|
| 42 |
+
no_matches = np.array([-1] * 6, dtype=np.intp)
|
| 43 |
+
missing = np.arange(6, dtype=np.intp)
|
| 44 |
+
tm.assert_numpy_array_equal(result[0], no_matches)
|
| 45 |
+
tm.assert_numpy_array_equal(result[1], missing)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_is_monotonic.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pandas import (
|
| 2 |
+
Index,
|
| 3 |
+
NaT,
|
| 4 |
+
date_range,
|
| 5 |
+
)
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_is_monotonic_with_nat():
|
| 9 |
+
# GH#31437
|
| 10 |
+
# PeriodIndex.is_monotonic_increasing should behave analogously to DatetimeIndex,
|
| 11 |
+
# in particular never be monotonic when we have NaT
|
| 12 |
+
dti = date_range("2016-01-01", periods=3)
|
| 13 |
+
pi = dti.to_period("D")
|
| 14 |
+
tdi = Index(dti.view("timedelta64[ns]"))
|
| 15 |
+
|
| 16 |
+
for obj in [pi, pi._engine, dti, dti._engine, tdi, tdi._engine]:
|
| 17 |
+
if isinstance(obj, Index):
|
| 18 |
+
# i.e. not Engines
|
| 19 |
+
assert obj.is_monotonic_increasing
|
| 20 |
+
assert obj.is_monotonic_increasing
|
| 21 |
+
assert not obj.is_monotonic_decreasing
|
| 22 |
+
assert obj.is_unique
|
| 23 |
+
|
| 24 |
+
dti1 = dti.insert(0, NaT)
|
| 25 |
+
pi1 = dti1.to_period("D")
|
| 26 |
+
tdi1 = Index(dti1.view("timedelta64[ns]"))
|
| 27 |
+
|
| 28 |
+
for obj in [pi1, pi1._engine, dti1, dti1._engine, tdi1, tdi1._engine]:
|
| 29 |
+
if isinstance(obj, Index):
|
| 30 |
+
# i.e. not Engines
|
| 31 |
+
assert not obj.is_monotonic_increasing
|
| 32 |
+
assert not obj.is_monotonic_increasing
|
| 33 |
+
assert not obj.is_monotonic_decreasing
|
| 34 |
+
assert obj.is_unique
|
| 35 |
+
|
| 36 |
+
dti2 = dti.insert(3, NaT)
|
| 37 |
+
pi2 = dti2.to_period("h")
|
| 38 |
+
tdi2 = Index(dti2.view("timedelta64[ns]"))
|
| 39 |
+
|
| 40 |
+
for obj in [pi2, pi2._engine, dti2, dti2._engine, tdi2, tdi2._engine]:
|
| 41 |
+
if isinstance(obj, Index):
|
| 42 |
+
# i.e. not Engines
|
| 43 |
+
assert not obj.is_monotonic_increasing
|
| 44 |
+
assert not obj.is_monotonic_increasing
|
| 45 |
+
assert not obj.is_monotonic_decreasing
|
| 46 |
+
assert obj.is_unique
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_nat.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
DatetimeIndex,
|
| 6 |
+
NaT,
|
| 7 |
+
PeriodIndex,
|
| 8 |
+
TimedeltaIndex,
|
| 9 |
+
)
|
| 10 |
+
import pandas._testing as tm
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
@pytest.mark.parametrize(
|
| 14 |
+
"index_without_na",
|
| 15 |
+
[
|
| 16 |
+
TimedeltaIndex(["1 days", "2 days"]),
|
| 17 |
+
PeriodIndex(["2011-01-01", "2011-01-02"], freq="D"),
|
| 18 |
+
DatetimeIndex(["2011-01-01", "2011-01-02"]),
|
| 19 |
+
DatetimeIndex(["2011-01-01", "2011-01-02"], tz="UTC"),
|
| 20 |
+
],
|
| 21 |
+
)
|
| 22 |
+
def test_nat(index_without_na):
|
| 23 |
+
empty_index = index_without_na[:0]
|
| 24 |
+
|
| 25 |
+
index_with_na = index_without_na.copy(deep=True)
|
| 26 |
+
index_with_na._data[1] = NaT
|
| 27 |
+
|
| 28 |
+
assert empty_index._na_value is NaT
|
| 29 |
+
assert index_with_na._na_value is NaT
|
| 30 |
+
assert index_without_na._na_value is NaT
|
| 31 |
+
|
| 32 |
+
idx = index_without_na
|
| 33 |
+
assert idx._can_hold_na
|
| 34 |
+
|
| 35 |
+
tm.assert_numpy_array_equal(idx._isnan, np.array([False, False]))
|
| 36 |
+
assert idx.hasnans is False
|
| 37 |
+
|
| 38 |
+
idx = index_with_na
|
| 39 |
+
assert idx._can_hold_na
|
| 40 |
+
|
| 41 |
+
tm.assert_numpy_array_equal(idx._isnan, np.array([False, True]))
|
| 42 |
+
assert idx.hasnans is True
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_sort_values.py
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
DatetimeIndex,
|
| 6 |
+
Index,
|
| 7 |
+
NaT,
|
| 8 |
+
PeriodIndex,
|
| 9 |
+
TimedeltaIndex,
|
| 10 |
+
timedelta_range,
|
| 11 |
+
)
|
| 12 |
+
import pandas._testing as tm
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def check_freq_ascending(ordered, orig, ascending):
|
| 16 |
+
"""
|
| 17 |
+
Check the expected freq on a PeriodIndex/DatetimeIndex/TimedeltaIndex
|
| 18 |
+
when the original index is generated (or generate-able) with
|
| 19 |
+
period_range/date_range/timedelta_range.
|
| 20 |
+
"""
|
| 21 |
+
if isinstance(ordered, PeriodIndex):
|
| 22 |
+
assert ordered.freq == orig.freq
|
| 23 |
+
elif isinstance(ordered, (DatetimeIndex, TimedeltaIndex)):
|
| 24 |
+
if ascending:
|
| 25 |
+
assert ordered.freq.n == orig.freq.n
|
| 26 |
+
else:
|
| 27 |
+
assert ordered.freq.n == -1 * orig.freq.n
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def check_freq_nonmonotonic(ordered, orig):
|
| 31 |
+
"""
|
| 32 |
+
Check the expected freq on a PeriodIndex/DatetimeIndex/TimedeltaIndex
|
| 33 |
+
when the original index is _not_ generated (or generate-able) with
|
| 34 |
+
period_range/date_range//timedelta_range.
|
| 35 |
+
"""
|
| 36 |
+
if isinstance(ordered, PeriodIndex):
|
| 37 |
+
assert ordered.freq == orig.freq
|
| 38 |
+
elif isinstance(ordered, (DatetimeIndex, TimedeltaIndex)):
|
| 39 |
+
assert ordered.freq is None
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class TestSortValues:
|
| 43 |
+
@pytest.fixture(params=[DatetimeIndex, TimedeltaIndex, PeriodIndex])
|
| 44 |
+
def non_monotonic_idx(self, request):
|
| 45 |
+
if request.param is DatetimeIndex:
|
| 46 |
+
return DatetimeIndex(["2000-01-04", "2000-01-01", "2000-01-02"])
|
| 47 |
+
elif request.param is PeriodIndex:
|
| 48 |
+
dti = DatetimeIndex(["2000-01-04", "2000-01-01", "2000-01-02"])
|
| 49 |
+
return dti.to_period("D")
|
| 50 |
+
else:
|
| 51 |
+
return TimedeltaIndex(
|
| 52 |
+
["1 day 00:00:05", "1 day 00:00:01", "1 day 00:00:02"]
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
def test_argmin_argmax(self, non_monotonic_idx):
|
| 56 |
+
assert non_monotonic_idx.argmin() == 1
|
| 57 |
+
assert non_monotonic_idx.argmax() == 0
|
| 58 |
+
|
| 59 |
+
def test_sort_values(self, non_monotonic_idx):
|
| 60 |
+
idx = non_monotonic_idx
|
| 61 |
+
ordered = idx.sort_values()
|
| 62 |
+
assert ordered.is_monotonic_increasing
|
| 63 |
+
ordered = idx.sort_values(ascending=False)
|
| 64 |
+
assert ordered[::-1].is_monotonic_increasing
|
| 65 |
+
|
| 66 |
+
ordered, dexer = idx.sort_values(return_indexer=True)
|
| 67 |
+
assert ordered.is_monotonic_increasing
|
| 68 |
+
tm.assert_numpy_array_equal(dexer, np.array([1, 2, 0], dtype=np.intp))
|
| 69 |
+
|
| 70 |
+
ordered, dexer = idx.sort_values(return_indexer=True, ascending=False)
|
| 71 |
+
assert ordered[::-1].is_monotonic_increasing
|
| 72 |
+
tm.assert_numpy_array_equal(dexer, np.array([0, 2, 1], dtype=np.intp))
|
| 73 |
+
|
| 74 |
+
def check_sort_values_with_freq(self, idx):
|
| 75 |
+
ordered = idx.sort_values()
|
| 76 |
+
tm.assert_index_equal(ordered, idx)
|
| 77 |
+
check_freq_ascending(ordered, idx, True)
|
| 78 |
+
|
| 79 |
+
ordered = idx.sort_values(ascending=False)
|
| 80 |
+
expected = idx[::-1]
|
| 81 |
+
tm.assert_index_equal(ordered, expected)
|
| 82 |
+
check_freq_ascending(ordered, idx, False)
|
| 83 |
+
|
| 84 |
+
ordered, indexer = idx.sort_values(return_indexer=True)
|
| 85 |
+
tm.assert_index_equal(ordered, idx)
|
| 86 |
+
tm.assert_numpy_array_equal(indexer, np.array([0, 1, 2], dtype=np.intp))
|
| 87 |
+
check_freq_ascending(ordered, idx, True)
|
| 88 |
+
|
| 89 |
+
ordered, indexer = idx.sort_values(return_indexer=True, ascending=False)
|
| 90 |
+
expected = idx[::-1]
|
| 91 |
+
tm.assert_index_equal(ordered, expected)
|
| 92 |
+
tm.assert_numpy_array_equal(indexer, np.array([2, 1, 0], dtype=np.intp))
|
| 93 |
+
check_freq_ascending(ordered, idx, False)
|
| 94 |
+
|
| 95 |
+
@pytest.mark.parametrize("freq", ["D", "h"])
|
| 96 |
+
def test_sort_values_with_freq_timedeltaindex(self, freq):
|
| 97 |
+
# GH#10295
|
| 98 |
+
idx = timedelta_range(start=f"1{freq}", periods=3, freq=freq).rename("idx")
|
| 99 |
+
|
| 100 |
+
self.check_sort_values_with_freq(idx)
|
| 101 |
+
|
| 102 |
+
@pytest.mark.parametrize(
|
| 103 |
+
"idx",
|
| 104 |
+
[
|
| 105 |
+
DatetimeIndex(
|
| 106 |
+
["2011-01-01", "2011-01-02", "2011-01-03"], freq="D", name="idx"
|
| 107 |
+
),
|
| 108 |
+
DatetimeIndex(
|
| 109 |
+
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"],
|
| 110 |
+
freq="h",
|
| 111 |
+
name="tzidx",
|
| 112 |
+
tz="Asia/Tokyo",
|
| 113 |
+
),
|
| 114 |
+
],
|
| 115 |
+
)
|
| 116 |
+
def test_sort_values_with_freq_datetimeindex(self, idx):
|
| 117 |
+
self.check_sort_values_with_freq(idx)
|
| 118 |
+
|
| 119 |
+
@pytest.mark.parametrize("freq", ["D", "2D", "4D"])
|
| 120 |
+
def test_sort_values_with_freq_periodindex(self, freq):
|
| 121 |
+
# here with_freq refers to being period_range-like
|
| 122 |
+
idx = PeriodIndex(
|
| 123 |
+
["2011-01-01", "2011-01-02", "2011-01-03"], freq=freq, name="idx"
|
| 124 |
+
)
|
| 125 |
+
self.check_sort_values_with_freq(idx)
|
| 126 |
+
|
| 127 |
+
@pytest.mark.parametrize(
|
| 128 |
+
"idx",
|
| 129 |
+
[
|
| 130 |
+
PeriodIndex(["2011", "2012", "2013"], name="pidx", freq="Y"),
|
| 131 |
+
Index([2011, 2012, 2013], name="idx"), # for compatibility check
|
| 132 |
+
],
|
| 133 |
+
)
|
| 134 |
+
def test_sort_values_with_freq_periodindex2(self, idx):
|
| 135 |
+
# here with_freq indicates this is period_range-like
|
| 136 |
+
self.check_sort_values_with_freq(idx)
|
| 137 |
+
|
| 138 |
+
def check_sort_values_without_freq(self, idx, expected):
|
| 139 |
+
ordered = idx.sort_values(na_position="first")
|
| 140 |
+
tm.assert_index_equal(ordered, expected)
|
| 141 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 142 |
+
|
| 143 |
+
if not idx.isna().any():
|
| 144 |
+
ordered = idx.sort_values()
|
| 145 |
+
tm.assert_index_equal(ordered, expected)
|
| 146 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 147 |
+
|
| 148 |
+
ordered = idx.sort_values(ascending=False)
|
| 149 |
+
tm.assert_index_equal(ordered, expected[::-1])
|
| 150 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 151 |
+
|
| 152 |
+
ordered, indexer = idx.sort_values(return_indexer=True, na_position="first")
|
| 153 |
+
tm.assert_index_equal(ordered, expected)
|
| 154 |
+
|
| 155 |
+
exp = np.array([0, 4, 3, 1, 2], dtype=np.intp)
|
| 156 |
+
tm.assert_numpy_array_equal(indexer, exp)
|
| 157 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 158 |
+
|
| 159 |
+
if not idx.isna().any():
|
| 160 |
+
ordered, indexer = idx.sort_values(return_indexer=True)
|
| 161 |
+
tm.assert_index_equal(ordered, expected)
|
| 162 |
+
|
| 163 |
+
exp = np.array([0, 4, 3, 1, 2], dtype=np.intp)
|
| 164 |
+
tm.assert_numpy_array_equal(indexer, exp)
|
| 165 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 166 |
+
|
| 167 |
+
ordered, indexer = idx.sort_values(return_indexer=True, ascending=False)
|
| 168 |
+
tm.assert_index_equal(ordered, expected[::-1])
|
| 169 |
+
|
| 170 |
+
exp = np.array([2, 1, 3, 0, 4], dtype=np.intp)
|
| 171 |
+
tm.assert_numpy_array_equal(indexer, exp)
|
| 172 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 173 |
+
|
| 174 |
+
def test_sort_values_without_freq_timedeltaindex(self):
|
| 175 |
+
# GH#10295
|
| 176 |
+
|
| 177 |
+
idx = TimedeltaIndex(
|
| 178 |
+
["1 hour", "3 hour", "5 hour", "2 hour ", "1 hour"], name="idx1"
|
| 179 |
+
)
|
| 180 |
+
expected = TimedeltaIndex(
|
| 181 |
+
["1 hour", "1 hour", "2 hour", "3 hour", "5 hour"], name="idx1"
|
| 182 |
+
)
|
| 183 |
+
self.check_sort_values_without_freq(idx, expected)
|
| 184 |
+
|
| 185 |
+
@pytest.mark.parametrize(
|
| 186 |
+
"index_dates,expected_dates",
|
| 187 |
+
[
|
| 188 |
+
(
|
| 189 |
+
["2011-01-01", "2011-01-03", "2011-01-05", "2011-01-02", "2011-01-01"],
|
| 190 |
+
["2011-01-01", "2011-01-01", "2011-01-02", "2011-01-03", "2011-01-05"],
|
| 191 |
+
),
|
| 192 |
+
(
|
| 193 |
+
[NaT, "2011-01-03", "2011-01-05", "2011-01-02", NaT],
|
| 194 |
+
[NaT, NaT, "2011-01-02", "2011-01-03", "2011-01-05"],
|
| 195 |
+
),
|
| 196 |
+
],
|
| 197 |
+
)
|
| 198 |
+
def test_sort_values_without_freq_datetimeindex(
|
| 199 |
+
self, index_dates, expected_dates, tz_naive_fixture
|
| 200 |
+
):
|
| 201 |
+
tz = tz_naive_fixture
|
| 202 |
+
|
| 203 |
+
# without freq
|
| 204 |
+
idx = DatetimeIndex(index_dates, tz=tz, name="idx")
|
| 205 |
+
expected = DatetimeIndex(expected_dates, tz=tz, name="idx")
|
| 206 |
+
|
| 207 |
+
self.check_sort_values_without_freq(idx, expected)
|
| 208 |
+
|
| 209 |
+
@pytest.mark.parametrize(
|
| 210 |
+
"idx,expected",
|
| 211 |
+
[
|
| 212 |
+
(
|
| 213 |
+
PeriodIndex(
|
| 214 |
+
[
|
| 215 |
+
"2011-01-01",
|
| 216 |
+
"2011-01-03",
|
| 217 |
+
"2011-01-05",
|
| 218 |
+
"2011-01-02",
|
| 219 |
+
"2011-01-01",
|
| 220 |
+
],
|
| 221 |
+
freq="D",
|
| 222 |
+
name="idx1",
|
| 223 |
+
),
|
| 224 |
+
PeriodIndex(
|
| 225 |
+
[
|
| 226 |
+
"2011-01-01",
|
| 227 |
+
"2011-01-01",
|
| 228 |
+
"2011-01-02",
|
| 229 |
+
"2011-01-03",
|
| 230 |
+
"2011-01-05",
|
| 231 |
+
],
|
| 232 |
+
freq="D",
|
| 233 |
+
name="idx1",
|
| 234 |
+
),
|
| 235 |
+
),
|
| 236 |
+
(
|
| 237 |
+
PeriodIndex(
|
| 238 |
+
[
|
| 239 |
+
"2011-01-01",
|
| 240 |
+
"2011-01-03",
|
| 241 |
+
"2011-01-05",
|
| 242 |
+
"2011-01-02",
|
| 243 |
+
"2011-01-01",
|
| 244 |
+
],
|
| 245 |
+
freq="D",
|
| 246 |
+
name="idx2",
|
| 247 |
+
),
|
| 248 |
+
PeriodIndex(
|
| 249 |
+
[
|
| 250 |
+
"2011-01-01",
|
| 251 |
+
"2011-01-01",
|
| 252 |
+
"2011-01-02",
|
| 253 |
+
"2011-01-03",
|
| 254 |
+
"2011-01-05",
|
| 255 |
+
],
|
| 256 |
+
freq="D",
|
| 257 |
+
name="idx2",
|
| 258 |
+
),
|
| 259 |
+
),
|
| 260 |
+
(
|
| 261 |
+
PeriodIndex(
|
| 262 |
+
[NaT, "2011-01-03", "2011-01-05", "2011-01-02", NaT],
|
| 263 |
+
freq="D",
|
| 264 |
+
name="idx3",
|
| 265 |
+
),
|
| 266 |
+
PeriodIndex(
|
| 267 |
+
[NaT, NaT, "2011-01-02", "2011-01-03", "2011-01-05"],
|
| 268 |
+
freq="D",
|
| 269 |
+
name="idx3",
|
| 270 |
+
),
|
| 271 |
+
),
|
| 272 |
+
(
|
| 273 |
+
PeriodIndex(
|
| 274 |
+
["2011", "2013", "2015", "2012", "2011"], name="pidx", freq="Y"
|
| 275 |
+
),
|
| 276 |
+
PeriodIndex(
|
| 277 |
+
["2011", "2011", "2012", "2013", "2015"], name="pidx", freq="Y"
|
| 278 |
+
),
|
| 279 |
+
),
|
| 280 |
+
(
|
| 281 |
+
# For compatibility check
|
| 282 |
+
Index([2011, 2013, 2015, 2012, 2011], name="idx"),
|
| 283 |
+
Index([2011, 2011, 2012, 2013, 2015], name="idx"),
|
| 284 |
+
),
|
| 285 |
+
],
|
| 286 |
+
)
|
| 287 |
+
def test_sort_values_without_freq_periodindex(self, idx, expected):
|
| 288 |
+
# here without_freq means not generateable by period_range
|
| 289 |
+
self.check_sort_values_without_freq(idx, expected)
|
| 290 |
+
|
| 291 |
+
def test_sort_values_without_freq_periodindex_nat(self):
|
| 292 |
+
# doesn't quite fit into check_sort_values_without_freq
|
| 293 |
+
idx = PeriodIndex(["2011", "2013", "NaT", "2011"], name="pidx", freq="D")
|
| 294 |
+
expected = PeriodIndex(["NaT", "2011", "2011", "2013"], name="pidx", freq="D")
|
| 295 |
+
|
| 296 |
+
ordered = idx.sort_values(na_position="first")
|
| 297 |
+
tm.assert_index_equal(ordered, expected)
|
| 298 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 299 |
+
|
| 300 |
+
ordered = idx.sort_values(ascending=False)
|
| 301 |
+
tm.assert_index_equal(ordered, expected[::-1])
|
| 302 |
+
check_freq_nonmonotonic(ordered, idx)
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
def test_order_stability_compat():
|
| 306 |
+
# GH#35922. sort_values is stable both for normal and datetime-like Index
|
| 307 |
+
pidx = PeriodIndex(["2011", "2013", "2015", "2012", "2011"], name="pidx", freq="Y")
|
| 308 |
+
iidx = Index([2011, 2013, 2015, 2012, 2011], name="idx")
|
| 309 |
+
ordered1, indexer1 = pidx.sort_values(return_indexer=True, ascending=False)
|
| 310 |
+
ordered2, indexer2 = iidx.sort_values(return_indexer=True, ascending=False)
|
| 311 |
+
tm.assert_numpy_array_equal(indexer1, indexer2)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimelike_/test_value_counts.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
from pandas import (
|
| 4 |
+
DatetimeIndex,
|
| 5 |
+
NaT,
|
| 6 |
+
PeriodIndex,
|
| 7 |
+
Series,
|
| 8 |
+
TimedeltaIndex,
|
| 9 |
+
date_range,
|
| 10 |
+
period_range,
|
| 11 |
+
timedelta_range,
|
| 12 |
+
)
|
| 13 |
+
import pandas._testing as tm
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TestValueCounts:
|
| 17 |
+
# GH#7735
|
| 18 |
+
|
| 19 |
+
def test_value_counts_unique_datetimeindex(self, tz_naive_fixture):
|
| 20 |
+
tz = tz_naive_fixture
|
| 21 |
+
orig = date_range("2011-01-01 09:00", freq="h", periods=10, tz=tz)
|
| 22 |
+
self._check_value_counts_with_repeats(orig)
|
| 23 |
+
|
| 24 |
+
def test_value_counts_unique_timedeltaindex(self):
|
| 25 |
+
orig = timedelta_range("1 days 09:00:00", freq="h", periods=10)
|
| 26 |
+
self._check_value_counts_with_repeats(orig)
|
| 27 |
+
|
| 28 |
+
def test_value_counts_unique_periodindex(self):
|
| 29 |
+
orig = period_range("2011-01-01 09:00", freq="h", periods=10)
|
| 30 |
+
self._check_value_counts_with_repeats(orig)
|
| 31 |
+
|
| 32 |
+
def _check_value_counts_with_repeats(self, orig):
|
| 33 |
+
# create repeated values, 'n'th element is repeated by n+1 times
|
| 34 |
+
idx = type(orig)(
|
| 35 |
+
np.repeat(orig._values, range(1, len(orig) + 1)), dtype=orig.dtype
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
exp_idx = orig[::-1]
|
| 39 |
+
if not isinstance(exp_idx, PeriodIndex):
|
| 40 |
+
exp_idx = exp_idx._with_freq(None)
|
| 41 |
+
expected = Series(range(10, 0, -1), index=exp_idx, dtype="int64", name="count")
|
| 42 |
+
|
| 43 |
+
for obj in [idx, Series(idx)]:
|
| 44 |
+
tm.assert_series_equal(obj.value_counts(), expected)
|
| 45 |
+
|
| 46 |
+
tm.assert_index_equal(idx.unique(), orig)
|
| 47 |
+
|
| 48 |
+
def test_value_counts_unique_datetimeindex2(self, tz_naive_fixture):
|
| 49 |
+
tz = tz_naive_fixture
|
| 50 |
+
idx = DatetimeIndex(
|
| 51 |
+
[
|
| 52 |
+
"2013-01-01 09:00",
|
| 53 |
+
"2013-01-01 09:00",
|
| 54 |
+
"2013-01-01 09:00",
|
| 55 |
+
"2013-01-01 08:00",
|
| 56 |
+
"2013-01-01 08:00",
|
| 57 |
+
NaT,
|
| 58 |
+
],
|
| 59 |
+
tz=tz,
|
| 60 |
+
)
|
| 61 |
+
self._check_value_counts_dropna(idx)
|
| 62 |
+
|
| 63 |
+
def test_value_counts_unique_timedeltaindex2(self):
|
| 64 |
+
idx = TimedeltaIndex(
|
| 65 |
+
[
|
| 66 |
+
"1 days 09:00:00",
|
| 67 |
+
"1 days 09:00:00",
|
| 68 |
+
"1 days 09:00:00",
|
| 69 |
+
"1 days 08:00:00",
|
| 70 |
+
"1 days 08:00:00",
|
| 71 |
+
NaT,
|
| 72 |
+
]
|
| 73 |
+
)
|
| 74 |
+
self._check_value_counts_dropna(idx)
|
| 75 |
+
|
| 76 |
+
def test_value_counts_unique_periodindex2(self):
|
| 77 |
+
idx = PeriodIndex(
|
| 78 |
+
[
|
| 79 |
+
"2013-01-01 09:00",
|
| 80 |
+
"2013-01-01 09:00",
|
| 81 |
+
"2013-01-01 09:00",
|
| 82 |
+
"2013-01-01 08:00",
|
| 83 |
+
"2013-01-01 08:00",
|
| 84 |
+
NaT,
|
| 85 |
+
],
|
| 86 |
+
freq="h",
|
| 87 |
+
)
|
| 88 |
+
self._check_value_counts_dropna(idx)
|
| 89 |
+
|
| 90 |
+
def _check_value_counts_dropna(self, idx):
|
| 91 |
+
exp_idx = idx[[2, 3]]
|
| 92 |
+
expected = Series([3, 2], index=exp_idx, name="count")
|
| 93 |
+
|
| 94 |
+
for obj in [idx, Series(idx)]:
|
| 95 |
+
tm.assert_series_equal(obj.value_counts(), expected)
|
| 96 |
+
|
| 97 |
+
exp_idx = idx[[2, 3, -1]]
|
| 98 |
+
expected = Series([3, 2, 1], index=exp_idx, name="count")
|
| 99 |
+
|
| 100 |
+
for obj in [idx, Series(idx)]:
|
| 101 |
+
tm.assert_series_equal(obj.value_counts(dropna=False), expected)
|
| 102 |
+
|
| 103 |
+
tm.assert_index_equal(idx.unique(), exp_idx)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/__init__.py
ADDED
|
File without changes
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/__init__.py
ADDED
|
File without changes
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_asof.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import timedelta
|
| 2 |
+
|
| 3 |
+
from pandas import (
|
| 4 |
+
DatetimeIndex,
|
| 5 |
+
Index,
|
| 6 |
+
Timestamp,
|
| 7 |
+
date_range,
|
| 8 |
+
isna,
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestAsOf:
|
| 13 |
+
def test_asof_partial(self):
|
| 14 |
+
index = date_range("2010-01-01", periods=2, freq="ME")
|
| 15 |
+
expected = Timestamp("2010-02-28")
|
| 16 |
+
result = index.asof("2010-02")
|
| 17 |
+
assert result == expected
|
| 18 |
+
assert not isinstance(result, Index)
|
| 19 |
+
|
| 20 |
+
def test_asof(self):
|
| 21 |
+
index = date_range("2020-01-01", periods=10)
|
| 22 |
+
|
| 23 |
+
dt = index[0]
|
| 24 |
+
assert index.asof(dt) == dt
|
| 25 |
+
assert isna(index.asof(dt - timedelta(1)))
|
| 26 |
+
|
| 27 |
+
dt = index[-1]
|
| 28 |
+
assert index.asof(dt + timedelta(1)) == dt
|
| 29 |
+
|
| 30 |
+
dt = index[0].to_pydatetime()
|
| 31 |
+
assert isinstance(index.asof(dt), Timestamp)
|
| 32 |
+
|
| 33 |
+
def test_asof_datetime_string(self):
|
| 34 |
+
# GH#50946
|
| 35 |
+
|
| 36 |
+
dti = date_range("2021-08-05", "2021-08-10", freq="1D")
|
| 37 |
+
|
| 38 |
+
key = "2021-08-09"
|
| 39 |
+
res = dti.asof(key)
|
| 40 |
+
exp = dti[4]
|
| 41 |
+
assert res == exp
|
| 42 |
+
|
| 43 |
+
# add a non-midnight time caused a bug
|
| 44 |
+
dti2 = DatetimeIndex([*list(dti), "2021-08-11 00:00:01"])
|
| 45 |
+
res = dti2.asof(key)
|
| 46 |
+
assert res == exp
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/meth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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
|
| 3 |
+
import dateutil
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from pandas import (
|
| 9 |
+
DatetimeIndex,
|
| 10 |
+
Index,
|
| 11 |
+
NaT,
|
| 12 |
+
PeriodIndex,
|
| 13 |
+
Timestamp,
|
| 14 |
+
date_range,
|
| 15 |
+
)
|
| 16 |
+
import pandas._testing as tm
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class TestDatetimeIndex:
|
| 20 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 21 |
+
def test_dti_astype_asobject_around_dst_transition(self, tzstr):
|
| 22 |
+
# GH#1345
|
| 23 |
+
|
| 24 |
+
# dates around a dst transition
|
| 25 |
+
rng = date_range("2/13/2010", "5/6/2010", tz=tzstr)
|
| 26 |
+
|
| 27 |
+
objs = rng.astype(object)
|
| 28 |
+
for i, x in enumerate(objs):
|
| 29 |
+
exval = rng[i]
|
| 30 |
+
assert x == exval
|
| 31 |
+
assert x.tzinfo == exval.tzinfo
|
| 32 |
+
|
| 33 |
+
objs = rng.astype(object)
|
| 34 |
+
for i, x in enumerate(objs):
|
| 35 |
+
exval = rng[i]
|
| 36 |
+
assert x == exval
|
| 37 |
+
assert x.tzinfo == exval.tzinfo
|
| 38 |
+
|
| 39 |
+
def test_astype(self):
|
| 40 |
+
# GH 13149, GH 13209
|
| 41 |
+
idx = DatetimeIndex(
|
| 42 |
+
["2016-05-16", "NaT", NaT, np.nan], dtype="M8[ns]", name="idx"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
result = idx.astype(object)
|
| 46 |
+
expected = Index(
|
| 47 |
+
[Timestamp("2016-05-16")] + [NaT] * 3, dtype=object, name="idx"
|
| 48 |
+
)
|
| 49 |
+
tm.assert_index_equal(result, expected)
|
| 50 |
+
|
| 51 |
+
result = idx.astype(np.int64)
|
| 52 |
+
expected = Index(
|
| 53 |
+
[1463356800000000000] + [-9223372036854775808] * 3,
|
| 54 |
+
dtype=np.int64,
|
| 55 |
+
name="idx",
|
| 56 |
+
)
|
| 57 |
+
tm.assert_index_equal(result, expected)
|
| 58 |
+
|
| 59 |
+
def test_astype2(self):
|
| 60 |
+
rng = date_range("1/1/2000", periods=10, name="idx")
|
| 61 |
+
result = rng.astype("i8")
|
| 62 |
+
tm.assert_index_equal(result, Index(rng.asi8, name="idx"))
|
| 63 |
+
tm.assert_numpy_array_equal(result.values, rng.asi8)
|
| 64 |
+
|
| 65 |
+
def test_astype_uint(self):
|
| 66 |
+
arr = date_range("2000", periods=2, name="idx")
|
| 67 |
+
|
| 68 |
+
with pytest.raises(TypeError, match=r"Do obj.astype\('int64'\)"):
|
| 69 |
+
arr.astype("uint64")
|
| 70 |
+
with pytest.raises(TypeError, match=r"Do obj.astype\('int64'\)"):
|
| 71 |
+
arr.astype("uint32")
|
| 72 |
+
|
| 73 |
+
def test_astype_with_tz(self):
|
| 74 |
+
# with tz
|
| 75 |
+
rng = date_range("1/1/2000", periods=10, tz="US/Eastern")
|
| 76 |
+
msg = "Cannot use .astype to convert from timezone-aware"
|
| 77 |
+
with pytest.raises(TypeError, match=msg):
|
| 78 |
+
# deprecated
|
| 79 |
+
rng.astype("datetime64[ns]")
|
| 80 |
+
with pytest.raises(TypeError, match=msg):
|
| 81 |
+
# check DatetimeArray while we're here deprecated
|
| 82 |
+
rng._data.astype("datetime64[ns]")
|
| 83 |
+
|
| 84 |
+
def test_astype_tzaware_to_tzaware(self):
|
| 85 |
+
# GH 18951: tz-aware to tz-aware
|
| 86 |
+
idx = date_range("20170101", periods=4, tz="US/Pacific", unit="ns")
|
| 87 |
+
result = idx.astype("datetime64[ns, US/Eastern]")
|
| 88 |
+
expected = date_range(
|
| 89 |
+
"20170101 03:00:00", periods=4, tz="US/Eastern", unit="ns"
|
| 90 |
+
)
|
| 91 |
+
tm.assert_index_equal(result, expected)
|
| 92 |
+
assert result.freq == expected.freq
|
| 93 |
+
|
| 94 |
+
def test_astype_tznaive_to_tzaware(self):
|
| 95 |
+
# GH 18951: tz-naive to tz-aware
|
| 96 |
+
idx = date_range("20170101", periods=4)
|
| 97 |
+
idx = idx._with_freq(None) # tz_localize does not preserve freq
|
| 98 |
+
msg = "Cannot use .astype to convert from timezone-naive"
|
| 99 |
+
with pytest.raises(TypeError, match=msg):
|
| 100 |
+
# dt64->dt64tz deprecated
|
| 101 |
+
idx.astype("datetime64[ns, US/Eastern]")
|
| 102 |
+
with pytest.raises(TypeError, match=msg):
|
| 103 |
+
# dt64->dt64tz deprecated
|
| 104 |
+
idx._data.astype("datetime64[ns, US/Eastern]")
|
| 105 |
+
|
| 106 |
+
def test_astype_str_nat(self, using_infer_string):
|
| 107 |
+
# GH 13149, GH 13209
|
| 108 |
+
# verify that we are returning NaT as a string (and not unicode)
|
| 109 |
+
|
| 110 |
+
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.nan])
|
| 111 |
+
result = idx.astype(str)
|
| 112 |
+
if using_infer_string:
|
| 113 |
+
expected = Index(["2016-05-16", None, None, None], dtype="str")
|
| 114 |
+
else:
|
| 115 |
+
expected = Index(["2016-05-16", "NaT", "NaT", "NaT"], dtype=object)
|
| 116 |
+
tm.assert_index_equal(result, expected)
|
| 117 |
+
|
| 118 |
+
def test_astype_str(self):
|
| 119 |
+
# test astype string - #10442
|
| 120 |
+
dti = date_range("2012-01-01", periods=4, name="test_name")
|
| 121 |
+
result = dti.astype(str)
|
| 122 |
+
expected = Index(
|
| 123 |
+
["2012-01-01", "2012-01-02", "2012-01-03", "2012-01-04"],
|
| 124 |
+
name="test_name",
|
| 125 |
+
dtype="str",
|
| 126 |
+
)
|
| 127 |
+
tm.assert_index_equal(result, expected)
|
| 128 |
+
|
| 129 |
+
def test_astype_str_tz_and_name(self):
|
| 130 |
+
# test astype string with tz and name
|
| 131 |
+
dti = date_range("2012-01-01", periods=3, name="test_name", tz="US/Eastern")
|
| 132 |
+
result = dti.astype(str)
|
| 133 |
+
expected = Index(
|
| 134 |
+
[
|
| 135 |
+
"2012-01-01 00:00:00-05:00",
|
| 136 |
+
"2012-01-02 00:00:00-05:00",
|
| 137 |
+
"2012-01-03 00:00:00-05:00",
|
| 138 |
+
],
|
| 139 |
+
name="test_name",
|
| 140 |
+
dtype="str",
|
| 141 |
+
)
|
| 142 |
+
tm.assert_index_equal(result, expected)
|
| 143 |
+
|
| 144 |
+
def test_astype_str_freq_and_name(self):
|
| 145 |
+
# test astype string with freqH and name
|
| 146 |
+
dti = date_range("1/1/2011", periods=3, freq="h", name="test_name")
|
| 147 |
+
result = dti.astype(str)
|
| 148 |
+
expected = Index(
|
| 149 |
+
["2011-01-01 00:00:00", "2011-01-01 01:00:00", "2011-01-01 02:00:00"],
|
| 150 |
+
name="test_name",
|
| 151 |
+
dtype="str",
|
| 152 |
+
)
|
| 153 |
+
tm.assert_index_equal(result, expected)
|
| 154 |
+
|
| 155 |
+
def test_astype_str_freq_and_tz(self):
|
| 156 |
+
# test astype string with freqH and timezone
|
| 157 |
+
dti = date_range(
|
| 158 |
+
"3/6/2012 00:00", periods=2, freq="h", tz="Europe/London", name="test_name"
|
| 159 |
+
)
|
| 160 |
+
result = dti.astype(str)
|
| 161 |
+
expected = Index(
|
| 162 |
+
["2012-03-06 00:00:00+00:00", "2012-03-06 01:00:00+00:00"],
|
| 163 |
+
dtype="str",
|
| 164 |
+
name="test_name",
|
| 165 |
+
)
|
| 166 |
+
tm.assert_index_equal(result, expected)
|
| 167 |
+
|
| 168 |
+
def test_astype_datetime64(self):
|
| 169 |
+
# GH 13149, GH 13209
|
| 170 |
+
idx = DatetimeIndex(
|
| 171 |
+
["2016-05-16", "NaT", NaT, np.nan], dtype="M8[ns]", name="idx"
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
result = idx.astype("datetime64[ns]")
|
| 175 |
+
tm.assert_index_equal(result, idx)
|
| 176 |
+
assert result is not idx
|
| 177 |
+
|
| 178 |
+
result = idx.astype("datetime64[ns]", copy=False)
|
| 179 |
+
tm.assert_index_equal(result, idx)
|
| 180 |
+
assert result is idx
|
| 181 |
+
|
| 182 |
+
idx_tz = DatetimeIndex(["2016-05-16", "NaT", NaT, np.nan], tz="EST", name="idx")
|
| 183 |
+
msg = "Cannot use .astype to convert from timezone-aware"
|
| 184 |
+
with pytest.raises(TypeError, match=msg):
|
| 185 |
+
# dt64tz->dt64 deprecated
|
| 186 |
+
result = idx_tz.astype("datetime64[ns]")
|
| 187 |
+
|
| 188 |
+
def test_astype_object(self):
|
| 189 |
+
rng = date_range("1/1/2000", periods=20)
|
| 190 |
+
|
| 191 |
+
casted = rng.astype("O")
|
| 192 |
+
exp_values = list(rng)
|
| 193 |
+
|
| 194 |
+
tm.assert_index_equal(casted, Index(exp_values, dtype=np.object_))
|
| 195 |
+
assert casted.tolist() == exp_values
|
| 196 |
+
|
| 197 |
+
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo"])
|
| 198 |
+
def test_astype_object_tz(self, tz):
|
| 199 |
+
idx = date_range(start="2013-01-01", periods=4, freq="ME", name="idx", tz=tz)
|
| 200 |
+
expected_list = [
|
| 201 |
+
Timestamp("2013-01-31", tz=tz),
|
| 202 |
+
Timestamp("2013-02-28", tz=tz),
|
| 203 |
+
Timestamp("2013-03-31", tz=tz),
|
| 204 |
+
Timestamp("2013-04-30", tz=tz),
|
| 205 |
+
]
|
| 206 |
+
expected = Index(expected_list, dtype=object, name="idx")
|
| 207 |
+
result = idx.astype(object)
|
| 208 |
+
tm.assert_index_equal(result, expected)
|
| 209 |
+
assert idx.tolist() == expected_list
|
| 210 |
+
|
| 211 |
+
def test_astype_object_with_nat(self):
|
| 212 |
+
idx = DatetimeIndex(
|
| 213 |
+
[datetime(2013, 1, 1), datetime(2013, 1, 2), NaT, datetime(2013, 1, 4)],
|
| 214 |
+
name="idx",
|
| 215 |
+
)
|
| 216 |
+
expected_list = [
|
| 217 |
+
Timestamp("2013-01-01"),
|
| 218 |
+
Timestamp("2013-01-02"),
|
| 219 |
+
NaT,
|
| 220 |
+
Timestamp("2013-01-04"),
|
| 221 |
+
]
|
| 222 |
+
expected = Index(expected_list, dtype=object, name="idx")
|
| 223 |
+
result = idx.astype(object)
|
| 224 |
+
tm.assert_index_equal(result, expected)
|
| 225 |
+
assert idx.tolist() == expected_list
|
| 226 |
+
|
| 227 |
+
@pytest.mark.parametrize(
|
| 228 |
+
"dtype",
|
| 229 |
+
[float, "timedelta64", "timedelta64[ns]", "datetime64", "datetime64[D]"],
|
| 230 |
+
)
|
| 231 |
+
def test_astype_raises(self, dtype):
|
| 232 |
+
# GH 13149, GH 13209
|
| 233 |
+
idx = DatetimeIndex(["2016-05-16", "NaT", NaT, np.nan])
|
| 234 |
+
msg = "Cannot cast DatetimeIndex to dtype"
|
| 235 |
+
if dtype == "datetime64":
|
| 236 |
+
msg = "Casting to unit-less dtype 'datetime64' is not supported"
|
| 237 |
+
with pytest.raises(TypeError, match=msg):
|
| 238 |
+
idx.astype(dtype)
|
| 239 |
+
|
| 240 |
+
def test_index_convert_to_datetime_array(self):
|
| 241 |
+
def _check_rng(rng):
|
| 242 |
+
converted = rng.to_pydatetime()
|
| 243 |
+
assert isinstance(converted, np.ndarray)
|
| 244 |
+
for x, stamp in zip(converted, rng):
|
| 245 |
+
assert isinstance(x, datetime)
|
| 246 |
+
assert x == stamp.to_pydatetime()
|
| 247 |
+
assert x.tzinfo == stamp.tzinfo
|
| 248 |
+
|
| 249 |
+
rng = date_range("20090415", "20090519")
|
| 250 |
+
rng_eastern = date_range("20090415", "20090519", tz="US/Eastern")
|
| 251 |
+
rng_utc = date_range("20090415", "20090519", tz="utc")
|
| 252 |
+
|
| 253 |
+
_check_rng(rng)
|
| 254 |
+
_check_rng(rng_eastern)
|
| 255 |
+
_check_rng(rng_utc)
|
| 256 |
+
|
| 257 |
+
def test_index_convert_to_datetime_array_explicit_pytz(self):
|
| 258 |
+
pytz = pytest.importorskip("pytz")
|
| 259 |
+
|
| 260 |
+
def _check_rng(rng):
|
| 261 |
+
converted = rng.to_pydatetime()
|
| 262 |
+
assert isinstance(converted, np.ndarray)
|
| 263 |
+
for x, stamp in zip(converted, rng):
|
| 264 |
+
assert isinstance(x, datetime)
|
| 265 |
+
assert x == stamp.to_pydatetime()
|
| 266 |
+
assert x.tzinfo == stamp.tzinfo
|
| 267 |
+
|
| 268 |
+
rng = date_range("20090415", "20090519")
|
| 269 |
+
rng_eastern = date_range("20090415", "20090519", tz=pytz.timezone("US/Eastern"))
|
| 270 |
+
rng_utc = date_range("20090415", "20090519", tz=pytz.utc)
|
| 271 |
+
|
| 272 |
+
_check_rng(rng)
|
| 273 |
+
_check_rng(rng_eastern)
|
| 274 |
+
_check_rng(rng_utc)
|
| 275 |
+
|
| 276 |
+
def test_index_convert_to_datetime_array_dateutil(self):
|
| 277 |
+
def _check_rng(rng):
|
| 278 |
+
converted = rng.to_pydatetime()
|
| 279 |
+
assert isinstance(converted, np.ndarray)
|
| 280 |
+
for x, stamp in zip(converted, rng):
|
| 281 |
+
assert isinstance(x, datetime)
|
| 282 |
+
assert x == stamp.to_pydatetime()
|
| 283 |
+
assert x.tzinfo == stamp.tzinfo
|
| 284 |
+
|
| 285 |
+
rng = date_range("20090415", "20090519")
|
| 286 |
+
rng_eastern = date_range("20090415", "20090519", tz="dateutil/US/Eastern")
|
| 287 |
+
rng_utc = date_range("20090415", "20090519", tz=dateutil.tz.tzutc())
|
| 288 |
+
|
| 289 |
+
_check_rng(rng)
|
| 290 |
+
_check_rng(rng_eastern)
|
| 291 |
+
_check_rng(rng_utc)
|
| 292 |
+
|
| 293 |
+
@pytest.mark.parametrize(
|
| 294 |
+
"tz, dtype",
|
| 295 |
+
[["US/Pacific", "datetime64[ns, US/Pacific]"], [None, "datetime64[ns]"]],
|
| 296 |
+
)
|
| 297 |
+
def test_integer_index_astype_datetime(self, tz, dtype):
|
| 298 |
+
# GH 20997, 20964, 24559
|
| 299 |
+
val = [Timestamp("2018-01-01", tz=tz).as_unit("ns")._value]
|
| 300 |
+
result = Index(val, name="idx").astype(dtype)
|
| 301 |
+
expected = DatetimeIndex(["2018-01-01"], tz=tz, name="idx").as_unit("ns")
|
| 302 |
+
tm.assert_index_equal(result, expected)
|
| 303 |
+
|
| 304 |
+
def test_dti_astype_period(self):
|
| 305 |
+
idx = DatetimeIndex([NaT, "2011-01-01", "2011-02-01"], name="idx")
|
| 306 |
+
|
| 307 |
+
res = idx.astype("period[M]")
|
| 308 |
+
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="M", name="idx")
|
| 309 |
+
tm.assert_index_equal(res, exp)
|
| 310 |
+
|
| 311 |
+
res = idx.astype("period[3M]")
|
| 312 |
+
exp = PeriodIndex(["NaT", "2011-01", "2011-02"], freq="3M", name="idx")
|
| 313 |
+
tm.assert_index_equal(res, exp)
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
class TestAstype:
|
| 317 |
+
@pytest.mark.parametrize("tz", [None, "US/Central"])
|
| 318 |
+
def test_astype_category(self, tz):
|
| 319 |
+
obj = date_range("2000", periods=2, tz=tz, name="idx", unit="ns")
|
| 320 |
+
result = obj.astype("category")
|
| 321 |
+
dti = DatetimeIndex(["2000-01-01", "2000-01-02"], tz=tz).as_unit("ns")
|
| 322 |
+
expected = pd.CategoricalIndex(
|
| 323 |
+
dti,
|
| 324 |
+
name="idx",
|
| 325 |
+
)
|
| 326 |
+
tm.assert_index_equal(result, expected)
|
| 327 |
+
|
| 328 |
+
result = obj._data.astype("category")
|
| 329 |
+
expected = expected.values
|
| 330 |
+
tm.assert_categorical_equal(result, expected)
|
| 331 |
+
|
| 332 |
+
@pytest.mark.parametrize("tz", [None, "US/Central"])
|
| 333 |
+
def test_astype_array_fallback(self, tz):
|
| 334 |
+
obj = date_range("2000", periods=2, tz=tz, name="idx")
|
| 335 |
+
result = obj.astype(bool)
|
| 336 |
+
expected = Index(np.array([True, True]), name="idx")
|
| 337 |
+
tm.assert_index_equal(result, expected)
|
| 338 |
+
|
| 339 |
+
result = obj._data.astype(bool)
|
| 340 |
+
expected = np.array([True, True])
|
| 341 |
+
tm.assert_numpy_array_equal(result, expected)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_delete.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from pandas import (
|
| 4 |
+
DatetimeIndex,
|
| 5 |
+
Series,
|
| 6 |
+
date_range,
|
| 7 |
+
)
|
| 8 |
+
import pandas._testing as tm
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TestDelete:
|
| 12 |
+
def test_delete(self, unit):
|
| 13 |
+
idx = date_range(
|
| 14 |
+
start="2000-01-01", periods=5, freq="ME", name="idx", unit=unit
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# preserve freq
|
| 18 |
+
expected_0 = date_range(
|
| 19 |
+
start="2000-02-01", periods=4, freq="ME", name="idx", unit=unit
|
| 20 |
+
)
|
| 21 |
+
expected_4 = date_range(
|
| 22 |
+
start="2000-01-01", periods=4, freq="ME", name="idx", unit=unit
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# reset freq to None
|
| 26 |
+
expected_1 = DatetimeIndex(
|
| 27 |
+
["2000-01-31", "2000-03-31", "2000-04-30", "2000-05-31"],
|
| 28 |
+
freq=None,
|
| 29 |
+
name="idx",
|
| 30 |
+
).as_unit(unit)
|
| 31 |
+
|
| 32 |
+
cases = {
|
| 33 |
+
0: expected_0,
|
| 34 |
+
-5: expected_0,
|
| 35 |
+
-1: expected_4,
|
| 36 |
+
4: expected_4,
|
| 37 |
+
1: expected_1,
|
| 38 |
+
}
|
| 39 |
+
for n, expected in cases.items():
|
| 40 |
+
result = idx.delete(n)
|
| 41 |
+
tm.assert_index_equal(result, expected)
|
| 42 |
+
assert result.name == expected.name
|
| 43 |
+
assert result.freq == expected.freq
|
| 44 |
+
|
| 45 |
+
with pytest.raises((IndexError, ValueError), match="out of bounds"):
|
| 46 |
+
# either depending on numpy version
|
| 47 |
+
idx.delete(5)
|
| 48 |
+
|
| 49 |
+
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo", "US/Pacific"])
|
| 50 |
+
def test_delete2(self, tz):
|
| 51 |
+
idx = date_range(
|
| 52 |
+
start="2000-01-01 09:00", periods=10, freq="h", name="idx", tz=tz
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
expected = date_range(
|
| 56 |
+
start="2000-01-01 10:00", periods=9, freq="h", name="idx", tz=tz
|
| 57 |
+
)
|
| 58 |
+
result = idx.delete(0)
|
| 59 |
+
tm.assert_index_equal(result, expected)
|
| 60 |
+
assert result.name == expected.name
|
| 61 |
+
assert result.freqstr == "h"
|
| 62 |
+
assert result.tz == expected.tz
|
| 63 |
+
|
| 64 |
+
expected = date_range(
|
| 65 |
+
start="2000-01-01 09:00", periods=9, freq="h", name="idx", tz=tz
|
| 66 |
+
)
|
| 67 |
+
result = idx.delete(-1)
|
| 68 |
+
tm.assert_index_equal(result, expected)
|
| 69 |
+
assert result.name == expected.name
|
| 70 |
+
assert result.freqstr == "h"
|
| 71 |
+
assert result.tz == expected.tz
|
| 72 |
+
|
| 73 |
+
def test_delete_slice(self, unit):
|
| 74 |
+
idx = date_range(
|
| 75 |
+
start="2000-01-01", periods=10, freq="D", name="idx", unit=unit
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# preserve freq
|
| 79 |
+
expected_0_2 = date_range(
|
| 80 |
+
start="2000-01-04", periods=7, freq="D", name="idx", unit=unit
|
| 81 |
+
)
|
| 82 |
+
expected_7_9 = date_range(
|
| 83 |
+
start="2000-01-01", periods=7, freq="D", name="idx", unit=unit
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# reset freq to None
|
| 87 |
+
expected_3_5 = DatetimeIndex(
|
| 88 |
+
[
|
| 89 |
+
"2000-01-01",
|
| 90 |
+
"2000-01-02",
|
| 91 |
+
"2000-01-03",
|
| 92 |
+
"2000-01-07",
|
| 93 |
+
"2000-01-08",
|
| 94 |
+
"2000-01-09",
|
| 95 |
+
"2000-01-10",
|
| 96 |
+
],
|
| 97 |
+
freq=None,
|
| 98 |
+
name="idx",
|
| 99 |
+
).as_unit(unit)
|
| 100 |
+
|
| 101 |
+
cases = {
|
| 102 |
+
(0, 1, 2): expected_0_2,
|
| 103 |
+
(7, 8, 9): expected_7_9,
|
| 104 |
+
(3, 4, 5): expected_3_5,
|
| 105 |
+
}
|
| 106 |
+
for n, expected in cases.items():
|
| 107 |
+
result = idx.delete(n)
|
| 108 |
+
tm.assert_index_equal(result, expected)
|
| 109 |
+
assert result.name == expected.name
|
| 110 |
+
assert result.freq == expected.freq
|
| 111 |
+
|
| 112 |
+
result = idx.delete(slice(n[0], n[-1] + 1))
|
| 113 |
+
tm.assert_index_equal(result, expected)
|
| 114 |
+
assert result.name == expected.name
|
| 115 |
+
assert result.freq == expected.freq
|
| 116 |
+
|
| 117 |
+
# TODO: belongs in Series.drop tests?
|
| 118 |
+
@pytest.mark.parametrize("tz", [None, "Asia/Tokyo", "US/Pacific"])
|
| 119 |
+
def test_delete_slice2(self, tz, unit):
|
| 120 |
+
dti = date_range(
|
| 121 |
+
"2000-01-01 09:00", periods=10, freq="h", name="idx", tz=tz, unit=unit
|
| 122 |
+
)
|
| 123 |
+
ts = Series(
|
| 124 |
+
1,
|
| 125 |
+
index=dti,
|
| 126 |
+
)
|
| 127 |
+
# preserve freq
|
| 128 |
+
result = ts.drop(ts.index[:5]).index
|
| 129 |
+
expected = dti[5:]
|
| 130 |
+
tm.assert_index_equal(result, expected)
|
| 131 |
+
assert result.name == expected.name
|
| 132 |
+
assert result.freq == expected.freq
|
| 133 |
+
assert result.tz == expected.tz
|
| 134 |
+
|
| 135 |
+
# reset freq to None
|
| 136 |
+
result = ts.drop(ts.index[[1, 3, 5, 7, 9]]).index
|
| 137 |
+
expected = dti[::2]._with_freq(None)
|
| 138 |
+
tm.assert_index_equal(result, expected)
|
| 139 |
+
assert result.name == expected.name
|
| 140 |
+
assert result.freq == expected.freq
|
| 141 |
+
assert result.tz == expected.tz
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_factorize.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
DatetimeIndex,
|
| 6 |
+
Index,
|
| 7 |
+
date_range,
|
| 8 |
+
factorize,
|
| 9 |
+
)
|
| 10 |
+
import pandas._testing as tm
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class TestDatetimeIndexFactorize:
|
| 14 |
+
def test_factorize(self):
|
| 15 |
+
idx1 = DatetimeIndex(
|
| 16 |
+
["2014-01", "2014-01", "2014-02", "2014-02", "2014-03", "2014-03"]
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
exp_arr = np.array([0, 0, 1, 1, 2, 2], dtype=np.intp)
|
| 20 |
+
exp_idx = DatetimeIndex(["2014-01", "2014-02", "2014-03"])
|
| 21 |
+
|
| 22 |
+
arr, idx = idx1.factorize()
|
| 23 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
| 24 |
+
tm.assert_index_equal(idx, exp_idx)
|
| 25 |
+
assert idx.freq == exp_idx.freq
|
| 26 |
+
|
| 27 |
+
arr, idx = idx1.factorize(sort=True)
|
| 28 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
| 29 |
+
tm.assert_index_equal(idx, exp_idx)
|
| 30 |
+
assert idx.freq == exp_idx.freq
|
| 31 |
+
|
| 32 |
+
# tz must be preserved
|
| 33 |
+
idx1 = idx1.tz_localize("Asia/Tokyo")
|
| 34 |
+
exp_idx = exp_idx.tz_localize("Asia/Tokyo")
|
| 35 |
+
|
| 36 |
+
arr, idx = idx1.factorize()
|
| 37 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
| 38 |
+
tm.assert_index_equal(idx, exp_idx)
|
| 39 |
+
assert idx.freq == exp_idx.freq
|
| 40 |
+
|
| 41 |
+
idx2 = DatetimeIndex(
|
| 42 |
+
["2014-03", "2014-03", "2014-02", "2014-01", "2014-03", "2014-01"]
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
exp_arr = np.array([2, 2, 1, 0, 2, 0], dtype=np.intp)
|
| 46 |
+
exp_idx = DatetimeIndex(["2014-01", "2014-02", "2014-03"])
|
| 47 |
+
arr, idx = idx2.factorize(sort=True)
|
| 48 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
| 49 |
+
tm.assert_index_equal(idx, exp_idx)
|
| 50 |
+
assert idx.freq == exp_idx.freq
|
| 51 |
+
|
| 52 |
+
exp_arr = np.array([0, 0, 1, 2, 0, 2], dtype=np.intp)
|
| 53 |
+
exp_idx = DatetimeIndex(["2014-03", "2014-02", "2014-01"])
|
| 54 |
+
arr, idx = idx2.factorize()
|
| 55 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
| 56 |
+
tm.assert_index_equal(idx, exp_idx)
|
| 57 |
+
assert idx.freq == exp_idx.freq
|
| 58 |
+
|
| 59 |
+
def test_factorize_preserves_freq(self):
|
| 60 |
+
# GH#38120 freq should be preserved
|
| 61 |
+
idx3 = date_range("2000-01", periods=4, freq="ME", tz="Asia/Tokyo")
|
| 62 |
+
exp_arr = np.array([0, 1, 2, 3], dtype=np.intp)
|
| 63 |
+
|
| 64 |
+
arr, idx = idx3.factorize()
|
| 65 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
| 66 |
+
tm.assert_index_equal(idx, idx3)
|
| 67 |
+
assert idx.freq == idx3.freq
|
| 68 |
+
|
| 69 |
+
arr, idx = factorize(idx3)
|
| 70 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
| 71 |
+
tm.assert_index_equal(idx, idx3)
|
| 72 |
+
assert idx.freq == idx3.freq
|
| 73 |
+
|
| 74 |
+
def test_factorize_tz(self, tz_naive_fixture, index_or_series):
|
| 75 |
+
tz = tz_naive_fixture
|
| 76 |
+
# GH#13750
|
| 77 |
+
base = date_range("2016-11-05", freq="h", periods=100, tz=tz)
|
| 78 |
+
idx = base.repeat(5)
|
| 79 |
+
|
| 80 |
+
exp_arr = np.arange(100, dtype=np.intp).repeat(5)
|
| 81 |
+
|
| 82 |
+
obj = index_or_series(idx)
|
| 83 |
+
|
| 84 |
+
arr, res = obj.factorize()
|
| 85 |
+
tm.assert_numpy_array_equal(arr, exp_arr)
|
| 86 |
+
expected = base._with_freq(None)
|
| 87 |
+
tm.assert_index_equal(res, expected)
|
| 88 |
+
assert res.freq == expected.freq
|
| 89 |
+
|
| 90 |
+
def test_factorize_dst(self, index_or_series):
|
| 91 |
+
# GH#13750
|
| 92 |
+
idx = date_range("2016-11-06", freq="h", periods=12, tz="US/Eastern")
|
| 93 |
+
obj = index_or_series(idx)
|
| 94 |
+
|
| 95 |
+
arr, res = obj.factorize()
|
| 96 |
+
tm.assert_numpy_array_equal(arr, np.arange(12, dtype=np.intp))
|
| 97 |
+
tm.assert_index_equal(res, idx)
|
| 98 |
+
if index_or_series is Index:
|
| 99 |
+
assert res.freq == idx.freq
|
| 100 |
+
|
| 101 |
+
idx = date_range("2016-06-13", freq="h", periods=12, tz="US/Eastern")
|
| 102 |
+
obj = index_or_series(idx)
|
| 103 |
+
|
| 104 |
+
arr, res = obj.factorize()
|
| 105 |
+
tm.assert_numpy_array_equal(arr, np.arange(12, dtype=np.intp))
|
| 106 |
+
tm.assert_index_equal(res, idx)
|
| 107 |
+
if index_or_series is Index:
|
| 108 |
+
assert res.freq == idx.freq
|
| 109 |
+
|
| 110 |
+
@pytest.mark.parametrize("sort", [True, False])
|
| 111 |
+
def test_factorize_no_freq_non_nano(self, tz_naive_fixture, sort):
|
| 112 |
+
# GH#51978 case that does not go through the fastpath based on
|
| 113 |
+
# non-None freq
|
| 114 |
+
tz = tz_naive_fixture
|
| 115 |
+
idx = date_range("2016-11-06", freq="h", periods=5, tz=tz)[[0, 4, 1, 3, 2]]
|
| 116 |
+
exp_codes, exp_uniques = idx.factorize(sort=sort)
|
| 117 |
+
|
| 118 |
+
res_codes, res_uniques = idx.as_unit("s").factorize(sort=sort)
|
| 119 |
+
|
| 120 |
+
tm.assert_numpy_array_equal(res_codes, exp_codes)
|
| 121 |
+
tm.assert_index_equal(res_uniques, exp_uniques.as_unit("s"))
|
| 122 |
+
|
| 123 |
+
res_codes, res_uniques = idx.as_unit("s").to_series().factorize(sort=sort)
|
| 124 |
+
tm.assert_numpy_array_equal(res_codes, exp_codes)
|
| 125 |
+
tm.assert_index_equal(res_uniques, exp_uniques.as_unit("s"))
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_fillna.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pandas._testing as tm
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TestDatetimeIndexFillNA:
|
| 8 |
+
@pytest.mark.parametrize("tz", ["US/Eastern", "Asia/Tokyo"])
|
| 9 |
+
def test_fillna_datetime64(self, tz):
|
| 10 |
+
# GH 11343
|
| 11 |
+
idx = pd.DatetimeIndex(["2011-01-01 09:00", pd.NaT, "2011-01-01 11:00"])
|
| 12 |
+
|
| 13 |
+
exp = pd.DatetimeIndex(
|
| 14 |
+
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"]
|
| 15 |
+
)
|
| 16 |
+
tm.assert_index_equal(idx.fillna(pd.Timestamp("2011-01-01 10:00")), exp)
|
| 17 |
+
|
| 18 |
+
# tz mismatch
|
| 19 |
+
exp = pd.Index(
|
| 20 |
+
[
|
| 21 |
+
pd.Timestamp("2011-01-01 09:00"),
|
| 22 |
+
pd.Timestamp("2011-01-01 10:00", tz=tz),
|
| 23 |
+
pd.Timestamp("2011-01-01 11:00"),
|
| 24 |
+
],
|
| 25 |
+
dtype=object,
|
| 26 |
+
)
|
| 27 |
+
tm.assert_index_equal(idx.fillna(pd.Timestamp("2011-01-01 10:00", tz=tz)), exp)
|
| 28 |
+
|
| 29 |
+
# object
|
| 30 |
+
exp = pd.Index(
|
| 31 |
+
[pd.Timestamp("2011-01-01 09:00"), "x", pd.Timestamp("2011-01-01 11:00")],
|
| 32 |
+
dtype=object,
|
| 33 |
+
)
|
| 34 |
+
tm.assert_index_equal(idx.fillna("x"), exp)
|
| 35 |
+
|
| 36 |
+
idx = pd.DatetimeIndex(["2011-01-01 09:00", pd.NaT, "2011-01-01 11:00"], tz=tz)
|
| 37 |
+
|
| 38 |
+
exp = pd.DatetimeIndex(
|
| 39 |
+
["2011-01-01 09:00", "2011-01-01 10:00", "2011-01-01 11:00"], tz=tz
|
| 40 |
+
)
|
| 41 |
+
tm.assert_index_equal(idx.fillna(pd.Timestamp("2011-01-01 10:00", tz=tz)), exp)
|
| 42 |
+
|
| 43 |
+
exp = pd.Index(
|
| 44 |
+
[
|
| 45 |
+
pd.Timestamp("2011-01-01 09:00", tz=tz),
|
| 46 |
+
pd.Timestamp("2011-01-01 10:00"),
|
| 47 |
+
pd.Timestamp("2011-01-01 11:00", tz=tz),
|
| 48 |
+
],
|
| 49 |
+
dtype=object,
|
| 50 |
+
)
|
| 51 |
+
tm.assert_index_equal(idx.fillna(pd.Timestamp("2011-01-01 10:00")), exp)
|
| 52 |
+
|
| 53 |
+
# object
|
| 54 |
+
exp = pd.Index(
|
| 55 |
+
[
|
| 56 |
+
pd.Timestamp("2011-01-01 09:00", tz=tz),
|
| 57 |
+
"x",
|
| 58 |
+
pd.Timestamp("2011-01-01 11:00", tz=tz),
|
| 59 |
+
],
|
| 60 |
+
dtype=object,
|
| 61 |
+
)
|
| 62 |
+
tm.assert_index_equal(idx.fillna("x"), exp)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_insert.py
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
import zoneinfo
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
from pandas import (
|
| 8 |
+
NA,
|
| 9 |
+
DatetimeIndex,
|
| 10 |
+
Index,
|
| 11 |
+
NaT,
|
| 12 |
+
Timestamp,
|
| 13 |
+
date_range,
|
| 14 |
+
)
|
| 15 |
+
import pandas._testing as tm
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class TestInsert:
|
| 19 |
+
@pytest.mark.parametrize(
|
| 20 |
+
"null", [None, np.nan, np.datetime64("NaT", "ns"), NaT, NA]
|
| 21 |
+
)
|
| 22 |
+
@pytest.mark.parametrize("tz", [None, "UTC", "US/Eastern"])
|
| 23 |
+
def test_insert_nat(self, tz, null):
|
| 24 |
+
# GH#16537, GH#18295 (test missing)
|
| 25 |
+
|
| 26 |
+
idx = DatetimeIndex(["2017-01-01"], tz=tz)
|
| 27 |
+
expected = DatetimeIndex(["NaT", "2017-01-01"], tz=tz)
|
| 28 |
+
if tz is not None and isinstance(null, np.datetime64):
|
| 29 |
+
expected = Index([null, idx[0]], dtype=object)
|
| 30 |
+
|
| 31 |
+
res = idx.insert(0, null)
|
| 32 |
+
tm.assert_index_equal(res, expected)
|
| 33 |
+
|
| 34 |
+
@pytest.mark.parametrize("tz", [None, "UTC", "US/Eastern"])
|
| 35 |
+
def test_insert_invalid_na(self, tz):
|
| 36 |
+
idx = DatetimeIndex(["2017-01-01"], tz=tz)
|
| 37 |
+
|
| 38 |
+
item = np.timedelta64("NaT", "ns")
|
| 39 |
+
result = idx.insert(0, item)
|
| 40 |
+
expected = Index([item, *list(idx)], dtype=object)
|
| 41 |
+
tm.assert_index_equal(result, expected)
|
| 42 |
+
|
| 43 |
+
def test_insert_empty_preserves_freq(self, tz_naive_fixture):
|
| 44 |
+
# GH#33573
|
| 45 |
+
tz = tz_naive_fixture
|
| 46 |
+
dti = DatetimeIndex([], tz=tz, freq="D")
|
| 47 |
+
item = Timestamp("2017-04-05").tz_localize(tz)
|
| 48 |
+
|
| 49 |
+
result = dti.insert(0, item)
|
| 50 |
+
assert result.freq == dti.freq
|
| 51 |
+
|
| 52 |
+
# But not when we insert an item that doesn't conform to freq
|
| 53 |
+
dti = DatetimeIndex([], tz=tz, freq="W-THU")
|
| 54 |
+
result = dti.insert(0, item)
|
| 55 |
+
assert result.freq is None
|
| 56 |
+
|
| 57 |
+
def test_insert(self, unit):
|
| 58 |
+
idx = DatetimeIndex(
|
| 59 |
+
["2000-01-04", "2000-01-01", "2000-01-02"], name="idx"
|
| 60 |
+
).as_unit(unit)
|
| 61 |
+
|
| 62 |
+
result = idx.insert(2, datetime(2000, 1, 5))
|
| 63 |
+
exp = DatetimeIndex(
|
| 64 |
+
["2000-01-04", "2000-01-01", "2000-01-05", "2000-01-02"], name="idx"
|
| 65 |
+
).as_unit(unit)
|
| 66 |
+
tm.assert_index_equal(result, exp)
|
| 67 |
+
|
| 68 |
+
# insertion of non-datetime should coerce to object index
|
| 69 |
+
result = idx.insert(1, "inserted")
|
| 70 |
+
expected = Index(
|
| 71 |
+
[
|
| 72 |
+
datetime(2000, 1, 4),
|
| 73 |
+
"inserted",
|
| 74 |
+
datetime(2000, 1, 1),
|
| 75 |
+
datetime(2000, 1, 2),
|
| 76 |
+
],
|
| 77 |
+
name="idx",
|
| 78 |
+
)
|
| 79 |
+
assert not isinstance(result, DatetimeIndex)
|
| 80 |
+
tm.assert_index_equal(result, expected)
|
| 81 |
+
assert result.name == expected.name
|
| 82 |
+
|
| 83 |
+
def test_insert2(self, unit):
|
| 84 |
+
idx = date_range("1/1/2000", periods=3, freq="ME", name="idx", unit=unit)
|
| 85 |
+
|
| 86 |
+
# preserve freq
|
| 87 |
+
expected_0 = DatetimeIndex(
|
| 88 |
+
["1999-12-31", "2000-01-31", "2000-02-29", "2000-03-31"],
|
| 89 |
+
name="idx",
|
| 90 |
+
freq="ME",
|
| 91 |
+
).as_unit(unit)
|
| 92 |
+
expected_3 = DatetimeIndex(
|
| 93 |
+
["2000-01-31", "2000-02-29", "2000-03-31", "2000-04-30"],
|
| 94 |
+
name="idx",
|
| 95 |
+
freq="ME",
|
| 96 |
+
).as_unit(unit)
|
| 97 |
+
|
| 98 |
+
# reset freq to None
|
| 99 |
+
expected_1_nofreq = DatetimeIndex(
|
| 100 |
+
["2000-01-31", "2000-01-31", "2000-02-29", "2000-03-31"],
|
| 101 |
+
name="idx",
|
| 102 |
+
freq=None,
|
| 103 |
+
).as_unit(unit)
|
| 104 |
+
expected_3_nofreq = DatetimeIndex(
|
| 105 |
+
["2000-01-31", "2000-02-29", "2000-03-31", "2000-01-02"],
|
| 106 |
+
name="idx",
|
| 107 |
+
freq=None,
|
| 108 |
+
).as_unit(unit)
|
| 109 |
+
|
| 110 |
+
cases = [
|
| 111 |
+
(0, datetime(1999, 12, 31), expected_0),
|
| 112 |
+
(-3, datetime(1999, 12, 31), expected_0),
|
| 113 |
+
(3, datetime(2000, 4, 30), expected_3),
|
| 114 |
+
(1, datetime(2000, 1, 31), expected_1_nofreq),
|
| 115 |
+
(3, datetime(2000, 1, 2), expected_3_nofreq),
|
| 116 |
+
]
|
| 117 |
+
|
| 118 |
+
for n, d, expected in cases:
|
| 119 |
+
result = idx.insert(n, d)
|
| 120 |
+
tm.assert_index_equal(result, expected)
|
| 121 |
+
assert result.name == expected.name
|
| 122 |
+
assert result.freq == expected.freq
|
| 123 |
+
|
| 124 |
+
def test_insert3(self, unit):
|
| 125 |
+
idx = date_range("1/1/2000", periods=3, freq="ME", name="idx", unit=unit)
|
| 126 |
+
|
| 127 |
+
# reset freq to None
|
| 128 |
+
result = idx.insert(3, datetime(2000, 1, 2))
|
| 129 |
+
expected = DatetimeIndex(
|
| 130 |
+
["2000-01-31", "2000-02-29", "2000-03-31", "2000-01-02"],
|
| 131 |
+
name="idx",
|
| 132 |
+
freq=None,
|
| 133 |
+
).as_unit(unit)
|
| 134 |
+
tm.assert_index_equal(result, expected)
|
| 135 |
+
assert result.name == expected.name
|
| 136 |
+
assert result.freq is None
|
| 137 |
+
|
| 138 |
+
@pytest.mark.parametrize("tz", ["US/Pacific", "Asia/Singapore"])
|
| 139 |
+
@pytest.mark.parametrize(
|
| 140 |
+
"to_ts",
|
| 141 |
+
[lambda x: x, lambda x: x.to_pydatetime()],
|
| 142 |
+
ids=["Timestamp", "datetime"],
|
| 143 |
+
)
|
| 144 |
+
def test_insert4(self, unit, tz, to_ts):
|
| 145 |
+
idx = date_range(
|
| 146 |
+
"1/1/2000 09:00", periods=6, freq="h", tz=tz, name="idx", unit=unit
|
| 147 |
+
)
|
| 148 |
+
# preserve freq
|
| 149 |
+
expected = date_range(
|
| 150 |
+
"1/1/2000 09:00", periods=7, freq="h", tz=tz, name="idx", unit=unit
|
| 151 |
+
)
|
| 152 |
+
tz = zoneinfo.ZoneInfo(tz)
|
| 153 |
+
d = to_ts(Timestamp("2000-01-01 15:00", tz=tz))
|
| 154 |
+
result = idx.insert(6, d)
|
| 155 |
+
tm.assert_index_equal(result, expected)
|
| 156 |
+
assert result.name == expected.name
|
| 157 |
+
assert result.freq == expected.freq
|
| 158 |
+
assert result.tz == expected.tz
|
| 159 |
+
|
| 160 |
+
@pytest.mark.parametrize("tz", ["US/Pacific", "Asia/Singapore"])
|
| 161 |
+
@pytest.mark.parametrize(
|
| 162 |
+
"to_ts",
|
| 163 |
+
[lambda x: x, lambda x: x.to_pydatetime()],
|
| 164 |
+
ids=["Timestamp", "datetime"],
|
| 165 |
+
)
|
| 166 |
+
def test_insert4_no_freq(self, unit, tz, to_ts):
|
| 167 |
+
idx = date_range(
|
| 168 |
+
"1/1/2000 09:00", periods=6, freq="h", tz=tz, name="idx", unit=unit
|
| 169 |
+
)
|
| 170 |
+
expected = DatetimeIndex(
|
| 171 |
+
[
|
| 172 |
+
"2000-01-01 09:00",
|
| 173 |
+
"2000-01-01 10:00",
|
| 174 |
+
"2000-01-01 11:00",
|
| 175 |
+
"2000-01-01 12:00",
|
| 176 |
+
"2000-01-01 13:00",
|
| 177 |
+
"2000-01-01 14:00",
|
| 178 |
+
"2000-01-01 10:00",
|
| 179 |
+
],
|
| 180 |
+
name="idx",
|
| 181 |
+
tz=tz,
|
| 182 |
+
freq=None,
|
| 183 |
+
).as_unit(unit)
|
| 184 |
+
# reset freq to None
|
| 185 |
+
d = to_ts(Timestamp("2000-01-01 10:00", tz=tz))
|
| 186 |
+
result = idx.insert(6, d)
|
| 187 |
+
tm.assert_index_equal(result, expected)
|
| 188 |
+
assert result.name == expected.name
|
| 189 |
+
assert result.tz == expected.tz
|
| 190 |
+
assert result.freq is None
|
| 191 |
+
|
| 192 |
+
# TODO: also changes DataFrame.__setitem__ with expansion
|
| 193 |
+
def test_insert_mismatched_tzawareness(self):
|
| 194 |
+
# see GH#7299
|
| 195 |
+
idx = date_range("1/1/2000", periods=3, freq="D", tz="Asia/Tokyo", name="idx")
|
| 196 |
+
|
| 197 |
+
# mismatched tz-awareness
|
| 198 |
+
item = Timestamp("2000-01-04")
|
| 199 |
+
result = idx.insert(3, item)
|
| 200 |
+
expected = Index(
|
| 201 |
+
[*list(idx[:3]), item, *list(idx[3:])], dtype=object, name="idx"
|
| 202 |
+
)
|
| 203 |
+
tm.assert_index_equal(result, expected)
|
| 204 |
+
|
| 205 |
+
# mismatched tz-awareness
|
| 206 |
+
item = datetime(2000, 1, 4)
|
| 207 |
+
result = idx.insert(3, item)
|
| 208 |
+
expected = Index(
|
| 209 |
+
[*list(idx[:3]), item, *list(idx[3:])], dtype=object, name="idx"
|
| 210 |
+
)
|
| 211 |
+
tm.assert_index_equal(result, expected)
|
| 212 |
+
|
| 213 |
+
# TODO: also changes DataFrame.__setitem__ with expansion
|
| 214 |
+
def test_insert_mismatched_tz(self):
|
| 215 |
+
# see GH#7299
|
| 216 |
+
# pre-2.0 with mismatched tzs we would cast to object
|
| 217 |
+
idx = date_range(
|
| 218 |
+
"1/1/2000", periods=3, freq="D", tz="Asia/Tokyo", unit="ns", name="idx"
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
# mismatched tz -> cast to object (could reasonably cast to same tz or UTC)
|
| 222 |
+
item = Timestamp("2000-01-04", tz="US/Eastern")
|
| 223 |
+
result = idx.insert(3, item)
|
| 224 |
+
expected = Index(
|
| 225 |
+
[*list(idx[:3]), item.tz_convert(idx.tz), *list(idx[3:])],
|
| 226 |
+
name="idx",
|
| 227 |
+
)
|
| 228 |
+
assert expected.dtype == idx.dtype
|
| 229 |
+
tm.assert_index_equal(result, expected)
|
| 230 |
+
|
| 231 |
+
item = datetime(2000, 1, 4, tzinfo=zoneinfo.ZoneInfo("US/Eastern"))
|
| 232 |
+
result = idx.insert(3, item)
|
| 233 |
+
expected = Index(
|
| 234 |
+
[*list(idx[:3]), item.astimezone(idx.tzinfo), *list(idx[3:])],
|
| 235 |
+
name="idx",
|
| 236 |
+
)
|
| 237 |
+
assert expected.dtype == idx.dtype
|
| 238 |
+
tm.assert_index_equal(result, expected)
|
| 239 |
+
|
| 240 |
+
@pytest.mark.parametrize(
|
| 241 |
+
"item", [0, np.int64(0), np.float64(0), np.array(0), np.timedelta64(456, "ns")]
|
| 242 |
+
)
|
| 243 |
+
def test_insert_mismatched_types_raises(self, tz_aware_fixture, item):
|
| 244 |
+
# GH#33703 dont cast these to dt64
|
| 245 |
+
tz = tz_aware_fixture
|
| 246 |
+
dti = date_range("2019-11-04", periods=9, freq="-1D", name=9, tz=tz)
|
| 247 |
+
|
| 248 |
+
result = dti.insert(1, item)
|
| 249 |
+
|
| 250 |
+
if isinstance(item, np.ndarray):
|
| 251 |
+
assert item.item() == 0
|
| 252 |
+
expected = Index([dti[0], 0, *list(dti[1:])], dtype=object, name=9)
|
| 253 |
+
else:
|
| 254 |
+
expected = Index([dti[0], item, *list(dti[1:])], dtype=object, name=9)
|
| 255 |
+
|
| 256 |
+
tm.assert_index_equal(result, expected)
|
| 257 |
+
|
| 258 |
+
def test_insert_castable_str(self, tz_aware_fixture):
|
| 259 |
+
# GH#33703
|
| 260 |
+
tz = tz_aware_fixture
|
| 261 |
+
dti = date_range("2019-11-04", periods=3, freq="-1D", name=9, tz=tz)
|
| 262 |
+
|
| 263 |
+
value = "2019-11-05"
|
| 264 |
+
result = dti.insert(0, value)
|
| 265 |
+
|
| 266 |
+
ts = Timestamp(value).tz_localize(tz)
|
| 267 |
+
expected = DatetimeIndex([ts, *list(dti)], dtype=dti.dtype, name=9)
|
| 268 |
+
tm.assert_index_equal(result, expected)
|
| 269 |
+
|
| 270 |
+
def test_insert_non_castable_str(self, tz_aware_fixture):
|
| 271 |
+
# GH#33703
|
| 272 |
+
tz = tz_aware_fixture
|
| 273 |
+
dti = date_range("2019-11-04", periods=3, freq="-1D", name=9, tz=tz)
|
| 274 |
+
|
| 275 |
+
value = "foo"
|
| 276 |
+
result = dti.insert(0, value)
|
| 277 |
+
|
| 278 |
+
expected = Index(["foo", *list(dti)], dtype=object, name=9)
|
| 279 |
+
tm.assert_index_equal(result, expected)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_isocalendar.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pandas import (
|
| 2 |
+
DataFrame,
|
| 3 |
+
DatetimeIndex,
|
| 4 |
+
date_range,
|
| 5 |
+
)
|
| 6 |
+
import pandas._testing as tm
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
|
| 10 |
+
# GH#6538: Check that DatetimeIndex and its TimeStamp elements
|
| 11 |
+
# return the same weekofyear accessor close to new year w/ tz
|
| 12 |
+
dates = ["2013/12/29", "2013/12/30", "2013/12/31"]
|
| 13 |
+
dates = DatetimeIndex(dates, tz="Europe/Brussels")
|
| 14 |
+
result = dates.isocalendar()
|
| 15 |
+
expected_data_frame = DataFrame(
|
| 16 |
+
[[2013, 52, 7], [2014, 1, 1], [2014, 1, 2]],
|
| 17 |
+
columns=["year", "week", "day"],
|
| 18 |
+
index=dates,
|
| 19 |
+
dtype="UInt32",
|
| 20 |
+
)
|
| 21 |
+
tm.assert_frame_equal(result, expected_data_frame)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_dti_timestamp_isocalendar_fields():
|
| 25 |
+
idx = date_range("2020-01-01", periods=10)
|
| 26 |
+
expected = tuple(idx.isocalendar().iloc[-1].to_list())
|
| 27 |
+
result = idx[-1].isocalendar()
|
| 28 |
+
assert result == expected
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_map.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from pandas import (
|
| 4 |
+
DatetimeIndex,
|
| 5 |
+
Index,
|
| 6 |
+
MultiIndex,
|
| 7 |
+
Period,
|
| 8 |
+
date_range,
|
| 9 |
+
)
|
| 10 |
+
import pandas._testing as tm
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class TestMap:
|
| 14 |
+
def test_map(self):
|
| 15 |
+
rng = date_range("1/1/2000", periods=10)
|
| 16 |
+
|
| 17 |
+
f = lambda x: x.strftime("%Y%m%d")
|
| 18 |
+
result = rng.map(f)
|
| 19 |
+
exp = Index([f(x) for x in rng])
|
| 20 |
+
tm.assert_index_equal(result, exp)
|
| 21 |
+
|
| 22 |
+
def test_map_fallthrough(self, capsys):
|
| 23 |
+
# GH#22067, check we don't get warnings about silently ignored errors
|
| 24 |
+
dti = date_range("2017-01-01", "2018-01-01", freq="B")
|
| 25 |
+
|
| 26 |
+
dti.map(lambda x: Period(year=x.year, month=x.month, freq="M"))
|
| 27 |
+
|
| 28 |
+
captured = capsys.readouterr()
|
| 29 |
+
assert captured.err == ""
|
| 30 |
+
|
| 31 |
+
def test_map_bug_1677(self):
|
| 32 |
+
index = DatetimeIndex(["2012-04-25 09:30:00.393000"])
|
| 33 |
+
f = index.asof
|
| 34 |
+
|
| 35 |
+
result = index.map(f)
|
| 36 |
+
expected = Index([f(index[0])])
|
| 37 |
+
tm.assert_index_equal(result, expected)
|
| 38 |
+
|
| 39 |
+
@pytest.mark.parametrize("name", [None, "name"])
|
| 40 |
+
def test_index_map(self, name):
|
| 41 |
+
# see GH#20990
|
| 42 |
+
count = 6
|
| 43 |
+
index = date_range("2018-01-01", periods=count, freq="ME", name=name).map(
|
| 44 |
+
lambda x: (x.year, x.month)
|
| 45 |
+
)
|
| 46 |
+
exp_index = MultiIndex.from_product(((2018,), range(1, 7)), names=[name, name])
|
| 47 |
+
tm.assert_index_equal(index, exp_index)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_normalize.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dateutil.tz import tzlocal
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from pandas.compat import WASM
|
| 6 |
+
import pandas.util._test_decorators as td
|
| 7 |
+
|
| 8 |
+
from pandas import (
|
| 9 |
+
DatetimeIndex,
|
| 10 |
+
NaT,
|
| 11 |
+
Timestamp,
|
| 12 |
+
date_range,
|
| 13 |
+
)
|
| 14 |
+
import pandas._testing as tm
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class TestNormalize:
|
| 18 |
+
def test_normalize(self):
|
| 19 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D")
|
| 20 |
+
|
| 21 |
+
result = rng.normalize()
|
| 22 |
+
expected = date_range("1/1/2000", periods=10, freq="D")
|
| 23 |
+
tm.assert_index_equal(result, expected)
|
| 24 |
+
|
| 25 |
+
arr_ns = np.array([1380585623454345752, 1380585612343234312]).astype(
|
| 26 |
+
"datetime64[ns]"
|
| 27 |
+
)
|
| 28 |
+
rng_ns = DatetimeIndex(arr_ns)
|
| 29 |
+
rng_ns_normalized = rng_ns.normalize()
|
| 30 |
+
|
| 31 |
+
arr_ns = np.array([1380585600000000000, 1380585600000000000]).astype(
|
| 32 |
+
"datetime64[ns]"
|
| 33 |
+
)
|
| 34 |
+
expected = DatetimeIndex(arr_ns)
|
| 35 |
+
tm.assert_index_equal(rng_ns_normalized, expected)
|
| 36 |
+
|
| 37 |
+
assert result.is_normalized
|
| 38 |
+
assert not rng.is_normalized
|
| 39 |
+
|
| 40 |
+
def test_normalize_nat(self):
|
| 41 |
+
dti = DatetimeIndex([NaT, Timestamp("2018-01-01 01:00:00")])
|
| 42 |
+
result = dti.normalize()
|
| 43 |
+
expected = DatetimeIndex([NaT, Timestamp("2018-01-01")])
|
| 44 |
+
tm.assert_index_equal(result, expected)
|
| 45 |
+
|
| 46 |
+
def test_normalize_tz(self):
|
| 47 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz="US/Eastern")
|
| 48 |
+
|
| 49 |
+
result = rng.normalize() # does not preserve freq
|
| 50 |
+
expected = date_range("1/1/2000", periods=10, freq="D", tz="US/Eastern")
|
| 51 |
+
tm.assert_index_equal(result, expected._with_freq(None))
|
| 52 |
+
|
| 53 |
+
assert result.is_normalized
|
| 54 |
+
assert not rng.is_normalized
|
| 55 |
+
|
| 56 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz="UTC")
|
| 57 |
+
|
| 58 |
+
result = rng.normalize()
|
| 59 |
+
expected = date_range("1/1/2000", periods=10, freq="D", tz="UTC")
|
| 60 |
+
tm.assert_index_equal(result, expected)
|
| 61 |
+
|
| 62 |
+
assert result.is_normalized
|
| 63 |
+
assert not rng.is_normalized
|
| 64 |
+
|
| 65 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz=tzlocal())
|
| 66 |
+
result = rng.normalize() # does not preserve freq
|
| 67 |
+
expected = date_range("1/1/2000", periods=10, freq="D", tz=tzlocal())
|
| 68 |
+
tm.assert_index_equal(result, expected._with_freq(None))
|
| 69 |
+
|
| 70 |
+
assert result.is_normalized
|
| 71 |
+
assert not rng.is_normalized
|
| 72 |
+
|
| 73 |
+
@td.skip_if_windows
|
| 74 |
+
@pytest.mark.skipif(
|
| 75 |
+
WASM, reason="tzset is available only on Unix-like systems, not WASM"
|
| 76 |
+
)
|
| 77 |
+
@pytest.mark.parametrize(
|
| 78 |
+
"timezone",
|
| 79 |
+
[
|
| 80 |
+
"US/Pacific",
|
| 81 |
+
"US/Eastern",
|
| 82 |
+
"UTC",
|
| 83 |
+
"Asia/Kolkata",
|
| 84 |
+
"Asia/Shanghai",
|
| 85 |
+
"Australia/Canberra",
|
| 86 |
+
],
|
| 87 |
+
)
|
| 88 |
+
def test_normalize_tz_local(self, timezone):
|
| 89 |
+
# GH#13459
|
| 90 |
+
with tm.set_timezone(timezone):
|
| 91 |
+
rng = date_range("1/1/2000 9:30", periods=10, freq="D", tz=tzlocal())
|
| 92 |
+
|
| 93 |
+
result = rng.normalize()
|
| 94 |
+
expected = date_range("1/1/2000", periods=10, freq="D", tz=tzlocal())
|
| 95 |
+
expected = expected._with_freq(None)
|
| 96 |
+
tm.assert_index_equal(result, expected)
|
| 97 |
+
|
| 98 |
+
assert result.is_normalized
|
| 99 |
+
assert not rng.is_normalized
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_repeat.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas import (
|
| 5 |
+
DatetimeIndex,
|
| 6 |
+
Timestamp,
|
| 7 |
+
date_range,
|
| 8 |
+
)
|
| 9 |
+
import pandas._testing as tm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class TestRepeat:
|
| 13 |
+
def test_repeat_range(self, tz_naive_fixture):
|
| 14 |
+
rng = date_range("1/1/2000", "1/1/2001")
|
| 15 |
+
|
| 16 |
+
result = rng.repeat(5)
|
| 17 |
+
assert result.freq is None
|
| 18 |
+
assert len(result) == 5 * len(rng)
|
| 19 |
+
|
| 20 |
+
def test_repeat_range2(self, tz_naive_fixture, unit):
|
| 21 |
+
tz = tz_naive_fixture
|
| 22 |
+
index = date_range("2001-01-01", periods=2, freq="D", tz=tz, unit=unit)
|
| 23 |
+
exp = DatetimeIndex(
|
| 24 |
+
["2001-01-01", "2001-01-01", "2001-01-02", "2001-01-02"], tz=tz
|
| 25 |
+
).as_unit(unit)
|
| 26 |
+
for res in [index.repeat(2), np.repeat(index, 2)]:
|
| 27 |
+
tm.assert_index_equal(res, exp)
|
| 28 |
+
assert res.freq is None
|
| 29 |
+
|
| 30 |
+
def test_repeat_range3(self, tz_naive_fixture, unit):
|
| 31 |
+
tz = tz_naive_fixture
|
| 32 |
+
index = date_range("2001-01-01", periods=2, freq="2D", tz=tz, unit=unit)
|
| 33 |
+
exp = DatetimeIndex(
|
| 34 |
+
["2001-01-01", "2001-01-01", "2001-01-03", "2001-01-03"], tz=tz
|
| 35 |
+
).as_unit(unit)
|
| 36 |
+
for res in [index.repeat(2), np.repeat(index, 2)]:
|
| 37 |
+
tm.assert_index_equal(res, exp)
|
| 38 |
+
assert res.freq is None
|
| 39 |
+
|
| 40 |
+
def test_repeat_range4(self, tz_naive_fixture, unit):
|
| 41 |
+
tz = tz_naive_fixture
|
| 42 |
+
index = DatetimeIndex(["2001-01-01", "NaT", "2003-01-01"], tz=tz).as_unit(unit)
|
| 43 |
+
exp = DatetimeIndex(
|
| 44 |
+
[
|
| 45 |
+
"2001-01-01",
|
| 46 |
+
"2001-01-01",
|
| 47 |
+
"2001-01-01",
|
| 48 |
+
"NaT",
|
| 49 |
+
"NaT",
|
| 50 |
+
"NaT",
|
| 51 |
+
"2003-01-01",
|
| 52 |
+
"2003-01-01",
|
| 53 |
+
"2003-01-01",
|
| 54 |
+
],
|
| 55 |
+
tz=tz,
|
| 56 |
+
).as_unit(unit)
|
| 57 |
+
for res in [index.repeat(3), np.repeat(index, 3)]:
|
| 58 |
+
tm.assert_index_equal(res, exp)
|
| 59 |
+
assert res.freq is None
|
| 60 |
+
|
| 61 |
+
def test_repeat(self, tz_naive_fixture, unit):
|
| 62 |
+
tz = tz_naive_fixture
|
| 63 |
+
reps = 2
|
| 64 |
+
msg = "the 'axis' parameter is not supported"
|
| 65 |
+
|
| 66 |
+
rng = date_range(start="2016-01-01", periods=2, freq="30Min", tz=tz, unit=unit)
|
| 67 |
+
|
| 68 |
+
expected_rng = DatetimeIndex(
|
| 69 |
+
[
|
| 70 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
| 71 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
| 72 |
+
Timestamp("2016-01-01 00:30:00", tz=tz),
|
| 73 |
+
Timestamp("2016-01-01 00:30:00", tz=tz),
|
| 74 |
+
]
|
| 75 |
+
).as_unit(unit)
|
| 76 |
+
|
| 77 |
+
res = rng.repeat(reps)
|
| 78 |
+
tm.assert_index_equal(res, expected_rng)
|
| 79 |
+
assert res.freq is None
|
| 80 |
+
|
| 81 |
+
tm.assert_index_equal(np.repeat(rng, reps), expected_rng)
|
| 82 |
+
with pytest.raises(ValueError, match=msg):
|
| 83 |
+
np.repeat(rng, reps, axis=1)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_resolution.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dateutil.tz import tzlocal
|
| 2 |
+
import pytest
|
| 3 |
+
|
| 4 |
+
from pandas.compat import (
|
| 5 |
+
IS64,
|
| 6 |
+
WASM,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
from pandas import date_range
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@pytest.mark.parametrize(
|
| 13 |
+
"freq,expected",
|
| 14 |
+
[
|
| 15 |
+
("YE", "day"),
|
| 16 |
+
("QE", "day"),
|
| 17 |
+
("ME", "day"),
|
| 18 |
+
("D", "day"),
|
| 19 |
+
("h", "hour"),
|
| 20 |
+
("min", "minute"),
|
| 21 |
+
("s", "second"),
|
| 22 |
+
("ms", "millisecond"),
|
| 23 |
+
("us", "microsecond"),
|
| 24 |
+
],
|
| 25 |
+
)
|
| 26 |
+
@pytest.mark.skipif(WASM, reason="OverflowError received on WASM")
|
| 27 |
+
def test_dti_resolution(request, tz_naive_fixture, freq, expected):
|
| 28 |
+
tz = tz_naive_fixture
|
| 29 |
+
if freq == "YE" and ((not IS64) or WASM) and isinstance(tz, tzlocal):
|
| 30 |
+
request.applymarker(
|
| 31 |
+
pytest.mark.xfail(reason="OverflowError inside tzlocal past 2038")
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
idx = date_range(start="2013-04-01", periods=30, freq=freq, tz=tz)
|
| 35 |
+
assert idx.resolution == expected
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_round.py
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
| 9 |
+
date_range,
|
| 10 |
+
)
|
| 11 |
+
import pandas._testing as tm
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class TestDatetimeIndexRound:
|
| 15 |
+
def test_round_daily(self):
|
| 16 |
+
dti = date_range("20130101 09:10:11", periods=5)
|
| 17 |
+
result = dti.round("D")
|
| 18 |
+
expected = date_range("20130101", periods=5)
|
| 19 |
+
tm.assert_index_equal(result, expected)
|
| 20 |
+
|
| 21 |
+
dti = dti.tz_localize("UTC").tz_convert("US/Eastern")
|
| 22 |
+
result = dti.round("D")
|
| 23 |
+
expected = date_range("20130101", periods=5).tz_localize("US/Eastern")
|
| 24 |
+
tm.assert_index_equal(result, expected)
|
| 25 |
+
|
| 26 |
+
result = dti.round("s")
|
| 27 |
+
tm.assert_index_equal(result, dti)
|
| 28 |
+
|
| 29 |
+
@pytest.mark.parametrize(
|
| 30 |
+
"freq, error_msg",
|
| 31 |
+
[
|
| 32 |
+
("YE", "<YearEnd: month=12> is a non-fixed frequency"),
|
| 33 |
+
("ME", "<MonthEnd> is a non-fixed frequency"),
|
| 34 |
+
("foobar", "Invalid frequency: foobar"),
|
| 35 |
+
],
|
| 36 |
+
)
|
| 37 |
+
def test_round_invalid(self, freq, error_msg):
|
| 38 |
+
dti = date_range("20130101 09:10:11", periods=5)
|
| 39 |
+
dti = dti.tz_localize("UTC").tz_convert("US/Eastern")
|
| 40 |
+
with pytest.raises(ValueError, match=error_msg):
|
| 41 |
+
dti.round(freq)
|
| 42 |
+
|
| 43 |
+
def test_round(self, tz_naive_fixture, unit):
|
| 44 |
+
tz = tz_naive_fixture
|
| 45 |
+
rng = date_range(start="2016-01-01", periods=5, freq="30Min", tz=tz, unit=unit)
|
| 46 |
+
elt = rng[1]
|
| 47 |
+
|
| 48 |
+
expected_rng = DatetimeIndex(
|
| 49 |
+
[
|
| 50 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
| 51 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
| 52 |
+
Timestamp("2016-01-01 01:00:00", tz=tz),
|
| 53 |
+
Timestamp("2016-01-01 02:00:00", tz=tz),
|
| 54 |
+
Timestamp("2016-01-01 02:00:00", tz=tz),
|
| 55 |
+
]
|
| 56 |
+
).as_unit(unit)
|
| 57 |
+
expected_elt = expected_rng[1]
|
| 58 |
+
|
| 59 |
+
result = rng.round(freq="h")
|
| 60 |
+
tm.assert_index_equal(result, expected_rng)
|
| 61 |
+
assert elt.round(freq="h") == expected_elt
|
| 62 |
+
|
| 63 |
+
msg = INVALID_FREQ_ERR_MSG
|
| 64 |
+
with pytest.raises(ValueError, match=msg):
|
| 65 |
+
rng.round(freq="foo")
|
| 66 |
+
with pytest.raises(ValueError, match=msg):
|
| 67 |
+
elt.round(freq="foo")
|
| 68 |
+
|
| 69 |
+
msg = "<MonthEnd> is a non-fixed frequency"
|
| 70 |
+
with pytest.raises(ValueError, match=msg):
|
| 71 |
+
rng.round(freq="ME")
|
| 72 |
+
with pytest.raises(ValueError, match=msg):
|
| 73 |
+
elt.round(freq="ME")
|
| 74 |
+
|
| 75 |
+
def test_round2(self, tz_naive_fixture):
|
| 76 |
+
tz = tz_naive_fixture
|
| 77 |
+
# GH#14440 & GH#15578
|
| 78 |
+
index = DatetimeIndex(["2016-10-17 12:00:00.0015"], tz=tz).as_unit("ns")
|
| 79 |
+
result = index.round("ms")
|
| 80 |
+
expected = DatetimeIndex(["2016-10-17 12:00:00.002000"], tz=tz).as_unit("ns")
|
| 81 |
+
tm.assert_index_equal(result, expected)
|
| 82 |
+
|
| 83 |
+
for freq in ["us", "ns"]:
|
| 84 |
+
tm.assert_index_equal(index, index.round(freq))
|
| 85 |
+
|
| 86 |
+
def test_round3(self, tz_naive_fixture):
|
| 87 |
+
tz = tz_naive_fixture
|
| 88 |
+
index = DatetimeIndex(["2016-10-17 12:00:00.00149"], tz=tz).as_unit("ns")
|
| 89 |
+
result = index.round("ms")
|
| 90 |
+
expected = DatetimeIndex(["2016-10-17 12:00:00.001000"], tz=tz).as_unit("ns")
|
| 91 |
+
tm.assert_index_equal(result, expected)
|
| 92 |
+
|
| 93 |
+
def test_round4(self, tz_naive_fixture):
|
| 94 |
+
index = DatetimeIndex(["2016-10-17 12:00:00.001501031"], dtype="M8[ns]")
|
| 95 |
+
result = index.round("10ns")
|
| 96 |
+
expected = DatetimeIndex(["2016-10-17 12:00:00.001501030"], dtype="M8[ns]")
|
| 97 |
+
tm.assert_index_equal(result, expected)
|
| 98 |
+
|
| 99 |
+
ts = "2016-10-17 12:00:00.001501031"
|
| 100 |
+
dti = DatetimeIndex([ts], dtype="M8[ns]")
|
| 101 |
+
with tm.assert_produces_warning(False):
|
| 102 |
+
dti.round("1010ns")
|
| 103 |
+
|
| 104 |
+
def test_no_rounding_occurs(self, tz_naive_fixture):
|
| 105 |
+
# GH 21262
|
| 106 |
+
tz = tz_naive_fixture
|
| 107 |
+
rng = date_range(start="2016-01-01", periods=5, freq="2Min", tz=tz, unit="ns")
|
| 108 |
+
|
| 109 |
+
expected_rng = DatetimeIndex(
|
| 110 |
+
[
|
| 111 |
+
Timestamp("2016-01-01 00:00:00", tz=tz),
|
| 112 |
+
Timestamp("2016-01-01 00:02:00", tz=tz),
|
| 113 |
+
Timestamp("2016-01-01 00:04:00", tz=tz),
|
| 114 |
+
Timestamp("2016-01-01 00:06:00", tz=tz),
|
| 115 |
+
Timestamp("2016-01-01 00:08:00", tz=tz),
|
| 116 |
+
]
|
| 117 |
+
).as_unit("ns")
|
| 118 |
+
|
| 119 |
+
result = rng.round(freq="2min")
|
| 120 |
+
tm.assert_index_equal(result, expected_rng)
|
| 121 |
+
|
| 122 |
+
@pytest.mark.parametrize(
|
| 123 |
+
"test_input, rounder, freq, expected",
|
| 124 |
+
[
|
| 125 |
+
(["2117-01-01 00:00:45"], "floor", "15s", ["2117-01-01 00:00:45"]),
|
| 126 |
+
(["2117-01-01 00:00:45"], "ceil", "15s", ["2117-01-01 00:00:45"]),
|
| 127 |
+
(
|
| 128 |
+
["2117-01-01 00:00:45.000000012"],
|
| 129 |
+
"floor",
|
| 130 |
+
"10ns",
|
| 131 |
+
["2117-01-01 00:00:45.000000010"],
|
| 132 |
+
),
|
| 133 |
+
(
|
| 134 |
+
["1823-01-01 00:00:01.000000012"],
|
| 135 |
+
"ceil",
|
| 136 |
+
"10ns",
|
| 137 |
+
["1823-01-01 00:00:01.000000020"],
|
| 138 |
+
),
|
| 139 |
+
(["1823-01-01 00:00:01"], "floor", "1s", ["1823-01-01 00:00:01"]),
|
| 140 |
+
(["1823-01-01 00:00:01"], "ceil", "1s", ["1823-01-01 00:00:01"]),
|
| 141 |
+
(["2018-01-01 00:15:00"], "ceil", "15min", ["2018-01-01 00:15:00"]),
|
| 142 |
+
(["2018-01-01 00:15:00"], "floor", "15min", ["2018-01-01 00:15:00"]),
|
| 143 |
+
(["1823-01-01 03:00:00"], "ceil", "3h", ["1823-01-01 03:00:00"]),
|
| 144 |
+
(["1823-01-01 03:00:00"], "floor", "3h", ["1823-01-01 03:00:00"]),
|
| 145 |
+
(
|
| 146 |
+
("NaT", "1823-01-01 00:00:01"),
|
| 147 |
+
"floor",
|
| 148 |
+
"1s",
|
| 149 |
+
("NaT", "1823-01-01 00:00:01"),
|
| 150 |
+
),
|
| 151 |
+
(
|
| 152 |
+
("NaT", "1823-01-01 00:00:01"),
|
| 153 |
+
"ceil",
|
| 154 |
+
"1s",
|
| 155 |
+
("NaT", "1823-01-01 00:00:01"),
|
| 156 |
+
),
|
| 157 |
+
],
|
| 158 |
+
)
|
| 159 |
+
def test_ceil_floor_edge(self, test_input, rounder, freq, expected):
|
| 160 |
+
dt = DatetimeIndex(list(test_input))
|
| 161 |
+
func = getattr(dt, rounder)
|
| 162 |
+
result = func(freq)
|
| 163 |
+
expected = DatetimeIndex(list(expected))
|
| 164 |
+
assert expected.equals(result)
|
| 165 |
+
|
| 166 |
+
@pytest.mark.parametrize(
|
| 167 |
+
"start, index_freq, periods",
|
| 168 |
+
[("2018-01-01", "12h", 25), ("2018-01-01 0:0:0.124999", "1ns", 1000)],
|
| 169 |
+
)
|
| 170 |
+
@pytest.mark.parametrize(
|
| 171 |
+
"round_freq",
|
| 172 |
+
[
|
| 173 |
+
"2ns",
|
| 174 |
+
"3ns",
|
| 175 |
+
"4ns",
|
| 176 |
+
"5ns",
|
| 177 |
+
"6ns",
|
| 178 |
+
"7ns",
|
| 179 |
+
"250ns",
|
| 180 |
+
"500ns",
|
| 181 |
+
"750ns",
|
| 182 |
+
"1us",
|
| 183 |
+
"19us",
|
| 184 |
+
"250us",
|
| 185 |
+
"500us",
|
| 186 |
+
"750us",
|
| 187 |
+
"1s",
|
| 188 |
+
"2s",
|
| 189 |
+
"3s",
|
| 190 |
+
"12h",
|
| 191 |
+
"1D",
|
| 192 |
+
],
|
| 193 |
+
)
|
| 194 |
+
def test_round_int64(self, start, index_freq, periods, round_freq):
|
| 195 |
+
dt = date_range(start=start, freq=index_freq, periods=periods, unit="ns")
|
| 196 |
+
unit = to_offset(round_freq).nanos
|
| 197 |
+
|
| 198 |
+
# test floor
|
| 199 |
+
result = dt.floor(round_freq)
|
| 200 |
+
diff = dt.asi8 - result.asi8
|
| 201 |
+
mod = result.asi8 % unit
|
| 202 |
+
assert (mod == 0).all(), f"floor not a {round_freq} multiple"
|
| 203 |
+
assert (0 <= diff).all() and (diff < unit).all(), "floor error"
|
| 204 |
+
|
| 205 |
+
# test ceil
|
| 206 |
+
result = dt.ceil(round_freq)
|
| 207 |
+
diff = result.asi8 - dt.asi8
|
| 208 |
+
mod = result.asi8 % unit
|
| 209 |
+
assert (mod == 0).all(), f"ceil not a {round_freq} multiple"
|
| 210 |
+
assert (0 <= diff).all() and (diff < unit).all(), "ceil error"
|
| 211 |
+
|
| 212 |
+
# test round
|
| 213 |
+
result = dt.round(round_freq)
|
| 214 |
+
diff = abs(result.asi8 - dt.asi8)
|
| 215 |
+
mod = result.asi8 % unit
|
| 216 |
+
assert (mod == 0).all(), f"round not a {round_freq} multiple"
|
| 217 |
+
assert (diff <= unit // 2).all(), "round error"
|
| 218 |
+
if unit % 2 == 0:
|
| 219 |
+
assert (result.asi8[diff == unit // 2] % 2 == 0).all(), (
|
| 220 |
+
"round half to even error"
|
| 221 |
+
)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_shift.py
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
import zoneinfo
|
| 3 |
+
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from pandas.errors import NullFrequencyError
|
| 7 |
+
|
| 8 |
+
import pandas as pd
|
| 9 |
+
from pandas import (
|
| 10 |
+
DatetimeIndex,
|
| 11 |
+
Series,
|
| 12 |
+
date_range,
|
| 13 |
+
)
|
| 14 |
+
import pandas._testing as tm
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class TestDatetimeIndexShift:
|
| 18 |
+
# -------------------------------------------------------------
|
| 19 |
+
# DatetimeIndex.shift is used in integer addition
|
| 20 |
+
|
| 21 |
+
def test_dti_shift_tzaware(self, tz_naive_fixture, unit):
|
| 22 |
+
# GH#9903
|
| 23 |
+
tz = tz_naive_fixture
|
| 24 |
+
idx = DatetimeIndex([], name="xxx", tz=tz).as_unit(unit)
|
| 25 |
+
tm.assert_index_equal(idx.shift(0, freq="h"), idx)
|
| 26 |
+
tm.assert_index_equal(idx.shift(3, freq="h"), idx)
|
| 27 |
+
|
| 28 |
+
idx = DatetimeIndex(
|
| 29 |
+
["2011-01-01 10:00", "2011-01-01 11:00", "2011-01-01 12:00"],
|
| 30 |
+
name="xxx",
|
| 31 |
+
tz=tz,
|
| 32 |
+
freq="h",
|
| 33 |
+
).as_unit(unit)
|
| 34 |
+
tm.assert_index_equal(idx.shift(0, freq="h"), idx)
|
| 35 |
+
exp = DatetimeIndex(
|
| 36 |
+
["2011-01-01 13:00", "2011-01-01 14:00", "2011-01-01 15:00"],
|
| 37 |
+
name="xxx",
|
| 38 |
+
tz=tz,
|
| 39 |
+
freq="h",
|
| 40 |
+
).as_unit(unit)
|
| 41 |
+
tm.assert_index_equal(idx.shift(3, freq="h"), exp)
|
| 42 |
+
exp = DatetimeIndex(
|
| 43 |
+
["2011-01-01 07:00", "2011-01-01 08:00", "2011-01-01 09:00"],
|
| 44 |
+
name="xxx",
|
| 45 |
+
tz=tz,
|
| 46 |
+
freq="h",
|
| 47 |
+
).as_unit(unit)
|
| 48 |
+
tm.assert_index_equal(idx.shift(-3, freq="h"), exp)
|
| 49 |
+
|
| 50 |
+
def test_dti_shift_freqs(self, unit):
|
| 51 |
+
# test shift for DatetimeIndex and non DatetimeIndex
|
| 52 |
+
# GH#8083
|
| 53 |
+
drange = date_range("20130101", periods=5, unit=unit)
|
| 54 |
+
result = drange.shift(1)
|
| 55 |
+
expected = DatetimeIndex(
|
| 56 |
+
["2013-01-02", "2013-01-03", "2013-01-04", "2013-01-05", "2013-01-06"],
|
| 57 |
+
dtype=f"M8[{unit}]",
|
| 58 |
+
freq="D",
|
| 59 |
+
)
|
| 60 |
+
tm.assert_index_equal(result, expected)
|
| 61 |
+
|
| 62 |
+
result = drange.shift(-1)
|
| 63 |
+
expected = DatetimeIndex(
|
| 64 |
+
["2012-12-31", "2013-01-01", "2013-01-02", "2013-01-03", "2013-01-04"],
|
| 65 |
+
dtype=f"M8[{unit}]",
|
| 66 |
+
freq="D",
|
| 67 |
+
)
|
| 68 |
+
tm.assert_index_equal(result, expected)
|
| 69 |
+
|
| 70 |
+
result = drange.shift(3, freq="2D")
|
| 71 |
+
expected = DatetimeIndex(
|
| 72 |
+
["2013-01-07", "2013-01-08", "2013-01-09", "2013-01-10", "2013-01-11"],
|
| 73 |
+
dtype=f"M8[{unit}]",
|
| 74 |
+
freq="D",
|
| 75 |
+
)
|
| 76 |
+
tm.assert_index_equal(result, expected)
|
| 77 |
+
|
| 78 |
+
def test_dti_shift_int(self, unit):
|
| 79 |
+
rng = date_range("1/1/2000", periods=20, unit=unit)
|
| 80 |
+
|
| 81 |
+
result = rng + 5 * rng.freq
|
| 82 |
+
expected = rng.shift(5)
|
| 83 |
+
tm.assert_index_equal(result, expected)
|
| 84 |
+
|
| 85 |
+
result = rng - 5 * rng.freq
|
| 86 |
+
expected = rng.shift(-5)
|
| 87 |
+
tm.assert_index_equal(result, expected)
|
| 88 |
+
|
| 89 |
+
def test_dti_shift_no_freq(self, unit):
|
| 90 |
+
# GH#19147
|
| 91 |
+
dti = DatetimeIndex(["2011-01-01 10:00", "2011-01-01"], freq=None).as_unit(unit)
|
| 92 |
+
with pytest.raises(NullFrequencyError, match="Cannot shift with no freq"):
|
| 93 |
+
dti.shift(2)
|
| 94 |
+
|
| 95 |
+
@pytest.mark.parametrize("tzstr", ["US/Eastern", "dateutil/US/Eastern"])
|
| 96 |
+
def test_dti_shift_localized(self, tzstr, unit):
|
| 97 |
+
dr = date_range("2011/1/1", "2012/1/1", freq="W-FRI", unit=unit)
|
| 98 |
+
dr_tz = dr.tz_localize(tzstr)
|
| 99 |
+
|
| 100 |
+
result = dr_tz.shift(1, "10min")
|
| 101 |
+
assert result.tz == dr_tz.tz
|
| 102 |
+
|
| 103 |
+
def test_dti_shift_across_dst(self, unit):
|
| 104 |
+
# GH 8616
|
| 105 |
+
idx = date_range(
|
| 106 |
+
"2013-11-03", tz="America/Chicago", periods=7, freq="h", unit=unit
|
| 107 |
+
)
|
| 108 |
+
ser = Series(index=idx[:-1], dtype=object)
|
| 109 |
+
result = ser.shift(freq="h")
|
| 110 |
+
expected = Series(index=idx[1:], dtype=object)
|
| 111 |
+
tm.assert_series_equal(result, expected)
|
| 112 |
+
|
| 113 |
+
@pytest.mark.parametrize(
|
| 114 |
+
"shift, result_time",
|
| 115 |
+
[
|
| 116 |
+
[0, "2014-11-14 00:00:00"],
|
| 117 |
+
[-1, "2014-11-13 23:00:00"],
|
| 118 |
+
[1, "2014-11-14 01:00:00"],
|
| 119 |
+
],
|
| 120 |
+
)
|
| 121 |
+
def test_dti_shift_near_midnight(self, shift, result_time, unit):
|
| 122 |
+
# GH 8616
|
| 123 |
+
tz = zoneinfo.ZoneInfo("US/Eastern")
|
| 124 |
+
dt_est = datetime(2014, 11, 14, 0, tzinfo=tz)
|
| 125 |
+
idx = DatetimeIndex([dt_est]).as_unit(unit)
|
| 126 |
+
ser = Series(data=[1], index=idx)
|
| 127 |
+
result = ser.shift(shift, freq="h")
|
| 128 |
+
exp_index = DatetimeIndex([result_time], tz=tz).as_unit(unit)
|
| 129 |
+
expected = Series(1, index=exp_index)
|
| 130 |
+
tm.assert_series_equal(result, expected)
|
| 131 |
+
|
| 132 |
+
def test_shift_periods(self, unit):
|
| 133 |
+
# GH#22458 : argument 'n' was deprecated in favor of 'periods'
|
| 134 |
+
idx = date_range(
|
| 135 |
+
start=datetime(2009, 1, 1), end=datetime(2010, 1, 1), periods=3, unit=unit
|
| 136 |
+
)
|
| 137 |
+
tm.assert_index_equal(idx.shift(periods=0), idx)
|
| 138 |
+
tm.assert_index_equal(idx.shift(0), idx)
|
| 139 |
+
|
| 140 |
+
@pytest.mark.parametrize("freq", ["B", "C"])
|
| 141 |
+
def test_shift_bday(self, freq, unit):
|
| 142 |
+
rng = date_range(
|
| 143 |
+
datetime(2009, 1, 1), datetime(2010, 1, 1), freq=freq, unit=unit
|
| 144 |
+
)
|
| 145 |
+
shifted = rng.shift(5)
|
| 146 |
+
assert shifted[0] == rng[5]
|
| 147 |
+
assert shifted.freq == rng.freq
|
| 148 |
+
|
| 149 |
+
shifted = rng.shift(-5)
|
| 150 |
+
assert shifted[5] == rng[0]
|
| 151 |
+
assert shifted.freq == rng.freq
|
| 152 |
+
|
| 153 |
+
shifted = rng.shift(0)
|
| 154 |
+
assert shifted[0] == rng[0]
|
| 155 |
+
assert shifted.freq == rng.freq
|
| 156 |
+
|
| 157 |
+
def test_shift_bmonth(self, performance_warning, unit):
|
| 158 |
+
rng = date_range(
|
| 159 |
+
datetime(2009, 1, 1),
|
| 160 |
+
datetime(2010, 1, 1),
|
| 161 |
+
freq=pd.offsets.BMonthEnd(),
|
| 162 |
+
unit=unit,
|
| 163 |
+
)
|
| 164 |
+
shifted = rng.shift(1, freq=pd.offsets.BDay())
|
| 165 |
+
assert shifted[0] == rng[0] + pd.offsets.BDay()
|
| 166 |
+
|
| 167 |
+
rng = date_range(
|
| 168 |
+
datetime(2009, 1, 1),
|
| 169 |
+
datetime(2010, 1, 1),
|
| 170 |
+
freq=pd.offsets.BMonthEnd(),
|
| 171 |
+
unit=unit,
|
| 172 |
+
)
|
| 173 |
+
with tm.assert_produces_warning(performance_warning):
|
| 174 |
+
shifted = rng.shift(1, freq=pd.offsets.CDay())
|
| 175 |
+
assert shifted[0] == rng[0] + pd.offsets.CDay()
|
| 176 |
+
|
| 177 |
+
def test_shift_empty(self, unit):
|
| 178 |
+
# GH#14811
|
| 179 |
+
dti = date_range(start="2016-10-21", end="2016-10-21", freq="BME", unit=unit)
|
| 180 |
+
result = dti.shift(1)
|
| 181 |
+
tm.assert_index_equal(result, dti)
|
.venv/lib/python3.13/site-packages/pandas/tests/indexes/datetimes/methods/test_snap.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from pandas.errors import Pandas4Warning
|
| 4 |
+
|
| 5 |
+
from pandas import (
|
| 6 |
+
DatetimeIndex,
|
| 7 |
+
date_range,
|
| 8 |
+
)
|
| 9 |
+
import pandas._testing as tm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning")
|
| 13 |
+
@pytest.mark.filterwarnings("ignore:Period with BDay freq:FutureWarning")
|
| 14 |
+
@pytest.mark.parametrize("tz", [None, "Asia/Shanghai", "Europe/Berlin"])
|
| 15 |
+
@pytest.mark.parametrize("name", [None, "my_dti"])
|
| 16 |
+
def test_dti_snap(name, tz, unit):
|
| 17 |
+
dti = DatetimeIndex(
|
| 18 |
+
[
|
| 19 |
+
"1/1/2002",
|
| 20 |
+
"1/2/2002",
|
| 21 |
+
"1/3/2002",
|
| 22 |
+
"1/4/2002",
|
| 23 |
+
"1/5/2002",
|
| 24 |
+
"1/6/2002",
|
| 25 |
+
"1/7/2002",
|
| 26 |
+
],
|
| 27 |
+
name=name,
|
| 28 |
+
tz=tz,
|
| 29 |
+
freq="D",
|
| 30 |
+
)
|
| 31 |
+
dti = dti.as_unit(unit)
|
| 32 |
+
|
| 33 |
+
result = dti.snap(freq="W-MON")
|
| 34 |
+
msg = "'w-mon' is deprecated and will be removed in a future version."
|
| 35 |
+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
|
| 36 |
+
expected = date_range("12/31/2001", "1/7/2002", name=name, tz=tz, freq="w-mon")
|
| 37 |
+
expected = expected.repeat([3, 4])
|
| 38 |
+
expected = expected.as_unit(unit)
|
| 39 |
+
tm.assert_index_equal(result, expected)
|
| 40 |
+
assert result.tz == expected.tz
|
| 41 |
+
assert result.freq is None
|
| 42 |
+
assert expected.freq is None
|
| 43 |
+
|
| 44 |
+
result = dti.snap(freq="B")
|
| 45 |
+
|
| 46 |
+
msg = "'b' is deprecated and will be removed in a future version."
|
| 47 |
+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
|
| 48 |
+
expected = date_range("1/1/2002", "1/7/2002", name=name, tz=tz, freq="b")
|
| 49 |
+
expected = expected.repeat([1, 1, 1, 2, 2])
|
| 50 |
+
expected = expected.as_unit(unit)
|
| 51 |
+
tm.assert_index_equal(result, expected)
|
| 52 |
+
assert result.tz == expected.tz
|
| 53 |
+
assert result.freq is None
|
| 54 |
+
assert expected.freq is None
|