/
githubmirror
/
spark
Обзор
Документация
Войти
/
githubmirror
/
spark
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
python/pyspark/pandas/tests/indexes/test_basic.py
248 строк
9 KB
tonghuaroot (童话)
[SPARK-57314][PS][TEST] Add tests for Index.equals in pandas-on-Spark
09 июн 2026, 03:54
09 июн 2026, 03:54
0993d43
Код
Авторство
О чём код?
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from datetime import datetime import numpy as np import pandas as pd import pyspark.pandas as ps from pyspark.loose_version import LooseVersion from pyspark.pandas.config import option_context from pyspark.pandas.exceptions import PandasNotImplementedError from pyspark.testing.pandasutils import PandasOnSparkTestCase, TestUtils class IndexBasicMixin: @property def pdf(self): return pd.DataFrame( {"a": [1, 2, 3, 4, 5, 6, 7, 8, 9], "b": [4, 5, 6, 3, 2, 1, 0, 0, 0]}, index=[0, 1, 3, 5, 6, 8, 9, 9, 9], ) @property def psdf(self): return ps.from_pandas(self.pdf) def test_index_basic(self): for pdf in [ pd.DataFrame(np.random.randn(10, 5), index=np.random.randint(100, size=10)), pd.DataFrame( np.random.randn(10, 5), index=np.random.randint(100, size=10).astype(np.int32) ), pd.DataFrame(np.random.randn(10, 5), index=np.random.randn(10)), pd.DataFrame(np.random.randn(10, 5), index=np.random.randn(10).astype(np.float32)), pd.DataFrame(np.random.randn(10, 5), index=list("abcdefghij")), pd.DataFrame( np.random.randn(10, 5), index=pd.date_range("2011-01-01", freq="D", periods=10) ), pd.DataFrame(np.random.randn(10, 5), index=pd.Categorical(list("abcdefghij"))), pd.DataFrame(np.random.randn(10, 5), columns=list("abcde")).set_index(["a", "b"]), ]: psdf = ps.from_pandas(pdf) self.assert_eq(psdf.index, pdf.index) self.assert_eq(psdf.index.dtype, pdf.index.dtype) self.assert_eq(ps.Index([])._summary(), "Index: 0 entries") with self.assertRaisesRegex(ValueError, "The truth value of a Index is ambiguous."): bool(ps.Index([1])) with self.assertRaisesRegex(TypeError, "Index.name must be a hashable type"): ps.Index([1, 2, 3], name=[(1, 2, 3)]) with self.assertRaisesRegex(TypeError, "Index.name must be a hashable type"): ps.Index([1.0, 2.0, 3.0], name=[(1, 2, 3)]) def test_multi_index_copy(self): arrays = [[1, 1, 2, 2], ["red", "blue", "red", "blue"]] idx = pd.MultiIndex.from_arrays(arrays, names=("number", "color")) pdf = pd.DataFrame(np.random.randn(4, 5), idx) psdf = ps.from_pandas(pdf) self.assert_eq(psdf.index.copy(), pdf.index.copy()) def test_holds_integer(self): def check_holds_integer(pidx): psidx = ps.from_pandas(pidx) if LooseVersion(pd.__version__) < "3.0.0": self.assert_eq(pidx.holds_integer(), psidx.holds_integer()) else: with self.assertRaises(AttributeError): psidx.holds_integer() check_holds_integer(pd.Index([1, 2, 3, 4])) check_holds_integer(pd.Index([1.1, 2.2, 3.3, 4.4])) check_holds_integer(pd.Index(["A", "B", "C", "D"])) check_holds_integer(pd.MultiIndex.from_tuples([("x", "a"), ("x", "b"), ("y", "a")])) check_holds_integer(pd.MultiIndex.from_tuples([(10, 1), (10, 2), (20, 1)])) def test_item(self): pidx = pd.Index([10]) psidx = ps.from_pandas(pidx) self.assert_eq(pidx.item(), psidx.item()) # with timestamp pidx = pd.Index([datetime(1990, 3, 9)]) psidx = ps.from_pandas(pidx) self.assert_eq(pidx.item(), psidx.item()) # MultiIndex pmidx = pd.MultiIndex.from_tuples([("a", "x")]) psmidx = ps.from_pandas(pmidx) self.assert_eq(pmidx.item(), psmidx.item()) # MultiIndex with timestamp pmidx = pd.MultiIndex.from_tuples([(datetime(1990, 3, 9), datetime(2019, 8, 15))]) psmidx = ps.from_pandas(pmidx) self.assert_eq(pidx.item(), psidx.item()) err_msg = "can only convert an array of size 1 to a Python scalar" with self.assertRaisesRegex(ValueError, err_msg): ps.Index([10, 20]).item() with self.assertRaisesRegex(ValueError, err_msg): ps.MultiIndex.from_tuples([("a", "x"), ("b", "y")]).item() def test_inferred_type(self): # Integer pidx = pd.Index([1, 2, 3]) psidx = ps.from_pandas(pidx) self.assert_eq(pidx.inferred_type, psidx.inferred_type) # Floating pidx = pd.Index([1.0, 2.0, 3.0]) psidx = ps.from_pandas(pidx) self.assert_eq(pidx.inferred_type, psidx.inferred_type) # String pidx = pd.Index(["a", "b", "c"]) psidx = ps.from_pandas(pidx) self.assert_eq(pidx.inferred_type, psidx.inferred_type) # Boolean pidx = pd.Index([True, False, True, False]) psidx = ps.from_pandas(pidx) self.assert_eq(pidx.inferred_type, psidx.inferred_type) # MultiIndex pmidx = pd.MultiIndex.from_tuples([("a", "x")]) psmidx = ps.from_pandas(pmidx) self.assert_eq(pmidx.inferred_type, psmidx.inferred_type) def test_view(self): pidx = pd.Index([1, 2, 3, 4], name="Koalas") psidx = ps.from_pandas(pidx) self.assert_eq(pidx.view(), psidx.view()) # MultiIndex pmidx = pd.MultiIndex.from_tuples([("a", "x"), ("b", "y"), ("c", "z")]) psmidx = ps.from_pandas(pmidx) self.assert_eq(pmidx.view(), psmidx.view()) def test_index_ops(self): pidx = pd.Index([1, 2, 3, 4, 5]) psidx = ps.from_pandas(pidx) self.assert_eq(psidx * 100 + psidx * 10 + psidx, pidx * 100 + pidx * 10 + pidx) pidx = pd.Index([1, 2, 3, 4, 5], name="a") psidx = ps.from_pandas(pidx) self.assert_eq(psidx * 100 + psidx * 10 + psidx, pidx * 100 + pidx * 10 + pidx) pdf = pd.DataFrame( index=pd.MultiIndex.from_tuples([(1, 2), (3, 4), (5, 6)], names=["a", "b"]) ) psdf = ps.from_pandas(pdf) pidx1 = pdf.index.get_level_values(0) pidx2 = pdf.index.get_level_values(1) psidx1 = psdf.index.get_level_values(0) psidx2 = psdf.index.get_level_values(1) self.assert_eq(psidx1 * 10 + psidx2, pidx1 * 10 + pidx2) def test_factorize(self): pidx = pd.Index(["a", "b", "a", "b"]) psidx = ps.from_pandas(pidx) pcodes, puniques = pidx.factorize(sort=True) kcodes, kuniques = psidx.factorize() self.assert_eq(pcodes.tolist(), kcodes.to_list()) self.assert_eq(puniques, kuniques) pmidx = pd.MultiIndex.from_tuples([("x", "a"), ("x", "b"), ("y", "c")]) psmidx = ps.from_pandas(pmidx) self.assertRaises(PandasNotImplementedError, lambda: psmidx.factorize()) def test_equals(self): # Single Index pidx = pd.Index(["a", "b", "c"]) psidx = ps.from_pandas(pidx) self.assert_eq(pidx.equals(pidx), psidx.equals(psidx)) with option_context("compute.ops_on_diff_frames", True): self.assert_eq( pidx.equals(pd.Index(["a", "b", "c"])), psidx.equals(ps.Index(["a", "b", "c"])), ) self.assert_eq( pidx.equals(pd.Index(["b", "b", "a"])), psidx.equals(ps.Index(["b", "b", "a"])), ) # equals ignores the name (unlike identical) self.assert_eq( pd.Index([1, 2, 3], name="x").equals(pd.Index([1, 2, 3], name="y")), ps.Index([1, 2, 3], name="x").equals(ps.Index([1, 2, 3], name="y")), ) # MultiIndex pmidx = pd.MultiIndex.from_tuples([("a", "x"), ("b", "y"), ("c", "z")]) psmidx = ps.from_pandas(pmidx) self.assert_eq(pmidx.equals(pmidx), psmidx.equals(psmidx)) with option_context("compute.ops_on_diff_frames", True): self.assert_eq( pmidx.equals(pd.MultiIndex.from_tuples([("a", "x"), ("b", "y"), ("c", "z")])), psmidx.equals(ps.MultiIndex.from_tuples([("a", "x"), ("b", "y"), ("c", "z")])), ) self.assert_eq( pmidx.equals(pd.MultiIndex.from_tuples([("c", "z"), ("b", "y"), ("a", "x")])), psmidx.equals(ps.MultiIndex.from_tuples([("c", "z"), ("b", "y"), ("a", "x")])), ) # Index vs MultiIndex (different type) -> not equal self.assert_eq(pidx.equals(pmidx), psidx.equals(psmidx)) class IndexBasicTests( IndexBasicMixin, PandasOnSparkTestCase, TestUtils, ): pass if __name__ == "__main__": from pyspark.testing import main main()