Skip to content

Commit

Permalink
DEPR: Series(dt64naive, dtype=dt64tz) -> will match DatetimeIndex (pa…
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and TLouf committed Jun 1, 2021
1 parent aae512c commit 58013f4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
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 @@ -697,6 +697,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

0 comments on commit 58013f4

Please sign in to comment.