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

DEPR: tz-aware array #30563

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
@@ -725,7 +725,9 @@ 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`)
-
- Removed the previously deprecated behavior of timezone-aware ``Series`` and ``Index`` being converted to UTC and
returning a ``datetime64[ns]`` dtype ndarray when converted to an array. Now an ``object`` dtype ndarray
containing :class:`Timestamp` with the original timezone is returned (:issue:`24596`)

.. _whatsnew_1000.performance:

15 changes: 0 additions & 15 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 0 additions & 16 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
@@ -33,7 +33,6 @@
)
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCDatetimeArray,
ABCDatetimeIndex,
ABCSeries,
ABCSparseArray,
@@ -716,21 +715,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)

# ----------------------------------------------------------------------
1 change: 0 additions & 1 deletion pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
@@ -568,7 +568,6 @@ def test_comparison_tzawareness_compat_scalars(self, op, box_with_array):
# Raising in __eq__ will fallback to NumPy, which warns, fails,
# then re-raises the original exception. So we just need to ignore.
@pytest.mark.filterwarnings("ignore:elementwise comp:DeprecationWarning")
@pytest.mark.filterwarnings("ignore:Converting timezone-aware:FutureWarning")
def test_scalar_comparison_tzawareness(
self, op, other, tz_aware_fixture, box_with_array
):
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
@@ -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]))

25 changes: 8 additions & 17 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
@@ -393,42 +393,33 @@ 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)

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)

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]")

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

# Future behavior with no warning
# object-dtype with a tz by default
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)
tm.assert_numpy_array_equal(result, expected)

result = np.asarray(idx, dtype=object)
tm.assert_numpy_array_equal(result, expected)

def test_to_frame_datetime_tz(self):
25 changes: 7 additions & 18 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
@@ -728,17 +728,14 @@ def test_view_tz(self):
tm.assert_series_equal(result, expected)

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)
@@ -747,23 +744,15 @@ 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="M8[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]")

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)
# object-dtype with a tz by default
result = np.asarray(ser)
tm.assert_numpy_array_equal(result, expected)

result = np.asarray(ser, dtype=object)
tm.assert_numpy_array_equal(result, expected)