### Install polars-whichlang Source: https://github.com/rmalouf/polars_whichlang/blob/main/README.md Install the library using pip. This is the only setup required. ```bash pip install polars-whichlang ``` -------------------------------- ### Detect Language in LazyFrame Source: https://context7.com/rmalouf/polars_whichlang/llms.txt Apply `detect_lang` to a LazyFrame for efficient processing within Polars' query engine. The syntax is identical to eager DataFrames. ```python # --- Lazy LazyFrame usage --- lazy_result = ( df.lazy() .with_columns(detect_lang("text").alias("lang")) .collect() ) print(lazy_result) ``` -------------------------------- ### detect_lang(text) Source: https://context7.com/rmalouf/polars_whichlang/llms.txt Detects the language of a string column and returns an ISO 639-3 three-letter language code. This function operates element-wise and integrates natively into Polars' execution engine, supporting both eager and lazy APIs. It accepts a column name, a Polars expression, or a Polars Series as input and returns a Polars expression of dtype String. Raises ComputeError if the input column is not of string dtype. ```APIDOC ## detect_lang(text) ### Description Registers and calls the compiled Rust plugin function `detect_lang` on a Polars string expression. The `text` argument accepts a column name (string), a `pl.Expr`, or a `pl.Series`. Returns a `pl.Expr` of dtype `String` containing ISO 639-3 three-letter language codes for each row. The function is element-wise and integrates natively into Polars' execution engine, making it compatible with both eager and lazy APIs. Raises `ComputeError` if the input column is not of string dtype. ### Parameters #### Path Parameters - **text** (string | pl.Expr | pl.Series) - Required - The input column or expression containing strings to analyze. ### Request Example ```python import polars as pl from polars_whichlang import detect_lang df = pl.DataFrame({ "id": [1, 2, 3, 4, 5, 6], "text": [ "This is a test.", # English "Đây là một bài kiểm tra.", # Vietnamese "Dies ist ein Test", # German "这是一个测试", # Mandarin "이건 테스트야", # Korean "Это тест", # Russian ], }) result = df.with_columns( detect_lang("text").alias("lang") ) print(result) # --- Lazy LazyFrame usage --- lazy_result = ( df.lazy() .with_columns(detect_lang("text").alias("lang")) .collect() ) print(lazy_result) # --- Using a Polars expression directly --- result_expr = df.with_columns( detect_lang(pl.col("text")).alias("lang") ) # --- Filtering to only English rows --- english_only = ( df.lazy() .filter(detect_lang("text") == pl.lit("eng")) .collect() ) print(english_only) # --- Error handling: non-string column raises ComputeError --- from polars.exceptions import ComputeError numeric_df = pl.DataFrame({"val": [1, 2, 3]}) try: numeric_df.with_columns(detect_lang("val").alias("lang")) except ComputeError as e: print(f"Error: {e}") # ComputeError: input column must be of type String ``` ### Response #### Success Response (pl.Expr) - **lang** (String) - A Polars expression that, when evaluated, will contain the ISO 639-3 three-letter language code for each input string. ``` -------------------------------- ### Handle Non-String Column Errors Source: https://context7.com/rmalouf/polars_whichlang/llms.txt Attempting to use `detect_lang` on a non-string column will raise a `ComputeError`. Ensure the input column is of string dtype. ```python # --- Error handling: non-string column raises ComputeError --- from polars.exceptions import ComputeError numeric_df = pl.DataFrame({"val": [1, 2, 3]}) try: numeric_df.with_columns(detect_lang("val").alias("lang")) except ComputeError as e: print(f"Error: {e}") # ComputeError: input column must be of type String ``` -------------------------------- ### Detect Language in Eager DataFrame Source: https://context7.com/rmalouf/polars_whichlang/llms.txt Use `detect_lang` within `with_columns` to add a language code column to an eager DataFrame. The input can be a column name or a Polars expression. ```python import polars as pl from polars_whichlang import detect_lang # --- Eager DataFrame usage --- df = pl.DataFrame({ "id": [1, 2, 3, 4, 5, 6], "text": [ "This is a test.", # English "Đây là một bài kiểm tra.", # Vietnamese "Dies ist ein Test", # German "这是一个测试", # Mandarin "이건 테스트야", # Korean "Это тест", # Russian ], }) result = df.with_columns( detect_lang("text").alias("lang") ) print(result) ``` -------------------------------- ### Use Polars Expression with detect_lang Source: https://context7.com/rmalouf/polars_whichlang/llms.txt Pass a Polars column expression directly to `detect_lang` for more complex query constructions. ```python # --- Using a Polars expression directly --- result_expr = df.with_columns( detect_lang(pl.col("text")).alias("lang") ) ``` -------------------------------- ### Filter Rows by Detected Language Source: https://context7.com/rmalouf/polars_whichlang/llms.txt Use `detect_lang` within a `filter` operation to select rows based on their identified language code. ```python # --- Filtering to only English rows --- english_only = ( df.lazy() .filter(detect_lang("text") == pl.lit("eng")) .collect() ) print(english_only) ``` -------------------------------- ### Detect Language in Polars DataFrame Source: https://github.com/rmalouf/polars_whichlang/blob/main/README.md Use the `detect_lang` function from `polars_whichlang` to add a language detection column to a Polars DataFrame. Ensure the text column is of string type. ```python import polars as pl from polars_whichlang import detect_lang df = pl.DataFrame( { "index": [1, 2, 3, 4], "text": [ "This is a test. ", "Đây là một bài kiểm tra.", "Dies ist ein Test", "这是一个测试" ], } ) df.with_columns(detect_lang('text').alias('lang')) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.