Skip to content

Commit 23b5471

Browse files
jorisvandenbosschePingviinituutti
authored andcommitted
DOC: fix to_numpy explanation for tz aware data (pandas-dev#24595)
1 parent 83d8baa commit 23b5471

File tree

3 files changed

+10
-27
lines changed

3 files changed

+10
-27
lines changed

doc/source/basics.rst

-21
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,6 @@ are two possibly useful representations:
9999

100100
Timezones may be preserved with ``dtype=object``
101101

102-
.. ipython:: python
103-
104-
ser = pd.Series(pd.date_range('2000', periods=2, tz="CET"))
105-
ser.to_numpy(dtype=object)
106-
107-
Or thrown away with ``dtype='datetime64[ns]'``
108-
109-
ser.to_numpy(dtype="datetime64[ns]")
110-
111-
:meth:`~Series.to_numpy` gives some control over the ``dtype`` of the
112-
resulting :class:`ndarray`. For example, consider datetimes with timezones.
113-
NumPy doesn't have a dtype to represent timezone-aware datetimes, so there
114-
are two possibly useful representations:
115-
116-
1. An object-dtype :class:`ndarray` with :class:`Timestamp` objects, each
117-
with the correct ``tz``
118-
2. A ``datetime64[ns]`` -dtype :class:`ndarray`, where the values have
119-
been converted to UTC and the timezone discarded
120-
121-
Timezones may be preserved with ``dtype=object``
122-
123102
.. ipython:: python
124103
125104
ser = pd.Series(pd.date_range('2000', periods=2, tz="CET"))

doc/source/timeseries.rst

+8-4
Original file line numberDiff line numberDiff line change
@@ -2425,21 +2425,25 @@ a convert on an aware stamp.
24252425
.. note::
24262426

24272427
Using :meth:`Series.to_numpy` on a ``Series``, returns a NumPy array of the data.
2428-
These values are converted to UTC, as NumPy does not currently support timezones (even though it is *printing* in the local timezone!).
2428+
NumPy does not currently support timezones (even though it is *printing* in the local timezone!),
2429+
therefore an object array of Timestamps is returned for timezone aware data:
24292430

24302431
.. ipython:: python
24312432
24322433
s_naive.to_numpy()
24332434
s_aware.to_numpy()
24342435
2435-
Further note that once converted to a NumPy array these would lose the tz tenor.
2436+
By converting to an object array of Timestamps, it preserves the timezone
2437+
information. For example, when converting back to a Series:
24362438

24372439
.. ipython:: python
24382440
24392441
pd.Series(s_aware.to_numpy())
24402442
2441-
However, these can be easily converted:
2443+
However, if you want an actual NumPy ``datetime64[ns]`` array (with the values
2444+
converted to UTC) instead of an array of objects, you can specify the
2445+
``dtype`` argument:
24422446

24432447
.. ipython:: python
24442448
2445-
pd.Series(s_aware.to_numpy()).dt.tz_localize('UTC').dt.tz_convert('US/Eastern')
2449+
s_aware.to_numpy(dtype='datetime64[ns]')

pandas/core/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,6 @@ def to_numpy(self, dtype=None, copy=False):
899899
``to_numpy()`` will return a NumPy array and the categorical dtype
900900
will be lost.
901901
902-
903902
For NumPy dtypes, this will be a reference to the actual data stored
904903
in this Series or Index (assuming ``copy=False``). Modifying the result
905904
in place will modify the data stored in the Series or Index (not that
@@ -910,7 +909,7 @@ def to_numpy(self, dtype=None, copy=False):
910909
expensive. When you need a no-copy reference to the underlying data,
911910
:attr:`Series.array` should be used instead.
912911
913-
This table lays out the different dtypes and return types of
912+
This table lays out the different dtypes and default return types of
914913
``to_numpy()`` for various dtypes within pandas.
915914
916915
================== ================================
@@ -920,6 +919,7 @@ def to_numpy(self, dtype=None, copy=False):
920919
period ndarray[object] (Periods)
921920
interval ndarray[object] (Intervals)
922921
IntegerNA ndarray[object]
922+
datetime64[ns] datetime64[ns]
923923
datetime64[ns, tz] ndarray[object] (Timestamps)
924924
================== ================================
925925

0 commit comments

Comments
 (0)