Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slice pandas series with elements not in the index

I have a pandas series indexed by tuples, like this:

from pandas import Series
s = Series({(0, 0): 1, (0, 1): 2, (0, 3): 3, (1, 0): 1, (1, 2): 4, (3, 0): 5})

I want to slice such a series by using indexes that are also tuples (using lexicographic ordering), but not necessarily in the index. Slicing seems to work when I pass an index that is on the series:

s[:(1,0)]
(0, 0)    1
(0, 1)    2
(0, 3)    3
(1, 0)    1
dtype: int64

but if I try slicing by an index which is not on the series there is an error:

s[:(1,1)]
...
ValueError: Index(...) must be called with a collection of some kind, 0 was passed

Ideally I'd like to get the series elements indexed by (0, 0), (0, 1), (0, 3), (1, 0), similar to what happens when slicing using dates in TimeSeries. Is there a simple way to achieve this?

like image 668
Javier Avatar asked Aug 02 '13 10:08

Javier


People also ask

How do you cut a series in Pandas?

slice() method is used to slice substrings from a string present in Pandas series object. It is very similar to Python's basic principal of slicing objects that works on [start:stop:step] which means it requires three parameters, where to start, where to end and how much elements to skip.

Can you use ILOC on a series?

iloc attribute enables purely integer-location based indexing for selection by position over the given Series object. Example #1: Use Series. iloc attribute to perform indexing over the given Series object.

How do I access the Pandas Series index?

Accessing Element from Series with Position In order to access the series element refers to the index number. Use the index operator [ ] to access an element in a series. The index must be an integer. In order to access multiple elements from a series, we use Slice operation.

How do you remove an index from a series?

Pandas Series: reset_index() function For a Series with a MultiIndex, only remove the specified levels from the index. Removes all levels by default. Just reset the index, without inserting it as a column in the new DataFrame. The name to use for the column containing the original Series values.


1 Answers

This works if you have a MultiIndex rather than an index of tuples:

In [11]: s.index = pd.MultiIndex.from_tuples(s.index)

In [12]: s
Out[12]: 
0  0    1
   1    2
   3    3
1  0    1
   2    4
3  0    5
dtype: int64

In [13]: s[:(1,1)]
Out[13]: 
0  0    1
   1    2
   3    3
1  0    1
dtype: int64

In a previous edit I had suggested this could be a bug, and had created an awful hack...

like image 159
Andy Hayden Avatar answered Oct 04 '22 03:10

Andy Hayden