BUG/API: Datetime-like Index.order reset freq #10295

Closed
sinhrks opened this Issue Jun 6, 2015 · 3 comments

Comments

Projects
None yet
2 participants
Member

sinhrks commented Jun 6, 2015

DatetimeIndex / TimedeltaIndex

import pandas as pd
idx = pd.date_range('2011-01-01', '2011-05-01', freq='M')

# freq must be preserved, because ``DatetimeIndex`` is ordered when it has freq
idx.order()
# DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-30'], dtype='datetime64[ns]', freq=None, tz=None)

# freq must be preserved like idx[::-1]
idx.order(ascending=False)
# DatetimeIndex(['2011-04-30', '2011-03-31', '2011-02-28', '2011-01-31'], dtype='datetime64[ns]', freq=None, tz=None)

idx[::-1]
# DatetimeIndex(['2011-04-30', '2011-03-31', '2011-02-28', '2011-01-31'], dtype='datetime64[ns]', freq='-1M', tz=None)

Internally, order may use take when return_indexer is True and should be fixed also.

PeriodIndex

Results in TypeError because it doesn't pass freq ( PeriodIndex without freq is meaningless ). Required to fix #7832 first to use the same flow as DatetimeIndex, I think.

idx = pd.period_range('2011-01-01', '2011-05-01', freq='M')
idx.order()
# TypeError: expected string or buffer

idx.order().freq is None
# True

sinhrks added this to the 0.17.0 milestone Jun 6, 2015

Member

sinhrks commented Jun 6, 2015

Found another issue related to this internally.

Index.take raises IndexError if given indices are not in the index.

import pandas as pd

idx = pd.Index([0, 1, 2])
# Int64Index([0, 1, 2], dtype='int64')

idx.take([0, 1, 2, 3])
# IndexError: index 3 is out of bounds for size 3

But DatetimeIndex and TimedeltaIndex don't if given index is looks like a slice.

didx = pd.DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'])

# NG IndexError not raised
dtidx.take([0, 1, 2, 3])
# DatetimeIndex(['2011-01-01', '2011-01-02', '2011-01-03'], dtype='datetime64[ns]', freq=None, tz=None)

dtidx = pd.TimedeltaIndex(['1 day', '2 day', '3 day'])
dtidx.take([0, 1, 2, 3])
# TimedeltaIndex(['1 days', '2 days', '3 days'], dtype='timedelta64[ns]', freq=None)

# normal slicing results in IndexError (OK)
didx[[0, 1, 2, 3]]
# IndexError: index 3 is out of bounds for axis 1 with size 3
Contributor

jreback commented Jun 14, 2015

these need some wrapping on the return e.g. _shallow_copy.

Member

sinhrks commented Jun 23, 2015

#10305 intends to fix this, passing existing attribtues to _simple_new.

sinhrks closed this in #10305 Aug 8, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment