Path: blob/main/py-polars/tests/unit/operations/namespaces/test_name.py
6940 views
from __future__ import annotations12from collections import OrderedDict34import polars as pl5from polars.testing import assert_frame_equal678def test_name_change_case() -> None:9df = pl.DataFrame(10schema={"ColX": pl.Int32, "ColY": pl.String},11).with_columns(12pl.all().name.to_uppercase(),13pl.all().name.to_lowercase(),14)15assert df.schema == OrderedDict(16[17("ColX", pl.Int32),18("ColY", pl.String),19("COLX", pl.Int32),20("COLY", pl.String),21("colx", pl.Int32),22("coly", pl.String),23]24)252627def test_name_prefix_suffix() -> None:28df = pl.DataFrame(29schema={"ColX": pl.Int32, "ColY": pl.String},30).with_columns(31pl.all().name.prefix("#"),32pl.all().name.suffix("!!"),33)34assert df.schema == OrderedDict(35[36("ColX", pl.Int32),37("ColY", pl.String),38("#ColX", pl.Int32),39("#ColY", pl.String),40("ColX!!", pl.Int32),41("ColY!!", pl.String),42]43)444546def test_name_update_all() -> None:47df = pl.DataFrame(48schema={49"col1": pl.UInt32,50"col2": pl.Float64,51"other": pl.UInt64,52}53)54assert (55df.select(56pl.col("col2").append(pl.col("other")),57pl.col("col1").append(pl.col("other")).name.keep(),58pl.col("col1").append(pl.col("other")).name.prefix("prefix_"),59pl.col("col1").append(pl.col("other")).name.suffix("_suffix"),60)61).schema == OrderedDict(62[63("col2", pl.Float64),64("col1", pl.UInt64),65("prefix_col1", pl.UInt64),66("col1_suffix", pl.UInt64),67]68)697071def test_name_map_chain_21164() -> None:72df = pl.DataFrame({"MyCol": [0, 1, 2]})73assert_frame_equal(74df.select(pl.all().name.to_lowercase().name.suffix("_suffix")),75df.select(mycol_suffix=pl.col("MyCol")),76)777879def test_when_then_keep_map_13858() -> None:80df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})8182assert_frame_equal(83df.with_columns(84pl.when(True)85.then(pl.int_range(3))86.otherwise(pl.all())87.name.keep()88.name.suffix("_other")89),90df.with_columns(a_other=pl.int_range(3), b_other=pl.int_range(3)),91)929394def test_keep_name_struct_field_23669() -> None:95df = pl.DataFrame(96[97pl.Series("foo", [{"x": 1}], pl.Struct({"x": pl.Int64})),98pl.Series("bar", [{"x": 2}], pl.Struct({"x": pl.Int64})),99]100)101assert_frame_equal(102df.select(pl.all().struct.field("x").name.keep()),103pl.DataFrame(104[105pl.Series("foo", [1], pl.Int64),106pl.Series("bar", [2], pl.Int64),107]108),109)110111112