Table(*arguments, **keyword_arguments)
Source¶
Named columns, each holding values of one type, wrapping a pandas DataFrame.
A Table never changes: every operation returns a new Table. Its rows are numbered from 0, and renumbered after a filter or a sort. Integer, boolean and text columns accept missing values.
- Create it with
from_csv,from_json,from_excel,from_parquet,from_stata,from_sas,from_xpt,from_pandasorfrom_arrays. - Write it with
to_csv,to_json,to_excel,to_parquetorto_stata. - Select rows with
filterandtake, check values withcheck, read a column withcolumn. - Change columns with
choose_columns,rename,convert,applyandadd_column. - Compute with
aggregate,pivot_tableanddescribe, and count each value of a column withcount_values. - Rearrange rows with
sortanddrop_duplicates, and combine two Tables withjoin. - Handle missing values with
count_missing,preview_missinganddrop_missing. - Read it with
row_count,column_names,column_types,to_value,to_pandasandis_equal, and show it withprevieworprint.
Calling ndp.Table(...), comparisons, brackets, for, in, len and bool are refused
with RefusedOperationError, which names the method to use instead.
Added in version 0.1.0
Examples:
import numdaplot as ndp
cities = ndp.Array.from_list(["Paris", "Lyon", "Paris"])
prices = ndp.Array.from_list([120.0, 80.0, 150.0])
sales = ndp.Table.from_arrays({"city": cities, "price": prices})
sales
# city price
# TEXT FLOAT64
# 0 Paris 120.0
# 1 Lyon 80.0
# 2 Paris 150.0
sales.row_count
# 3
sales.column_names
# ['city', 'price']
Reading files¶
from_csv(path, options=None)
Source¶
Return a Table read from a CSV file whose first row holds the column names.
Each column takes the type of all its values, even in a long file: a column mixing numbers and texts is read
as texts written as in the file, such as 01.
Added in version 0.1.0
Parameters:
-
path(FilePath)The path of the file, a
FilePathsuch as"sales.csv"orPath("data/sales.csv"). -
options(CsvReadOptions | None, default:None)The separator and the encoding,
","and UTF-8 when omitted.
Returns:
-
SelfA new Table, empty for an empty file.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
FileNotFoundErrorWhen there is no file at the path.
-
FileAccessErrorWhen the file system refuses the file, for a missing permission for example.
-
(InvalidColumnNameError, DuplicateColumnError)When a column name is empty, even once cleaned, or appears twice, or two names become the same once cleaned.
Examples:
from_json(path, options=None)
Source¶
Return a Table read from a JSON file in UTF-8 holding one object per row.
Each column takes the type of its JSON values: a text that looks like a number or a date stays a text.
Added in version 0.1.0
Parameters:
-
path(FilePath)The path of the file, a
FilePathsuch as"sales.json"orPath("data/sales.json"). -
options(JsonReadOptions | None, default:None)The layout of the rows, one list of objects when omitted.
Returns:
-
SelfA new Table, with one column per name found in the objects.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
FileNotFoundErrorWhen there is no file at the path.
-
FileAccessErrorWhen the file system refuses the file, for a missing permission for example.
-
(InvalidColumnNameError, DuplicateColumnError)When a column name is empty, even once cleaned, or two names become the same once cleaned.
Examples:
import tempfile
from pathlib import Path
import numdaplot as ndp
path = Path(tempfile.mkdtemp()) / "sales.jsonl"
_ = path.write_text('{"city": "Paris", "count": 2}\n{"city": "Lyon", "count": null}\n')
ndp.Table.from_json(path, ndp.JsonReadOptions(layout=ndp.JsonLayout.LINES))
# city count
# TEXT INT64
# 0 Paris 2
# 1 Lyon <NA>
from_excel(path, options=None)
Source¶
Return a Table read from a sheet of an Excel file whose first row holds the column names.
Needs the excel extra: pip install numdaplot[excel].
Added in version 0.1.0
Parameters:
-
path(FilePath)The path of the file, a
FilePathsuch as"sales.xlsx"orPath("data/sales.xlsx"). -
options(ExcelReadOptions | None, default:None)The sheet to read, the first one when omitted.
Returns:
-
Self
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
FileNotFoundErrorWhen there is no file at the path.
-
FileAccessErrorWhen the file system refuses the file, for a missing permission for example.
-
MissingDependencyErrorWhen the
excelextra is not installed. -
(InvalidColumnNameError, DuplicateColumnError)When a cell of the first row is empty, even once cleaned, is not a text, or repeats another one, or two names become the same once cleaned.
-
TimeOverflowErrorWhen a column holds a datetime outside 1677 to 2262, or a duration beyond about 292 years.
Examples:
from_parquet(path, options=None)
Source¶
Return a Table read from a Parquet file.
Needs the parquet extra: pip install numdaplot[parquet].
Added in version 0.1.0
Parameters:
-
path(FilePath)The path of the file, a
FilePathsuch as"sales.parquet"orPath("data/sales.parquet"). -
options(ParquetReadOptions | None, default:None)Whether the column names are cleaned, cleaned when omitted.
Returns:
-
SelfA new Table, with the types stored in the file.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
FileNotFoundErrorWhen there is no file at the path.
-
FileAccessErrorWhen the file system refuses the file, for a missing permission for example.
-
MissingDependencyErrorWhen the
parquetextra is not installed. -
TimeOverflowErrorWhen a column holds a datetime outside 1677 to 2262, or a duration beyond about 292 years.
-
DuplicateColumnErrorWhen two column names become the same once cleaned.
Examples:
from_stata(path, options=None)
Source¶
Return a Table read from a Stata file (.dta).
Added in version 0.1.0
Parameters:
-
path(FilePath)The path of the file, a
FilePathsuch as"sales.dta"orPath("data/sales.dta"). -
options(StataReadOptions | None, default:None)Whether the column names are cleaned, cleaned when omitted.
Returns:
-
SelfA new Table, with the types stored in the file.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
FileNotFoundErrorWhen there is no file at the path.
-
FileAccessErrorWhen the file system refuses the file, for a missing permission for example.
-
TimeOverflowErrorWhen a column holds a datetime outside 1677 to 2262, or a duration beyond about 292 years.
-
DuplicateColumnErrorWhen two column names become the same once cleaned.
Examples:
from_sas(path, options=None)
Source¶
Return a Table read from a SAS data file (.sas7bdat). numdaplot does not write this format.
The example reads airline.sas7bdat, a SAS data file of 32 rows.
Added in version 0.1.0
Parameters:
-
path(FilePath)The path of the file, a
FilePathsuch as"sales.sas7bdat"orPath("data/sales.sas7bdat"). -
options(SasReadOptions | None, default:None)The encoding of the texts, UTF-8 when omitted.
Returns:
-
SelfA new Table. SAS stores every number as a decimal, so numbers become
FLOAT64.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
FileNotFoundErrorWhen there is no file at the path.
-
FileAccessErrorWhen the file system refuses the file, for a missing permission for example.
-
TimeOverflowErrorWhen a column holds a datetime outside 1677 to 2262, or a duration beyond about 292 years.
-
DuplicateColumnErrorWhen two column names become the same once cleaned.
Examples:
import numdaplot as ndp
airline = ndp.Table.from_sas("airline.sas7bdat")
airline.preview(ndp.Preview(count=3))
# YEAR Y W R L K
# FLOAT64 FLOAT64 FLOAT64 FLOAT64 FLOAT64 FLOAT64
# 0 1948.0 1.214 0.243 0.1454 1.415 0.612
# 1 1949.0 1.354 0.260 0.2181 1.384 0.559
# 2 1950.0 1.569 0.278 0.3157 1.388 0.573
from_xpt(path, options=None)
Source¶
Return a Table read from a SAS transport file (.xpt). numdaplot does not write this format.
The example reads paxraw_d_short.xpt, a SAS transport file of 100 rows.
Added in version 0.1.0
Parameters:
-
path(FilePath)The path of the file, a
FilePathsuch as"sales.xpt"orPath("data/sales.xpt"). -
options(XptReadOptions | None, default:None)The encoding of the texts, UTF-8 when omitted.
Returns:
-
SelfA new Table. SAS stores every number as a decimal, so numbers become
FLOAT64.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
FileNotFoundErrorWhen there is no file at the path.
-
FileAccessErrorWhen the file system refuses the file, for a missing permission for example.
-
TimeOverflowErrorWhen a column holds a datetime outside 1677 to 2262, or a duration beyond about 292 years.
-
DuplicateColumnErrorWhen two column names become the same once cleaned.
Examples:
Writing files¶
to_csv(path, options=None, on_conflict=Conflict.ERROR)
Source¶
Write the Table in a CSV file, column names on the first row, without row numbers.
Missing values are written as empty values, and times of day as texts such as 15:35:00.
Added in version 0.1.0
Parameters:
-
path(FilePath)The path of the file, a
FilePathsuch as"sales.csv"orPath("data/sales.csv"). -
options(CsvWriteOptions | None, default:None)The separator and the encoding,
","and UTF-8 when omitted. -
on_conflict(Conflict, default:Conflict.ERROR)ERRORby default, refusing to overwrite an existing file, orREPLACEto overwrite it.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
FileExistsErrorWhen a file exists at the path and
on_conflictisERROR. -
FileAccessErrorWhen the file system refuses the file: missing folder, missing permission and so on.
Examples:
to_json(path, options=None, on_conflict=Conflict.ERROR)
Source¶
Write the Table in a JSON file in UTF-8, one object per row.
Missing values are written as null, dates and durations in the ISO 8601 format, times of day as texts such
as 15:35:00, and decimals with every digit of their value unless options gives their number.
Added in version 0.1.0
Parameters:
-
path(FilePath)The path of the file, a
FilePathsuch as"sales.json"orPath("data/sales.json"). -
options(JsonWriteOptions | None, default:None)The layout of the rows and the number of decimals, one list of objects with every digit when omitted.
-
on_conflict(Conflict, default:Conflict.ERROR)ERRORby default, refusing to overwrite an existing file, orREPLACEto overwrite it.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
FileExistsErrorWhen a file exists at the path and
on_conflictisERROR. -
FileAccessErrorWhen the file system refuses the file: missing folder, missing permission and so on.
-
ValueConversionErrorAt the first infinite value of a decimal column, which JSON cannot write: replace it first.
Examples:
to_excel(path, options=None, on_conflict=Conflict.ERROR)
Source¶
Write the Table in a sheet of an Excel file, column names on the first row.
Times of day are written as texts such as 15:35:00. Needs the excel extra:
pip install numdaplot[excel].
Added in version 0.1.0
Parameters:
-
path(FilePath)The path of the file, a
FilePathsuch as"sales.xlsx"orPath("data/sales.xlsx"). -
options(ExcelWriteOptions | None, default:None)The name of the sheet,
"Sheet1"when omitted. -
on_conflict(Conflict, default:Conflict.ERROR)ERRORby default, refusing to overwrite an existing file, orREPLACEto overwrite it.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
FileExistsErrorWhen a file exists at the path and
on_conflictisERROR. -
FileAccessErrorWhen the file system refuses the file: missing folder, missing permission and so on.
-
MissingDependencyErrorWhen the
excelextra is not installed.
Examples:
import tempfile
from pathlib import Path
import numdaplot as ndp
path = Path(tempfile.mkdtemp()) / "sales.xlsx"
table = ndp.Table.from_arrays({"price": ndp.Array.from_list([1.5])})
table.to_excel(path, ndp.ExcelWriteOptions(sheet="Sales"))
ndp.Table.from_excel(path, ndp.ExcelReadOptions(sheet="Sales")).row_count
# 1
to_parquet(path, on_conflict=Conflict.ERROR)
Source¶
Write the Table in a Parquet file, which keeps the type of each column.
Times of day are stored as Parquet times, read back as TIME. Needs the parquet extra:
pip install numdaplot[parquet].
Added in version 0.1.0
Parameters:
-
path(FilePath)The path of the file, a
FilePathsuch as"sales.parquet"orPath("data/sales.parquet"). -
on_conflict(Conflict, default:Conflict.ERROR)ERRORby default, refusing to overwrite an existing file, orREPLACEto overwrite it.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
FileExistsErrorWhen a file exists at the path and
on_conflictisERROR. -
FileAccessErrorWhen the file system refuses the file: missing folder, missing permission and so on.
-
MissingDependencyErrorWhen the
parquetextra is not installed.
Examples:
to_stata(path, on_conflict=Conflict.ERROR)
Source¶
Write the Table in a Stata file (.dta).
Stata stores integers and booleans holding missing values as decimals, a missing text as an empty text, and
times of day as texts such as 15:35:00, as it has no type for them. Column names that Stata refuses, such
as names with spaces, are changed by pandas with a warning.
Added in version 0.1.0
Parameters:
-
path(FilePath)The path of the file, a
FilePathsuch as"sales.dta"orPath("data/sales.dta"). -
on_conflict(Conflict, default:Conflict.ERROR)ERRORby default, refusing to overwrite an existing file, orREPLACEto overwrite it.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
FileExistsErrorWhen a file exists at the path and
on_conflictisERROR. -
FileAccessErrorWhen the file system refuses the file: missing folder, missing permission and so on.
Examples:
Information¶
column_names: list[str]
Source¶
Names of the columns, in order. Changing the returned list never changes the Table.
Added in version 0.1.0
Examples:
column_time_zones: dict[str, TimeZone | None]
Source¶
Time zone of each datetime column, by column name, None for a datetime column without zone.
Columns of other types are not listed.
Added in version 0.1.0
Examples:
Conversion and comparison¶
from_pandas(frame, column_names=ColumnNames.CLEANED)
Source¶
Return a Table holding a copy of a pandas DataFrame; changing the DataFrame never changes the Table.
Added in version 0.1.0
Parameters:
-
frame(pandas.DataFrame) -
column_names(ColumnNames, default:ColumnNames.CLEANED)Whether the column names are cleaned,
ColumnNames.CLEANEDby default: the spaces, tabs and line breaks at the start and the end of each name are removed.ColumnNames.KEPTkeeps the names as written.
Returns:
-
SelfA new Table.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
UnsupportedIndexErrorWhen the index holds other values: call
reset_index()first to keep them as a column. -
(InvalidColumnNameError, DuplicateColumnError)When a column name is not a non-empty text, even once cleaned, or appears twice, or two names become the same once cleaned.
-
UnsupportedTypeErrorWhen a column has a type numdaplot does not support, such as complex numbers or datetimes with a time zone.
-
TimeOverflowErrorWhen a column holds a datetime outside 1677 to 2262, or a duration beyond about 292 years.
Examples:
from_arrays(columns)
Source¶
Return a Table whose columns hold the values of one-dimensional Arrays.
Added in version 0.1.0
Parameters:
-
columns(dict[str, Array])One Array per column, by column name, all of the same length.
Returns:
-
SelfA new Table, columns in the order of the dictionary.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
EmptyListErrorWhen there is no column.
-
InvalidColumnNameErrorWhen a column name is not a non-empty text.
-
DimensionErrorWhen an Array does not have one dimension.
-
ShapeMismatchErrorWhen the Arrays do not have the same length.
-
TimeOverflowErrorWhen a column holds a datetime outside 1677 to 2262, or a duration beyond about 292 years.
Examples:
to_pandas()
Source¶
Return a pandas copy of the Table; changing it never changes the Table.
Integers, booleans and texts use the pandas dtypes that accept missing values (pd.NA).
Added in version 0.1.0
Returns:
-
pandas.DataFrameA copy, rows numbered from 0. Times of day are
datetime.timeobjects, as pandas has no type for them.
Examples:
column(column_name)
Source¶
Return the values of a column as an Array.
Added in version 0.1.0
Parameters:
-
column_name(str)The name of the column.
Returns:
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen no column has this name.
-
UnsupportedMissingErrorWhen an integer or boolean column has missing values, which an Array cannot hold: remove them with
drop_missing, or convert an integer column toFLOAT64first. -
TimeZoneMismatchErrorWhen the column holds datetimes with a time zone, which an Array cannot hold: remove the zone first with
TimeZoneMode.REMOVE.
Examples:
to_value()
Source¶
Return the single value of a Table of one row and one column, as a Python value.
Added in version 0.1.0
Returns:
-
AnyThe value,
Nonewhen it is missing. A datetime with a time zone keeps its zone.
Raises:
-
NotSingleValueErrorWhen the Table does not hold exactly one value.
Examples:
is_equal(equality)
Source¶
Return whether this Table and another one have the same columns, in the same order, with the same values.
Two missing values at the same row are equal. Datetime columns in different time zones are different, even for the same instants.
Added in version 0.1.0
Parameters:
-
equality(Equality)The other Table, and whether the type of each column must also be the same.
Returns:
-
boolTrue when both Tables are equal.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
Examples:
Selection¶
filter(row_filter)
Source¶
Return the rows satisfying a filter.
Added in version 0.1.0
Parameters:
Returns:
-
SelfA new Table with the kept rows, in their order, numbered from 0.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen a condition names a column the Table does not have.
-
IncompatibleTypesErrorWhen a rule does not apply to the type of one of its columns.
Examples:
import numdaplot as ndp
cities = ndp.Array.from_list(["Paris", "Lyon", "Pau"])
prices = ndp.Array.from_list([120.0, 80.0, 150.0])
table = ndp.Table.from_arrays({"city": cities, "price": prices})
expensive = ndp.Condition(rule=ndp.Greater(100), columns=["price"])
table.filter(ndp.Filter(conditions=[expensive]))
# city price
# TEXT FLOAT64
# 0 Paris 120.0
# 1 Pau 150.0
take(region)
Source¶
Return the rows and the columns of a region.
A row chosen with Index gives a Table of one row. Use to_value to read a single value.
Added in version 0.1.0
Parameters:
-
region(Region)The rows and the columns, every one of a dimension when omitted. Columns are positions or names; negative positions count from the end.
Returns:
-
SelfA new Table, rows numbered from 0, columns in the order of the region.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
InvalidPositionErrorWhen a position is not a whole number inside its dimension.
-
ColumnNotFoundErrorWhen a column name matches no column.
-
DuplicateColumnErrorWhen a column name appears twice in the region.
Examples:
check(rule)
Source¶
Return which values satisfy a rule, in every column.
To check some columns only, choose them first with choose_columns.
Added in version 0.1.0
Parameters:
-
rule(Rule)A
Rule, such asndp.Greater(10)orndp.IsIn(["Paris"]): seeRulefor every rule. A missing value only satisfiesIsMissing.
Returns:
-
SelfA new Table with the same columns and rows, holding True where the value satisfies the rule. Every column is of type
BOOL.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
IncompatibleTypesErrorWhen the rule does not apply to the type of one of the columns.
Examples:
Columns¶
choose_columns(choice)
Source¶
Return the Table with only some columns, or without some columns.
Added in version 0.1.0
Parameters:
-
choice(ColumnChoice)The columns, and whether to keep them, in the order of the list, or to drop them.
Returns:
-
SelfA new Table.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen a named column does not exist.
Examples:
rename(renaming)
Source¶
Return the Table with renamed columns, every renaming applied at once.
Two columns can swap their names in a single call.
Added in version 0.1.0
Parameters:
-
renaming(Renaming)The
NameChangeof each renamed column.
Returns:
-
SelfA new Table, columns in the same order.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen a renamed column does not exist.
-
DuplicateColumnErrorWhen a new name is the name of a column kept as it is.
Examples:
convert(conversion)
Source¶
Return the Table with columns converted to another type, missing values staying missing.
Changed in version 0.2.0
Converts every column of TableConversion.columns.
Version history (1)
Added in version 0.1.0
Parameters:
-
conversion(TableConversion)The columns, their new type, and the date format or the time zone change. Accepted: between number types, from numbers, booleans, categories, dates, datetimes and durations to texts, from texts and categories to numbers, booleans (
"true"or"false"in any case), dates or datetimes, from any other type to categories, between dates and datetimes, and a time zone change of datetimes.
Returns:
-
SelfA new Table, each column at its place.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen a column does not exist, before any column is converted.
-
IncompatibleTypesErrorWhen the conversion is not accepted.
-
InvalidConversionErrorWhen the format, the time zone change or the boolean rule does not apply to the conversion.
-
InvalidLimitsErrorWhen
Bracketscut integers and a bracket between two limits holds no integer. -
UnmatchedValueError -
ValueConversionErrorAt the first value that cannot be stored in the new type, such as
"abc"read as a number, 2.5 stored as an integer, 3 read as a boolean withZERO_ONE,"31/02/2026"read as a date, or"25:00"read as a time of day. -
TimeOverflowErrorWhen a value converted to
DATETIMEis outside 1677 to 2262. -
TimeZoneMismatchErrorWhen the time zone change does not apply to whether the datetimes have a zone.
-
The first column that fails stops the operation, and the error names it.
Examples:
import numdaplot as ndp
zip_codes = ndp.Array.from_list(["06000", "75001"])
department_codes = ndp.Array.from_list(["06", "75"])
places = ndp.Table.from_arrays({"zip_code": zip_codes, "department": department_codes})
places.convert(ndp.TableConversion(columns=["zip_code", "department"], dtype=ndp.Type.INT32))
# zip_code department
# INT32 INT32
# 0 6000 6
# 1 75001 75
apply(application)
Source¶
Return the Table with a math function, a time function or a value mapping applied to each value of columns.
Unlike Array.apply, an integer or boolean column keeps its type when a ValueMapping gives it missing
values, since a Table column can hold them.
Added in version 0.2.0
Parameters:
-
application(TableApplication)
Returns:
-
SelfA new Table, each column replaced at its place, under its name. Missing values stay missing.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen a column does not exist, before any column is changed.
-
IncompatibleTypesErrorWhen the function does not apply to the type of a column, or a
ValueMappingwithUnmatched.KEEPgives new values of another family than the kept values, such as texts among numbers. -
OutOfDomainErrorWhen a value is outside the domain of a math function, such as the square root of -1.
-
TimeZoneMismatchErrorWhen a column holds datetimes with a time zone: remove it first with
convertand aTimeZoneChange. -
UnmatchedValueErrorWhen a value has no
ValueChangein aValueMappingwhoseothersisUnmatched.ERROR. -
ValueConversionErrorWhen a
ValueMappinggives a number that does not fit the number type of a column. -
The first column that fails stops the operation, and the error names it.
Warns:
-
CalculationWarningWhen a result of a math function becomes an infinite value or NaN.
Examples:
import numdaplot as ndp
first_answers = ndp.Array.from_list([1, 7, 2])
second_answers = ndp.Array.from_list([9, 3, 1])
survey = ndp.Table.from_arrays({"q1": first_answers, "q2": second_answers})
unknown_answers = [ndp.ValueChange(7, None), ndp.ValueChange(9, None)]
mapping = ndp.ValueMapping(changes=unknown_answers, others=ndp.Unmatched.KEEP)
survey.apply(ndp.TableApplication(function=mapping, columns=["q1", "q2"]))
# q1 q2
# INT64 INT64
# 0 1 <NA>
# 1 <NA> 3
# 2 2 1
add_column(new_column)
Source¶
Return the Table with one more column, or with a column replaced.
Added in version 0.1.0
Parameters:
-
new_column(NewColumn)The name, a one-dimensional Array with one value per row, and what to do when the name exists.
Returns:
-
SelfA new Table. A new column is placed last; a replaced column keeps its place.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
DimensionErrorWhen the Array does not have one dimension.
-
ShapeMismatchErrorWhen the Array does not have one value per row.
-
DuplicateColumnError -
TimeOverflowErrorWhen a column holds a datetime outside 1677 to 2262, or a duration beyond about 292 years.
Examples:
Calculation¶
aggregate(aggregation)
Source¶
Return statistics of the whole Table, or of each group of rows.
Added in version 0.1.0
Changed in version 0.2.0
Gives the number of rows of each group with the row_count of the aggregation.
Parameters:
-
aggregation(Aggregation)The calculations, the columns forming the groups, and the name of the row count column.
Returns:
-
SelfA new Table: the group columns, then the
row_countcolumn, then one column per calculation; one row per group, sorted by group values with missing values as a group of their own, last. Without groups, a single row. A statistic needing a value gives a missing value for a group without present value.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen a calculation or a group names a column the Table does not have.
-
IncompatibleTypesErrorWhen a statistic does not apply to the type of its column.
Examples:
import numdaplot as ndp
cities = ndp.Array.from_list(["Pau", "Lyon", "Pau"])
prices = ndp.Array.from_list([10.0, 30.0, 20.0])
table = ndp.Table.from_arrays({"city": cities, "price": prices})
average_price = ndp.Calculation(column="price", function=ndp.Mean())
table.aggregate(ndp.Aggregation(calculations=[average_price], group_by=["city"]))
# city price_mean
# TEXT FLOAT64
# 0 Lyon 30.0
# 1 Pau 15.0
pivot_table(pivot)
Source¶
Return one statistic for each combination of groups of rows and values of a column, as a wide Table.
aggregate gives one row per combination: pivot_table gives one column per value of a column, as
Plot.heatmap draws them.
Added in version 0.1.0
Parameters:
-
pivot(PivotTable)The columns forming the rows and the columns of the result, and the statistic of each cell.
Returns:
-
SelfA new Table: the columns of
rows, then one column per value ofcolumns, named after the value; one row per group, sorted, with missing values as a group of their own, last.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen a setting names a column the Table does not have.
-
RefusedOperationErrorWhen the column of
columnsholds missing values: remove them withdrop_missing. -
DuplicateColumnErrorWhen a value written as a text is also the name of a column of
rows. -
InvalidColumnNameErrorWhen a value written as a text is empty.
-
IncompatibleTypesErrorWhen the statistic does not apply to the type of its column.
Examples:
import numdaplot as ndp
hours = ndp.Array.from_list([8, 8, 9])
weekdays = ndp.Array.from_list(["Mon", "Tue", "Mon"])
fares = ndp.Array.from_list([7.0, 9.0, 5.0])
trips = ndp.Table.from_arrays({"hour": hours, "weekday": weekdays, "fare": fares})
trip_count = ndp.Calculation(column="fare", function=ndp.Count(missing=ndp.CountedMissing.INCLUDED))
trips.pivot_table(ndp.PivotTable(rows=["hour"], columns="weekday", calculation=trip_count))
# hour Mon Tue
# INT64 INT64 INT64
# 0 8 1 1
# 1 9 1 0
describe(description=None)
Source¶
Return statistics describing columns, one row per statistic and one column per described column.
Added in version 0.1.0
Parameters:
-
description(Description | None, default:None)The statistics and the columns. When omitted, every number column is described with the statistics of pandas.
Returns:
-
SelfA new Table whose first column,
statistic, names each statistic, such asquantile_25. A cell is missing when the statistic does not apply to the column or finds no value.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen a described column does not exist.
-
DuplicateColumnErrorWhen a described column is named
statistic.
Examples:
count_values(value_count)
Source¶
Return how many rows hold each value of a column, as a number or as a fraction of every row.
With weights, each row counts as much as its weight, such as the people of a population a survey answer
stands for.
Added in version 0.2.0
Parameters:
-
value_count(ValueCount)The counted column, the measure, a number of rows by default, and the column of weights.
Returns:
-
SelfA new Table: the counted column holding each value once, then
value_count, orvalue_ratioforMeasure.RATIO, or with weightsweighted_countorweighted_ratio. Rows go from the most frequent value to the least frequent, values with the same count sorted by value. The missing value has a row of its own, last, so the ratios add up to 1.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen the counted column or the column of weights does not exist.
-
DuplicateColumnErrorWhen the counted column is named like the measure column, such as
value_count. -
IncompatibleTypesErrorWhen the column of weights does not hold numbers.
-
InvalidWeightErrorWhen a weight is negative.
Examples:
import numdaplot as ndp
answers = ndp.Table.from_arrays({"sex": ndp.Array.from_list([2.0, 1.0, 2.0, None, 2.0, 1.0])})
answers.count_values(ndp.ValueCount(column="sex"))
# sex value_count
# FLOAT64 INT64
# 0 2.0 3
# 1 1.0 2
# 2 NaN 1
answers.count_values(ndp.ValueCount(column="sex", measure=ndp.Measure.RATIO))
# sex value_ratio
# FLOAT64 FLOAT64
# 0 2.0 0.500000
# 1 1.0 0.333333
# 2 NaN 0.166667
Layout¶
sort(sorting)
Source¶
Return the rows sorted by one or several columns.
Added in version 0.1.0
Parameters:
-
sorting(TableSorting)The sort keys, each with its order and the place of its missing values.
Returns:
-
SelfA new Table, rows numbered from 0. Rows equal on every key keep their order.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen a key names a column the Table does not have.
Examples:
drop_duplicates(deduplication=None)
Source¶
Return the Table without its duplicated rows.
Added in version 0.1.0
Parameters:
-
deduplication(Deduplication | None, default:None)The compared columns, every column when omitted, and the row kept in each group. Two missing values are equal.
Returns:
-
SelfA new Table, remaining rows in their order, numbered from 0.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen a compared column does not exist.
Examples:
Join¶
join(join)
Source¶
Return the rows of this Table and of another Table put side by side where their keys match.
Added in version 0.1.0
Parameters:
-
join(Join)The other Table, the key columns, the rows kept, how the columns both Tables have are told apart, and how many times each key may appear.
Returns:
-
SelfA new Table: the keys, then the other columns of this Table, then those of the other Table. The rows of this Table come first, in their order, each once per match, then the unmatched rows of the other Table kept by the mode. Missing values fill the columns of a Table that has no matching row, each column keeping its type.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen a key is missing from one of the Tables.
-
IncompatibleTypesError -
TimeZoneMismatchErrorWhen the two columns of a key hold datetimes of different time zones, or with and without one: change them first with
TimeZoneChange. -
JoinRelationErrorWhen a key is repeated in a Table where the
relationof the join expects it once. -
DuplicateColumnErrorWhen both Tables have a column besides the keys and the join has no
suffixes, or a suffixed name is already the name of another column.
Examples:
import numdaplot as ndp
accidents = ndp.Table.from_arrays(
{"Num_Acc": ndp.Array.from_list([101, 102]), "dep": ndp.Array.from_list(["75", "69"])}
)
users = ndp.Table.from_arrays(
{"Num_Acc": ndp.Array.from_list([101, 101, 103]), "grav": ndp.Array.from_list([1, 3, 2])}
)
accidents.join(ndp.Join(other=users, keys=["Num_Acc"], mode=ndp.JoinMode.LEFT))
# Num_Acc dep grav
# INT64 TEXT INT64
# 0 101 75 1
# 1 101 75 3
# 2 102 69 <NA>
Missing values¶
count_missing(missing_count=None, axis=None)
Source¶
Return the number or the ratio of missing values, for the whole Table, each row or each column.
Added in version 0.1.0
Parameters:
-
missing_count(MissingCount | None, default:None)The measure, the number of missing values when omitted.
-
axis(Axis | None, default:None)One count per row with
Axis.EACH_ROW, one per column withAxis.EACH_COLUMN, a single count for the whole Table when omitted.
Returns:
-
Self
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
Examples:
import numdaplot as ndp
cities = ndp.Array.from_list(["Paris", "Lyon", "Pau", "Nice"])
prices = ndp.Array.from_list([120.0, None, None, 90.0])
table = ndp.Table.from_arrays({"city": cities, "price": prices})
table.count_missing().to_value()
# 2
table.count_missing(ndp.MissingCount(measure=ndp.Measure.RATIO), axis=ndp.Axis.EACH_COLUMN)
# column missing_ratio
# TEXT FLOAT64
# 0 city 0.0
# 1 price 0.5
preview_missing()
Source¶
Show the number and the percentage of missing values of each column, then of the whole Table.
In a Jupyter notebook the summary is shown as an HTML table, elsewhere as text. Nothing is returned:
use count_missing to compute with these numbers.
Added in version 0.1.0
Examples:
import numdaplot as ndp
cities = ndp.Array.from_list(["Paris", "Lyon", "Pau"])
prices = ndp.Array.from_list([120.0, None, 150.0])
table = ndp.Table.from_arrays({"city": cities, "price": prices})
table.preview_missing()
# column type missing_count missing_ratio
# 0 city TEXT 0 0.0 %
# 1 price FLOAT64 1 33.3 %
# 2 (all columns) 1 16.7 %
drop_missing(missing_drop=None, axis=Axis.EACH_ROW)
Source¶
Return the Table without its rows, or its columns, holding missing values.
Added in version 0.1.0
Parameters:
-
missing_drop(MissingDrop | None, default:None)The columns looked at and how many missing values are needed: a single one in any column when omitted.
-
axis(Axis, default:Axis.EACH_ROW)Axis.EACH_ROWby default to remove rows,Axis.EACH_COLUMNto remove columns.
Returns:
-
SelfA new Table, remaining rows in their order and numbered from 0, or remaining columns in their order.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
-
ColumnNotFoundErrorWhen a looked at column does not exist.
Examples:
Display¶
preview(preview=None)
Source¶
Show a few rows, with their row numbers and the type of each column under its name.
In a Jupyter notebook the rows are shown as an HTML table, elsewhere as text. Every column is shown unless the settings ask for a compact display. Nothing is returned.
Added in version 0.1.0
Parameters:
-
preview(Preview | None, default:None)How many rows, which ones, and how the columns are shown: the first 5, with every column, when omitted.
Raises:
-
InvalidTypeErrorWhen an argument is not of the type its annotation names.
Examples:
__repr__()
Source¶
Return the text of the Table, with the type of each column under its name.
A long Table only shows its first and last rows, as in pandas. Every column is shown, going on after a line break, ended by a backslash, when they do not fit in the width of the terminal.
Added in version 0.1.0
Returns:
-
strThe text shown by
print(table).
Examples:
Refused operations¶
__eq__(other)
Source¶
Refuse table == other; use is_equal.
Added in version 0.1.0
Parameters:
-
other(object)Any value.
Raises:
-
RefusedOperationErrorAlways.
__ne__(other)
Source¶
Refuse table != other; use is_equal.
Added in version 0.1.0
Parameters:
-
other(object)Any value.
Raises:
-
RefusedOperationErrorAlways.
__lt__(other)
Source¶
Refuse table < other; use filter.
Added in version 0.1.0
Parameters:
-
other(object)Any value.
Raises:
-
RefusedOperationErrorAlways.
Examples:
__le__(other)
Source¶
Refuse table <= other; use filter.
Added in version 0.1.0
Parameters:
-
other(object)Any value.
Raises:
-
RefusedOperationErrorAlways.
__gt__(other)
Source¶
Refuse table > other; use filter.
Added in version 0.1.0
Parameters:
-
other(object)Any value.
Raises:
-
RefusedOperationErrorAlways.
__ge__(other)
Source¶
Refuse table >= other; use filter.
Added in version 0.1.0
Parameters:
-
other(object)Any value.
Raises:
-
RefusedOperationErrorAlways.
__getitem__(key)
Source¶
Refuse table[...]; use column, take or filter.
Added in version 0.1.0
Parameters:
-
key(object)Any value.
Raises:
-
RefusedOperationErrorAlways.
__setitem__(key, value)
Source¶
Refuse table[...] = value; use add_column, which returns a new Table.
Added in version 0.1.0
Parameters:
-
key(object)Any value.
-
value(object)Any value.
Raises:
-
RefusedOperationErrorAlways.
__iter__()
Source¶
Refuse for row in table; iterate over the values of a column instead.
Added in version 0.1.0
Raises:
-
RefusedOperationErrorAlways.
Examples:
__contains__(value)
Source¶
Refuse value in table; use filter with ndp.Equal or ndp.IsIn, or column_names.
Added in version 0.1.0
Parameters:
-
value(object)Any value.
Raises:
-
RefusedOperationErrorAlways.
__len__()
Source¶
Refuse len(table); read the row_count attribute instead.
Added in version 0.1.0
Raises:
-
RefusedOperationErrorAlways.
__bool__()
Source¶
Refuse if table: and bool(table), whose meaning would be unclear.
Added in version 0.1.0
Raises:
-
RefusedOperationErrorAlways.