Skip to content

Consider allowing a CollectionNotStr rather than SequenceNotStr for column spec. #1876

Description

@randolf-scholz

Afaik, pandas to this day lacks the ability to directly construct a DataFrame with a specific schema. (pandas-dev/pandas#4464)

The most direct way I know of to get a DataFrame of a desired schema, given we already have a raw data object, is:

import pandas as pd
data = [(1, 1.2, "a"), (2, 2.3, "b")]
schema = {"id": int, "value": float, "name": str}
df = pd.DataFrame(data, columns=schema).astype(schema)

This works at runtime but flags with type-checking, because schema is not SequenceNotStr, requiring us to use a runtime-redundant list(schema) instead.

Proposal: introduce a CollectionNotStr, which would match dicts like schema and use that instead of SequnceNotStr where appropriate.

from typing import Protocol, Iterator


class CollectionNotStr[T](Protocol):
    def __iter__(self) -> Iterator[T]: ...
    def __len__(self) -> int: ...
    def __contains__(self, value: object, /) -> bool: ...
    
x: CollectionNotStr = {"id": int, "value": float}  # OK
y: CollectionNotStr = "sas"  # ERROR

This affects most methods that accept a columns=... parameter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions