Skip to content

Commit

Permalink
BUG: ArrowExtensionArray.to_numpy from timestamp to int (pandas-dev#5…
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke authored and cbpygit committed Jan 2, 2024
1 parent ab4ae04 commit d24977f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
16 changes: 6 additions & 10 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1322,16 +1322,12 @@ def to_numpy(
copy = False

if pa.types.is_timestamp(pa_type) or pa.types.is_duration(pa_type):
result = data._maybe_convert_datelike_array()
if (pa.types.is_timestamp(pa_type) and pa_type.tz is not None) or (
dtype is not None and dtype.kind == "O"
):
dtype = object
else:
# GH 55997
dtype = None
na_value = pa_type.to_pandas_dtype().type("nat", pa_type.unit)
result = result.to_numpy(dtype=dtype, na_value=na_value)
# GH 55997
if dtype != object and na_value is self.dtype.na_value:
na_value = lib.no_default
result = data._maybe_convert_datelike_array().to_numpy(
dtype=dtype, na_value=na_value
)
elif pa.types.is_time(pa_type) or pa.types.is_date(pa_type):
# convert to list of python datetime.time objects before
# wrapping in ndarray
Expand Down
13 changes: 12 additions & 1 deletion pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3030,7 +3030,10 @@ def test_to_numpy_temporal(pa_type, dtype):
value = pd.Timestamp(1, unit=pa_type.unit, tz=pa_type.tz).as_unit(pa_type.unit)

if dtype == object or (pa.types.is_timestamp(pa_type) and pa_type.tz is not None):
na = pd.NA
if dtype == object:
na = pd.NA
else:
na = pd.NaT
expected = np.array([value, na], dtype=object)
assert result[0].unit == value.unit
else:
Expand Down Expand Up @@ -3142,3 +3145,11 @@ def test_string_to_time_parsing_cast():
ArrowExtensionArray(pa.array([time(11, 41, 43, 76160)], from_pandas=True))
)
tm.assert_series_equal(result, expected)


def test_to_numpy_timestamp_to_int():
# GH 55997
ser = pd.Series(["2020-01-01 04:30:00"], dtype="timestamp[ns][pyarrow]")
result = ser.to_numpy(dtype=np.int64)
expected = np.array([1577853000000000000])
tm.assert_numpy_array_equal(result, expected)

0 comments on commit d24977f

Please sign in to comment.