Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: ArrowExtensionArray.to_numpy from timestamp to int #56567

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,16 +1313,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 @@ -2996,7 +2996,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 @@ -3108,3 +3111,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)