dotnet/machinelearning · error · ArgumentException

Strings.InvalidColumnName (formatted with columnName)

Error message

Strings.InvalidColumnName (formatted with columnName)

What it means

The DataFrameColumnCollection indexer by name throws ArgumentException(Strings.InvalidColumnName, columnName) when no column with that name exists (IndexOf returns -1). It is the getter for collection[columnName].

Source

Thrown at src/Microsoft.Data.Analysis/DataFrameColumnCollection.cs:188

            //reset RowCount as DataFrame is now empty
            RowCount = 0;
        }

        /// <summary>
        /// An indexer based on <see cref="DataFrameColumn.Name"/>
        /// </summary>
        /// <param name="columnName">The name of a <see cref="DataFrameColumn"/></param>
        /// <returns>A <see cref="DataFrameColumn"/> if it exists.</returns>
        /// <exception cref="ArgumentException">Throws if <paramref name="columnName"/> is not present in this <see cref="DataFrame"/></exception>
        public DataFrameColumn this[string columnName]
        {
            get
            {
                int columnIndex = IndexOf(columnName);
                if (columnIndex == -1)
                {
                    throw new ArgumentException(String.Format(Strings.InvalidColumnName, columnName), nameof(columnName));
                }
                return this[columnIndex];
            }
            set
            {
                int columnIndex = IndexOf(columnName);
                DataFrameColumn newColumn = value;
                newColumn.SetName(columnName);
                if (columnIndex == -1)
                {
                    Insert(Count, newColumn);
                }
                else
                {
                    this[columnIndex] = newColumn;
                }
            }
        }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Verify the exact name with df.Columns.Contains(name) before indexing.
  2. List available names (foreach over df.Columns) to spot typos.
  3. Use TryGetColumnIndex or IndexOf and handle the miss gracefully.

Example fix

// before
var col = df.Columns["Sals"]; // typo
// after
if (df.Columns.Contains("Sales"))
    var col = df.Columns["Sales"];
Defensive patterns

Strategy: validation

Validate before calling

if (!df.Columns.Contains("Sales"))
    throw new InvalidOperationException("Missing expected column 'Sales'");
var col = df.Columns["Sales"];

Type guard

bool TryGetColumn(DataFrame df, string name, out DataFrameColumn col)
{
    var i = df.Columns.IndexOf(name);
    col = i >= 0 ? df.Columns[i] : null;
    return i >= 0;
}

Try / catch

try
{
    var col = df.Columns[name];
}
catch (ArgumentException ex) when (ex.Message.Contains("does not exist") || ex.ParamName == "columnName")
{
    // handle missing column
}

Prevention

When it happens

Trigger: Reading df.Columns["colName"] (or the setter) with a name not present in the collection.

Common situations: Typos or casing mismatches in column names, schema changes between file versions (renamed CSV/parquet headers), column dropped in an earlier pipeline step.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/d5f50e797a6826ab. Report an issue: GitHub.