Skip to content
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

query operator #623

Closed
priyankat99 opened this issue Dec 26, 2023 · 2 comments
Closed

query operator #623

priyankat99 opened this issue Dec 26, 2023 · 2 comments

Comments

@priyankat99
Copy link

i see querying by logical operators has been deprecated:
https://danfo.jsdata.org/api-reference/dataframe/danfo.dataframe.query#query-a-dataframe-using-logical-operators

is there a reason it was deprecated? i need to filter my DF to remove certain values like so:

 const filtered = this.datapoints.query({
        column: "ModelName",
        is: "==",
        to: "model"  
      }).query({
        column: "Timestamp",
        is: ">=",
        to: start  
      }).query({
        column: "Timestamp",
        is: "<=",
        to: end 
      }) 

was this logic deprecated because it is inefficient? im not sure how to achieve this with the updated logic in query.
any help or clarification would be appreciated!

@Xloka
Copy link

Xloka commented Mar 17, 2024

@priyankat99 you can't chain it but you can do this

const query = [
  {
    column: "ModelName",
    is: "==",
    to: "model"  
  },
  {
    column: "Timestamp",
    is: ">=",
    to: start  
  },
  {
    column: "Timestamp",
    is: "<=",
    to: end 
  }
];

const finalQuery = query.reduce((acc, condition, index) => {
  if (condition.is === '>=') {
    return index === 0 ? this.datapoints[condition.column].gte(condition.to) : acc.and(this.datapoints[condition.column].gte(condition.to));
  } else if (condition.is === '<=') {
    return index === 0 ? this.datapoints[condition.column].lte(condition.to) : acc.and(this.datapoints[condition.column].lte(condition.to));
  } else if (condition.is === '==') {
    return index === 0 ? this.datapoints[condition.column].eq(condition.to) : acc.and(this.datapoints[condition.column].eq(condition.to));
  }
}, null);

const filtered = this.datapoints.query(finalQuery);

@risenW
Copy link
Member

risenW commented Apr 2, 2025

i see querying by logical operators has been deprecated: https://danfo.jsdata.org/api-reference/dataframe/danfo.dataframe.query#query-a-dataframe-using-logical-operators

is there a reason it was deprecated? i need to filter my DF to remove certain values like so:

 const filtered = this.datapoints.query({
        column: "ModelName",
        is: "==",
        to: "model"  
      }).query({
        column: "Timestamp",
        is: ">=",
        to: start  
      }).query({
        column: "Timestamp",
        is: "<=",
        to: end 
      }) 

was this logic deprecated because it is inefficient? im not sure how to achieve this with the updated logic in query. any help or clarification would be appreciated!

This is quite old, but I'll still answer it as it can help others.

The old query method with logical operators (is: "==", etc.) was deprecated in favor of a more powerful and chainable boolean operations approach. This new approach:

  • Is more performant since it uses tensor operations under the hood
  • Provides better type safety and error handling
  • Allows for more complex boolean operations through chaining

Here's how you can rewrite your filtering logic using the modern approach:

const filtered = this.datapoints.query(
    this.datapoints["ModelName"].eq("model")
        .and(this.datapoints["Timestamp"].ge(start))
        .and(this.datapoints["Timestamp"].le(end))
)

@risenW risenW closed this as completed Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants