-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Conversation
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') |
@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 |
offset = periods * freq | ||
try: | ||
offset = periods * freq | ||
except TypeError: |
There was a problem hiding this comment.
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
pls add a test and a whatsnew note in 0.17.1 |
@jreback |
I indicated above. The |
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. |
@hamedhsn you can add tests in |
f14446d
to
0393571
Compare
@jreback this also should be fine |
@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 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. |
@MaximilianR that would be a major change to add a freq to a non-timeseries Index. So wouldn't want to support that (here). |
@jreback That's exactly what I'm saying. This PR enables idx = Index(range(5))
result = idx.shift(periods=2, freq=2)
expected = pd.Index(range(4, 9))
self.assert_index_equal(result, expected) |
that's out of scope for this PR you are free to propose it but make an issue |
I don't think I'm being clear. Your comment here would fix the concern, but that hasn't been added. |
@@ -26,7 +26,7 @@ Enhancements | |||
|
|||
Other Enhancements | |||
^^^^^^^^^^^^^^^^^^ | |||
|
|||
- raising ``NotImplementedError`` for ``Index.shift()`` when no ``DatetimeIndex`` is used. |
There was a problem hiding this comment.
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)
@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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rebase artifcacts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add the issue number
There was a problem hiding this comment.
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
a lot closer....still a couple of comments |
@jreback applied your comments |
@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): |
There was a problem hiding this comment.
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()
c92864e
to
cb1d357
Compare
@jreback should be fine now hopefully |
@hamedhsn if you would like to update according to comments would be great |
@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() |
There was a problem hiding this comment.
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).
@jreback added the other two tests under their own test classes |
xref #11452 |
idx = self.create_index() | ||
self.assertRaises(NotImplementedError, idx.shift, 1) | ||
|
||
result = idx.shift(periods=2, freq=2) |
There was a problem hiding this comment.
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
)
@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. |
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
@jreback applied your comments.rebased it |
merged via a05eceb thanks! |
It seems I still have this issue see my comment in: #11452. I'm using pandas v0.19.2 via pip. |
…eIndex
closes #8038