Skip to content
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

BUG: Index name is not preserved during some conversions #10875

Closed
soupault opened this issue Aug 21, 2015 · 2 comments
Closed

BUG: Index name is not preserved during some conversions #10875

soupault opened this issue Aug 21, 2015 · 2 comments
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@soupault
Copy link
Contributor

The issue is existing name of Index is dropped during conversion to either TimedeltaIndex or DatetimeIndex.

In [42]: vec = ['01:02:03', '01:02:04']
In [43]: i = pd.Index(vec, name='label')
In [44]: i
Out[44]: Index(['01:02:03', '01:02:04'], dtype='object', name='label')

In [45]: pd.to_datetime(i)
Out[45]: DatetimeIndex(['2015-08-21 01:02:03', '2015-08-21 01:02:04'], dtype='datetime64[ns]', freq=None, tz=None)

In [46]: pd.to_timedelta(i)
Out[46]: TimedeltaIndex(['01:02:03', '01:02:04'], dtype='timedelta64[ns]', freq=None)
@soupault
Copy link
Contributor Author

For to_timedelta:
Could add to_timedelta method to pandas/core/index.py.

Simplest solution is to add something like this to pandas/tseries/timedeltas.py:

elif isinstance(arg, ABCSeries):
    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):
+    from pandas import TimedeltaIndex
+    values = _convert_listlike(arg.values, box=box, unit=unit)
+    return TimedeltaIndex(values, name=arg.name)
elif is_list_like(arg):
    return _convert_listlike(arg, box=box, unit=unit)

@soupault soupault changed the title BUG: Index name is not preserved during several conversions BUG: Index name is not preserved during some conversions Aug 21, 2015
@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Aug 21, 2015
@jreback jreback added this to the Next Major Release milestone Aug 21, 2015
@jreback jreback modified the milestones: 0.17.0, Next Major Release Aug 31, 2015
chris-b1 added a commit to chris-b1/pandas that referenced this issue Aug 31, 2015
in to_datetime, to_timedelta
jreback added a commit that referenced this issue Aug 31, 2015
@jreback
Copy link
Contributor

jreback commented Aug 31, 2015

closed by #10945

@jreback jreback closed this as completed Aug 31, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

2 participants