Skip to content

DuplicateColumnError(column_name, original_names=None, advice=None) Source

Bases: NumdaplotError

Raised when a column name appears twice.

For example in a file header, in a list of columns, when renaming or adding a column, or when two names of a file become the same once their spaces and line breaks are removed.

Added in version 0.1.0

Changed in version 0.2.0

Takes an advice, such as giving each Calculation its own result_name.

Parameters:

  • column_name (str)

    The repeated name.

  • original_names (list[str] | None, default: None)

    The two names, as written in the file, that both become column_name once cleaned. When omitted, the name itself appears twice.

  • advice (str | None, default: None)

    A sentence added at the end of the message, telling how to avoid the repeated name, no advice when omitted.

Attributes:

  • column_name

    The repeated name.

  • original_names

    The two names as written before cleaning, None when the name itself appears twice.

  • advice

    The sentence added at the end of the message, None without advice.

Examples:

import tempfile
from pathlib import Path
import numdaplot as ndp

try:
    ndp.ColumnChoice(columns=["price", "price"])
except ndp.DuplicateColumnError as error:
    print(error)
# The column name 'price' appears more than once.
path = Path(tempfile.mkdtemp()) / "sales.csv"
_ = path.write_text("id,id \n1,2\n")
try:
    ndp.Table.from_csv(path)
except ndp.DuplicateColumnError as error:
    print(error)
# The column names 'id' and 'id ' both become 'id' once cleaned. Use ColumnNames.KEPT to keep them as written.
first_mean = ndp.Calculation(column="price", function=ndp.Mean())
second_mean = ndp.Calculation(column="price", function=ndp.Mean())
try:
    ndp.Aggregation(calculations=[first_mean, second_mean])
except ndp.DuplicateColumnError as error:
    print(error)
# The column name 'price_mean' appears more than once. Give each Calculation its own result_name.