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.
Afaik, pandas to this day lacks the ability to directly construct a
DataFramewith a specific schema. (pandas-dev/pandas#4464)The most direct way I know of to get a
DataFrameof a desired schema, given we already have a raw data object, is:This works at runtime but flags with type-checking, because
schemais notSequenceNotStr, requiring us to use a runtime-redundantlist(schema)instead.Proposal: introduce a
CollectionNotStr, which would match dicts likeschemaand use that instead ofSequnceNotStrwhere appropriate.This affects most methods that accept a
columns=...parameter.