withCategoric method

DataFrame withCategoric({
  1. required String name,
  2. required CategoricColumn column,
  3. bool ignoreLengthMismatch = false,
})

A new data frame with a categoric column added.

Example:

final d = """
  a
  1
  2
  3
""".parseAsCsv();
print(d);
print(d.withCategoric(
  name: "b",
  column: CategoricColumn(["red", "green", "blue"]),
));

.-.
|a|
:-:
|1|
|2|
|3|
'-'

.-.-------.
|a|b      |
:-+-------:
|1|red    |
|2|green  |
|3|blue   |
'-'-------'

Implementation

DataFrame withCategoric({
  required String name,
  required CategoricColumn column,
  bool ignoreLengthMismatch = false,
}) {
  if (!_columnNameOkay(name)) {
    throw PackhorseError.badColumnName(name);
  }
  if (!ignoreLengthMismatch && column.length != rowNumber) {
    throw PackhorseError.badStructure(
        "Adding column of length ${column.length} to data frame"
        "with $rowNumber rows.");
  }
  return copy..categoricColumns[name] = column;
}