8000 DEPR: box arg in to_datetime by jbrockmendel · Pull Request #30111 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

DEPR: box arg in to_datetime #30111

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 4 commits into from
Dec 6, 2019
Merged
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
Next Next commit
remove box arg, revert renames
  • Loading branch information
jbrockmendel committed Dec 6, 2019
commit ef222e9d07b5b6208c6d097e2086d7f3ee2c827b
55 changes: 24 additions & 31 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
parse_time_string,
)
from pandas._libs.tslibs.strptime import array_strptime
from pandas.util._decorators import deprecate_kwarg

from pandas.core.dtypes.common import (
ensure_object,
Expand All @@ -41,7 +40,6 @@
from pandas.core import algorithms
from pandas.core.algorithms import unique


# ---------------------------------------------------------------------
# types used in annotations

Expand Down Expand Up @@ -154,11 +152,11 @@ def _maybe_cache(arg, format, cache, convert_listlike):
return cache_array


def _wrap_as_indexlike(
def _box_as_indexlike(
dt_array: ArrayLike, utc: Optional[bool] = None, name: Optional[str] = None
) -> Union[ABCIndex, ABCDatetimeIndex]:
"""
Properly wraps the ndarray of datetimes to DatetimeIndex
Properly boxes the ndarray of datetimes to DatetimeIndex
if it is possible or to generic Index instead

Parameters
Expand All @@ -184,7 +182,7 @@ def _wrap_as_indexlike(
return Index(dt_array, name=name)


def _convert_and_cache(
def _convert_and_box_cache(
arg: DatetimeScalarOrArrayConvertible,
cache_array: ABCSeries,
name: Optional[str] = None,
Expand All @@ -207,7 +205,7 @@ def _convert_and_cache(
from pandas import Series

result = Series(arg).map(cache_array)
return _wrap_as_indexlike(result, utc=None, name=name)
return _box_as_indexlike(result, utc=None, name=name)


def _return_parsed_timezone_results(result, timezones, tz, name):
Expand Down Expand Up @@ -263,8 +261,6 @@ def _convert_listlike_datetimes(
----------
arg : list, tuple, ndarray, Series, Index
date to be parced
box : boolean
True boxes result as an Index-like, False returns an ndarray
name : object
None or string for the Index name
tz : object
Expand Down Expand Up @@ -321,26 +317,25 @@ def _convert_listlike_datetimes(
raise ValueError("cannot specify both format and unit")
arg = getattr(arg, "values", arg)
result, tz_parsed = tslib.array_with_unit_to_datetime(arg, unit, errors=errors)
if True:
if errors == "ignore":
from pandas import Index
if errors == "ignore":
from pandas import Index

result = Index(result, name=name)
result = Index(result, name=name)
else:
result = DatetimeIndex(result, name=name)
# GH 23758: We may still need to localize the result with tz
# GH 25546: Apply tz_parsed first (from arg), then tz (from caller)
# result will be naive but in UTC
try:
result = result.tz_localize("UTC").tz_convert(tz_parsed)
except AttributeError:
# Regular Index from 'ignore' path
return result
if tz is not None:
if result.tz is None:
result = result.tz_localize(tz)
else:
result = DatetimeIndex(result, name=name)
# GH 23758: We may still need to localize the result with tz
# GH 25546: Apply tz_parsed first (from arg), then tz (from caller)
# result will be naive but in UTC
try:
result = result.tz_localize("UTC").tz_convert(tz_parsed)
except AttributeError:
# Regular Index from 'ignore' path
return result
if tz is not None:
if result.tz is None:
result = result.tz_localize(tz)
else:
result = result.tz_convert(tz)
result = result.tz_convert(tz)
return result
elif getattr(arg, "ndim", 1) > 1:
raise TypeError(
Expand Down Expand Up @@ -443,7 +438,7 @@ def _convert_listlike_datetimes(
return DatetimeIndex._simple_new(result, name=name, tz=tz_parsed)

utc = tz == "utc"
return _wrap_as_indexlike(result, utc=utc, name=name)
return _box_as_indexlike(result, utc=utc, name=name)


def _adjust_to_origin(arg, origin, unit):
Expand Down Expand Up @@ -525,14 +520,12 @@ def _adjust_to_origin(arg, origin, unit):
return arg


@deprecate_kwarg(old_arg_name="box", new_arg_name=None)
def to_datetime(
arg,
errors="raise",
dayfirst=False,
yearfirst=False,
utc=None,
box=True,
format=None,
exact=True,
unit=None,
Expand Down Expand Up @@ -729,14 +722,14 @@ def to_datetime(
elif isinstance(arg, ABCIndexClass):
cache_array = _maybe_cache(arg, format, cache, convert_listlike)
if not cache_array.empty:
result = _convert_and_cache(arg, cache_array, name=arg.name)
result = _convert_and_box_cache(arg, cache_array, name=arg.name)
else:
convert_listlike = partial(convert_listlike, name=arg.name)
result = convert_listlike(arg, format)
elif is_list_like(arg):
cache_array = _maybe_cache(arg, format, cache, convert_listlike)
if not cache_array.empty:
result = _convert_and_cache(arg, cache_array)
result = _convert_and_box_cache(arg, cache_array)
else:
result = convert_listlike(arg, format)
else:
Expand Down
0