{"record":{"id":"2a6eb22a481dc9d1","repo":"pola-rs/polars","slug":"cannot-initialize-series-from-dataframe-without-an","errorCode":null,"errorMessage":"cannot initialize Series from DataFrame without any columns","messagePattern":"cannot initialize Series from DataFrame without any columns","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/construction/series.py","lineNumber":571,"sourceCode":"\ndef dataframe_to_pyseries(\n    name: str | None,\n    values: DataFrame,\n    *,\n    dtype: PolarsDataType | None = None,\n    strict: bool = True,\n) -> PySeries:\n    \"\"\"Construct a new PySeries from a Polars DataFrame.\"\"\"\n    if values.width > 1:\n        name = name or \"\"\n        s = values.to_struct(name)\n    elif values.width == 1:\n        s = values.to_series()\n        if name is not None:\n            s = s.alias(name)\n    else:\n        msg = \"cannot initialize Series from DataFrame without any columns\"\n        raise TypeError(msg)\n\n    if dtype is not None and dtype != s.dtype:\n        s = s.cast(dtype, strict=strict)\n\n    return s._s\n","sourceCodeStart":553,"sourceCodeEnd":577,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_utils/construction/series.py#L553-L577","documentation":"dataframe_to_pyseries turns a polars DataFrame into a Series by making a struct (width > 1) or taking the only column (width == 1). A DataFrame with zero columns has nothing to convert, so pl.Series(df) raises this TypeError instead of returning an undefined Series.","triggerScenarios":"pl.Series(pl.DataFrame()); pl.Series(df.select([])); pl.Series(df.drop(df.columns)); dynamic selections like df.select(pl.all().exclude(pattern)) that exclude everything.","commonSituations":"Config-driven column selection where the exclusion pattern matches all columns; parsing/filtering steps earlier in the pipeline silently produced an empty frame; generic code doing pl.Series(sub_df) on user-supplied column sets.","solutions":["Guard before converting: if df.width == 0: create an explicit empty Series instead.","Fix the selection so at least one column remains (check df.columns before/after select).","For a known dtype, fall back to pl.Series(name, [], dtype=...)."],"exampleFix":"// before\ns = pl.Series(df.select(pl.all().exclude(\"tmp_*\")))  # all columns excluded\n\n// after\nsub = df.select(pl.all().exclude(\"tmp_*\"))\ns = pl.Series(sub) if sub.width else pl.Series(\"struct\", [], dtype=pl.Null)","handlingStrategy":"validation","validationCode":"if df.width == 0:\n    raise ValueError(\"cannot build Series: DataFrame has no columns\")\ns = pl.Series(df)","typeGuard":"def has_columns(df: pl.DataFrame) -> bool:\n    return df.width > 0","tryCatchPattern":"try:\n    s = pl.Series(df)\nexcept TypeError as e:\n    if \"without any columns\" in str(e):\n        s = pl.Series(name or \"\", [], dtype=pl.Null)  # explicit empty fallback\n    else:\n        raise","preventionTips":["Check df.width after dynamic select/exclude chains.","Validate config-driven column lists are non-empty before selection.","Log df.columns when a pipeline stage produces an empty frame."],"tags":["dataframe","series","empty","width"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}