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

ERR: Index.shift() gives confusing error message when no DatetimeIndex #8038

Closed
jorisvandenbossche opened this issue Aug 15, 2014 · 4 comments
Labels
API Design Bug Error Reporting Incorrect or improved errors from pandas
Milestone

Comments

@jorisvandenbossche
Copy link
Member

In [1]: idx = pd.Index(range(5))

In [2]: idx
Out[2]: Int64Index([0, 1, 2, 3, 4], dtype='int64')

In [3]: idx.shift(1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-d8362983dbf7> in <module>()
----> 1 idx.shift(1)

c:\users\vdbosscj\scipy\pandas-joris\pandas\core\index.pyc in shift(self, period
s, freq)
   1096             return self
   1097
-> 1098         offset = periods * freq
   1099         return Index([idx + offset for idx in self], name=self.name)
   1100

TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'

Should shift be allowed at all when it is no DatetimeIndex?

@jreback
Copy link
Contributor

jreback commented Aug 15, 2014

this is a bug as the freq=None. I am not sure of the usecase of this for non-datetimelike (though its seems to 'work')

e.g.

idx.shift(periods=1,freq=1)

@jreback
Copy link
Contributor

jreback commented Aug 15, 2014

In [6]: i = Index(range(5))

In [7]: i
Out[7]: Int64Index([0, 1, 2, 3, 4], dtype='int64')

In [8]: i.shift(periods=1,freq=1)
Out[8]: Int64Index([1, 2, 3, 4, 5], dtype='int64')

In [9]: i.shift(periods=1,freq=2)
Out[9]: Int64Index([2, 3, 4, 5, 6], dtype='int64')

In [10]: i.shift(periods=1,freq=3)
Out[10]: Int64Index([3, 4, 5, 6, 7], dtype='int64')

In [11]: i.shift(periods=2,freq=3)
Out[11]: Int64Index([6, 7, 8, 9, 10], dtype='int64')
In [12]: i = date_range('20130101',periods=5)

In [13]: i
Out[13]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-01-01, ..., 2013-01-05]
Length: 5, Freq: D, Timezone: None

In [15]: i.shift(1)
Out[15]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-01-02, ..., 2013-01-06]
Length: 5, Freq: D, Timezone: None

In [16]: i.shift(2)
Out[16]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-01-03, ..., 2013-01-07]
Length: 5, Freq: D, Timezone: None

In [17]: i.shift(2,freq='2D')
Out[17]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-01-05, ..., 2013-01-09]
Length: 5, Freq: None, Timezone: None

@jorisvandenbossche
Copy link
Member Author

yes, you can indeed 'misuse' the shift function on a normal Int64Index, eg also:

In [10]: idx.shift(3, freq=1.5)
Out[10]: Float64Index([4.5, 5.5, 6.5, 7.5, 8.5], dtype='float64')

but I don't really know what the use of this could be (just idx + 1 instead)? And if we even should allow it, as I think a user expects something else from a shift function (how it works on a series, shifting the values)

So maybe raising on a non-Datetime/PeriodIndex?

BTW, what is best way to really shift an index?

@jreback jreback added this to the 0.16.1 milestone Mar 8, 2015
@jreback jreback modified the milestones: 0.17.0, 0.16.1 Apr 28, 2015
@jreback jreback modified the milestones: Next Major Release, 0.17.0 Aug 15, 2015
@jreback jreback modified the milestones: 0.17.1, Next Major Release Oct 18, 2015
@jreback jreback modified the milestones: Next Major Release, 0.17.1 Nov 15, 2015
@jreback
Copy link
Contributor

jreback commented Nov 18, 2015

closed by #11211

@jreback jreback closed this as completed Nov 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Bug Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants