-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DEPR: astype dt64<->dt64tz #39258
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: astype dt64<->dt64tz #39258
Changes from 5 commits
4e31298
c555220
47a3a95
1a60424
b162a1b
13f35de
02a4a43
0e4c3ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -942,6 +942,32 @@ def coerce_indexer_dtype(indexer, categories): | |
return ensure_int64(indexer) | ||
|
||
|
||
def _find_level() -> int: | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Find the appropriate stacklevel with which to issue a warning for astype. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would move this elsewhere, maybe to pandas.util._validation (or similar). maybe just pass the names in as needed e.g. L958 (or hard-code ok too) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe util._exceptions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated + green |
||
""" | ||
import inspect | ||
jbrockmendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
stack = inspect.stack() | ||
|
||
# find the lowest-level "astype" call that got us here | ||
for n in range(2, 6): | ||
if stack[n].function == "astype": | ||
break | ||
|
||
while stack[n].function in ["astype", "apply", "_astype"]: | ||
# e.g. | ||
# bump up Block.astype -> BlockManager.astype -> NDFrame.astype | ||
# bump up Datetime.Array.astype -> DatetimeIndex.astype | ||
n += 1 | ||
|
||
if stack[n].function == "__init__": | ||
# Series.__init__ | ||
n += 1 | ||
|
||
return n | ||
|
||
|
||
def astype_dt64_to_dt64tz( | ||
values: ArrayLike, dtype: DtypeObj, copy: bool, via_utc: bool = False | ||
) -> DatetimeArray: | ||
|
@@ -964,6 +990,16 @@ def astype_dt64_to_dt64tz( | |
if copy: | ||
# this should be the only copy | ||
values = values.copy() | ||
|
||
level = _find_level() | ||
warnings.warn( | ||
"Using .astype to convert from timezone-naive dtype to " | ||
"timezone-aware dtype is deprecated and will raise in a " | ||
"future version. Use ser.dt.tz_localize instead.", | ||
FutureWarning, | ||
stacklevel=level, | ||
) | ||
|
||
# FIXME: GH#33401 this doesn't match DatetimeArray.astype, which | ||
# goes through the `not via_utc` path | ||
return values.tz_localize("UTC").tz_convert(dtype.tz) | ||
|
@@ -973,6 +1009,15 @@ def astype_dt64_to_dt64tz( | |
|
||
if values.tz is None and aware: | ||
dtype = cast(DatetimeTZDtype, dtype) | ||
level = _find_level() | ||
warnings.warn( | ||
"Using .astype to convert from timezone-naive dtype to " | ||
"timezone-aware dtype is deprecated and will raise in a " | ||
"future version. Use obj.tz_localize instead.", | ||
FutureWarning, | ||
stacklevel=level, | ||
) | ||
|
||
return values.tz_localize(dtype.tz) | ||
|
||
elif aware: | ||
|
@@ -984,6 +1029,16 @@ def astype_dt64_to_dt64tz( | |
return result | ||
|
||
elif values.tz is not None: | ||
level = _find_level() | ||
warnings.warn( | ||
"Using .astype to convert from timezone-aware dtype to " | ||
"timezone-naive dtype is deprecated and will raise in a " | ||
"future version. Use obj.tz_localize(None) or " | ||
"obj.tz_convert('UTC').tz_localize(None) instead", | ||
FutureWarning, | ||
stacklevel=level, | ||
) | ||
|
||
result = values.tz_convert("UTC").tz_localize(None) | ||
if copy: | ||
result = result.copy() | ||
|
Uh oh!
There was an error while loading. Please reload this page.