code: def test_label_encoder():
      """Tests basic LabelEncoder functionality."""
      col_a = ["red", "green", "blue", "red"]
      col_b = ["warm", "cold", "cold", "hot"]
      col_c = [1, 2, 3, 4]
      in_df = pd.DataFrame.from_dict(
          {"A": col_a, "B": col_b, "C": col_c}
      )
      ds = ray.data.from_pandas(in_df)
  
      encoder = LabelEncoder("A")
  
      # Transform with unfitted preprocessor.
      with pytest.raises(PreprocessorNotFittedException):
          encoder.transform(ds)
  
      # Fit data.
      encoder.fit(ds)
  
      assert encoder.stats_ == {
          "unique_values(A)": {"blue": 0, "green": 1, "red": 2}
      }
  
      # Transform data.
      transformed = encoder.transform(ds)
      out_df = transformed.to_pandas()
  
      processed_col_a = [2, 1, 0, 2]
      processed_col_b = col_b
      processed_col_c = col_c
      expected_df = pd.DataFrame.from_dict(
          {
              "A": processed_col_a,
              "B": processed_col_b,
              "C": processed_col_c,
          }
      )
      assert out_df.equals(expected_df)
  
      # Inverse transform data.
      inverse_transformed = encoder.inverse_transform(transformed)
      inverse_df = inverse_transformed.to_pandas()
  
      assert inverse_df.equals(in_df)
  
      # Inverse transform without fitting.
      new_encoder = LabelEncoder("A")
  
      with pytest.raises(RuntimeError):
          new_encoder.inverse_transform(ds)
  
      # Inverse transform on fitted preprocessor that hasn't transformed anything.
      new_encoder.fit(ds)
      inv_non_fitted = new_encoder.inverse_transform(transformed)
      inv_non_fitted_df = inv_non_fitted.to_pandas()
  
      assert inv_non_fitted_df.equals(in_df)
  
      # Transform batch.
      pred_col_a = ["blue", "red", "yellow"]
      pred_col_b = ["cold", "unknown", None]
      pred_col_c = [10, 20, None]
      pred_in_df = pd.DataFrame.from_dict(
          {"A": pred_col_a, "B": pred_col_b, "C": pred_col_c}
      )
  
      pred_out_df = encoder.transform_batch(pred_in_df)
  
      pred_processed_col_a = [0, 2, None]
      pred_processed_col_b = pred_col_b
      pred_processed_col_c = pred_col_c
      pred_expected_df = pd.DataFrame.from_dict(
          {
              "A": pred_processed_col_a,
              "B": pred_processed_col_b,
              "C": pred_processed_col_c,
          }
      )
      assert pred_out_df.equals(pred_expected_df)
  
      # Test null behavior.
      null_col = [1, None]
      nonnull_col = [1, 1]
      null_df = pd.DataFrame.from_dict({"A": null_col})
      null_ds = ray.data.from_pandas(null_df)
      nonnull_df = pd.DataFrame.from_dict({"A": nonnull_col})
      nonnull_ds = ray.data.from_pandas(nonnull_df)
      null_encoder = LabelEncoder("A")
  
      # Verify fit fails for null values.
      with pytest.raises(ValueError):
          null_encoder.fit(null_ds)
      null_encoder.fit(nonnull_ds)
  
      # Verify transform fails for null values.
      with pytest.raises(ValueError):
          null_encoder.transform(null_ds).materialize()
      null_encoder.transform(nonnull_ds)
  
      # Verify transform_batch fails for null values.
      with pytest.raises(ValueError):
          null_encoder.transform_batch(null_df)
      null_encoder.transform_batch(nonnull_df)
  
file_path: ray/python/ray/data/tests/preprocessors/test_encoder.py
imports: import numpy
  import pandas
  import pytest
  import ray
  from ray.data.preprocessor import PreprocessorNotFittedException
  from ray.data.preprocessors import Categorizer, LabelEncoder, MultiHotEncoder, OneHotEncoder, OrdinalEncoder
label_desc: Use image node_id 7 for calling a global function with example usage: test_label_encoder() without return types