Skip to content

Commit 5ce06b5

Browse files
authored
BUG: to_datetime preserves name of Index argument in the result (#22918)
* BUG: to_datetime preserves name of Index argument in the result * correct test
1 parent a277e4a commit 5ce06b5

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ Datetimelike
655655
- Bug in :class:`DatetimeIndex` incorrectly allowing indexing with ``Timedelta`` object (:issue:`20464`)
656656
- Bug in :class:`DatetimeIndex` where frequency was being set if original frequency was ``None`` (:issue:`22150`)
657657
- Bug in rounding methods of :class:`DatetimeIndex` (:meth:`~DatetimeIndex.round`, :meth:`~DatetimeIndex.ceil`, :meth:`~DatetimeIndex.floor`) and :class:`Timestamp` (:meth:`~Timestamp.round`, :meth:`~Timestamp.ceil`, :meth:`~Timestamp.floor`) could give rise to loss of precision (:issue:`22591`)
658+
- Bug in :func:`to_datetime` with an :class:`Index` argument that would drop the ``name`` from the result (:issue:`21697`)
658659

659660
Timedelta
660661
^^^^^^^^^

pandas/core/tools/datetimes.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ def _convert_and_box_cache(arg, cache_array, box, errors, name=None):
9999
result = Series(arg).map(cache_array)
100100
if box:
101101
if errors == 'ignore':
102-
return Index(result)
102+
return Index(result, name=name)
103103
else:
104104
return DatetimeIndex(result, name=name)
105105
return result.values
106106

107107

108-
def _return_parsed_timezone_results(result, timezones, box, tz):
108+
def _return_parsed_timezone_results(result, timezones, box, tz, name):
109109
"""
110110
Return results from array_strptime if a %z or %Z directive was passed.
111111
@@ -119,6 +119,9 @@ def _return_parsed_timezone_results(result, timezones, box, tz):
119119
True boxes result as an Index-like, False returns an ndarray
120120
tz : object
121121
None or pytz timezone object
122+
name : string, default None
123+
Name for a DatetimeIndex
124+
122125
Returns
123126
-------
124127
tz_result : ndarray of parsed dates with timezone
@@ -136,7 +139,7 @@ def _return_parsed_timezone_results(result, timezones, box, tz):
136139
in zip(result, timezones)])
137140
if box:
138141
from pandas import Index
139-
return Index(tz_results)
142+
return Index(tz_results, name=name)
140143
return tz_results
141144

142145

@@ -209,7 +212,7 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
209212
if box:
210213
if errors == 'ignore':
211214
from pandas import Index
212-
return Index(result)
215+
return Index(result, name=name)
213216

214217
return DatetimeIndex(result, tz=tz, name=name)
215218
return result
@@ -252,7 +255,7 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
252255
arg, format, exact=exact, errors=errors)
253256
if '%Z' in format or '%z' in format:
254257
return _return_parsed_timezone_results(
255-
result, timezones, box, tz)
258+
result, timezones, box, tz, name)
256259
except tslibs.OutOfBoundsDatetime:
257260
if errors == 'raise':
258261
raise

pandas/tests/indexes/datetimes/test_tools.py

+17
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ def test_to_datetime_parse_timezone_malformed(self, offset):
233233
with pytest.raises(ValueError):
234234
pd.to_datetime([date], format=fmt)
235235

236+
def test_to_datetime_parse_timezone_keeps_name(self):
237+
# GH 21697
238+
fmt = '%Y-%m-%d %H:%M:%S %z'
239+
arg = pd.Index(['2010-01-01 12:00:00 Z'], name='foo')
240+
result = pd.to_datetime(arg, format=fmt)
241+
expected = pd.DatetimeIndex(['2010-01-01 12:00:00'], tz='UTC',
242+
name='foo')
243+
tm.assert_index_equal(result, expected)
244+
236245

237246
class TestToDatetime(object):
238247
def test_to_datetime_pydatetime(self):
@@ -765,6 +774,14 @@ def test_unit_rounding(self, cache):
765774
expected = pd.Timestamp('2015-06-19 19:55:31.877000093')
766775
assert result == expected
767776

777+
@pytest.mark.parametrize('cache', [True, False])
778+
def test_unit_ignore_keeps_name(self, cache):
779+
# GH 21697
780+
expected = pd.Index([15e9] * 2, name='name')
781+
result = pd.to_datetime(expected, errors='ignore', box=True, unit='s',
782+
cache=cache)
783+
tm.assert_index_equal(result, expected)
784+
768785
@pytest.mark.parametrize('cache', [True, False])
769786
def test_dataframe(self, cache):
770787

0 commit comments

Comments
 (0)