File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -2307,7 +2307,12 @@ def kind(self) -> str:
2307
2307
@cache_readonly
2308
2308
def itemsize (self ) -> int :
2309
2309
"""Return the number of bytes in this dtype"""
2310
- return self .numpy_dtype .itemsize
2310
+ try :
2311
+ # Use PyArrow's bit_width for fixed-width types
2312
+ return self .pyarrow_dtype .bit_width // 8 # convert from bit to bytes
2313
+ except (AttributeError , NotImplementedError , ValueError ):
2314
+ # Fall back to numpy dtype for variable-width or unsupported types
2315
+ return self .numpy_dtype .itemsize
2311
2316
2312
2317
def construct_array_type (self ) -> type_t [ArrowExtensionArray ]:
2313
2318
"""
Original file line number Diff line number Diff line change @@ -3576,6 +3576,29 @@ def test_timestamp_dtype_disallows_decimal():
3576
3576
pd .array (vals , dtype = ArrowDtype (pa .timestamp ("us" )))
3577
3577
3578
3578
3579
+ def test_arrow_dtype_itemsize ():
3580
+ # Regression test for GH#57948 where date32[day] was incorrectly
3581
+ # reporting 8 bytes instead of 4.
3582
+
3583
+ # date32 should be 4 bytes, not 8
3584
+ dtype = ArrowDtype (pa .date32 ())
3585
+ assert dtype .itemsize == 4
3586
+
3587
+ # Testing other fixed-width types
3588
+ assert ArrowDtype (pa .int32 ()).itemsize == 4
3589
+ assert ArrowDtype (pa .int64 ()).itemsize == 8
3590
+ assert ArrowDtype (pa .float32 ()).itemsize == 4
3591
+ assert ArrowDtype (pa .float64 ()).itemsize == 8
3592
+ assert ArrowDtype (pa .date64 ()).itemsize == 8
3593
+
3594
+ # Test that variable-width types fall back gracefully
3595
+ string_dtype = ArrowDtype (pa .string ())
3596
+ assert isinstance (string_dtype .itemsize , int )
3597
+
3598
+ list_dtype = ArrowDtype (pa .list_ (pa .int32 ()))
3599
+ assert isinstance (list_dtype .itemsize , int )
3600
+
3601
+
3579
3602
def test_timestamp_dtype_matches_to_datetime ():
3580
3603
# GH#61775
3581
3604
dtype1 = "datetime64[ns, US/Eastern]"
You can’t perform that action at this time.
0 commit comments