Skip to content

Commit

Permalink
handling cases where np_dtype is DatetimeTZDtype
Browse files Browse the repository at this point in the history
  • Loading branch information
Koookadooo committed Dec 9, 2024
1 parent b86e696 commit c1f4735
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2276,11 +2276,9 @@ def name(self) -> str: # type: ignore[override]
def numpy_dtype(self) -> np.dtype:
"""Return an instance of the related numpy dtype"""
if pa.types.is_timestamp(self.pyarrow_dtype):
# Preserve timezone information if present
if self.pyarrow_dtype.tz is not None:
# Use PyArrow's to_pandas_dtype method for timezone-aware types
return self.pyarrow_dtype.to_pandas_dtype()
# Fall back to naive datetime64 for timezone-naive timestamps
# Handle timezone-aware timestamps
return np.dtype(f"datetime64[{self.pyarrow_dtype.unit}]")
return np.dtype(f"datetime64[{self.pyarrow_dtype.unit}]")
if pa.types.is_duration(self.pyarrow_dtype):
return np.dtype(f"timedelta64[{self.pyarrow_dtype.unit}]")
Expand All @@ -2289,10 +2287,15 @@ def numpy_dtype(self) -> np.dtype:
):
return np.dtype(str)
try:
return np.dtype(self.pyarrow_dtype.to_pandas_dtype())
np_dtype = self.pyarrow_dtype.to_pandas_dtype()
if isinstance(np_dtype, DatetimeTZDtype):
# Convert timezone-aware to naive datetime64
return np.dtype(f"datetime64[{np_dtype.unit}]")
return np.dtype(np_dtype)
except (NotImplementedError, TypeError):
return np.dtype(object)


@cache_readonly
def kind(self) -> str:
if pa.types.is_timestamp(self.pyarrow_dtype):
Expand Down

0 comments on commit c1f4735

Please sign in to comment.