From 03f6f407b27619c51984b930d37387b7043b29ae Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 30 Aug 2015 17:39:39 -0500 Subject: [PATCH] BUG: Index name lost in conv #10875 in to_datetime, to_timedelta --- doc/source/whatsnew/v0.17.0.txt | 2 +- pandas/tests/test_index.py | 5 +++++ pandas/tseries/timedeltas.py | 8 +++++--- pandas/tseries/tools.py | 13 ++++++++----- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 3e81a923a114c..834514b603a80 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -770,7 +770,7 @@ Bug Fixes - Bug in ``filter`` (regression from 0.16.0) and ``transform`` when grouping on multiple keys, one of which is datetime-like (:issue:`10114`) - +- Bug in ``to_datetime`` and ``to_timedelta`` causing ``Index`` name to be lost (:issue:`10875`) - Bug that caused segfault when resampling an empty Series (:issue:`10228`) diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index b54e129c7a4e1..17aa6c30cd185 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -1732,6 +1732,11 @@ def test_equals_op_multiindex(self): df.index == index_a tm.assert_numpy_array_equal(index_a == mi3, np.array([False, False, False])) + def test_conversion_preserves_name(self): + #GH 10875 + i = pd.Index(['01:02:03', '01:02:04'], name='label') + self.assertEqual(i.name, pd.to_datetime(i).name) + self.assertEqual(i.name, pd.to_timedelta(i).name) class TestCategoricalIndex(Base, tm.TestCase): _holder = CategoricalIndex diff --git a/pandas/tseries/timedeltas.py b/pandas/tseries/timedeltas.py index 886d6ff42ced6..282e1d603ed84 100644 --- a/pandas/tseries/timedeltas.py +++ b/pandas/tseries/timedeltas.py @@ -8,7 +8,7 @@ from pandas import compat from pandas.core.common import (ABCSeries, is_integer_dtype, is_timedelta64_dtype, is_list_like, - isnull, _ensure_object) + isnull, _ensure_object, ABCIndexClass) from pandas.util.decorators import deprecate_kwarg @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): """ unit = _validate_timedelta_unit(unit) - def _convert_listlike(arg, box, unit): + def _convert_listlike(arg, box, unit, name=None): if isinstance(arg, (list,tuple)) or ((hasattr(arg,'__iter__') and not hasattr(arg,'dtype'))): arg = np.array(list(arg), dtype='O') @@ -51,7 +51,7 @@ def _convert_listlike(arg, box, unit): if box: from pandas import TimedeltaIndex - value = TimedeltaIndex(value,unit='ns') + value = TimedeltaIndex(value,unit='ns', name=name) return value if arg is None: @@ -60,6 +60,8 @@ def _convert_listlike(arg, box, unit): from pandas import Series values = _convert_listlike(arg.values, box=False, unit=unit) return Series(values, index=arg.index, name=arg.name, dtype='m8[ns]') + elif isinstance(arg, ABCIndexClass): + return _convert_listlike(arg, box=box, unit=unit, name=arg.name) elif is_list_like(arg): return _convert_listlike(arg, box=box, unit=unit) diff --git a/pandas/tseries/tools.py b/pandas/tseries/tools.py index 6f08448b47b1e..efd1ff9ba34fd 100644 --- a/pandas/tseries/tools.py +++ b/pandas/tseries/tools.py @@ -8,6 +8,7 @@ import pandas.tslib as tslib import pandas.core.common as com from pandas.compat import StringIO, callable +from pandas.core.common import ABCIndexClass import pandas.compat as compat from pandas.util.decorators import deprecate_kwarg @@ -277,7 +278,7 @@ def _to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, from pandas.core.series import Series from pandas.tseries.index import DatetimeIndex - def _convert_listlike(arg, box, format): + def _convert_listlike(arg, box, format, name=None): if isinstance(arg, (list,tuple)): arg = np.array(arg, dtype='O') @@ -286,7 +287,7 @@ def _convert_listlike(arg, box, format): if com.is_datetime64_ns_dtype(arg): if box and not isinstance(arg, DatetimeIndex): try: - return DatetimeIndex(arg, tz='utc' if utc else None) + return DatetimeIndex(arg, tz='utc' if utc else None, name=name) except ValueError: pass @@ -294,7 +295,7 @@ def _convert_listlike(arg, box, format): elif format is None and com.is_integer_dtype(arg) and unit=='ns': result = arg.astype('datetime64[ns]') if box: - return DatetimeIndex(result, tz='utc' if utc else None) + return DatetimeIndex(result, tz='utc' if utc else None, name=name) return result @@ -355,13 +356,13 @@ def _convert_listlike(arg, box, format): require_iso8601=require_iso8601) if com.is_datetime64_dtype(result) and box: - result = DatetimeIndex(result, tz='utc' if utc else None) + result = DatetimeIndex(result, tz='utc' if utc else None, name=name) return result except ValueError as e: try: values, tz = tslib.datetime_to_datetime64(arg) - return DatetimeIndex._simple_new(values, None, tz=tz) + return DatetimeIndex._simple_new(values, name=name, tz=tz) except (ValueError, TypeError): raise e @@ -372,6 +373,8 @@ def _convert_listlike(arg, box, format): elif isinstance(arg, Series): values = _convert_listlike(arg.values, False, format) return Series(values, index=arg.index, name=arg.name) + elif isinstance(arg, ABCIndexClass): + return _convert_listlike(arg, box, format, name=arg.name) elif com.is_list_like(arg): return _convert_listlike(arg, box, format)