Skip to content

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

Merged
merged 8 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
REF: move find_stack_level to util._exceptions
  • Loading branch information
jbrockmendel committed Jan 20, 2021
commit 0e4c3caf0dfee14eee192e339411327790f999e7
26 changes: 1 addition & 25 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from contextlib import suppress
from datetime import datetime, timedelta
import inspect
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -39,6 +38,7 @@
tz_compare,
)
from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, Scalar
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -943,30 +943,6 @@ def coerce_indexer_dtype(indexer, categories):
return ensure_int64(indexer)


def find_stack_level() -> int:
"""
Find the appropriate stacklevel with which to issue a warning for astype.
"""
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:
Expand Down
25 changes: 25 additions & 0 deletions pandas/util/_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import inspect
from typing import Tuple


Expand All @@ -17,3 +18,27 @@ def rewrite_exception(old_name: str, new_name: str):
args = args + err.args[1:]
err.args = args
raise


def find_stack_level() -> int:
"""
Find the appropriate stacklevel with which to issue a warning for astype.
"""
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