pandas_query_sql 0.8.2__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,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,110 @@
1
+ Metadata-Version: 2.4
2
+ Name: pandas_query_sql
3
+ Version: 0.8.2
4
+ Summary: sqldf for pandas
5
+ Author: Alexander Plavin, Greg Lamp, Michael Dempsey
6
+ Author-email: Alexander Plavin <alexander@plav.in>, Greg Lamp <lamp.greg@gmail.com>, Michael Dempsey <bluestealth@bluestealth.pw>
7
+ License-Expression: MIT
8
+ License-File: LICENSE.txt
9
+ Requires-Dist: numpy
10
+ Requires-Dist: packaging
11
+ Requires-Dist: pandas
12
+ Requires-Dist: sqlalchemy
13
+ Requires-Dist: psycopg2 ; extra == 'postgresql'
14
+ Requires-Dist: psycopg2-binary ; extra == 'postgresql-binary'
15
+ Requires-Python: >=3.10
16
+ Project-URL: Homepage, https://github.com/bluestealth/pandasql/
17
+ Provides-Extra: postgresql
18
+ Provides-Extra: postgresql-binary
19
+ Description-Content-Type: text/markdown
20
+
21
+ pandasql
22
+ ========
23
+
24
+ This is a fork of the original `pandasql`, with support of multiple SQL
25
+ backends and more convenient interface. See below for more info.
26
+
27
+ `pandasql` allows you to query `pandas` DataFrames using SQL syntax. It works
28
+ similarly to `sqldf` in R. `pandasql` seeks to provide a more familiar way of
29
+ manipulating and cleaning data for people new to Python or `pandas`.
30
+
31
+ ## Installation
32
+ ```
33
+ $ pip install -U pandasql
34
+ ```
35
+
36
+ ## Basics
37
+ In addition to the original pandasql's ``sqldf`` function this fork has
38
+ a class ``PandaSQL``, which new users are encouraged to use.
39
+
40
+ `PandaSQL` Class
41
+ ---
42
+ The class is more convenient when you need to perform multiple queries. `PandaSQL` takes 2 arguments:
43
+ - `db_uri`: an optional SQLAlchemy connection string (defaults to in-memory SQLite database)
44
+ - `persist`: an optional boolean to determine if loaded tables are persisted in the database (holds connection open, default False)
45
+
46
+ `sqldf` Function
47
+ ---
48
+ The main function used in pandasql is `sqldf`. `sqldf` accepts 3 parameters:
49
+ - an sql query string
50
+ - an optional SQLAlchemy connection string (defaults to in-memory SQLite database)
51
+ - an optional dict of session/environment variables (defaults to `**locals()`,`**globals()`)
52
+
53
+
54
+ ## Querying
55
+ `pandasql` uses [SQLite syntax](http://www.sqlite.org/lang.html). Any `pandas`
56
+ dataframes will be automatically detected by `pandasql`. You can query them as
57
+ you would any regular SQL table.
58
+
59
+
60
+ ```
61
+ $ python
62
+ >>> from pandasql import PandaSQL, load_meat, load_birth
63
+ >>> meat = load_meat()
64
+ >>> births = load_births()
65
+ >>> pdsql = PandaSQL()
66
+ >>> print pysqldf("SELECT * FROM meat LIMIT 10;").head()
67
+ date beef veal pork lamb_and_mutton broilers other_chicken turkey
68
+ 0 1944-01-01 00:00:00 751 85 1280 89 None None None
69
+ 1 1944-02-01 00:00:00 713 77 1169 72 None None None
70
+ 2 1944-03-01 00:00:00 741 90 1128 75 None None None
71
+ 3 1944-04-01 00:00:00 650 89 978 66 None None None
72
+ 4 1944-05-01 00:00:00 681 106 1029 78 None None None
73
+ ```
74
+
75
+ joins and aggregations are also supported
76
+ ```
77
+ >>> q = """SELECT
78
+ m.date, m.beef, b.births
79
+ FROM
80
+ meats m
81
+ INNER JOIN
82
+ births b
83
+ ON m.date = b.date;"""
84
+ >>> joined = pyqldf(q)
85
+ >>> print joined.head()
86
+ date beef births
87
+ 403 2012-07-01 00:00:00 2200.8 368450
88
+ 404 2012-08-01 00:00:00 2367.5 359554
89
+ 405 2012-09-01 00:00:00 2016.0 361922
90
+ 406 2012-10-01 00:00:00 2343.7 347625
91
+ 407 2012-11-01 00:00:00 2206.6 320195
92
+
93
+ >>> q = "select
94
+ strftime('%Y', date) as year
95
+ , SUM(beef) as beef_total
96
+ FROM
97
+ meat
98
+ GROUP BY
99
+ year;"
100
+ >>> print pysqldf(q).head()
101
+ year beef_total
102
+ 0 1944 8801
103
+ 1 1945 9936
104
+ 2 1946 9010
105
+ 3 1947 10096
106
+ 4 1948 8766
107
+ ```
108
+ ---
109
+ ## More Info
110
+ 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,74 @@
1
+ [project]
2
+ name = "pandas_query_sql"
3
+ version = "0.8.2"
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.10"
17
+ readme = "README.md"
18
+ license = "MIT"
19
+ license-files = ["LICENSE.txt"]
20
+
21
+ [project.urls]
22
+ Homepage = "https://github.com/bluestealth/pandasql/"
23
+
24
+ [project.optional-dependencies]
25
+ postgresql = [
26
+ "psycopg2",
27
+ ]
28
+ postgresql-binary = [
29
+ "psycopg2-binary",
30
+ ]
31
+
32
+ [dependency-groups]
33
+ dev = [
34
+ "ruff",
35
+ "pytest",
36
+ "psycopg2-binary",
37
+ "testcontainers[postgres]",
38
+ 'ty',
39
+ ]
40
+
41
+ [build-system]
42
+ requires = ["uv_build>=0.11.28,<0.12"]
43
+ build-backend = "uv_build"
44
+
45
+ [tool.uv.build-backend]
46
+ module-name = "pandasql"
47
+
48
+ [tool.ty.src]
49
+ exclude = ["examples/**"]
50
+
51
+ [tool.ruff]
52
+ line-length = 120
53
+ target-version = 'py310'
54
+
55
+ [tool.ruff.lint]
56
+ select = ['ALL']
57
+ ignore = ['COM812', 'FBT001', 'FBT002', 'TRY003', 'EM101', 'PLR2004', 'N818']
58
+ flake8-quotes = {}
59
+ isort = { known-first-party = [] }
60
+ mccabe = { max-complexity = 14 }
61
+ pydocstyle = { convention = 'google' }
62
+
63
+ [tool.ruff.format]
64
+ quote-style = "double"
65
+ indent-style = "space"
66
+
67
+ [tool.ruff.lint.per-file-ignores]
68
+ 'src/pandasql/__init__.py' = ['D']
69
+ "examples/**/*.py" = ['D', 'INP001']
70
+ "tests/**/*.py" = ['D', 'BLE001']
71
+
72
+ [tool.ruff.lint.extend-per-file-ignores]
73
+ "examples/**/*.py" = ['T']
74
+ "tests/**/*.py" = ['T', 'S101', 'PD901', 'PLR']
@@ -0,0 +1,23 @@
1
+ import pathlib
2
+
3
+ import pandas as pd
4
+
5
+ from .sqldf import PandaSQL, PandaSQLException, sqldf
6
+
7
+ __all__ = ["PandaSQL", "PandaSQLException", "sqldf"]
8
+
9
+ _ROOT = pathlib.Path(__file__).absolute().parent
10
+
11
+
12
+ def get_data(path: str) -> pathlib.Path:
13
+ return _ROOT.joinpath("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