Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch: fix dtypes rendering #1481

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 28 additions & 33 deletions narwhals/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,16 @@ class Int16(NumericType):
>>> ser_pl = pl.Series(data)
>>> ser_pa = pa.chunked_array([data])

>>> def func(ser):
... ser_nw = nw.from_native(ser, series_only=True)
... return ser_nw.cast(nw.Int16).dtype

>>> def func(ser):
... ser_nw = nw.from_native(ser, series_only=True)
... return ser_nw.cast(nw.Int16).dtype

>>> func(ser_pd)
Int16
>>> func(ser_pl)
Int16
>>> func(ser_pa)
Int16
>>> func(ser_pd)
Int16
>>> func(ser_pl)
Int16
>>> func(ser_pa)
Int16
"""


Expand All @@ -135,12 +134,10 @@ class Int8(NumericType):
>>> ser_pl = pl.Series(data)
>>> ser_pa = pa.chunked_array([data])


>>> def func(ser):
... ser_nw = nw.from_native(ser, series_only=True)
... return ser_nw.cast(nw.Int8).dtype


>>> func(ser_pd)
Int8
>>> func(ser_pl)
Expand All @@ -163,12 +160,10 @@ class UInt64(NumericType):
>>> ser_pl = pl.Series(data)
>>> ser_pa = pa.chunked_array([data])


>>> def func(ser):
... ser_nw = nw.from_native(ser, series_only=True)
... return ser_nw.cast(nw.UInt64).dtype


>>> func(ser_pd)
UInt64
>>> func(ser_pl)
Expand All @@ -191,12 +186,10 @@ class UInt32(NumericType):
>>> ser_pl = pl.Series(data)
>>> ser_pa = pa.chunked_array([data])


>>> def func(ser):
... ser_nw = nw.from_native(ser, series_only=True)
... return ser_nw.cast(nw.UInt32).dtype


>>> func(ser_pd)
UInt32
>>> func(ser_pl)
Expand Down Expand Up @@ -245,7 +238,6 @@ class UInt8(NumericType):
>>> ser_pl = pl.Series(data)
>>> ser_pa = pa.chunked_array([data])


>>> def func(ser):
... ser_nw = nw.from_native(ser, series_only=True)
... return ser_nw.cast(nw.UInt8).dtype
Expand Down Expand Up @@ -294,12 +286,10 @@ class Float32(NumericType):
>>> ser_pl = pl.Series(data)
>>> ser_pa = pa.chunked_array([data])


>>> def func(ser):
... ser_nw = nw.from_native(ser, series_only=True)
... return ser_nw.cast(nw.Float32).dtype


>>> func(ser_pd)
Float32
>>> func(ser_pl)
Expand All @@ -322,7 +312,6 @@ class String(DType):
>>> ser_pl = pl.Series(data)
>>> ser_pa = pa.chunked_array([data])


>>> nw.from_native(ser_pd, series_only=True).dtype
String
>>> nw.from_native(ser_pl, series_only=True).dtype
Expand All @@ -345,7 +334,6 @@ class Boolean(DType):
>>> ser_pl = pl.Series(data)
>>> ser_pa = pa.chunked_array([data])


>>> nw.from_native(ser_pd, series_only=True).dtype
Boolean
>>> nw.from_native(ser_pl, series_only=True).dtype
Expand All @@ -355,10 +343,12 @@ class Boolean(DType):
"""


class Object(DType): ...
class Object(DType):
"""Data type for wrapping arbitrary Python objects."""


class Unknown(DType): ...
class Unknown(DType):
"""Type representing DataType values that could not be determined statically."""


class Datetime(TemporalType):
Expand All @@ -370,8 +360,7 @@ class Datetime(TemporalType):
`import zoneinfo; zoneinfo.available_timezones()` for a full list).

