pandas-query-sql 0.8.0__tar.gz

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.
@@ -0,0 +1,11 @@
1
+ ##Authors
2
+
3
+ - Greg Lamp
4
+ - Austin Ogilvie
5
+
6
+ ##Patches
7
+ - Colin Dickson
8
+ - stonebig
9
+
10
+ ##Fork author
11
+ - Alexander Plavin
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2013 Yhat, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,113 @@
1
+ Metadata-Version: 2.1
2
+ Name: pandas_query_sql
3
+ Version: 0.8.0
4
+ Summary: sqldf for pandas
5
+ Author-Email: Alexander Plavin <alexander@plav.in>, Greg Lamp <lamp.greg@gmail.com>, Michael Dempsey <bluestealth@bluestealth.pw>
6
+ License: MIT
7
+ Classifier: License :: OSI Approved :: MIT License
8
+ Project-URL: Homepage, https://github.com/bluestealth/pandasql/
9
+ Requires-Python: >=3.8
10
+ Requires-Dist: numpy
11
+ Requires-Dist: packaging
12
+ Requires-Dist: pandas
13
+ Requires-Dist: sqlalchemy
14
+ Requires-Dist: psycopg2-binary; extra == "postgresql"
15
+ Requires-Dist: black; extra == "linters"
16
+ Requires-Dist: ruff; extra == "linters"
17
+ Requires-Dist: pytest; extra == "testing"
18
+ Requires-Dist: psycopg2-binary; extra == "testing"
19
+ Provides-Extra: postgresql
20
+ Provides-Extra: linters
21
+ Provides-Extra: testing
22
+ Description-Content-Type: text/markdown
23
+
24
+ pandasql
25
+ ========
26
+
27
+ This is a fork of the original `pandasql`, with support of multiple SQL
28
+ backends and more convenient interface. See below for more info.
29
+
30
+ `pandasql` allows you to query `pandas` DataFrames using SQL syntax. It works
31
+ similarly to `sqldf` in R. `pandasql` seeks to provide a more familiar way of
32
+ manipulating and cleaning data for people new to Python or `pandas`.
33
+
34
+ ## Installation
35
+ ```
36
+ $ pip install -U pandasql
37
+ ```
38
+
39
+ ## Basics
40
+ In addition to the original pandasql's ``sqldf`` function this fork has
41
+ a class ``PandaSQL``, which new users are encouraged to use.
42
+
43
+ `PandaSQL` Class
44
+ ---
45
+ The class is more convenient when you need to perform multiple queries. `PandaSQL` takes 2 arguments:
46
+ - `db_uri`: an optional SQLAlchemy connection string (defaults to in-memory SQLite database)
47
+ - `persist`: an optional boolean to determine if loaded tables are persisted in the database (holds connection open, default False)
48
+
49
+ `sqldf` Function
50
+ ---
51
+ The main function used in pandasql is `sqldf`. `sqldf` accepts 3 parameters:
52
+ - an sql query string
53
+ - an optional SQLAlchemy connection string (defaults to in-memory SQLite database)
54
+ - an optional dict of session/environment variables (defaults to `**locals()`,`**globals()`)
55
+
56
+
57
+ ## Querying
58
+ `pandasql` uses [SQLite syntax](http://www.sqlite.org/lang.html). Any `pandas`
59
+ dataframes will be automatically detected by `pandasql`. You can query them as
60
+ you would any regular SQL table.
61
+
62
+
63
+ ```
64
+ $ python
65
+ >>> from pandasql import PandaSQL, load_meat, load_birth
66
+ >>> meat = load_meat()
67
+ >>> births = load_births()
68
+ >>> pdsql = PandaSQL()
69
+ >>> print pysqldf("SELECT * FROM meat LIMIT 10;").head()
70
+ date beef veal pork lamb_and_mutton broilers other_chicken turkey
71
+ 0 1944-01-01 00:00:00 751 85 1280 89 None None None
72
+ 1 1944-02-01 00:00:00 713 77 1169 72 None None None
73
+ 2 1944-03-01 00:00:00 741 90 1128 75 None None None
74
+ 3 1944-04-01 00:00:00 650 89 978 66 None None None
75
+ 4 1944-05-01 00:00:00 681 106 1029 78 None None None
76
+ ```
77
+
78
+ joins and aggregations are also supported
79
+ ```
80
+ >>> q = """SELECT
81
+ m.date, m.beef, b.births
82
+ FROM
83
+ meats m
84
+ INNER JOIN
85
+ births b
86
+ ON m.date = b.date;"""
87
+ >>> joined = pyqldf(q)
88
+ >>> print joined.head()
89
+ date beef births
90
+ 403 2012-07-01 00:00:00 2200.8 368450
91
+ 404 2012-08-01 00:00:00 2367.5 359554
92
+ 405 2012-09-01 00:00:00 2016.0 361922
93
+ 406 2012-10-01 00:00:00 2343.7 347625
94
+ 407 2012-11-01 00:00:00 2206.6 320195
95
+
96
+ >>> q = "select
97
+ strftime('%Y', date) as year
98
+ , SUM(beef) as beef_total
99
+ FROM
100
+ meat
101
+ GROUP BY
102
+ year;"
103
+ >>> print pysqldf(q).head()
104
+ year beef_total
105
+ 0 1944 8801
106
+ 1 1945 9936
107
+ 2 1946 9010
108
+ 3 1947 10096
109
+ 4 1948 8766
110
+ ```
111
+ ---
112
+ ## More Info
113
+ More information and code samples available in the [examples](https://github.com/bluestealth/pandasql/blob/master/examples/demo.py) folder.
@@ -0,0 +1,90 @@
1
+ pandasql
2
+ ========
3
+
4
+ This is a fork of the original `pandasql`, with support of multiple SQL
5
+ backends and more convenient interface. See below for more info.
6
+
7
+ `pandasql` allows you to query `pandas` DataFrames using SQL syntax. It works
8
+ similarly to `sqldf` in R. `pandasql` seeks to provide a more familiar way of
9
+ manipulating and cleaning data for people new to Python or `pandas`.
10
+
11
+ ## Installation
12
+ ```
13
+ $ pip install -U pandasql
14
+ ```
15
+
16
+ ## Basics
17
+ In addition to the original pandasql's ``sqldf`` function this fork has
18
+ a class ``PandaSQL``, which new users are encouraged to use.
19
+
20
+ `PandaSQL` Class
21
+ ---
22
+ The class is more convenient when you need to perform multiple queries. `PandaSQL` takes 2 arguments:
23
+ - `db_uri`: an optional SQLAlchemy connection string (defaults to in-memory SQLite database)
24
+ - `persist`: an optional boolean to determine if loaded tables are persisted in the database (holds connection open, default False)
25
+
26
+ `sqldf` Function
27
+ ---
28
+ The main function used in pandasql is `sqldf`. `sqldf` accepts 3 parameters:
29
+ - an sql query string
30
+ - an optional SQLAlchemy connection string (defaults to in-memory SQLite database)
31
+ - an optional dict of session/environment variables (defaults to `**locals()`,`**globals()`)
32
+
33
+
34
+ ## Querying
35
+ `pandasql` uses [SQLite syntax](http://www.sqlite.org/lang.html). Any `pandas`
36
+ dataframes will be automatically detected by `pandasql`. You can query them as
37
+ you would any regular SQL table.
38
+
39
+
40
+ ```
41
+ $ python
42
+ >>> from pandasql import PandaSQL, load_meat, load_birth
43
+ >>> meat = load_meat()
44
+ >>> births = load_births()
45
+ >>> pdsql = PandaSQL()
46
+ >>> print pysqldf("SELECT * FROM meat LIMIT 10;").head()
47
+ date beef veal pork lamb_and_mutton broilers other_chicken turkey
48
+ 0 1944-01-01 00:00:00 751 85 1280 89 None None None
49
+ 1 1944-02-01 00:00:00 713 77 1169 72 None None None
50
+ 2 1944-03-01 00:00:00 741 90 1128 75 None None None
51
+ 3 1944-04-01 00:00:00 650 89 978 66 None None None
52
+ 4 1944-05-01 00:00:00 681 106 1029 78 None None None
53
+ ```
54
+
55
+ joins and aggregations are also supported
56
+ ```
57
+ >>> q = """SELECT
58
+ m.date, m.beef, b.births
59
+ FROM
60
+ meats m
61
+ INNER JOIN
62
+ births b
63
+ ON m.date = b.date;"""
64
+ >>> joined = pyqldf(q)
65
+ >>> print joined.head()
66
+ date beef births
67
+ 403 2012-07-01 00:00:00 2200.8 368450
68
+ 404 2012-08-01 00:00:00 2367.5 359554
69
+ 405 2012-09-01 00:00:00 2016.0 361922
70
+ 406 2012-10-01 00:00:00 2343.7 347625
71
+ 407 2012-11-01 00:00:00 2206.6 320195
72
+
73
+ >>> q = "select
74
+ strftime('%Y', date) as year
75
+ , SUM(beef) as beef_total
76
+ FROM
77
+ meat
78
+ GROUP BY
79
+ year;"
80
+ >>> print pysqldf(q).head()
81
+ year beef_total
82
+ 0 1944 8801
83
+ 1 1945 9936
84
+ 2 1946 9010
85
+ 3 1947 10096
86
+ 4 1948 8766
87
+ ```
88
+ ---
89
+ ## More Info
90
+ More information and code samples available in the [examples](https://github.com/bluestealth/pandasql/blob/master/examples/demo.py) folder.
@@ -0,0 +1,109 @@
1
+ [project]
2
+ name = "pandas_query_sql"
3
+ version = "0.8.0"
4
+ description = "sqldf for pandas"
5
+ authors = [
6
+ { name = "Alexander Plavin", email = "alexander@plav.in" },
7
+ { name = "Greg Lamp", email = "lamp.greg@gmail.com" },
8
+ { name = "Michael Dempsey", email = "bluestealth@bluestealth.pw" },
9
+ ]
10
+ dependencies = [
11
+ "numpy",
12
+ "packaging",
13
+ "pandas",
14
+ "sqlalchemy",
15
+ ]
16
+ requires-python = ">=3.8"
17
+ readme = "README.md"
18
+ classifiers = [
19
+ "License :: OSI Approved :: MIT License",
20
+ ]
21
+
22
+ [project.license]
23
+ text = "MIT"
24
+
25
+ [project.urls]
26
+ Homepage = "https://github.com/bluestealth/pandasql/"
27
+
28
+ [project.optional-dependencies]
29
+ postgresql = [
30
+ "psycopg2-binary",
31
+ ]
32
+ linters = [
33
+ "black",
34
+ "ruff",
35
+ ]
36
+ testing = [
37
+ "pytest",
38
+ "psycopg2-binary",
39
+ ]
40
+
41
+ [build-system]
42
+ requires = [
43
+ "pdm-backend",
44
+ ]
45
+ build-backend = "pdm.backend"
46
+
47
+ [tool.pdm]
48
+ distribution = true
49
+
50
+ [tool.pdm.package-dir]
51
+ pandasql = "pandasql"
52
+
53
+ [tool.setuptools.package-data]
54
+ pandasql = [
55
+ "data/births.csv",
56
+ "data/birth_by_months.csv",
57
+ "data/meat.csv",
58
+ ]
59
+
60
+ [tool.ruff]
61
+ line-length = 120
62
+ target-version = "py38"
63
+
64
+ [tool.ruff.lint]
65
+ extend-select = [
66
+ "Q",
67
+ "RUF100",
68
+ "C90",
69
+ "UP",
70
+ "I",
71
+ "D",
72
+ "T",
73
+ ]
74
+ extend-ignore = []
75
+
76
+ [tool.ruff.lint.flake8-quotes]
77
+
78
+ [tool.ruff.lint.isort]
79
+ known-first-party = []
80
+
81
+ [tool.ruff.lint.mccabe]
82
+ max-complexity = 14
83
+
84
+ [tool.ruff.lint.pydocstyle]
85
+ convention = "google"
86
+
87
+ [tool.ruff.lint.per-file-ignores]
88
+ "src/pandasql/__init__.py" = [
89
+ "D",
90
+ ]
91
+ "examples/**/*.py" = [
92
+ "D",
93
+ ]
94
+ "tests/**/*.py" = [
95
+ "D",
96
+ ]
97
+
98
+ [tool.ruff.lint.extend-per-file-ignores]
99
+ "examples/**/*.py" = [
100
+ "T",
101
+ ]
102
+ "tests/**/*.py" = [
103
+ "T",
104
+ "E721",
105
+ "F811",
106
+ ]
107
+
108
+ [tool.ruff.format]
109
+ quote-style = "double"
@@ -0,0 +1,23 @@
1
+ import os
2
+
3
+ import pandas as pd
4
+
5
+ from .sqldf import PandaSQL, PandaSQLException, sqldf
6
+
7
+ __all__ = ["PandaSQL", "PandaSQLException", "sqldf"]
8
+
9
+ _ROOT = os.path.abspath(os.path.dirname(__file__))
10
+
11
+
12
+ def get_data(path: str) -> str:
13
+ return os.path.join(_ROOT, "data", path)
14
+
15
+
16
+ def load_meat() -> pd.DataFrame:
17
+ filename = get_data("meat.csv")
18
+ return pd.read_csv(filepath_or_buffer=filename, parse_dates=[0])
19
+
20
+
21
+ def load_births() -> pd.DataFrame:
22
+ filename = get_data("births_by_month.csv")
23
+ return pd.read_csv(filepath_or_buffer=filename, parse_dates=[0])
@@ -0,0 +1,64 @@
1
+ year,births,rate
2
+ 1910,2777000,30.1
3
+ 1915,2965000,29.5
4
+ 1920,2950000,27.7
5
+ 1925,2909000,25.1
6
+ 1930,2618000,21.3
7
+ 1935,2377000,18.7
8
+ 1940,2559000,19.4
9
+ 1945,2858000,20.4
10
+ 1950,3632000,24.1
11
+ 1952,3913000,25.1
12
+ 1953,3965000,25.1
13
+ 1954,4078000,25.3
14
+ 1955,4104000,25
15
+ 1956,4218000,25.2
16
+ 1957,4308000,25.3
17
+ 1958,4255000,24.5
18
+ 1959,4295000,24.3
19
+ 1960,4257850,23.7
20
+ 1961,4268326,23.3
21
+ 1962,4167362,22.4
22
+ 1963,4098020,21.7
23
+ 1964,4027490,21
24
+ 1965,3760358,19.4
25
+ 1966,3606274,18.4
26
+ 1967,3520959,17.8
27
+ 1968,3501564,17.5
28
+ 1969,3600206,17.8
29
+ 1970,3731386,18.4
30
+ 1971,3555970,17.2
31
+ 1972,3258411,15.6
32
+ 1973,3136965,14.9
33
+ 1974,3159958,14.9
34
+ 1975,3144198,14.8
35
+ 1976,3167788,14.8
36
+ 1977,3326632,15.4
37
+ 1978,3333279,15.3
38
+ 1979,3494398,15.9
39
+ 1980,3612258,15.9
40
+ 1982,3680537,15.9
41
+ 1983,3638933,15.5
42
+ 1984,3669141,15.5
43
+ 1985,3760561,15.8
44
+ 1986,3731000,15.5
45
+ 1987,3829000,15.7
46
+ 1988,3913000,15.9
47
+ 1989,4021000,16.2
48
+ 1990,4179000,16.7
49
+ 1991,4111000,16.2
50
+ 1992,4084000,16
51
+ 1993,4039000,15.7
52
+ 1994,3979000,15.3
53
+ 1995,3892000,14.8
54
+ 1996,3899000,14.7
55
+ 1997,3882000,14.5
56
+ 1998,3941553,14.6
57
+ 1999,3959417,14.5
58
+ 2000,4058814,14.7
59
+ 2001,4025933,14.1
60
+ 2002,4021726,13.9
61
+ 2003,4089950,14.1
62
+ 2004,4112052,14
63
+ 2005,4138349,14
64
+ 2009,4131019,13.8