pandasquery 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pandasquery/PandasQuery.py +113 -0
- pandasquery/__init__.py +1 -0
- pandasquery/conditions/__init__.py +3 -0
- pandasquery/conditions/pandas_query_and_condition.py +18 -0
- pandasquery/conditions/pandas_query_condition.py +38 -0
- pandasquery/conditions/pandas_query_or_condition.py +18 -0
- pandasquery/enums/__init__.py +2 -0
- pandasquery/enums/pandas_query_condition_binder_values.py +6 -0
- pandasquery/enums/pandas_query_operators_enum.py +9 -0
- pandasquery-0.0.1.dist-info/METADATA +78 -0
- pandasquery-0.0.1.dist-info/RECORD +13 -0
- pandasquery-0.0.1.dist-info/WHEEL +5 -0
- pandasquery-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import pandas
|
|
2
|
+
from typing import Optional
|
|
3
|
+
from pandasquery.conditions import PandasQueryCondition
|
|
4
|
+
from pandasquery.conditions import PandasQueryAndCondition
|
|
5
|
+
from pandasquery.conditions import PandasQueryOrCondition
|
|
6
|
+
from pandasquery.enums import PandasQueryConditionBinderValues
|
|
7
|
+
from pandasquery.enums import PandasQueryOperatorsEnum
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PandasQuery:
|
|
11
|
+
def __init__(self):
|
|
12
|
+
self.conditions: list[PandasQueryCondition] = []
|
|
13
|
+
|
|
14
|
+
def where(
|
|
15
|
+
self,
|
|
16
|
+
column: str,
|
|
17
|
+
operatorOrValue: any,
|
|
18
|
+
valueIfNotProvidedBefore: Optional[any] = None,
|
|
19
|
+
*,
|
|
20
|
+
binder: Optional[PandasQueryConditionBinderValues] = None,
|
|
21
|
+
):
|
|
22
|
+
if binder is None and len(self.conditions) != 0:
|
|
23
|
+
raise Exception(
|
|
24
|
+
"Binder can be None only for the first condition. Use orWhere or andWhere instead of where over and over again."
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
self.conditions.append(
|
|
28
|
+
PandasQueryAndCondition(
|
|
29
|
+
column=column,
|
|
30
|
+
operatorOrValue=operatorOrValue,
|
|
31
|
+
valueIfNotProvidedBefore=valueIfNotProvidedBefore,
|
|
32
|
+
)
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
return self
|
|
36
|
+
|
|
37
|
+
def andWhere(
|
|
38
|
+
self,
|
|
39
|
+
column: str,
|
|
40
|
+
operatorOrValue: any,
|
|
41
|
+
valueIfNotProvidedBefore: Optional[any] = None,
|
|
42
|
+
):
|
|
43
|
+
if len(self.conditions) == 0:
|
|
44
|
+
raise Exception(
|
|
45
|
+
"Please start the conditions using `where` method and continue using auxiliaries such as `andWhere` and `orWhere`."
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
self.conditions.append(
|
|
49
|
+
PandasQueryAndCondition(
|
|
50
|
+
column=column,
|
|
51
|
+
operatorOrValue=operatorOrValue,
|
|
52
|
+
valueIfNotProvidedBefore=valueIfNotProvidedBefore,
|
|
53
|
+
)
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
return self
|
|
57
|
+
|
|
58
|
+
def orWhere(
|
|
59
|
+
self,
|
|
60
|
+
column: str,
|
|
61
|
+
operatorOrValue: any,
|
|
62
|
+
valueIfNotProvidedBefore: Optional[any] = None,
|
|
63
|
+
):
|
|
64
|
+
if len(self.conditions) == 0:
|
|
65
|
+
raise Exception(
|
|
66
|
+
"Please start the conditions using `where` method and continue using auxiliaries such as `andWhere` and `orWhere`."
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
self.conditions.append(
|
|
70
|
+
PandasQueryOrCondition(
|
|
71
|
+
column=column,
|
|
72
|
+
operatorOrValue=operatorOrValue,
|
|
73
|
+
valueIfNotProvidedBefore=valueIfNotProvidedBefore,
|
|
74
|
+
)
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
return self
|
|
78
|
+
|
|
79
|
+
def apply(self, dataframe: pandas.DataFrame) -> pandas.DataFrame:
|
|
80
|
+
_mask = pandas.Series(True, index=dataframe.index)
|
|
81
|
+
for condition in self.conditions:
|
|
82
|
+
column, value, operator = condition.unpack()
|
|
83
|
+
|
|
84
|
+
if operator not in PandasQueryOperatorsEnum:
|
|
85
|
+
raise Exception(f"Invalid operator found in conditions. {operator}")
|
|
86
|
+
|
|
87
|
+
if condition.is_and():
|
|
88
|
+
if operator == PandasQueryOperatorsEnum.EqualTo:
|
|
89
|
+
_mask &= dataframe[column] == value
|
|
90
|
+
elif operator == PandasQueryOperatorsEnum.GreaterThan:
|
|
91
|
+
_mask &= dataframe[column] > value
|
|
92
|
+
elif operator == PandasQueryOperatorsEnum.GreaterThanOrEqualTo:
|
|
93
|
+
_mask &= dataframe[column] >= value
|
|
94
|
+
elif operator == PandasQueryOperatorsEnum.LesserThan:
|
|
95
|
+
_mask &= dataframe[column] < value
|
|
96
|
+
elif operator == PandasQueryOperatorsEnum.LesserThanOrEqualTo:
|
|
97
|
+
_mask &= dataframe[column] <= value
|
|
98
|
+
elif condition.is_or():
|
|
99
|
+
if operator == PandasQueryOperatorsEnum.EqualTo:
|
|
100
|
+
_mask |= dataframe[column] == value
|
|
101
|
+
elif operator == PandasQueryOperatorsEnum.GreaterThan:
|
|
102
|
+
_mask |= dataframe[column] > value
|
|
103
|
+
elif operator == PandasQueryOperatorsEnum.GreaterThanOrEqualTo:
|
|
104
|
+
_mask |= dataframe[column] >= value
|
|
105
|
+
elif operator == PandasQueryOperatorsEnum.LesserThan:
|
|
106
|
+
_mask |= dataframe[column] < value
|
|
107
|
+
elif operator == PandasQueryOperatorsEnum.LesserThanOrEqualTo:
|
|
108
|
+
_mask |= dataframe[column] <= value
|
|
109
|
+
else:
|
|
110
|
+
raise Exception(
|
|
111
|
+
"Unknown condition type was passed. Condition binder must be in {AND, OR}."
|
|
112
|
+
)
|
|
113
|
+
return dataframe[_mask]
|
pandasquery/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .PandasQuery import PandasQuery
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
from pandasquery.conditions import PandasQueryCondition
|
|
3
|
+
from pandasquery.enums import PandasQueryConditionBinderValues
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class PandasQueryAndCondition(PandasQueryCondition):
|
|
7
|
+
def __init__(
|
|
8
|
+
self,
|
|
9
|
+
column: str,
|
|
10
|
+
operatorOrValue: any,
|
|
11
|
+
valueIfNotProvidedBefore: Optional[any] = None,
|
|
12
|
+
):
|
|
13
|
+
super().__init__(
|
|
14
|
+
column=column,
|
|
15
|
+
operatorOrValue=operatorOrValue,
|
|
16
|
+
valueIfNotProvidedBefore=valueIfNotProvidedBefore,
|
|
17
|
+
binder=PandasQueryConditionBinderValues.AND,
|
|
18
|
+
)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
from pandasquery.enums import PandasQueryConditionBinderValues
|
|
3
|
+
from pandasquery.enums import PandasQueryOperatorsEnum
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class PandasQueryCondition:
|
|
7
|
+
def __init__(
|
|
8
|
+
self,
|
|
9
|
+
column: str,
|
|
10
|
+
operatorOrValue: any,
|
|
11
|
+
valueIfNotProvidedBefore: Optional[any] = None,
|
|
12
|
+
*,
|
|
13
|
+
binder: Optional[PandasQueryConditionBinderValues] = None,
|
|
14
|
+
):
|
|
15
|
+
self.column = column
|
|
16
|
+
self.binder = binder
|
|
17
|
+
|
|
18
|
+
if valueIfNotProvidedBefore is not None:
|
|
19
|
+
self.value = valueIfNotProvidedBefore
|
|
20
|
+
self.operator = operatorOrValue
|
|
21
|
+
else:
|
|
22
|
+
self.value = operatorOrValue
|
|
23
|
+
self.operator = PandasQueryOperatorsEnum.EqualTo
|
|
24
|
+
|
|
25
|
+
def unpack(self) -> tuple[str, any]:
|
|
26
|
+
return (self.column, self.value, self.operator)
|
|
27
|
+
|
|
28
|
+
def is_and(self):
|
|
29
|
+
return self.binder == PandasQueryConditionBinderValues.AND
|
|
30
|
+
|
|
31
|
+
def is_or(self):
|
|
32
|
+
return self.binder == PandasQueryConditionBinderValues.OR
|
|
33
|
+
|
|
34
|
+
def __str__(self):
|
|
35
|
+
return f"PandasQueryCondition({self.column}, {self.operator}, {self.value})"
|
|
36
|
+
|
|
37
|
+
def __repr__(self):
|
|
38
|
+
return self.__str__()
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
from pandasquery.enums import PandasQueryConditionBinderValues
|
|
3
|
+
from .pandas_query_condition import PandasQueryCondition
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class PandasQueryOrCondition(PandasQueryCondition):
|
|
7
|
+
def __init__(
|
|
8
|
+
self,
|
|
9
|
+
column: str,
|
|
10
|
+
operatorOrValue: any,
|
|
11
|
+
valueIfNotProvidedBefore: Optional[any] = None,
|
|
12
|
+
):
|
|
13
|
+
super().__init__(
|
|
14
|
+
column=column,
|
|
15
|
+
operatorOrValue=operatorOrValue,
|
|
16
|
+
valueIfNotProvidedBefore=valueIfNotProvidedBefore,
|
|
17
|
+
binder=PandasQueryConditionBinderValues.OR,
|
|
18
|
+
)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pandasquery
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Provides an efficient simple interface to query a Pandas DataFrame or Series.
|
|
5
|
+
Author-email: Taha Shieenavaz <tahashieenavaz@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/tahashieenavaz/pandas-query
|
|
8
|
+
Project-URL: Repository, https://github.com/tahashieenavaz/pandas-query
|
|
9
|
+
Project-URL: Documentation, https://github.com/tahashieenavaz/pandas-query#readme
|
|
10
|
+
Project-URL: Issues, https://github.com/tahashieenavaz/pandas-query/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/tahashieenavaz/pandas-query/blob/main/CHANGELOG.md
|
|
12
|
+
Keywords: series,pandas,dataframe,search,query
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Requires-Python: >=3.9
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
Requires-Dist: numpy
|
|
28
|
+
Requires-Dist: pandas
|
|
29
|
+
Provides-Extra: dev
|
|
30
|
+
Requires-Dist: build; extra == "dev"
|
|
31
|
+
Requires-Dist: pytest; extra == "dev"
|
|
32
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
33
|
+
Requires-Dist: ruff; extra == "dev"
|
|
34
|
+
Requires-Dist: mypy; extra == "dev"
|
|
35
|
+
Requires-Dist: twine; extra == "dev"
|
|
36
|
+
|
|
37
|
+
# Pandas Query
|
|
38
|
+
|
|
39
|
+
Provides an efficient simple class to query a Pandas DataFrame or Series.
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
For using the package inside your projects simply install the package using `pip`:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install pandasquery
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Alternatively, for development after cloning the repository use:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install -e .
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
You can instantiate queries with `Query` class and apply them memory-efficiently to the same dataframe as you please:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from pandasquery import Query
|
|
61
|
+
|
|
62
|
+
# load the data
|
|
63
|
+
dataframe = load_dataframe()
|
|
64
|
+
|
|
65
|
+
# create the specific query
|
|
66
|
+
youth_query = Query().where("age", "<=", 30)
|
|
67
|
+
|
|
68
|
+
# apply it directly to the dataframe
|
|
69
|
+
print(youth_query.apply(dataframe))
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Methods
|
|
73
|
+
|
|
74
|
+
### where
|
|
75
|
+
### on
|
|
76
|
+
### andWhere
|
|
77
|
+
### orWhere
|
|
78
|
+
### apply
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
pandasquery/PandasQuery.py,sha256=5a3Cr1AtpnzlghEKMN0bCRSj_r-vmCI-Ate5CB6og8A,4317
|
|
2
|
+
pandasquery/__init__.py,sha256=L6IONWLL1H09Kb_dujLST4_eaiLznKgLCCcqv3N1ZO0,37
|
|
3
|
+
pandasquery/conditions/__init__.py,sha256=AmFXxr7rNuSGw4Zz2tW77rzfOACBezaf8WJs4JZaHCA,183
|
|
4
|
+
pandasquery/conditions/pandas_query_and_condition.py,sha256=fxeHEXgRCsdf7J9tp6E6AQabkdCySrJft8sxyS9qLvg,576
|
|
5
|
+
pandasquery/conditions/pandas_query_condition.py,sha256=RMSciPdHIAwGFyg-gMmqZuLJ93OqdM6Vsi5OzKTfqtQ,1158
|
|
6
|
+
pandasquery/conditions/pandas_query_or_condition.py,sha256=4TsoaVDixhB7fG564c1lqH01ROya8tOAzEkeBlRfDyA,575
|
|
7
|
+
pandasquery/enums/__init__.py,sha256=rSrCxMM9-o3Fgwcuio2BgqE6t-JnZlHunJLoab0ET5M,149
|
|
8
|
+
pandasquery/enums/pandas_query_condition_binder_values.py,sha256=nMaqWGp5QUfmvrt6fmmViAKNih5NnnIekZ_6M4ZTweQ,110
|
|
9
|
+
pandasquery/enums/pandas_query_operators_enum.py,sha256=EPeo45kJu2XUl0_BgcujXSt2ZgGnVClgJNloYLVN8e8,192
|
|
10
|
+
pandasquery-0.0.1.dist-info/METADATA,sha256=_o2GDwCG3VcFKJ2qa4uOHyc-hY562dMCtv0YQ_IsUJs,2395
|
|
11
|
+
pandasquery-0.0.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
12
|
+
pandasquery-0.0.1.dist-info/top_level.txt,sha256=2IVvd83P-MWy125LGQwDYxJ89t9ZS_wMy-O3dxbxV_s,12
|
|
13
|
+
pandasquery-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pandasquery
|