Skip to content

Commit a712191

Browse files
mroeschkejreback
authored andcommitted
DEPR: tz-aware Series/DatetimIndex.__array__ returns an object array of Timestamps (#30516)
1 parent e7a6e5b commit a712191

File tree

7 files changed

+27
-74
lines changed

7 files changed

+27
-74
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
691691
- Removed the previously deprecated keyword "data" from :func:`parallel_coordinates`, use "frame" instead (:issue:`6956`)
692692
- Removed the previously deprecated keyword "colors" from :func:`parallel_coordinates`, use "color" instead (:issue:`6956`)
693693
- Removed the previously deprecated keywords "verbose" and "private_key" from :func:`read_gbq` (:issue:`30200`)
694+
- Calling ``np.array`` and ``np.asarray`` on tz-aware :class:`Series` and :class:`DatetimeIndex` will now return an object array of tz-aware :class:`Timestamp` (:issue:`24596`)
694695
-
695696

696697
.. _whatsnew_1000.performance:

pandas/core/indexes/datetimes.py

-15
Original file line numberDiff line numberDiff line change
@@ -299,21 +299,6 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):
299299
# --------------------------------------------------------------------
300300

301301
def __array__(self, dtype=None):
302-
if (
303-
dtype is None
304-
and isinstance(self._data, DatetimeArray)
305-
and getattr(self.dtype, "tz", None)
306-
):
307-
msg = (
308-
"Converting timezone-aware DatetimeArray to timezone-naive "
309-
"ndarray with 'datetime64[ns]' dtype. In the future, this "
310-
"will return an ndarray with 'object' dtype where each "
311-
"element is a 'pandas.Timestamp' with the correct 'tz'.\n\t"
312-
"To accept the future behavior, pass 'dtype=object'.\n\t"
313-
"To keep the old behavior, pass 'dtype=\"datetime64[ns]\"'."
314-
)
315-
warnings.warn(msg, FutureWarning, stacklevel=3)
316-
dtype = "M8[ns]"
317302
return np.asarray(self._data, dtype=dtype)
318303

319304
@cache_readonly

pandas/core/series.py

