Skip to content

Commit 03f6f40

Browse files
committed
BUG: Index name lost in conv pandas-dev#10875
in to_datetime, to_timedelta
1 parent ba0704f commit 03f6f40

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

doc/source/whatsnew/v0.17.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ Bug Fixes
770770
- Bug in ``filter`` (regression from 0.16.0) and ``transform`` when grouping on multiple keys, one of which is datetime-like (:issue:`10114`)
771771

772772

773-
773+
- Bug in ``to_datetime`` and ``to_timedelta`` causing ``Index`` name to be lost (:issue:`10875`)
774774

775775

776776
- Bug that caused segfault when resampling an empty Series (:issue:`10228`)

pandas/tests/test_index.py

+5
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,11 @@ def test_equals_op_multiindex(self):
17321732
df.index == index_a
17331733
tm.assert_numpy_array_equal(index_a == mi3, np.array([False, False, False]))
17341734

1735+
def test_conversion_preserves_name(self):
1736+
#GH 10875
1737+
i = pd.Index(['01:02:03', '01:02:04'], name='label')
1738+
self.assertEqual(i.name, pd.to_datetime(i).name)
1739+
self.assertEqual(i.name, pd.to_timedelta(i).name)
17351740

17361741
class TestCategoricalIndex(Base, tm.TestCase):
17371742
_holder = CategoricalIndex

pandas/tseries/timedeltas.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pandas import compat
99
from pandas.core.common import (ABCSeries, is_integer_dtype,
1010
is_timedelta64_dtype, is_list_like,
11-
isnull, _ensure_object)
11+
isnull, _ensure_object, ABCIndexClass)
1212
from pandas.util.decorators import deprecate_kwarg
1313

1414
@deprecate_kwarg(old_arg_name='coerce', new_arg_name='errors',
@@ -35,7 +35,7 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise', coerce=None):
3535
"""
3636
unit = _validate_timedelta_unit(unit)
3737

38-
def _convert_listlike(arg, box, unit):
38+
def _convert_listlike(arg, box, unit, name=None):
3939

4040
if isinstance(arg, (list,tuple)) or ((hasattr(arg,'__iter__') and not hasattr(arg,'dtype'))):
4141
arg = np.array(list(arg), dtype='O')
@@ -51,7 +51,7 @@ def _convert_listlike(arg, box, unit):
5151

5252
if box:
5353
from pandas import TimedeltaIndex
54-
value = TimedeltaIndex(value,unit='ns')
54+
value = TimedeltaIndex(value,unit='ns', name=name)
5555
return value
5656

5757
if arg is None:
@@ -60,6 +60,8 @@ def _convert_listlike(arg, box, unit):
6060
from pandas import Series
6161
values = _convert_listlike(arg.values, box=False, unit=unit)
6262
return Series(values, index=arg.index, name=arg.name, dtype='m8[ns]')
63+
elif isinstance(arg, ABCIndexClass):
64+
return _convert_listlike(arg, box=box, unit=unit, name=arg.name)
6365
elif is_list_like(arg):
6466
return _convert_listlike(arg, box=box, unit=unit)
6567

pandas/tseries/tools.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pandas.tslib as tslib
99
import pandas.core.common as com
1010
from pandas.compat import StringIO, callable
11+
from pandas.core.common import ABCIndexClass
1112
import pandas.compat as compat
1213
from pandas.util.decorators import deprecate_kwarg
1314

@@ -277,7 +278,7 @@ def _to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
277278
from pandas.core.series import Series
278279
from pandas.tseries.index import DatetimeIndex
279280

280-
def _convert_listlike(arg, box, format):
281+
def _convert_listlike(arg, box, format, name=None):
281282

282283
if isinstance(arg, (list,tuple)):
283284
arg = np.array(arg, dtype='O')
@@ -286,15 +287,15 @@ def _convert_listlike(arg, box, format):
286287
if com.is_datetime64_ns_dtype(arg):
287288
if box and not isinstance(arg, DatetimeIndex):
288289
try:
289-
return DatetimeIndex(arg, tz='utc' if utc else None)
290+
return DatetimeIndex(arg, tz='utc' if utc else None, name=name)
290291
except ValueError:
291292
pass
292293

293294
return arg
294295
elif format is None and com.is_integer_dtype(arg) and unit=='ns':
295296
result = arg.astype('datetime64[ns]')
296297
if box:
297-
return DatetimeIndex(result, tz='utc' if utc else None)
298+
return DatetimeIndex(result, tz='utc' if utc else None, name=name)
298299

299300
return result
300301

@@ -355,13 +356,13 @@ def _convert_listlike(arg, box, format):
355356
require_iso8601=require_iso8601)
356357

357358
if com.is_datetime64_dtype(result) and box:
358-
result = DatetimeIndex(result, tz='utc' if utc else None)
359+
result = DatetimeIndex(result, tz='utc' if utc else None, name=name)
359360
return result
360361

361362
except ValueError as e:
362363
try:
363364
values, tz = tslib.datetime_to_datetime64(arg)
364-
return DatetimeIndex._simple_new(values, None, tz=tz)
365+
return DatetimeIndex._simple_new(values, name=name, tz=tz)
365366
except (ValueError, TypeError):
366367
raise e
367368

@@ -372,6 +373,8 @@ def _convert_listlike(arg, box, format):
372373
elif isinstance(arg, Series):
373374
values = _convert_listlike(arg.values, False, format)
374375
return Series(values, index=arg.index, name=arg.name)
376+
elif isinstance(arg, ABCIndexClass):
377+
return _convert_listlike(arg, box, format, name=arg.name)
375378
elif com.is_list_like(arg):
376379
return _convert_listlike(arg, box, format)
377380

0 commit comments

Comments
 (0)