Skip to content

Commit 4b967a8

Browse files
jbrockmendelJulianWgs
authored andcommitted
DEPR: Series(dt64naive, dtype=dt64tz) -> will match DatetimeIndex (pandas-dev#41662)
1 parent d59daca commit 4b967a8

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ Deprecations
697697
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)
698698
- Deprecated passing arguments as positional in :meth:`DataFrame.reset_index` (other than ``"level"``) and :meth:`Series.reset_index` (:issue:`41485`)
699699
- Deprecated construction of :class:`Series` or :class:`DataFrame` with ``DatetimeTZDtype`` data and ``datetime64[ns]`` dtype. Use ``Series(data).dt.tz_localize(None)`` instead (:issue:`41555`,:issue:`33401`)
700+
- In a future version, constructing :class:`Series` or :class:`DataFrame` with ``datetime64[ns]`` data and ``DatetimeTZDtype`` will treat the data as wall-times instead of as UTC times (matching DatetimeIndex behavior). To treat the data as UTC times, use ``pd.Series(data).dt.tz_localize("UTC").dt.tz_convert(dtype.tz)`` or ``pd.Series(data.view("int64"), dtype=dtype)`` (:issue:`33401`)
700701
- Deprecated passing arguments as positional in :meth:`DataFrame.set_axis` and :meth:`Series.set_axis` (other than ``"labels"``) (:issue:`41485`)
701702
- Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`)
702703
- Deprecated passing arguments as positional (other than ``filepath_or_buffer``) in :func:`read_csv` (:issue:`41485`)

pandas/core/dtypes/cast.py

+16
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,22 @@ def maybe_cast_to_datetime(
16571657
# Numeric values are UTC at this point,
16581658
# so localize and convert
16591659
# equiv: Series(dta).astype(dtype) # though deprecated
1660+
if getattr(vdtype, "kind", None) == "M":
1661+
# GH#24559, GH#33401 deprecate behavior inconsistent
1662+
# with DatetimeArray/DatetimeIndex
1663+
warnings.warn(
1664+
"In a future version, constructing a Series "
1665+
"from datetime64[ns] data and a "
1666+
"DatetimeTZDtype will interpret the data "
1667+
"as wall-times instead of "
1668+
"UTC times, matching the behavior of "
1669+
"DatetimeIndex. To treat the data as UTC "
1670+
"times, use pd.Series(data).dt"
1671+
".tz_localize('UTC').tz_convert(dtype.tz) "
1672+
"or pd.Series(data.view('int64'), dtype=dtype)",
1673+
FutureWarning,
1674+
stacklevel=5,
1675+
)
16601676

16611677
value = dta.tz_localize("UTC").tz_convert(dtype.tz)
16621678
except OutOfBoundsDatetime:

pandas/tests/series/test_constructors.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,21 @@ def test_construction_consistency(self):
10941094
result = Series(ser.dt.tz_convert("UTC"), dtype=ser.dtype)
10951095
tm.assert_series_equal(result, ser)
10961096

1097-
result = Series(ser.values, dtype=ser.dtype)
1097+
msg = "will interpret the data as wall-times"
1098+
with tm.assert_produces_warning(FutureWarning, match=msg):
1099+
# deprecate behavior inconsistent with DatetimeIndex GH#33401
1100+
result = Series(ser.values, dtype=ser.dtype)
1101+
tm.assert_series_equal(result, ser)
1102+
1103+
with tm.assert_produces_warning(None):
1104+
# one suggested alternative to the deprecated usage
1105+
middle = Series(ser.values).dt.tz_localize("UTC")
1106+
result = middle.dt.tz_convert(ser.dtype.tz)
1107+
tm.assert_series_equal(result, ser)
1108+
1109+
with tm.assert_produces_warning(None):
1110+
# the other suggested alternative to the deprecated usage
1111+
result = Series(ser.values.view("int64"), dtype=ser.dtype)
10981112
tm.assert_series_equal(result, ser)
10991113

11001114
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)