Skip to content

API: DatetimeIndex / TimedeltaIndex 2d slicing should result in Index #10774

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
sinhrks opened this issue Aug 9, 2015 · 6 comments
Closed

API: DatetimeIndex / TimedeltaIndex 2d slicing should result in Index #10774

sinhrks opened this issue Aug 9, 2015 · 6 comments
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas good first issue Index Related to the Index class or subclasses Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@sinhrks
Copy link
Member

sinhrks commented Aug 9, 2015

Following slices basically result in the same Index.

import pandas as pd
pd.Index([1, 2, 3])[:, None]
# Int64Index([[1], [2], [3]], dtype='int64')
pd.MultiIndex.from_tuples([('a', 1), ('b', 2)])[:, None]
# MultiIndex(levels=[[u'a', u'b'], [1, 2]],
#            labels=[[[0], [1]], [[0], [1]]])

pd.period_range('2011-01-01', freq='M', periods=3)[:, None]
# PeriodIndex(['2011-01', '2011-02', '2011-03'], dtype='int64', freq='M')

But DatetimeIndex and TimedeltaIndex doesn't because of following line.

pd.date_range('2011-01-01', freq='M', periods=3)[:, None]
# array([['2011-01-31T09:00:00.000000000+0900'],
#        ['2011-02-28T09:00:00.000000000+0900'],
#        ['2011-03-31T09:00:00.000000000+0900']], dtype='datetime64[ns]')
pd.timedelta_range('1 days', freq='D', periods=3)[:, None]
# array([[ 86400000000000],
#        [172800000000000],
#        [259200000000000]], dtype='timedelta64[ns]')

I understand these must be Index also, or any reason for this?

@sinhrks sinhrks added the Indexing Related to indexing on series/frames, not to indexes themselves label Aug 9, 2015
@jreback
Copy link
Contributor

jreback commented Aug 9, 2015

prob an oversite

but not sure we should allow 2-d results at all
it's very odd (and I don't any utility)

@sinhrks
Copy link
Member Author

sinhrks commented Aug 9, 2015

Yep. Raise IndexError as the same as explicit 2 indices?

pd.Index([1, 2, 3])[:, 0]
# IndexError: too many indices for array

@jreback
Copy link
Contributor

jreback commented Aug 9, 2015

sounds right

@shoyer
Copy link
Member

shoyer commented Aug 12, 2015

I noticed this the other day. I agree with @jreback that we should raise an IndexError if the result has two or more dimensions.

@jreback jreback added Difficulty Novice Error Reporting Incorrect or improved errors from pandas labels Aug 15, 2015
@jreback jreback added this to the Next Major Release milestone Aug 15, 2015
@toobaz toobaz added Index Related to the Index class or subclasses and removed Indexing Related to indexing on series/frames, not to indexes themselves labels Jun 28, 2019
@ShaharNaveh
Copy link
Member

@jreback Since #31150 this is raising a Deprecation warning

if np.ndim(result) > 1:
warnings.warn(
"Support for multi-dimensional indexing (e.g. `index[:, None]`) "
"on an Index is deprecated and will be removed in a future "
"version. Convert to a numpy array before indexing instead.",
DeprecationWarning,
stacklevel=3,
)


Should it raise an IndexError instead of a deprecation warning? (as was agreed above)

@jbrockmendel jbrockmendel added the Indexing Related to indexing on series/frames, not to indexes themselves label Sep 21, 2020
@mroeschke
Copy link
Member

Since the 2D indexing behavior is currently deprecated, we should appropriately raise once this behavior is enforced. Closing but we can reopen if we don't get the correct behavior in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas good first issue Index Related to the Index class or subclasses Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

8 participants