Skip to content

Propagate name in more cases when using to_datetime #21848

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

Closed
wants to merge 2 commits into from
Closed
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
36 changes: 17 additions & 19 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ def _convert_and_box_cache(arg, cache_array, box, errors, name=None):
result = Series(arg).map(cache_array)
if box:
if errors == 'ignore':
return Index(result)
return Index(result, name=name)
else:
return DatetimeIndex(result, name=name)
return result.values


def _return_parsed_timezone_results(result, timezones, box, tz):
def _return_parsed_timezone_results(result, timezones, tz):
"""
Return results from array_strptime if a %z or %Z directive was passed.

Expand All @@ -114,28 +114,21 @@ def _return_parsed_timezone_results(result, timezones, box, tz):
int64 date representations of the dates
timezones : ndarray
pytz timezone objects
box : boolean
True boxes result as an Index-like, False returns an ndarray
tz : object
None or pytz timezone object

Returns
-------
tz_result : ndarray of parsed dates with timezone
Returns:

- Index-like if box=True
- ndarray of Timestamps if box=False

"""
if tz is not None:
raise ValueError("Cannot pass a tz argument when "
"parsing strings with timezone "
"information.")
tz_results = np.array([Timestamp(res).tz_localize(zone) for res, zone
in zip(result, timezones)])
if box:
from pandas import Index
return Index(tz_results)
tz_results = np.array([
Timestamp(res).tz_localize(zone)
for res, zone in zip(result, timezones)
])
return tz_results


Expand Down Expand Up @@ -208,9 +201,9 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
if box:
if errors == 'ignore':
from pandas import Index
return Index(result)

return DatetimeIndex(result, tz=tz, name=name)
return Index(result, name=name)
else:
return DatetimeIndex(result, tz=tz, name=name)
return result
elif getattr(arg, 'ndim', 1) > 1:
raise TypeError('arg must be a string, datetime, list, tuple, '
Expand Down Expand Up @@ -250,8 +243,13 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
result, timezones = array_strptime(
arg, format, exact=exact, errors=errors)
if '%Z' in format or '%z' in format:
return _return_parsed_timezone_results(
result, timezones, box, tz)
tz_results = _return_parsed_timezone_results(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing return here actually

result, timezones, tz)
if box:
from pandas import Index
return Index(tz_results, name=name)
else:
return tz_results
except tslibs.OutOfBoundsDatetime:
if errors == 'raise':
raise
Expand Down