{"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":578,"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":560,"sourceCodeEnd":584,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/construction/series.py#L560-L584","documentation":"`dataframe_to_pyseries` converts a Polars DataFrame to a single Series only when it has exactly one column (width == 1). If the DataFrame has zero or multiple columns, conversion is impossible and TypeError is raised (message says 'without any columns' but it triggers for width != 1 cases handled here).","triggerScenarios":"pl.Series(df) or pl.Series(name, df) where df.width != 1 — notably an empty DataFrame with no columns, or a multi-column DataFrame.","commonSituations":"Selecting no columns from a DataFrame (df.select() yields width 0) then wrapping in Series; accidentally passing a whole multi-column DataFrame where a single column was intended.","solutions":["Select exactly one column before converting: pl.Series(df['col']) or df.to_series().","Check df.width before converting and handle width 0 / width > 1 explicitly.","If multiple columns are needed, keep it as a DataFrame rather than a Series."],"exampleFix":"// before\ns = pl.Series(df.select())  # empty DataFrame\n// after\nif df.width == 1:\n    s = pl.Series(df)\nelse:\n    raise ValueError(f'expected 1 column, got {df.width}')","handlingStrategy":"validation","validationCode":"if not isinstance(values, pl.Series) and values.width != 1:\n    raise ValueError(f'need exactly 1 column to build a Series, got {values.width}')","typeGuard":null,"tryCatchPattern":"try:\n    s = pl.Series(df)\nexcept TypeError as e:\n    if 'without any columns' in str(e):\n        s = df.to_series()  # or select a specific column\n    else:\n        raise","preventionTips":["Check df.width == 1 before DataFrame->Series conversion.","Convert via pl.Series(df['col']) to be explicit about which column.","Guard df.select() results; an empty selection yields width 0."],"tags":["polars","series","dataframe","conversion"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}