question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

filter(): %in% %notin% | & operators and slice() operation

See original GitHub issue

Hey @machow , I hope you are doing well.

I have some questions and suggestions regarding filter() that I would like to discuss here if I may.

(1) Enable logical operators &, |, or and, or inside filter(). Right now I could not find a way to use or logic, for example: filter(_.x>2 | ._x==0.5)

(2) Would you consider implementing %in% (and %notin%) operators to do something like filter(_.country %in% ['Australia', 'Ireland']) ? Currently we have to rely on pandas syntax filter(_.country.isin(["Australia","Ireland"])

(3) Since siuba currently does not have slice(), is there an easy way to accomplish the same task using filter()?

Thank you

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ghostcommented, Jun 3, 2021

The in operator relies on the __contains__(a, b) method being defined for the object on the right-hand side. This means that you would need to create a siuba aware wrapper class for sequences.

The wrapper would define the __contains__(a, b) method.

class SiubaSequenceWrapper:
    def __init__(self, value):
        self.value = value
    def __contains__(self, other):
        return other.isin(self.value)

The final syntax would be:

filter(_.country in SiubaSequenceWrapper(['Australia', 'Ireland']))

I’m sure you can think of a better name for the wrapper… (This is how you can do it. I don’t know if you should do it!)

1reaction
machowcommented, Jun 5, 2021

@davidwales unfortunately the __contains__ method requires that you return a Boolean 😕. AFAIK siuba’s symbolic class can’t handle __iter__, __contains__, or the keywords and, or (these keywords throw errors for pandas and bumpy arrays too for the same reason).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use “not in” operator in Filter - R-bloggers
How to Use “not in” operator in Filter, To filter for rows in a data frame that is not in a list of...
Read more >
filter - MuleSoft Documentation
If it returns false for a value or index in the array, that item gets filtered out of the output. If there are...
Read more >
Built-in template tags and filters - Django documentation
This document describes Django's built-in template tags and filters. ... key named 'items' , data.items will return data['items'] instead of data.items() .
Read more >
Indexing, Slicing and Subsetting DataFrames in Python
Query / select a subset of data using a set of criteria using the following operators: == , != , > , <...
Read more >
Array.prototype.slice() - JavaScript - MDN Web Docs
The slice() method returns a shallow copy of a portion of an array into a new array ... If start >= array.length ,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found