Skip to content

Commit

Permalink
Add DataFrame.schema (#1992)
Browse files Browse the repository at this point in the history
  • Loading branch information
zundertj committed Dec 5, 2021
1 parent 334643c commit ae2aad5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions py-polars/polars/internals/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1561,9 +1561,32 @@ def dtypes(self) -> tp.List[Type[DataType]]:
│ 3 ┆ 8 ┆ c │
└─────┴─────┴─────┘
See Also
--------
schema : Return a dict of [column name, dtype]
"""
return [DTYPES[idx] for idx in self._df.dtypes()]

@property
def schema(self) -> Dict[str, Type[DataType]]:
"""
Get a dict[column name, DataType]
Examples
--------
>>> df = pl.DataFrame(
... {
... "foo": [1, 2, 3],
... "bar": [6.0, 7.0, 8.0],
... "ham": ["a", "b", "c"],
... }
... )
>>> df.schema
{'foo': <class 'polars.datatypes.Int64'>, 'bar': <class 'polars.datatypes.Float64'>, 'ham': <class 'polars.datatypes.Utf8'>}
"""
return {c: self[c].dtype for c in self.columns}

def describe(self) -> "DataFrame":
"""
Summary statistics for a DataFrame. Only summarizes numeric datatypes at the moment and returns nulls for non numeric datatypes.
Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,3 +1308,11 @@ def __repr__(self): # type: ignore
assert sys.getrefcount(foos[0]) == base_count + 1
del df
assert sys.getrefcount(foos[0]) == base_count


def test_schema() -> None:
df = pl.DataFrame(
{"foo": [1, 2, 3], "bar": [6.0, 7.0, 8.0], "ham": ["a", "b", "c"]}
)
expected = {"foo": pl.Int64, "bar": pl.Float64, "ham": pl.Utf8}
assert df.schema == expected

0 comments on commit ae2aad5

Please sign in to comment.