Skip to content

REF: prelim for fixing array_to_datetime #24000

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 2 commits into from
Nov 29, 2018
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
5 changes: 1 addition & 4 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,7 @@ def array_with_unit_to_datetime(ndarray values, object unit,
@cython.boundscheck(False)
cpdef array_to_datetime(ndarray[object] values, str errors='raise',
bint dayfirst=False, bint yearfirst=False,
object format=None, object utc=None,
bint require_iso8601=False):
object utc=None, bint require_iso8601=False):
"""
Converts a 1D array of date-like values to a numpy array of either:
1) datetime64[ns] data
Expand All @@ -488,8 +487,6 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
dayfirst parsing behavior when encountering datetime strings
yearfirst : bool, default False
yearfirst parsing behavior when encountering datetime strings
format : str, default None
format of the string to parse
utc : bool, default None
indicator whether the dates should be UTC
require_iso8601 : bool, default False
Expand Down
54 changes: 28 additions & 26 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
require_iso8601 = not infer_datetime_format
format = None

tz_parsed = None
result = None
try:
result = None

if format is not None:
# shortcut formatting here
if format == '%Y%m%d':
Expand Down Expand Up @@ -267,7 +267,8 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
raise
result = arg

if result is None and (format is None or infer_datetime_format):
if result is None:
assert format is None or infer_datetime_format
result, tz_parsed = tslib.array_to_datetime(
arg,
errors=errors,
Expand All @@ -276,36 +277,37 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
yearfirst=yearfirst,
require_iso8601=require_iso8601
)
if tz_parsed is not None:
if box:
# We can take a shortcut since the datetime64 numpy array
# is in UTC
return DatetimeIndex._simple_new(result, name=name,
tz=tz_parsed)
else:
# Convert the datetime64 numpy array to an numpy array
# of datetime objects
result = [Timestamp(ts, tz=tz_parsed).to_pydatetime()
for ts in result]
return np.array(result, dtype=object)

if box:
# Ensure we return an Index in all cases where box=True
if is_datetime64_dtype(result):
return DatetimeIndex(result, tz=tz, name=name)
elif is_object_dtype(result):
# e.g. an Index of datetime objects
from pandas import Index
return Index(result, name=name)
return result

except ValueError as e:
# Fallback to try to convert datetime objects
try:
values, tz = conversion.datetime_to_datetime64(arg)
return DatetimeIndex._simple_new(values, name=name, tz=tz)
except (ValueError, TypeError):
raise e

if tz_parsed is not None:
if box:
# We can take a shortcut since the datetime64 numpy array
# is in UTC
return DatetimeIndex._simple_new(result, name=name,
tz=tz_parsed)
else:
# Convert the datetime64 numpy array to an numpy array
# of datetime objects
result = [Timestamp(ts, tz=tz_parsed).to_pydatetime()
for ts in result]
return np.array(result, dtype=object)

if box:
# Ensure we return an Index in all cases where box=True
if is_datetime64_dtype(result):
return DatetimeIndex(result, tz=tz, name=name)
elif is_object_dtype(result):
# e.g. an Index of datetime objects
from pandas import Index
return Index(result, name=name)
return result


def _adjust_to_origin(arg, origin, unit):
"""
Expand Down