Is your feature request related to a problem?
I wondering if you maybe interested in include a new method context
(I've chosen that name but can be other) which provides convenient slicing around an index (positional or not) for better data exploration. I see it as head
or tail
methods but generalized where the anchor can be other than the first or last index.
Describe the solution you'd like
I've implemented this https://github.com/mmngreco/pandas-context as a proof of concept which has the following code:
import pandas as pd
def context(
pdobj, loc=None, iloc=None, around=2, pre=None, post=None, method="nearest"
):
if loc is not None:
idx = pdobj.index.get_loc(loc, method=method)
elif iloc is not None:
idx = iloc
else:
raise ValueError
if pre is None:
pre = around
if post is None:
post = around
if pre > idx:
pre = idx
ini = idx - pre
end = idx + post + 1
return pdobj.iloc[ini:end]
pd.core.frame.NDFrame.context = context
Here're some examples of usage:
>>> import pandas as pd
... import numpy as np
... import pandas_context
... n = 1000 # long data
... data = np.random.randn(n)
... index = pd.date_range("19990101", periods=n)
... df = pd.DataFrame(data=data, index=index)
... date = index[50]
>>> date
Timestamp('1999-02-20 00:00:00', freq='D')
>>> df.context(date)
0
1999-02-18 1.086181
1999-02-19 0.174156
1999-02-20 -0.762305 # <-----
1999-02-21 -0.110677
1999-02-22 -0.766751
>>> df.context(date, around=1)
0
1999-02-19 0.174156
1999-02-20 -0.762305 # <-----
1999-02-21 -0.110677
>>> df.context(date, pre=3, post=1)
0
1999-02-17 -0.809791
1999-02-18 1.086181
1999-02-19 0.174156
1999-02-20 -0.762305 # <-----
1999-02-21 -0.110677
API breaking implications
[this should provide a description of how this feature will affect the API] None (I think)
Let me know if you are interested in this feature I will be glad to open a PR.
Comment From: jreback
i am almost always -1 on any new api
i do see the appeal here - but an insurmountable blocker is that this mixes positional and location based indexing and would be outright rejected on that alone
possible a .mid() that is only positional might gain some traction
Comment From: asdf8601
Ok, it makes sense. No problem on my side. So, the implementation would be something like this:
import pandas as pd
def mid(
pdobj, loc=None, around=2, pre=None, post=None, method="nearest"
):
idx = pdobj.index.get_loc(loc, method=method)
if pre is None:
pre = around
if post is None:
post = around
if pre > idx:
pre = idx
ini = idx - pre
end = idx + post + 1
return pdobj.iloc[ini:end]
pd.core.frame.NDFrame.mid = mid
The implementation should extend the NDFrame class.
Comment From: jbrockmendel
-1 on new API here.