Skip to content

Commit d24977f

Browse files
mroeschkecbpygit
authored andcommitted
BUG: ArrowExtensionArray.to_numpy from timestamp to int (pandas-dev#56567)
1 parent ab4ae04 commit d24977f

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

pandas/core/arrays/arrow/array.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -1322,16 +1322,12 @@ def to_numpy(
13221322
copy = False
13231323

13241324
if pa.types.is_timestamp(pa_type) or pa.types.is_duration(pa_type):
1325-
result = data._maybe_convert_datelike_array()
1326-
if (pa.types.is_timestamp(pa_type) and pa_type.tz is not None) or (
1327-
dtype is not None and dtype.kind == "O"
1328-
):
1329-
dtype = object
1330-
else:
1331-
# GH 55997
1332-
dtype = None
1333-
na_value = pa_type.to_pandas_dtype().type("nat", pa_type.unit)
1334-
result = result.to_numpy(dtype=dtype, na_value=na_value)
1325+
# GH 55997
1326+
if dtype != object and na_value is self.dtype.na_value:
1327+
na_value = lib.no_default
1328+
result = data._maybe_convert_datelike_array().to_numpy(
1329+
dtype=dtype, na_value=na_value
1330+
)
13351331
elif pa.types.is_time(pa_type) or pa.types.is_date(pa_type):
13361332
# convert to list of python datetime.time objects before
13371333
# wrapping in ndarray

pandas/tests/extension/test_arrow.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -3030,7 +3030,10 @@ def test_to_numpy_temporal(pa_type, dtype):
30303030
value = pd.Timestamp(1, unit=pa_type.unit, tz=pa_type.tz).as_unit(pa_type.unit)
30313031

30323032
if dtype == object or (pa.types.is_timestamp(pa_type) and pa_type.tz is not None):
3033-
na = pd.NA
3033+
if dtype == object:
3034+
na = pd.NA
3035+
else:
3036+
na = pd.NaT
30343037
expected = np.array([value, na], dtype=object)
30353038
assert result[0].unit == value.unit
30363039
else:
@@ -3142,3 +3145,11 @@ def test_string_to_time_parsing_cast():
31423145
ArrowExtensionArray(pa.array([time(11, 41, 43, 76160)], from_pandas=True))
31433146
)
31443147
tm.assert_series_equal(result, expected)
3148+
3149+
3150+
def test_to_numpy_timestamp_to_int():
3151+
# GH 55997
3152+
ser = pd.Series(["2020-01-01 04:30:00"], dtype="timestamp[ns][pyarrow]")
3153+
result = ser.to_numpy(dtype=np.int64)
3154+
expected = np.array([1577853000000000000])
3155+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)