Skip to content

Commit

Permalink
Minor changes to docstrings.
Browse files Browse the repository at this point in the history
  • Loading branch information
root-11 committed Jan 24, 2024
1 parent 56b902f commit 97af15a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 80 deletions.
2 changes: 2 additions & 0 deletions tablite/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class Config(object):
Config.workdir = pathlib.Path(os.environ.get("TABLITE_TMPDIR", f"{tempfile.gettempdir()}/tablite-tmp"))
to overwrite, first import the config class, then set the new workdir.
```
>>> from tablite import config
>>> from pathlib import Path
>>> config.workdir = Path("/this/new/location")
```
for every new table or record this path will be used.
PAGE_SIZE = 1_000_000 sets the page size limit.
Expand Down
174 changes: 94 additions & 80 deletions tablite/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def from_pandas(cls, df):
Creates Table using pd.to_dict('list')
similar to:
```
>>> import pandas as pd
>>> df = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6]})
>>> df
Expand All @@ -228,7 +229,6 @@ def from_pandas(cls, df):
2 3 6
>>> df.to_dict('list')
{'a': [1, 2, 3], 'b': [4, 5, 6]}
>>> t = Table.from_dict(df.to_dict('list))
>>> t.show()
+===+===+===+
Expand All @@ -239,6 +239,7 @@ def from_pandas(cls, df):
| 1 | 2| 5|
| 2 | 3| 6|
+===+===+===+
```
"""
return import_utils.from_pandas(cls, df)

Expand Down Expand Up @@ -529,74 +530,79 @@ def groupby(self, keys, functions, tqdm=_tqdm, pbar=None):
returns: table
Example:
```
t = Table()
t.add_column('A', data=[1, 1, 2, 2, 3, 3] * 2)
t.add_column('B', data=[1, 2, 3, 4, 5, 6] * 2)
t.add_column('C', data=[6, 5, 4, 3, 2, 1] * 2)
t.show()
# +=====+=====+=====+
# | A | B | C |
# | int | int | int |
# +-----+-----+-----+
# | 1| 1| 6|
# | 1| 2| 5|
# | 2| 3| 4|
# | 2| 4| 3|
# | 3| 5| 2|
# | 3| 6| 1|
# | 1| 1| 6|
# | 1| 2| 5|
# | 2| 3| 4|
# | 2| 4| 3|
# | 3| 5| 2|
# | 3| 6| 1|
# +=====+=====+=====+
+=====+=====+=====+
| A | B | C |
| int | int | int |
+-----+-----+-----+
| 1| 1| 6|
| 1| 2| 5|
| 2| 3| 4|
| 2| 4| 3|
| 3| 5| 2|
| 3| 6| 1|
| 1| 1| 6|
| 1| 2| 5|
| 2| 3| 4|
| 2| 4| 3|
| 3| 5| 2|
| 3| 6| 1|
+=====+=====+=====+
g = t.groupby(keys=['A', 'C'], functions=[('B', gb.sum)])
g.show()
# +===+===+===+======+
# | # | A | C |Sum(B)|
# |row|int|int| int |
# +---+---+---+------+
# |0 | 1| 6| 2|
# |1 | 1| 5| 4|
# |2 | 2| 4| 6|
# |3 | 2| 3| 8|
# |4 | 3| 2| 10|
# |5 | 3| 1| 12|
# +===+===+===+======+
+===+===+===+======+
| # | A | C |Sum(B)|
|row|int|int| int |
+---+---+---+------+
|0 | 1| 6| 2|
|1 | 1| 5| 4|
|2 | 2| 4| 6|
|3 | 2| 3| 8|
|4 | 3| 2| 10|
|5 | 3| 1| 12|
+===+===+===+======+
```
Cheat sheet:
# list of unique values
list of unique values
```
>>> g1 = t.groupby(keys=['A'], functions=[])
>>> g1['A'][:]
[1,2,3]
# alternatively:
```
alternatively:
>>> t['A'].unique()
[1,2,3]
# list of unique values, grouped by longest combination.
list of unique values, grouped by longest combination.
```
>>> g2 = t.groupby(keys=['A', 'B'], functions=[])
>>> g2['A'][:], g2['B'][:]
([1,1,2,2,3,3], [1,2,3,4,5,6])
# alternatively:
```
alternatively:
```
>>> list(zip(*t.index('A', 'B').keys()))
[(1,1,2,2,3,3) (1,2,3,4,5,6)]
# A key (unique values) and count hereof.
```
A key (unique values) and count hereof.
```
>>> g3 = t.groupby(keys=['A'], functions=[('A', gb.count)])
>>> g3['A'][:], g3['Count(A)'][:]
([1,2,3], [4,4,4])
# alternatively:
```
alternatively:
```
>>> t['A'].histogram()
([1,2,3], [4,4,4])
```
for more exmaples see:
https://github.com/root-11/tablite/blob/master/tests/test_groupby.py
Expand All @@ -610,40 +616,40 @@ def pivot(self, rows, columns, functions, values_as_rows=True, tqdm=_tqdm, pbar=
param: functions: aggregation functions from the Groupby class as
example:
```
t.show()
# +=====+=====+=====+
# | A | B | C |
# | int | int | int |
# +-----+-----+-----+
# | 1| 1| 6|
# | 1| 2| 5|
# | 2| 3| 4|
# | 2| 4| 3|
# | 3| 5| 2|
# | 3| 6| 1|
# | 1| 1| 6|
# | 1| 2| 5|
# | 2| 3| 4|
# | 2| 4| 3|
# | 3| 5| 2|
# | 3| 6| 1|
# +=====+=====+=====+
+=====+=====+=====+
| A | B | C |
| int | int | int |
+-----+-----+-----+
| 1| 1| 6|
| 1| 2| 5|
| 2| 3| 4|
| 2| 4| 3|
| 3| 5| 2|
| 3| 6| 1|
| 1| 1| 6|
| 1| 2| 5|
| 2| 3| 4|
| 2| 4| 3|
| 3| 5| 2|
| 3| 6| 1|
+=====+=====+=====+
t2 = t.pivot(rows=['C'], columns=['A'], functions=[('B', gb.sum)])
t2.show()
# +===+===+========+=====+=====+=====+
# | # | C |function|(A=1)|(A=2)|(A=3)|
# |row|int| str |mixed|mixed|mixed|
# +---+---+--------+-----+-----+-----+
# |0 | 6|Sum(B) | 2|None |None |
# |1 | 5|Sum(B) | 4|None |None |
# |2 | 4|Sum(B) |None | 6|None |
# |3 | 3|Sum(B) |None | 8|None |
# |4 | 2|Sum(B) |None |None | 10|
# |5 | 1|Sum(B) |None |None | 12|
# +===+===+========+=====+=====+=====+
+===+===+========+=====+=====+=====+
| # | C |function|(A=1)|(A=2)|(A=3)|
|row|int| str |mixed|mixed|mixed|
+---+---+--------+-----+-----+-----+
|0 | 6|Sum(B) | 2|None |None |
|1 | 5|Sum(B) | 4|None |None |
|2 | 4|Sum(B) |None | 6|None |
|3 | 3|Sum(B) |None | 8|None |
|4 | 2|Sum(B) |None |None | 10|
|5 | 1|Sum(B) |None |None | 12|
+===+===+========+=====+=====+=====+
```
"""
return pivots.pivot(self, rows, columns, functions, values_as_rows, tqdm=tqdm, pbar=pbar)

Expand All @@ -660,6 +666,7 @@ def merge(self, left, right, new, criteria):
:returns: T
Example:
```
>>> c.show()
+==+====+====+====+====+
| #| A | B | C | D |
Expand All @@ -686,7 +693,7 @@ def merge(self, left, right, new, criteria):
| 5|None| 16| 6|
| 6|None| 17| 7|
+==+====+====+====+
```
"""
return merge.where(self, criteria,left,right,new)

Expand Down Expand Up @@ -715,10 +722,12 @@ def left_join(self, other, left_keys, right_keys, left_columns=None, right_colum
:param right_columns: list of right columns to retain, if None, all are retained.
:return: new Table
Example:
```
SQL: SELECT number, letter FROM numbers LEFT JOIN letters ON numbers.colour == letters.color
Tablite: left_join = numbers.left_join(
letters, left_keys=['colour'], right_keys=['color'], left_columns=['number'], right_columns=['letter']
)
```
"""
return joins.left_join(self, other, left_keys, right_keys, left_columns, right_columns, merge_keys=merge_keys, tqdm=tqdm, pbar=pbar)

Expand All @@ -731,10 +740,12 @@ def inner_join(self, other, left_keys, right_keys, left_columns=None, right_colu
:param right_columns: list of right columns to retain, if None, all are retained.
:return: new Table
Example:
```
SQL: SELECT number, letter FROM numbers JOIN letters ON numbers.colour == letters.color
Tablite: inner_join = numbers.inner_join(
letters, left_keys=['colour'], right_keys=['color'], left_columns=['number'], right_columns=['letter']
)
```
"""
return joins.inner_join(self, other, left_keys, right_keys, left_columns, right_columns, merge_keys=merge_keys, tqdm=tqdm, pbar=pbar)

Expand All @@ -747,10 +758,12 @@ def outer_join(self, other, left_keys, right_keys, left_columns=None, right_colu
:param right_columns: list of right columns to retain, if None, all are retained.
:return: new Table
Example:
```
SQL: SELECT number, letter FROM numbers OUTER JOIN letters ON numbers.colour == letters.color
Tablite: outer_join = numbers.outer_join(
letters, left_keys=['colour'], right_keys=['color'], left_columns=['number'], right_columns=['letter']
)
```
"""
return joins.outer_join(self, other, left_keys, right_keys, left_columns, right_columns, merge_keys=merge_keys, tqdm=tqdm, pbar=pbar)

Expand All @@ -774,11 +787,12 @@ def lookup(self, other, *criteria, all=True, tqdm=_tqdm):
RIGHT must be a value that the OPERATOR can compare.
Examples:
('column A', "==", 'column B') # comparison of two columns
('Date', "<", DataTypes.date(24,12) ) # value from column 'Date' is before 24/12.
f = lambda L,R: all( ord(L) < ord(R) ) # uses custom function.
('text 1', f, 'text 2')
value from column 'text 1' is compared with value from column 'text 2'
```
('column A', "==", 'column B') # comparison of two columns
('Date', "<", DataTypes.date(24,12) ) # value from column 'Date' is before 24/12.
f = lambda L,R: all( ord(L) < ord(R) ) # uses custom function.
('text 1', f, 'text 2') value from column 'text 1' is compared with value from column 'text 2'
```
"""
return lookup.lookup(self, other, *criteria, all=all, tqdm=tqdm)

Expand Down Expand Up @@ -871,7 +885,7 @@ def pivot_transpose(self, columns, keep=None, column_name="transpose", value_nam
transpose columns 1,2 and 3 and transpose the remaining columns, except `sum`.
Input:
```
| col1 | col2 | col3 | sun | mon | tue | ... | sat | sum |
|------|------|------|-----|-----|-----|-----|-----|------|
| 1234 | 2345 | 3456 | 456 | 567 | | ... | | 1023 |
Expand All @@ -887,7 +901,7 @@ def pivot_transpose(self, columns, keep=None, column_name="transpose", value_nam
|1234| 2345| 3456| sun | 456|
|1234| 2345| 3456| mon | 567|
|1244| 2445| 4456| mon | 7|
```
"""
return pivots.pivot_transpose(self, columns, keep, column_name, value_name, tqdm=tqdm)

Expand Down

0 comments on commit 97af15a

Please sign in to comment.