Notes:
Adapted from Polars implementation at:
https://github.com/pola-rs/polars/blob/py-1.7.1/py-polars/polars/datatypes/classes.py#L398-L457
Adapted from [Polars implementation](https://github.com/pola-rs/polars/blob/py-1.7.1/py-polars/polars/datatypes/classes.py#L398-L457)
"""

def __init__(
Expand Down Expand Up @@ -416,8 +405,7 @@ class Duration(TemporalType):
time_unit: Unit of time. Defaults to `'us'` (microseconds).

Notes:
Adapted from Polars implementation at:
https://github.com/pola-rs/polars/blob/py-1.7.1/py-polars/polars/datatypes/classes.py#L460-L502
Adapted from [Polars implementation](https://github.com/pola-rs/polars/blob/py-1.7.1/py-polars/polars/datatypes/classes.py#L460-L502)
"""

def __init__(
Expand Down Expand Up @@ -463,7 +451,6 @@ class Categorical(DType):
>>> ser_pl = pl.Series(data)
>>> ser_pa = pa.chunked_array([data])


>>> nw.from_native(ser_pd, series_only=True).cast(nw.Categorical).dtype
Categorical
>>> nw.from_native(ser_pl, series_only=True).cast(nw.Categorical).dtype
Expand All @@ -473,7 +460,8 @@ class Categorical(DType):
"""


class Enum(DType): ...
class Enum(DType):
"""A fixed categorical encoding of a unique set of strings."""


class Field:
Expand All @@ -482,7 +470,6 @@ class Field:
Arguments:
name: The name of the field within its parent `Struct`.
dtype: The `DataType` of the field's values.

"""

name: str
Expand All @@ -507,7 +494,8 @@ class Struct(DType):
"""Struct composite type.

Arguments:
fields: The fields that make up the struct. Can be either a sequence of Field objects or a mapping of column names to data types.
fields: The fields that make up the struct. Can be either a sequence of Field
objects or a mapping of column names to data types.

Examples:
>>> import polars as pl
Expand All @@ -517,7 +505,6 @@ class Struct(DType):
>>> ser_pl = pl.Series(data)
>>> ser_pa = pa.chunked_array([data])


>>> nw.from_native(ser_pl, series_only=True).dtype
Struct({'a': Int64, 'b': List(String)})
>>> nw.from_native(ser_pa, series_only=True).dtype
Expand Down Expand Up @@ -618,6 +605,13 @@ def __repr__(self) -> str:


class Array(DType):
"""Fixed length list type.

Arguments:
inner: The datatype of the values within each array.
width: the length of each array.
"""

def __init__(self, inner: DType | type[DType], width: int | None = None) -> None:
self.inner = inner
if width is None:
Expand Down Expand Up @@ -648,4 +642,5 @@ def __repr__(self) -> str:
return f"{class_name}({self.inner!r}, {self.width})"


class Date(TemporalType): ...
class Date(TemporalType):
"""Data type representing a calendar date."""
6 changes: 2 additions & 4 deletions narwhals/stable/v1/_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class Datetime(NwDatetime):
`import zoneinfo; zoneinfo.available_timezones()` for a full list).

Notes:
Adapted from Polars implementation at:
https://github.com/pola-rs/polars/blob/py-1.7.1/py-polars/polars/datatypes/classes.py#L398-L457
Adapted from [Polars implementation](https://github.com/pola-rs/polars/blob/py-1.7.1/py-polars/polars/datatypes/classes.py#L398-L457)
"""

def __hash__(self) -> int:
Expand All @@ -51,8 +50,7 @@ class Duration(NwDuration):
time_unit: Unit of time. Defaults to `'us'` (microseconds).

Notes:
Adapted from Polars implementation at:
https://github.com/pola-rs/polars/blob/py-1.7.1/py-polars/polars/datatypes/classes.py#L460-L502
Adapted from [Polars implementation](https://github.com/pola-rs/polars/blob/py-1.7.1/py-polars/polars/datatypes/classes.py#L460-L502)
"""

def __hash__(self) -> int:
Expand Down
Loading