Skip to content

API: ArrowDtype.type #51307

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

Merged
merged 8 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 40 additions & 1 deletion pandas/core/arrays/arrow/dtype.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
from __future__ import annotations

from datetime import (
date,
datetime,
time,
timedelta,
)
import re

import numpy as np

from pandas._libs.tslibs import (
Timedelta,
Timestamp,
)
from pandas._typing import (
TYPE_CHECKING,
DtypeObj,
Expand Down Expand Up @@ -90,7 +100,36 @@ def type(self):
"""
Returns pyarrow.DataType.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring will need updating

"""
return type(self.pyarrow_dtype)
pa_type = self.pyarrow_dtype
if pa.types.is_integer(pa_type):
return int
elif pa.types.is_floating(pa_type):
return float
elif pa.types.is_string(pa_type):
return str
elif pa.types.is_binary(pa_type):
return bytes
elif pa.types.is_boolean(pa_type):
return bool
elif pa.types.is_duration(pa_type):
if pa_type.unit == "ns":
return Timedelta
else:
return timedelta
elif pa.types.is_timestamp(pa_type):
if pa_type.unit == "ns":
return Timestamp
else:
return datetime
Comment on lines +121 to +124
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(not for this PR, just noting from seeing the diff)

FWIW, now Timestamp supports multiple resolutions, we should make it return a Timestamp for all cases, I think (it's something we should also do on the pyarrow side, but on the short term it will be easier done on the pandas side to ensure consistent behaviour across pyarrow versions)

elif pa.types.is_date(pa_type):
return date
elif pa.types.is_time(pa_type):
return time
elif pa.types.is_null(pa_type):
# TODO: None? pd.NA? pa.null?
return type(pa_type)
else:
raise NotImplementedError(pa_type)

@property
def name(self) -> str: # type: ignore[override]
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import numpy as np

from pandas._libs import (
Timestamp,
internals as libinternals,
lib,
writers,
Expand Down Expand Up @@ -63,6 +62,7 @@
is_string_dtype,
)
from pandas.core.dtypes.dtypes import (
DatetimeTZDtype,
ExtensionDtype,
PandasDtype,
PeriodDtype,
Expand Down Expand Up @@ -2180,17 +2180,16 @@ def get_block_type(dtype: DtypeObj):
-------
cls : class, subclass of Block
"""
# We use vtype and kind checks because they are much more performant
# We use kind checks because it is much more performant
# than is_foo_dtype
vtype = dtype.type
kind = dtype.kind

cls: type[Block]

if isinstance(dtype, SparseDtype):
# Need this first(ish) so that Sparse[datetime] is sparse
cls = ExtensionBlock
elif vtype is Timestamp:
elif isinstance(dtype, DatetimeTZDtype):
cls = DatetimeTZBlock
elif isinstance(dtype, PeriodDtype):
cls = NDArrayBackedExtensionBlock
Expand Down