Skip to content

err#8038: Index.shift() gives confusing error message when no Datetim… #11211

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
wants to merge 1 commit into from

Conversation

hamedhsn
Copy link

…eIndex

closes #8038

@max-sixty
Copy link
Contributor

Is this the behavior you want though, vs. a clearer error message? This doesn't seem like it's shifting the index; it seems like it's adding 1...

        result = pd.Index(range(0,10,2)).shift(1)
        expected = pd.Index([1, 3, 5, 7, 9], dtype='int64')

@jreback
Copy link
Contributor

jreback commented Oct 1, 2015

@hamedhsn I don't this should even work on a non-datetimelike index as it doesn't make any sense. So we should prob raise a TypeError.

@jreback jreback added the Error Reporting Incorrect or improved errors from pandas label Oct 1, 2015
offset = periods * freq
try:
offset = periods * freq
except TypeError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, pls. raise a NotImplementedError for all ops in core/index.py for .shift. Then define this in tseries/base/DatetimeIndexOpsMixIn

@jreback
Copy link
Contributor

jreback commented Oct 5, 2015

pls add a test and a whatsnew note in 0.17.1

@hamedhsn
Copy link
Author

hamedhsn commented Oct 6, 2015

@jreback
1)why do I need to change tseries/base/DatetimeIndexOpsMixIn ?
2)where should I add tests?

@jreback
Copy link
Contributor

jreback commented Oct 6, 2015

I indicated above. The .shfit incore/index.pyapplies toIndex/Int64Indexetc. These are not valid for shifting and so should raiseNotImplementedError. Then make the actual change intseries/base/DatetimeIndexOpsMixInwhich is the base class for the datetimelikes (`DatetimeIndex,TimeDeltaIndex,PeriodIndex``) where shifting makes sense

@hamedhsn
Copy link
Author

hamedhsn commented Oct 6, 2015

sure, but there is no issue with DatetimeIndex using shift() with or without freq. that was my question.

the error happens for Index/Int64Index which i changed it to raise NotImplementedError.
btw where do i have to add the related tests?

@jreback
Copy link
Contributor

jreback commented Oct 7, 2015

@hamedhsn you can add tests in test_index (though the timeseries ones are prob in tseries/test_base)

@hamedhsn hamedhsn force-pushed the err#8038 branch 4 times, most recently from f14446d to 0393571 Compare October 14, 2015 21:40
@hamedhsn
Copy link
Author

@jreback this also should be fine

@max-sixty
Copy link
Contributor

@jreback @hamedhsn I think this behavior is confusing. I think there are some reasons to shift a non-DateTime-like index, but it shouldn't be done with a freq.

Do you guys think this is clear and expected behavior?

        idx = Index(range(5))   
        result = idx.shift(periods=2, freq=2)
        expected = pd.Index(range(4, 9))
        self.assert_index_equal(result, expected)

A version I could see being helpful is to shift by a number of items. i.e. this:

        idx = Index(range(5,10))   
        result = idx.shift(1)
        expected = pd.Index(range(6, 11))
        self.assert_index_equal(result, expected)

...although I'm more against the current implementation than I am fighting for this specific implementation.

As ever, open to being wrong though.

@jreback
Copy link
Contributor

jreback commented Oct 15, 2015

@MaximilianR that would be a major change to add a freq to a non-timeseries Index. So wouldn't want to support that (here).

@max-sixty
Copy link
Contributor

@jreback That's exactly what I'm saying. This PR enables freq for shifting non-timeseries index (or am I woefully mistaken)? This is a copy paste from the new test code:

        idx = Index(range(5))   
        result = idx.shift(periods=2, freq=2)
        expected = pd.Index(range(4, 9))
        self.assert_index_equal(result, expected)

@jreback
Copy link
Contributor

jreback commented Oct 15, 2015

that's out of scope for this PR
would be unexpected as freq is not a property of an Index

you are free to propose it but make an issue

@max-sixty
Copy link
Contributor

I don't think I'm being clear. Your comment here would fix the concern, but that hasn't been added.
Or I'm likely missing something, so I'll leave it for now

@@ -26,7 +26,7 @@ Enhancements

Other Enhancements
^^^^^^^^^^^^^^^^^^

- raising ``NotImplementedError`` for ``Index.shift()`` when no ``DatetimeIndex`` is used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

say: raise NotImplementedError in Index.shift for non-supported index types. (and include the issue number)

@hamedhsn
Copy link
Author

@jreback is this implementation correct? let me know if I need to anything

@@ -27,10 +27,14 @@ Enhancements

Other Enhancements
^^^^^^^^^^^^^^^^^^
<<<<<<< HEAD
- raising ``NotImplementedError`` for ``Index.shift()`` when no ``DatetimeIndex`` is used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebase artifcacts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put in API changes as this 'worked' before

@jreback
Copy link
Contributor

jreback commented Oct 18, 2015

a lot closer....still a couple of comments

@jreback jreback added this to the 0.17.1 milestone Oct 18, 2015
@hamedhsn
Copy link
Author

@jreback applied your comments

@hamedhsn
Copy link
Author

@jreback applied the comments

@@ -45,6 +45,14 @@ def test_ops_properties_basic(self):
self.assertEqual(s.day,10)
self.assertRaises(AttributeError, lambda : s.weekday)

def test_shift_index(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this to test_index.py inside that Base class

further you will need to create the index with self.create_index()

@hamedhsn hamedhsn force-pushed the err#8038 branch 2 times, most recently from c92864e to cb1d357 Compare November 11, 2015 22:19
@hamedhsn
Copy link
Author

@jreback should be fine now hopefully

@jreback jreback modified the milestones: Next Major Release, 0.17.1 Nov 15, 2015
@jreback
Copy link
Contributor

jreback commented Nov 15, 2015

@hamedhsn if you would like to update according to comments would be great

@hamedhsn
Copy link
Author

@jreback I think I did, used self.create_index(), put that in DateTimelike. and for the other one moved that to based class in test_index.py and used self.create_index(). and rebased that.

# test shift for datetimeIndex and non datetimeIndex
# err8083

drange = self.create_index()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is ONLY true for DatetimeIndex. Need separate expectation for TimedeltaIndex/PeriodIndex (IOW separate methods).

@hamedhsn
Copy link
Author

@jreback added the other two tests under their own test classes

@jreback
Copy link
Contributor

jreback commented Nov 18, 2015

xref #11452

idx = self.create_index()
self.assertRaises(NotImplementedError, idx.shift, 1)

result = idx.shift(periods=2, freq=2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

take out this call right here as it will fail by definition (leave the assertRaises)

@jreback
Copy link
Contributor

jreback commented Nov 18, 2015

@hamedhsn ok, tests look good (you just need to remove an error line and they would pass). pls add the other requested tests and good 2 go.

@jreback jreback added this to the 0.17.1 milestone Nov 18, 2015
err#8038 : add fix, note and test

err#8038 : add fix, note and test

fix added with tests

modification

Index.shift() gives confusing error message when no DatetimeIndex

test, fix note

change tests

add issue number

test change

test change

add tests

add test
@hamedhsn
Copy link
Author

@jreback applied your comments.rebased it

@jreback
Copy link
Contributor

jreback commented Nov 18, 2015

merged via a05eceb

thanks!

@melroy89
Copy link

It seems I still have this issue see my comment in: #11452. I'm using pandas v0.19.2 via pip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ERR: Index.shift() gives confusing error message when no DatetimeIndex
4 participants