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: Series(dt64naive, dtype=dt64tz) -> will match DatetimeIndex #41662

Merged
merged 4 commits into from
May 31, 2021
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.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ Deprecations
- Deprecated passing arguments (apart from ``value``) as positional in :meth:`DataFrame.fillna` and :meth:`Series.fillna` (:issue:`41485`)
- Deprecated passing arguments as positional in :meth:`DataFrame.reset_index` (other than ``"level"``) and :meth:`Series.reset_index` (:issue:`41485`)
- 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`)
- 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`)
- Deprecated passing arguments as positional in :meth:`DataFrame.set_axis` and :meth:`Series.set_axis` (other than ``"labels"``) (:issue:`41485`)
- Deprecated passing arguments as positional in :meth:`DataFrame.where` and :meth:`Series.where` (other than ``"cond"`` and ``"other"``) (:issue:`41485`)
- Deprecated passing arguments as positional (other than ``filepath_or_buffer``) in :func:`read_csv` (:issue:`41485`)
Expand Down
16 changes: 16 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,22 @@ def maybe_cast_to_datetime(
# Numeric values are UTC at this point,
# so localize and convert
# equiv: Series(dta).astype(dtype) # though deprecated
if getattr(vdtype, "kind", None) == "M":
# GH#24559, GH#33401 deprecate behavior inconsistent
# with DatetimeArray/DatetimeIndex
warnings.warn(
"In a future version, constructing a Series "
"from datetime64[ns] data and a "
"DatetimeTZDtype will interpret the data "
"as wall-times instead of "
"UTC times, matching the behavior of "
"DatetimeIndex. To treat the data as UTC "
"times, use pd.Series(data).dt"
".tz_localize('UTC').tz_convert(dtype.tz) "
"or pd.Series(data.view('int64'), dtype=dtype)",
FutureWarning,
stacklevel=5,
)

value = dta.tz_localize("UTC").tz_convert(dtype.tz)
except OutOfBoundsDatetime:
Expand Down
16 changes: 15 additions & 1 deletion pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,21 @@ def test_construction_consistency(self):
result = Series(ser.dt.tz_convert("UTC"), dtype=ser.dtype)
tm.assert_series_equal(result, ser)

result = Series(ser.values, dtype=ser.dtype)
msg = "will interpret the data as wall-times"
with tm.assert_produces_warning(FutureWarning, match=msg):
# deprecate behavior inconsistent with DatetimeIndex GH#33401
result = Series(ser.values, dtype=ser.dtype)
tm.assert_series_equal(result, ser)

with tm.assert_produces_warning(None):
# one suggested alternative to the deprecated usage
middle = Series(ser.values).dt.tz_localize("UTC")
result = middle.dt.tz_convert(ser.dtype.tz)
tm.assert_series_equal(result, ser)

with tm.assert_produces_warning(None):
# the other suggested alternative to the deprecated usage
result = Series(ser.values.view("int64"), dtype=ser.dtype)
tm.assert_series_equal(result, ser)

@pytest.mark.parametrize(
Expand Down