|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from typing import Callable |
| 4 | + |
| 5 | +import pyarrow as pa |
3 | 6 | import pytest |
4 | 7 |
|
5 | 8 | import narwhals.stable.v1 as nw |
@@ -71,3 +74,37 @@ def test_concat_str_with_lit(constructor: Constructor) -> None: |
71 | 74 | result = df.with_columns(b=nw.concat_str("a", nw.lit("ab"))) |
72 | 75 | expected = {"a": ["cat", "dog", "pig"], "b": ["catab", "dogab", "pigab"]} |
73 | 76 | assert_equal_data(result, expected) |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize( |
| 80 | + ("input_schema", "input_values", "expected_function"), |
| 81 | + [ |
| 82 | + ( |
| 83 | + [("store", pa.large_string()), ("item", pa.large_string())], |
| 84 | + ["a", "b"], |
| 85 | + pa.types.is_large_string, |
| 86 | + ), |
| 87 | + ( |
| 88 | + [("store", pa.large_string()), ("item", pa.int32())], |
| 89 | + [0, 1], |
| 90 | + pa.types.is_large_string, |
| 91 | + ), |
| 92 | + ([("store", pa.string()), ("item", pa.int32())], [0, 1], pa.types.is_string), |
| 93 | + ([("store", pa.string()), ("item", pa.string())], ["a", "b"], pa.types.is_string), |
| 94 | + ], |
| 95 | +) |
| 96 | +def test_pyarrow_string_type( |
| 97 | + input_schema: list[tuple[str, pa.DataType]], |
| 98 | + input_values: list[object], |
| 99 | + expected_function: Callable[[pa.DataType], bool], |
| 100 | +) -> None: |
| 101 | + df = pa.table( |
| 102 | + {"store": ["foo", "bar"], "item": input_values}, schema=pa.schema(input_schema) |
| 103 | + ) |
| 104 | + result = ( |
| 105 | + nw.from_native(df) |
| 106 | + .with_columns(store_item=nw.concat_str("store", "item", separator="-")) |
| 107 | + .to_native() |
| 108 | + .schema |
| 109 | + ) |
| 110 | + assert expected_function(result.field("store_item").type) |
0 commit comments