-16
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
)
3434
from pandas.core.dtypes.generic import (
3535
ABCDataFrame,
36-
ABCDatetimeArray,
3736
ABCDatetimeIndex,
3837
ABCSeries,
3938
ABCSparseArray,
@@ -717,21 +716,6 @@ def __array__(self, dtype=None):
717716
array(['1999-12-31T23:00:00.000000000', ...],
718717
dtype='datetime64[ns]')
719718
"""
720-
if (
721-
dtype is None
722-
and isinstance(self.array, ABCDatetimeArray)
723-
and getattr(self.dtype, "tz", None)
724-
):
725-
msg = (
726-
"Converting timezone-aware DatetimeArray to timezone-naive "
727-
"ndarray with 'datetime64[ns]' dtype. In the future, this "
728-
"will return an ndarray with 'object' dtype where each "
729-
"element is a 'pandas.Timestamp' with the correct 'tz'.\n\t"
730-
"To accept the future behavior, pass 'dtype=object'.\n\t"
731-
"To keep the old behavior, pass 'dtype=\"datetime64[ns]\"'."
732-
)
733-
warnings.warn(msg, FutureWarning, stacklevel=3)
734-
dtype = "M8[ns]"
735719
return np.asarray(self.array, dtype)
736720

737721
# ----------------------------------------------------------------------

pandas/tests/dtypes/test_missing.py

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from datetime import datetime
22
from decimal import Decimal
3-
from warnings import catch_warnings, filterwarnings
43

54
import numpy as np
65
import pytest
@@ -315,23 +314,21 @@ def test_array_equivalent():
315314
assert not array_equivalent(
316315
TimedeltaIndex([0, np.nan]), TimedeltaIndex([1, np.nan])
317316
)
318-
with catch_warnings():
319-
filterwarnings("ignore", "Converting timezone", FutureWarning)
320-
assert array_equivalent(
321-
DatetimeIndex([0, np.nan], tz="US/Eastern"),
322-
DatetimeIndex([0, np.nan], tz="US/Eastern"),
323-
)
324-
assert not array_equivalent(
325-
DatetimeIndex([0, np.nan], tz="US/Eastern"),
326-
DatetimeIndex([1, np.nan], tz="US/Eastern"),
327-
)
328-
assert not array_equivalent(
329-
DatetimeIndex([0, np.nan]), DatetimeIndex([0, np.nan], tz="US/Eastern")
330-
)
331-
assert not array_equivalent(
332-
DatetimeIndex([0, np.nan], tz="CET"),
333-
DatetimeIndex([0, np.nan], tz="US/Eastern"),
334-
)
317+
assert array_equivalent(
318+
DatetimeIndex([0, np.nan], tz="US/Eastern"),
319+
DatetimeIndex([0, np.nan], tz="US/Eastern"),
320+
)
321+
assert not array_equivalent(
322+
DatetimeIndex([0, np.nan], tz="US/Eastern"),
323+
DatetimeIndex([1, np.nan], tz="US/Eastern"),
324+
)
325+
assert not array_equivalent(
326+
DatetimeIndex([0, np.nan]), DatetimeIndex([0, np.nan], tz="US/Eastern")
327+
)
328+
assert not array_equivalent(
329+
DatetimeIndex([0, np.nan], tz="CET"),
330+
DatetimeIndex([0, np.nan], tz="US/Eastern"),
331+
)
335332

336333
assert not array_equivalent(DatetimeIndex([0, np.nan]), TimedeltaIndex([0, np.nan]))
337334

pandas/tests/generic/test_frame.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,10 @@ def test_to_xarray_index_types(self, index):
222222

223223
# idempotency
224224
# categoricals are not preserved
225-
# datetimes w/tz are not preserved
225+
# datetimes w/tz are preserved
226226
# column names are lost
227227
expected = df.copy()
228228
expected["f"] = expected["f"].astype(object)
229-
expected["h"] = expected["h"].astype("datetime64[ns]")
230229
expected.columns.name = None
231230
tm.assert_frame_equal(
232231
result.to_dataframe(),
@@ -271,7 +270,6 @@ def test_to_xarray(self):
271270
result = result.to_dataframe()
272271
expected = df.copy()
273272
expected["f"] = expected["f"].astype(object)
274-
expected["h"] = expected["h"].astype("datetime64[ns]")
275273
expected.columns.name = None
276274
tm.assert_frame_equal(result, expected, check_index_type=False)
277275

pandas/tests/indexes/datetimes/test_datetime.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,13 @@ def test_asarray_tz_naive(self):
393393
# This shouldn't produce a warning.
394394
idx = pd.date_range("2000", periods=2)
395395
# M8[ns] by default
396-
with tm.assert_produces_warning(None):
397-
result = np.asarray(idx)
396+
result = np.asarray(idx)
398397

399398
expected = np.array(["2000-01-01", "2000-01-02"], dtype="M8[ns]")
400399
tm.assert_numpy_array_equal(result, expected)
401400

402401
# optionally, object
403-
with tm.assert_produces_warning(None):
404-
result = np.asarray(idx, dtype=object)
402+
result = np.asarray(idx, dtype=object)
405403

406404
expected = np.array([pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-02")])
407405
tm.assert_numpy_array_equal(result, expected)
@@ -410,24 +408,20 @@ def test_asarray_tz_aware(self):
410408
tz = "US/Central"
411409
idx = pd.date_range("2000", periods=2, tz=tz)
412410
expected = np.array(["2000-01-01T06", "2000-01-02T06"], dtype="M8[ns]")
413-
# We warn by default and return an ndarray[M8[ns]]
414-
with tm.assert_produces_warning(FutureWarning):
415-
result = np.asarray(idx)
411+
result = np.asarray(idx, dtype="datetime64[ns]")
416412

417413
tm.assert_numpy_array_equal(result, expected)
418414

419415
# Old behavior with no warning
420-
with tm.assert_produces_warning(None):
421-
result = np.asarray(idx, dtype="M8[ns]")
416+
result = np.asarray(idx, dtype="M8[ns]")
422417

423418
tm.assert_numpy_array_equal(result, expected)
424419

425420
# Future behavior with no warning
426421
expected = np.array(
427422
[pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)]
428423
)
429-
with tm.assert_produces_warning(None):
430-
result = np.asarray(idx, dtype=object)
424+
result = np.asarray(idx, dtype=object)
431425

432426
tm.assert_numpy_array_equal(result, expected)
433427

pandas/tests/series/test_timeseries.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -731,14 +731,12 @@ def test_asarray_tz_naive(self):
731731
# This shouldn't produce a warning.
732732
ser = pd.Series(pd.date_range("2000", periods=2))
733733
expected = np.array(["2000-01-01", "2000-01-02"], dtype="M8[ns]")
734-
with tm.assert_produces_warning(None):
735-
result = np.asarray(ser)
734+
result = np.asarray(ser)
736735

737736
tm.assert_numpy_array_equal(result, expected)
738737

739738
# optionally, object
740-
with tm.assert_produces_warning(None):
741-
result = np.asarray(ser, dtype=object)
739+
result = np.asarray(ser, dtype=object)
742740

743741
expected = np.array([pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-02")])
744742
tm.assert_numpy_array_equal(result, expected)
@@ -747,23 +745,19 @@ def test_asarray_tz_aware(self):
747745
tz = "US/Central"
748746
ser = pd.Series(pd.date_range("2000", periods=2, tz=tz))
749747
expected = np.array(["2000-01-01T06", "2000-01-02T06"], dtype="M8[ns]")
750-
# We warn by default and return an ndarray[M8[ns]]
751-
with tm.assert_produces_warning(FutureWarning):
752-
result = np.asarray(ser)
748+
result = np.asarray(ser, dtype="datetime64[ns]")
753749

754750
tm.assert_numpy_array_equal(result, expected)
755751

756752
# Old behavior with no warning
757-
with tm.assert_produces_warning(None):
758-
result = np.asarray(ser, dtype="M8[ns]")
753+
result = np.asarray(ser, dtype="M8[ns]")
759754

760755
tm.assert_numpy_array_equal(result, expected)
761756

762757
# Future behavior with no warning
763758
expected = np.array(
764759
[pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)]
765760
)
766-
with tm.assert_produces_warning(None):
767-
result = np.asarray(ser, dtype=object)
761+
result = np.asarray(ser, dtype=object)
768762

769763
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)