arrow icon indicating copy to clipboard operation
arrow copied to clipboard

[Doc][Python] Timestamp with tz loses its time zone after `to_numpy`

Open sharkdtu opened this issue 1 year ago • 3 comments

Describe the bug, including details regarding any error messages, version, and platform.

Timestamptz type loses its time zone after to_numpy.

Versions / Dependencies

In [44]: pa.__version__
Out[44]: '19.0.1'

Reproduction script

In [39]: import pyarrow as pa

In [40]: x = pa.array([1735689600, 1735689600, 1735689600], type=pa.timestamp("s", tz='UTC'))

In [41]: print(x.type)
timestamp[s, tz=UTC]

In [42]: y = x.to_numpy()

In [43]: print(y.dtype)
datetime64[s]

Component(s)

Python

sharkdtu avatar Feb 28 '25 12:02 sharkdtu

This behaviour is expected as NumPy datetimes are not timezone aware, see https://numpy.org/devdocs/reference/arrays.datetime.html#datetimes-and-timedeltas.

You can convert pyarrow tz-aware timestamp array to

  • numpy datetime64 with loss of tz information,
  • pandas tz-aware datetime64 dtype
  • a Pyhon object
arr_tz = pa.array([1735689600, 1735689600, 1735689600], type=pa.timestamp("s", tz='UTC'))

# numpy datetime64 dtype (losing tz information)
>>> arr_tz.to_numpy()
array(['2025-01-01T00:00:00', '2025-01-01T00:00:00',
       '2025-01-01T00:00:00'], dtype='datetime64[s]')
# pandas tz-aware datetime64 dtype
>>> arr_tz.to_pandas().array
0   2025-01-01 00:00:00+00:00
1   2025-01-01 00:00:00+00:00
2   2025-01-01 00:00:00+00:00
dtype: datetime64[s, UTC]
# python object
>>> arr_tz.to_pandas(timestamp_as_object=True).to_numpy()
array([datetime.datetime(2025, 1, 1, 0, 0, tzinfo=<UTC>),
       datetime.datetime(2025, 1, 1, 0, 0, tzinfo=<UTC>),
       datetime.datetime(2025, 1, 1, 0, 0, tzinfo=<UTC>)], dtype=object)

I will keep this issue open as this needs to be documented in https://arrow.apache.org/docs/python/numpy.html. Also connected and need to go into the docs: https://github.com/apache/arrow/issues/41162.

AlenkaF avatar Mar 03 '25 08:03 AlenkaF

Hello @AlenkaF I’d like to work on documenting the timezone behavior when converting PyArrow timestamp arrays to NumPy. I plan to add a section to the Python NumPy documentation explaining the loss of timezone info and the recommended alternatives.

Bhupendra-glitch avatar Jan 01 '26 05:01 Bhupendra-glitch

Hi @AlenkaF I've opened up a PR for this. Could you please take a look? 🙏

alex-anast avatar Apr 22 '26 21:04 alex-anast