Skip to content

DEPR: tz-aware Series/DatetimIndex.__array__ returns an object array of Timestamps #30516

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

Merged
merged 3 commits into from
Jan 3, 2020
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
- Removed the previously deprecated keyword "data" from :func:`parallel_coordinates`, use "frame" instead (:issue:`6956`)
- Removed the previously deprecated keyword "colors" from :func:`parallel_coordinates`, use "color" instead (:issue:`6956`)
- Removed the previously deprecated keywords "verbose" and "private_key" from :func:`read_gbq` (:issue:`30200`)
- 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`)
-

.. _whatsnew_1000.performance:
Expand Down
15 changes: 0 additions & 15 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,21 +310,6 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):
# --------------------------------------------------------------------

def __array__(self, dtype=None):
if (
dtype is None
and isinstance(self._data, DatetimeArray)
and getattr(self.dtype, "tz", None)
):
msg = (
"Converting timezone-aware DatetimeArray to timezone-naive "
"ndarray with 'datetime64[ns]' dtype. In the future, this "
"will return an ndarray with 'object' dtype where each "
"element is a 'pandas.Timestamp' with the correct 'tz'.\n\t"
"To accept the future behavior, pass 'dtype=object'.\n\t"
"To keep the old behavior, pass 'dtype=\"datetime64[ns]\"'."
)
warnings.warn(msg, FutureWarning, stacklevel=3)
dtype = "M8[ns]"
return np.asarray(self._data, dtype=dtype)

@property
Expand Down
16 changes: 0 additions & 16 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
)
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCDatetimeArray,
ABCDatetimeIndex,
ABCSeries,
ABCSparseArray,
Expand Down Expand Up @@ -740,21 +739,6 @@ def __array__(self, dtype=None):
array(['1999-12-31T23:00:00.000000000', ...],
dtype='datetime64[ns]')
"""
if (
dtype is None
and isinstance(self.array, ABCDatetimeArray)
and getattr(self.dtype, "tz", None)
):
msg = (
"Converting timezone-aware DatetimeArray to timezone-naive "
"ndarray with 'datetime64[ns]' dtype. In the future, this "
"will return an ndarray with 'object' dtype where each "
"element is a 'pandas.Timestamp' with the correct 'tz'.\n\t"
"To accept the future behavior, pass 'dtype=object'.\n\t"
"To keep the old behavior, pass 'dtype=\"datetime64[ns]\"'."
)
warnings.warn(msg, FutureWarning, stacklevel=3)
dtype = "M8[ns]"
return np.asarray(self.array, dtype)

# ----------------------------------------------------------------------
Expand Down
33 changes: 15 additions & 18 deletions pandas/tests/dtypes/test_missing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime
from decimal import Decimal
from warnings import catch_warnings, filterwarnings

import numpy as np
import pytest
Expand Down Expand Up @@ -315,23 +314,21 @@ def test_array_equivalent():
assert not array_equivalent(
TimedeltaIndex([0, np.nan]), TimedeltaIndex([1, np.nan])
)
with catch_warnings():
filterwarnings("ignore", "Converting timezone", FutureWarning)
assert array_equivalent(
DatetimeIndex([0, np.nan], tz="US/Eastern"),
DatetimeIndex([0, np.nan], tz="US/Eastern"),
)
assert not array_equivalent(
DatetimeIndex([0, np.nan], tz="US/Eastern"),
DatetimeIndex([1, np.nan], tz="US/Eastern"),
)
assert not array_equivalent(
DatetimeIndex([0, np.nan]), DatetimeIndex([0, np.nan], tz="US/Eastern")
)
assert not array_equivalent(
DatetimeIndex([0, np.nan], tz="CET"),
DatetimeIndex([0, np.nan], tz="US/Eastern"),
)
assert array_equivalent(
DatetimeIndex([0, np.nan], tz="US/Eastern"),
DatetimeIndex([0, np.nan], tz="US/Eastern"),
)
assert not array_equivalent(
DatetimeIndex([0, np.nan], tz="US/Eastern"),
DatetimeIndex([1, np.nan], tz="US/Eastern"),
)
assert not array_equivalent(
DatetimeIndex([0, np.nan]), DatetimeIndex([0, np.nan], tz="US/Eastern")
)
assert not array_equivalent(
DatetimeIndex([0, np.nan], tz="CET"),
DatetimeIndex([0, np.nan], tz="US/Eastern"),
)

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

Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/generic/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,10 @@ def test_to_xarray_index_types(self, index):

# idempotency
# categoricals are not preserved
# datetimes w/tz are not preserved
# datetimes w/tz are preserved
# column names are lost
expected = df.copy()
expected["f"] = expected["f"].astype(object)
expected["h"] = expected["h"].astype("datetime64[ns]")
expected.columns.name = None
tm.assert_frame_equal(
result.to_dataframe(),
Expand Down Expand Up @@ -271,7 +270,6 @@ def test_to_xarray(self):
result = result.to_dataframe()
expected = df.copy()
expected["f"] = expected["f"].astype(object)
expected["h"] = expected["h"].astype("datetime64[ns]")
expected.columns.name = None
tm.assert_frame_equal(result, expected, check_index_type=False)

Expand Down
16 changes: 5 additions & 11 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,13 @@ def test_asarray_tz_naive(self):
# This shouldn't produce a warning.
idx = pd.date_range("2000", periods=2)
# M8[ns] by default
with tm.assert_produces_warning(None):
result = np.asarray(idx)
result = np.asarray(idx)

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

# optionally, object
with tm.assert_produces_warning(None):
result = np.asarray(idx, dtype=object)
result = np.asarray(idx, dtype=object)

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

tm.assert_numpy_array_equal(result, expected)

# Old behavior with no warning
with tm.assert_produces_warning(None):
result = np.asarray(idx, dtype="M8[ns]")
result = np.asarray(idx, dtype="M8[ns]")

tm.assert_numpy_array_equal(result, expected)

# Future behavior with no warning
expected = np.array(
[pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)]
)
with tm.assert_produces_warning(None):
result = np.asarray(idx, dtype=object)
result = np.asarray(idx, dtype=object)

tm.assert_numpy_array_equal(result, expected)

Expand Down
16 changes: 5 additions & 11 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,14 +731,12 @@ def test_asarray_tz_naive(self):
# This shouldn't produce a warning.
ser = pd.Series(pd.date_range("2000", periods=2))
expected = np.array(["2000-01-01", "2000-01-02"], dtype="M8[ns]")
with tm.assert_produces_warning(None):
result = np.asarray(ser)
result = np.asarray(ser)

tm.assert_numpy_array_equal(result, expected)

# optionally, object
with tm.assert_produces_warning(None):
result = np.asarray(ser, dtype=object)
result = np.asarray(ser, dtype=object)

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

tm.assert_numpy_array_equal(result, expected)

# Old behavior with no warning
with tm.assert_produces_warning(None):
result = np.asarray(ser, dtype="M8[ns]")
result = np.asarray(ser, dtype="M8[ns]")

tm.assert_numpy_array_equal(result, expected)

# Future behavior with no warning
expected = np.array(
[pd.Timestamp("2000-01-01", tz=tz), pd.Timestamp("2000-01-02", tz=tz)]
)
with tm.assert_produces_warning(None):
result = np.asarray(ser, dtype=object)
result = np.asarray(ser, dtype=object)

tm.assert_numpy_array_equal(result, expected)