Skip to content

Commit c1f4735

Browse files
committed
handling cases where np_dtype is DatetimeTZDtype
1 parent b86e696 commit c1f4735

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

pandas/core/dtypes/dtypes.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2276,11 +2276,9 @@ def name(self) -> str: # type: ignore[override]
22762276
def numpy_dtype(self) -> np.dtype:
22772277
"""Return an instance of the related numpy dtype"""
22782278
if pa.types.is_timestamp(self.pyarrow_dtype):
2279-
# Preserve timezone information if present
22802279
if self.pyarrow_dtype.tz is not None:
2281-
# Use PyArrow's to_pandas_dtype method for timezone-aware types
2282-
return self.pyarrow_dtype.to_pandas_dtype()
2283-
# Fall back to naive datetime64 for timezone-naive timestamps
2280+
# Handle timezone-aware timestamps
2281+
return np.dtype(f"datetime64[{self.pyarrow_dtype.unit}]")
22842282
return np.dtype(f"datetime64[{self.pyarrow_dtype.unit}]")
22852283
if pa.types.is_duration(self.pyarrow_dtype):
22862284
return np.dtype(f"timedelta64[{self.pyarrow_dtype.unit}]")
@@ -2289,10 +2287,15 @@ def numpy_dtype(self) -> np.dtype:
22892287
):
22902288
return np.dtype(str)
22912289
try:
2292-
return np.dtype(self.pyarrow_dtype.to_pandas_dtype())
2290+
np_dtype = self.pyarrow_dtype.to_pandas_dtype()
2291+
if isinstance(np_dtype, DatetimeTZDtype):
2292+
# Convert timezone-aware to naive datetime64
2293+
return np.dtype(f"datetime64[{np_dtype.unit}]")
2294+
return np.dtype(np_dtype)
22932295
except (NotImplementedError, TypeError):
22942296
return np.dtype(object)
22952297

2298+
22962299
@cache_readonly
22972300
def kind(self) -> str:
22982301
if pa.types.is_timestamp(self.pyarrow_dtype):

0 commit comments

Comments
 (0)