dotnet/machinelearning · error · ArgumentNullException

Value cannot be null. (Parameter 'idColumns')

Error message

Value cannot be null. (Parameter 'idColumns')

What it means

Thrown by DataFrame.Melt when the idColumns argument is null. Melt requires at least one identifier column to keep per-row during the wide-to-long reshape. Raised as a standard ArgumentNullException naming 'idColumns'.

Source

Thrown at src/Microsoft.Data.Analysis/DataFrame.cs:753

        /// Note: The output rows are ordered by value column (all rows for the first value column,
        /// then all rows for the second, etc.), which differs from pandas.melt() which orders by
        /// source row.
        /// </remarks>
        public DataFrame Melt(IEnumerable<string> idColumns, IEnumerable<string> valueColumns = null, string variableName = "variable", string valueName = "value", bool dropNulls = false)
        {
            if (string.IsNullOrWhiteSpace(variableName))
            {
                throw new ArgumentException(Strings.ParameterMustNotBeNullOrWhitespace, nameof(variableName));
            }

            if (string.IsNullOrWhiteSpace(valueName))
            {
                throw new ArgumentException(Strings.ParameterMustNotBeNullOrWhitespace, nameof(valueName));
            }

            if (idColumns == null)
            {
                throw new ArgumentNullException(nameof(idColumns));
            }

            var idColumnList = idColumns.ToList();

            HashSet<string> idColumnSet = null;

            if (valueColumns is null)
            {
                idColumnSet = [.. idColumnList];
            }

            var valueColumnList = valueColumns?.ToList()
                ?? _columnCollection
                    .Where(c => !idColumnSet.Contains(c.Name))
                    .Select(c => c.Name)
                    .ToList();

            if (idColumnList.Count == 0)

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Pass a non-empty collection of existing ID column names, e.g. new[] { "Id" }
  2. Check the source collection for null before calling Melt
  3. Use an empty-list default and rely on the Count==0 validation instead of null

Example fix

// before
df.Melt(null, values);
// after
df.Melt(new[] { "Id", "Date" }, values);
Defensive patterns

Strategy: validation

Validate before calling

if (idColumns == null || !idColumns.Any()) throw new InvalidOperationException("idColumns must contain at least one column");

Try / catch

try { df.Melt(idColumns, valueColumns); }
catch (ArgumentNullException ex) when (ex.ParamName == "idColumns") { /* provide defaults and retry */ }

Prevention

When it happens

Trigger: Calling df.Melt(null, valueColumns) explicitly, or a variable that is null being passed as idColumns.

Common situations: Piping a nullable collection from config or a previous query that returned null; overloading the default parameter to null then calling without a real list.

Related errors


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