code: def __init__(
      self,
      df: pd.DataFrame,
      sample_size: int = 1000,
      batch_size: int = 8096,
  ):
      fields = []
      casts = {}
      sample_stride = max(1, floor(len(df) / sample_size))
  
      # Shallow copy and add named index levels as columns
      # (this is faster that df.reset_index, which seems to perform a deep copy)
      df = df.copy(deep=False)
      for i, index_name in enumerate(getattr(df.index, "names", [])):
          if isinstance(index_name, str):
              df[index_name] = df.index.get_level_values(i)
      df.reset_index(drop=True, inplace=True)
  
      for col, pd_type in df.dtypes.items():
          if not isinstance(col, str):
              continue
          try:
              # We will expand categoricals (not yet supported in VegaFusion)
              if isinstance(pd_type, pd.CategoricalDtype):
                  cat = df[col].cat
                  field = pa.Schema.from_pandas(
                      pd.DataFrame({col: cat.categories})
                  ).field(col)
              else:
                  candidate_schema = pa.Schema.from_pandas(
                      df.iloc[::sample_stride][[col]]
                  )
                  field = candidate_schema.field(col)
  
              # Convert Decimal columns to float
              if isinstance(
                  field.type, (pa.Decimal128Type, pa.Decimal256Type)
              ):
                  field = pa.field(col, pa.float64())
                  casts[col] = "float64"
  
              # Get inferred pyarrow type
              fields.append(field)
  
          except pa.ArrowTypeError:
              if pd_type.kind == "O":
                  fields.append(pa.field(col, pa.string()))
                  casts[col] = str
              else:
                  raise
      self._df = df
      self._schema = pa.schema(fields)
      self._casts = casts
      self._batch_size = batch_size
  
file_path: vegafusion/python/vegafusion/vegafusion/datasource/pandas_datasource.py
imports: from typing import Iterable
  from math import floor
  import pandas
  import pyarrow
  from .datasource import Datasource
label_desc: Use image node_id 1 to create a new PandasDatasource object from inherited base classes: Datasource with example: obj = PandasDatasource(df, sample_size, batch_size)