@@ -2174,28 +2174,89 @@ export default class DataFrame extends NDframe {
21742174
21752175 /**
21762176 * Queries the DataFrame for rows that meet the boolean criteria.
2177- * @param condition An array of boolean mask, one for each row in the DataFrame. Rows where the value are true will be returned.
2178- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
2177+ * @param options
2178+ * - `column` A column name to query with.
2179+ * - `is` A logical operator. Can be one of the following: [">", "<", "<=", ">=", "==", "!="]
2180+ * - `to` A value to query with.
2181+ * - `condition` An array of boolean mask, one for each row in the DataFrame. Rows where the value are true will be returned.
2182+ * If specified, then other parameters are ignored.
2183+ * - `inplace` Boolean indicating whether to perform the operation inplace or not. Defaults to false
21792184 **/
2180- query ( condition , options ) {
2181- const { inplace } = { inplace : false , ...options } ;
2185+ query ( options ) {
2186+ const { inplace, condition, column, is, to } = { inplace : false , ...options } ;
2187+
2188+ if ( condition ) {
2189+ const result = _iloc ( {
2190+ ndFrame : this ,
2191+ rows : condition
2192+ } ) ;
2193+
2194+ if ( inplace ) {
2195+ this . $setValues ( result . values , false , false ) ;
2196+ this . $setIndex ( result . index ) ;
2197+ return ;
2198+ } else {
2199+ return result ;
2200+ }
21822201
2183- if ( ! condition ) {
2184- throw new Error ( "ParamError: condition must be specified" ) ;
21852202 }
21862203
2187- const result = _iloc ( {
2188- ndFrame : this ,
2189- rows : condition
2190- } ) ;
2204+ const operators = [ ">" , "<" , "<=" , ">=" , "==" , "!=" ] ;
21912205
2192- if ( inplace ) {
2193- this . $setValues ( result . values , false , false ) ;
2194- this . $setIndex ( result . index ) ;
2206+ let columnIndex , operator , value ;
2207+
2208+ if ( column ) {
2209+ if ( this . columns . includes ( column ) ) {
2210+ columnIndex = this . columns . indexOf ( column ) ;
2211+ } else {
2212+ throw new Error ( `ParamError: column ${ column } not found in column names` ) ;
2213+ }
2214+ } else {
2215+ throw new Error ( `ParamError: specify a column name to query` ) ;
2216+ }
2217+
2218+ if ( is ) {
2219+ if ( operators . includes ( is ) ) {
2220+ operator = is ;
2221+ } else {
2222+ throw new Error ( `ParamError: specified operato ${ is } is not a supported. operator must be one of ${ operators } ` ) ;
2223+ }
21952224 } else {
2196- return result ;
2225+ throw new Error ( `ParamError: specify an operator to apply. operator must be one of ${ operators } ` ) ;
21972226 }
21982227
2228+ if ( to ) {
2229+ value = to ;
2230+ } else {
2231+ throw new Error ( "ParamError: specify a value to query by" ) ;
2232+ }
2233+
2234+ let data = this . values ;
2235+ let index = this . index ;
2236+ let newData = [ ] ;
2237+ let newIndex = [ ] ;
2238+
2239+ for ( var i = 0 ; i < data . length ; i ++ ) {
2240+ let dataValue = data [ i ] ;
2241+ let elem = dataValue [ columnIndex ] ;
2242+ //use eval function for easy operation
2243+ //eval() takes in a string expression e.g eval('2>5')
2244+ if ( eval ( `elem${ operator } value` ) ) {
2245+ newData . push ( dataValue ) ;
2246+ newIndex . push ( index [ i ] ) ;
2247+ }
2248+ }
2249+
2250+ if ( inplace ) {
2251+ this . $setValues ( newData , false , false ) ;
2252+ this . $setIndex ( newIndex ) ;
2253+ return ;
2254+ } else {
2255+ return new DataFrame ( newData , {
2256+ index : newIndex ,
2257+ config : { ...this . config }
2258+ } ) ;
2259+ }
21992260 }
22002261
22012262 /**
0 commit comments