Skip to content

Query on Series #12226

@max-sixty

Description

@max-sixty

I often find myself building a chain of logic, and then wanting to slice over some expression. Here's a contrived example:

In [12]: pd.Series(range(10)).mul(5).pipe(lambda x: x**2).pipe(lambda x: x-500)
Out[12]: 
0    -500
1    -475
2    -400
3    -275
4    -100
5     125
6     400
7     725
8    1100
9    1525
dtype: int64

...and then having to assign to a separate variable and slice using that. Which is not the worst affliction that could strike western civilization, but can disrupt the flow in a repl a little.

In [13]: s=pd.Series(range(10)).mul(5).pipe(lambda x: x**2).pipe(lambda x: x-500)

In [14]: s[s>200]
Out[14]: 
6     400
7     725
8    1100
9    1525
dtype: int64

Instead, would it be reasonable to extend query or filter to do this. For example:

pd.Series(range(10)).mul(5).pipe(lambda x: x**2).pipe(lambda x: x-500).query('>200')
or
pd.Series(range(10)).mul(5).pipe(lambda x: x**2).pipe(lambda x: x-500).filter(lambda x: x>200)

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignIndexingRelated to indexing on series/frames, not to indexes themselvesReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions