Skip to content

Import and export

Creating a Daf from other data and files, and converting it back out.

Python structures

from_lod

from_lod(
    records_lod,
    *,
    keyfield="",
    dtypes=None,
    name="",
    cols=None,
    ignore_extra_keys=False,
    fast=False,
)

Make a Daf from a list of dicts, one dict for each row.

Without cols or dtypes, the columns are all the keys of all the dicts, in the order in which they first appear. The keys of the first dict come first. A dict that lacks a key gets NULL there. A key that first appears in a later dict adds a column at the right, and the rows before it get NULL there. You do not need to look through the data first. Empty dicts and items that are not dicts are skipped.

If cols is given, those are the columns. Otherwise the keys of dtypes are the columns. You chose the columns, so a dict with a key that is not one of them raises ValueError, which names the keys, because that value would be lost. Pass ignore_extra_keys=True to leave such keys out on purpose, as when you pick a few columns of wide records.

With fast=True the first dict is checked fully, and every dict for its number of keys. Use it when you built every dict yourself, with the same keys in the same order, as in a loop. Each row is the values of a dict, in the dict's own order. The columns are cols, or the keys of dtypes, or the keys of the first dict. With cols or dtypes, the first dict must have the columns as its keys, in order, or it raises ValueError. A later dict with the wrong number of keys raises ValueError. A later dict with its keys in another order is added anyway, and its values land in the wrong columns.

Parameters:

Name Type Description Default
records_lod T_loda

The rows, as dicts.

required
keyfield str | int | T_ta | T_la

Column, or tuple or list of columns, whose values identify rows.

''
dtypes T_dtype_dict | None

Type for each column. When given, it also selects the columns.

None
name str

Name of the new Daf.

''
cols T_ls | None

Column names to use, in order.

None
ignore_extra_keys bool

With cols or dtypes, leave out keys that are not columns, and do not raise.

False
fast bool

If True, take each dict's values as a row. Check the first dict fully, and every dict's length. See above.

False

Returns:

Type Description
'Daf'

The new Daf.

Raises:

Type Description
ValueError

cols or dtypes is given, a dict has a key that is not one of those columns, and ignore_extra_keys is not True.

Examples:

Make a Daf from a list of dicts. A key a dict lacks gives NULL in that row:

>>> Daf.from_lod([{'name': 'Ana Lopez', 'age': 34}, {'name': 'Ben Okafor'}])

|    name    | age |
| ---------: | --: |
|  Ana Lopez |  34 |
| Ben Okafor |     |

%% daf rows=2; cols=2; keyfield=''; name=''

The columns are the keys in the order first seen:

>>> Daf.from_lod([{'name': 'Ana Lopez'}, {'name': 'Ben Okafor', 'age': 52}, {'city': 'Boston'}])

|    name    | age |  city  |
| ---------: | --: | -----: |
|  Ana Lopez |     |        |
| Ben Okafor |  52 |        |
|            |     | Boston |

%% daf rows=3; cols=3; keyfield=''; name=''

Give the columns and their order with cols. Leave out other keys with ignore_extra_keys=True:

>>> Daf.from_lod([{'name': 'Ana Lopez', 'age': 34}], cols=['age', 'name'])

| age |   name    |
| --: | --------: |
|  34 | Ana Lopez |

%% daf rows=1; cols=2; keyfield=''; name=''

>>> Daf.from_lod([{'name': 'Ana Lopez', 'age': 34, 'city': 'Denver'}], cols=['name', 'age'], ignore_extra_keys=True)

|   name    | age |
| --------: | --: |
| Ana Lopez |  34 |

%% daf rows=1; cols=2; keyfield=''; name=''

Give the keyfield and a name. The values stay as they are, so text stays text:

>>> stock = Daf.from_lod([{'id': 'a1', 'qty': '3'}, {'id': 'b2', 'qty': '10'}], keyfield='id', name='stock')
>>> stock.name, stock.select_record('b2')
('stock', {'id': 'b2', 'qty': '10'})

dtypes records the types, and selects the columns. It does not convert. Call apply_dtypes() for that:

>>> typed = Daf.from_lod([{'id': 'a1', 'qty': '3'}], dtypes={'id': str, 'qty': int})
>>> print(typed.to_md(show='quotes'))
|  id  | qty |
| ---: | --: |
| 'a1' | '3' |

>>> print(typed.apply_dtypes().to_md(show=['dtypes', 'quotes']))
| id: str | qty: int |
| ------: | -------: |
|    'a1' |        3 |

With fast=True, each dict's values become the row as they are. Every dict must have the keys in the same order. Only the first is checked fully:

>>> Daf.from_lod([{'id': 'a1', 'qty': 3}, {'id': 'b2', 'qty': 10}], fast=True).lol
[['a1', 3], ['b2', 10]]

to_daf

to_daf(name=None)

Give this Daf itself, so code can end the same way on a Daf or a SqlDaf.

SqlDaf.to_daf() reads the rows of a table into memory. A Daf is in memory already, so nothing is copied. That lets one line read a table of either kind, as in table.select_records(keys).to_daf().apply_dtypes().

This Daf is returned, not a copy. A change to the result changes this Daf. A SqlDaf gives rows of their own instead. Use copy('editable') for rows of your own.

name sets the name of this Daf, since the result is this Daf. It is here so that code that names the result of SqlDaf.to_daf() runs on a Daf too.

Parameters:

Name Type Description Default
name str | None

A new name for this Daf. If None, the name is left as it is.

None

Returns:

Type Description
'Daf'

This Daf.

Examples:

Create a small Daf of items in stock:

>>> daf = Daf(cols=['id', 'qty'], lol=[['a1', 3], ['b2', 10]], keyfield='id')
>>> daf

| id | qty |
| -: | --: |
| a1 |   3 |
| b2 |  10 |

%% daf rows=2; cols=2; keyfield='id'; name=''

to_daf() gives the Daf itself, so the same line reads a Daf or a SqlDaf:

>>> daf.select_records(['b2']).to_daf().lol
[['b2', 10]]
>>> daf.to_daf() is daf
True
>>> daf.to_daf(name='items').name
'items'

to_lod

to_lod()

Make a list of dicts, one dict for each row.

The dicts are new, but the values in them are the same objects as in the Daf. An empty Daf gives an empty list. See iter_dict() to go through rows one at a time without building the whole list.

Returns:

Type Description
T_loda

The rows as dicts.

Raises:

Type Description
KeysDisabledError

The Daf has rows and no column names.

Examples:

Create a small Daf of people, one with a list of hobbies in a cell:

>>> daf = Daf(cols=['id', 'tags'], lol=[['p1', 'none'], ['p2', ['chess']]])
>>> daf

| id |   tags    |
| -: | --------: |
| p1 |      none |
| p2 | ['chess'] |

%% daf rows=2; cols=2; keyfield=''; name=''

Make a list of dicts. A list in a cell is the same object in the dict and the Daf:

>>> lod = daf.to_lod()
>>> lod
[{'id': 'p1', 'tags': 'none'}, {'id': 'p2', 'tags': ['chess']}]
>>> lod[1]['tags'].append('hiking')
>>> daf

| id |        tags         |
| -: | ------------------: |
| p1 |                none |
| p2 | ['chess', 'hiking'] |

%% daf rows=2; cols=2; keyfield=''; name=''

from_lot

from_lot(
    records_lot,
    cols=None,
    dtypes=None,
    keyfield="",
    name="",
)

Make a Daf from a list of tuples, one tuple for each row.

Without cols the Daf has no column names, as with Daf(lol=...). Call set_cols() to give it spreadsheet names such as A and B, or to name them. A Daf with no names cannot give its rows as dicts or KeyedList objects.

Parameters:

Name Type Description Default
records_lot T_lota

The rows, as tuples.

required
cols T_ls | None

Column names. They must be as many as the items in each tuple.

None
dtypes T_dtype_dict | None

Type for each column.

None
keyfield str | int | T_ta | T_la

Column, or tuple or list of columns, whose values identify rows.

''
name str

Name of the new Daf.

''

Returns:

Type Description
'Daf'

The new Daf.

Raises:

Type Description
ValueError

A tuple does not have as many items as there are columns.

Examples:

Make a Daf from a list of tuples, with the column names:

>>> Daf.from_lot([('p1', 'Ana Lopez'), ('p2', 'Ben Okafor')], cols=['id', 'name'])

| id |    name    |
| -: | ---------: |
| p1 |  Ana Lopez |
| p2 | Ben Okafor |

%% daf rows=2; cols=2; keyfield=''; name=''

With no names, the Daf has no columns until set_cols() gives them, as A, B and so on:

>>> Daf.from_lot([('p1', 'Ana Lopez')]).columns()
[]
>>> Daf.from_lot([('p1', 'Ana Lopez')]).set_cols()

| A  |     B     |
| -: | --------: |
| p1 | Ana Lopez |

%% daf rows=1; cols=2; keyfield=''; name=''

Give the keyfield, the types and a name:

>>> stock = Daf.from_lot([('a1', 3), ('b2', 10)], cols=['id', 'qty'], keyfield='id', dtypes={'id': str, 'qty': int}, name='stock')
>>> stock.keyfield, stock.name, stock.select_record('b2')
('id', 'stock', {'id': 'b2', 'qty': 10})

from_dod

from_dod(
    dod,
    keyfield="rowkey",
    dtypes=None,
    ignore_extra_keys=False,
)

Make a Daf from a dict of dicts, where the outer key names the row.

A dict of dicts usually does not repeat the row key inside each row. A Daf table always has it as a column. If the inner dicts lack the keyfield column, it is added from the outer keys. The new Daf has that keyfield.

If dtypes is given, its keys are the columns, and it must name the keyfield column too. An inner dict with a key that dtypes does not name raises ValueError, which names the keys, because that value would be lost. Pass ignore_extra_keys=True to leave such keys out on purpose.

Use to_dod() to go back.

Parameters:

Name Type Description Default
dod T_doda

A dict that maps a row key to a dict of that row.

required
keyfield str

The column that holds the outer key.

'rowkey'
dtypes T_dtype_dict | None

Type for each column. When given, it also selects the columns.

None
ignore_extra_keys bool

With dtypes, leave out keys that it does not name, and do not raise.

False

Returns:

Type Description
'Daf'

The new Daf.

Raises:

Type Description
ValueError

dtypes is given, an inner dict has a key that it does not name, and ignore_extra_keys is not True.

KeyError

dtypes does not name the keyfield column.

Examples:

Make a Daf from a dict of dicts. The outer keys go into a column named rowkey:

>>> daf = Daf.from_dod({'p1': {'age': 34}, 'p2': {'age': 52}})
>>> daf

| rowkey | age |
| -----: | --: |
|     p1 |  34 |
|     p2 |  52 |

%% daf rows=2; cols=2; keyfield='rowkey'; name=''

Give the types, and leave out keys that have no type, with ignore_extra_keys=True:

>>> dod = {'p1': {'age': 34, 'zip': '80202'}, 'p2': {'age': 52, 'zip': '73301'}}
>>> Daf.from_dod(dod, dtypes={'rowkey': str, 'age': int}, ignore_extra_keys=True)

| rowkey | age |
| -----: | --: |
|     p1 |  34 |
|     p2 |  52 |

%% daf rows=2; cols=2; keyfield='rowkey'; name=''

The outer keys go into a column named by keyfield, which becomes the keyfield:

>>> stock = Daf.from_dod({'a1': {'qty': 3}, 'b2': {'qty': 10}}, keyfield='id')
>>> stock

| id | qty |
| -: | --: |
| a1 |   3 |
| b2 |  10 |

%% daf rows=2; cols=2; keyfield='id'; name=''

to_dod

to_dod(remove_keyfield=True)

Make a dict of dicts, where the keyfield value names each row.

The keyfield column is left out of the inner dicts by default. Pass remove_keyfield=False to keep it there too. The Daf must have a keyfield. Without one, a KeysDisabledError is raised, unless the Daf has no rows.

Parameters:

Name Type Description Default
remove_keyfield bool

If True, do not repeat the key inside each inner dict.

True

Returns:

Type Description
T_doda

A dict that maps each key to a dict of that row.

Raises:

Type Description
KeysDisabledError

The Daf has no keyfield.

Examples:

Create a small Daf of people, keyed by id:

>>> daf = Daf(cols=['id', 'name'], lol=[['p1', 'Ana Lopez']], keyfield='id')
>>> daf

| id |   name    |
| -: | --------: |
| p1 | Ana Lopez |

%% daf rows=1; cols=2; keyfield='id'; name=''

Make a dict of dicts under each key, without the key, and with it:

>>> daf.to_dod()
{'p1': {'name': 'Ana Lopez'}}
>>> daf.to_dod(remove_keyfield=False)
{'p1': {'id': 'p1', 'name': 'Ana Lopez'}}

from_cols_dol

from_cols_dol(cols_dol, keyfield='', dtypes=None)

Make a Daf from a dict of lists, where each list is a column.

All the lists must be as long as each other. If one differs in length from the first, a ValueError is raised, because the rows could not be made without losing values or guessing them.

Parameters:

Name Type Description Default
cols_dol T_dola

Maps a column name to the list of its values.

required
keyfield str

Column, or tuple or list of columns, whose values identify rows.

''
dtypes T_dtype_dict | None

Type for each column.

None

Returns:

Type Description
'Daf'

The new Daf.

Raises:

Type Description
ValueError

A list has a different length from the first.

Examples:

Make a Daf from a dict of columns, each a list:

>>> daf = Daf.from_cols_dol({'name': ['Ana Lopez', 'Ben Okafor', 'Chen Wu'], 'age': [34, 52, 27]})
>>> daf

|    name    | age |
| ---------: | --: |
|  Ana Lopez |  34 |
| Ben Okafor |  52 |
|    Chen Wu |  27 |

%% daf rows=3; cols=2; keyfield=''; name=''

Give the keyfield and the types:

>>> stock = Daf.from_cols_dol({'id': ['a1', 'b2'], 'qty': [3, 10]}, keyfield='id', dtypes={'id': str, 'qty': int})
>>> stock.select_record('b2'), stock.dtypes
({'id': 'b2', 'qty': 10}, {'id': <class 'str'>, 'qty': <class 'int'>})

to_cols_dol

to_cols_dol()

Make a dict of lists, one list of values for each column.

This turns the table on its side: a list for each column, in place of a list for each row. Use it to hand the data to code that wants columns, such as a plotting library. Each list is new, so changing it does not change the Daf. A dict or a list in a cell is the same object in both.

Returns:

Type Description
dict

A dict that maps each column name to the list of its values.

Examples:

Make a dict of columns, each a list:

>>> Daf(cols=['name', 'age'], lol=[['Ana Lopez', 34], ['Ben Okafor', 52]]).to_cols_dol()
{'name': ['Ana Lopez', 'Ben Okafor'], 'age': [34, 52]}

from_lod_to_cols

from_lod_to_cols(lod, cols=None, keyfield='', dtypes=None)

Make a Daf in which each dict becomes a column, not a row.

The keys of the dicts become the first column, and each dict adds one more column after it. Use this to compare several results side by side, such as the same features measured in several tries. It is a transpose of from_lod().

Without cols, the first column is named key and the others A, B and so on. The names in cols include the first column. The keyfield is not set unless keyfield is given.

Parameters:

Name Type Description Default
lod T_loda

The dicts. They should all have the same keys.

required
cols List | None

Column names, starting with the name of the column of keys.

None
keyfield str

Column whose values identify rows.

''
dtypes T_dtype_dict | None

Type for each column of the dicts, before the change. It must name every key of the dicts.

None

Returns:

Type Description
'Daf'

The new Daf.

Raises:

Type Description
ValueError

dtypes is given and a dict has a key that it does not name. The keys would be matched to the wrong values. The message mentions ignore_extra_keys, which this method does not take. Name all the keys.

Examples:

Make a Daf from a list of dicts, with each dict as a column. The first column holds the keys:

>>> daf = Daf.from_lod_to_cols([{'visits': 4, 'orders': 2}, {'visits': 7, 'orders': 3}], cols=['measure', 'week1', 'week2'])
>>> daf

| measure | week1 | week2 |
| ------: | ----: | ----: |
|  visits |     4 |     7 |
|  orders |     2 |     3 |

%% daf rows=2; cols=3; keyfield=''; name=''

Name the columns, and make the column of keys the keyfield:

>>> rows = Daf.from_lod_to_cols([{'age': 34, 'visits': 4}, {'age': 52, 'visits': 7}], cols=['field', 'p1', 'p2'], keyfield='field')
>>> rows

| field  | p1 | p2 |
| -----: | -: | -: |
|    age | 34 | 52 |
| visits |  4 |  7 |

%% daf rows=2; cols=3; keyfield='field'; name=''

>>> rows.select_record('visits')
{'field': 'visits', 'p1': 4, 'p2': 7}

to_attrib_dict

to_attrib_dict()

Make a dict with the column names and the rows.

This is deprecated. It keeps the rows as they are, not copied. Use to_lod() or to_cols_dol() instead.

Returns:

Type Description
dict

A dict with the keys cols and lol.

Examples:

Make a dict of the Daf's attributes, such as its name and keyfield, for saving it:

>>> Daf(cols=['id', 'name'], lol=[['p1', 'Ana Lopez']]).to_attrib_dict()
{'cols': ['id', 'name'], 'lol': [['p1', 'Ana Lopez']]}

Values and lists

to_value

to_value(default=_MISSING, astype=None)

Get the one value of a Daf that has one row and one column.

Use it on the result of a selection, as in my_daf[1, 'n'].to_value().

Parameters:

Name Type Description Default
default Any

Returned if the Daf is not one cell. Without it, an error is raised.

_MISSING
astype Callable | str | None

A type or function to convert the value with.

None

Returns:

Type Description
Any

The value.

Raises:

Type Description
ValueError

The Daf is not one cell and no default is given.

Examples:

Create a small Daf of people:

>>> daf = Daf(cols=['id', 'city', 'age'], lol=[[1, 'Denver', 34], [2, 'Austin', 52], [3, 'Boston', 27]], keyfield='id')
>>> daf

| id |  city  | age |
| -: | -----: | --: |
|  1 | Denver |  34 |
|  2 | Austin |  52 |
|  3 | Boston |  27 |

%% daf rows=3; cols=3; keyfield='id'; name=''

Select one cell, and read it as a value:

>>> daf[1, 'age'].to_value()
52

Convert the value with astype, here from text:

>>> Daf(cols=['qty'], lol=[['3']]).to_value(astype=int)
3

A Daf that is not one cell raises, unless default is given. The default may be None:

>>> print(Daf(cols=['qty'], lol=[[1], [2]]).to_value(default=None))
None

to_list

to_list(
    unique=False,
    flatten=False,
    omit_nulls=False,
    default=_MISSING,
    astype=None,
)

Get the values of a Daf that has one row or one column, as a list.

Use it on the result of a selection, as in my_daf[:, 'v'].to_list(). A column is read from the Daf, so use col() if you only need the list. An empty Daf gives an empty list. A Daf with more than one row and more than one column is not accepted.

Parameters:

Name Type Description Default
unique bool

If True, leave out repeated values and keep the order.

False
flatten bool

If True, join items that are lists into one list.

False
omit_nulls bool

If True, leave out the empty values.

False
default Any

Replaces NULL, None and NaN values. This may itself be None.

_MISSING
astype Callable | str | type | None

A type or function to convert each value with.

None

Returns:

Type Description
list

The list.

Raises:

Type Description
ValueError

The Daf has more than one row and more than one column.

Examples:

Create a small Daf of people:

>>> daf = Daf(cols=['id', 'city', 'age'], lol=[[1, 'Denver', 34], [2, 'Austin', 52], [3, 'Boston', 27]], keyfield='id')
>>> daf

| id |  city  | age |
| -: | -----: | --: |
|  1 | Denver |  34 |
|  2 | Austin |  52 |
|  3 | Boston |  27 |

%% daf rows=3; cols=3; keyfield='id'; name=''

Read one column, or one row, as a list:

>>> daf[:, 'city'].to_list()
['Denver', 'Austin', 'Boston']
>>> daf[1].to_list()
[2, 'Austin', 52]

The options, on a column whose cells hold lists, with one empty cell:

>>> tags = Daf(cols=['tags'], lol=[[['chess', 'hiking']], [''], [['hiking', 'jazz']]])
>>> tags.to_list()
[['chess', 'hiking'], '', ['hiking', 'jazz']]
>>> tags.to_list(flatten=True)
['chess', 'hiking', '', 'hiking', 'jazz']
>>> tags.to_list(flatten=True, unique=True)
['chess', 'hiking', '', 'jazz']
>>> tags.to_list(omit_nulls=True)
[['chess', 'hiking'], ['hiking', 'jazz']]
>>> tags.to_list(default=None)
[['chess', 'hiking'], None, ['hiking', 'jazz']]

Convert each value with astype:

>>> Daf(cols=['age'], lol=[['34'], ['52']]).to_list(astype=int)
[34, 52]

to_lota

to_lota()

Make a list of tuples, one tuple for each row.

This is handy for making composite keys. The tuples are new, but the values in them are the same objects as in the Daf.

Returns:

Type Description
T_lota

The rows as tuples.

Examples:

Make a list of tuples, one for each row:

>>> Daf(cols=['id', 'name'], lol=[['p1', 'Ana Lopez'], ['p2', 'Ben Okafor']]).to_lota()
[('p1', 'Ana Lopez'), ('p2', 'Ben Okafor')]

to_dict

to_dict()

Get the one row of a Daf as a dict.

Use it on the result of a selection, as in my_daf[1].to_dict(). A column is not turned into a dict. Use to_list() for that. An empty Daf gives an empty dict. A Daf with no column names raises KeysDisabledError.

Returns:

Type Description
T_da

The row, as a dict that maps column names to values.

Raises:

Type Description
KeysDisabledError

The Daf has a row and no column names.

ValueError

The Daf has more than one row.

Examples:

Create a small Daf of people:

>>> daf = Daf(cols=['id', 'city', 'age'], lol=[[1, 'Denver', 34], [2, 'Austin', 52], [3, 'Boston', 27]], keyfield='id')
>>> daf

| id |  city  | age |
| -: | -----: | --: |
|  1 | Denver |  34 |
|  2 | Austin |  52 |
|  3 | Boston |  27 |

%% daf rows=3; cols=3; keyfield='id'; name=''

Select one row, and read it as a dict:

>>> daf[1].to_dict()
{'id': 2, 'city': 'Austin', 'age': 52}

to_klist

to_klist(irow=0)

Get one row as a KeyedList.

The KeyedList shares the row and the column names with the Daf, so it costs little. A negative position counts from the end. A position that is out of range raises IndexError. A Daf with no rows gives an empty KeyedList.

Parameters:

Name Type Description Default
irow int

The row position.

0

Returns:

Type Description
KeyedList

The row.

Raises:

Type Description
IndexError

The position is out of range, for a Daf that has rows.

Examples:

Create a small Daf of people:

>>> daf = Daf(cols=['id', 'city', 'age'], lol=[[1, 'Denver', 34], [2, 'Austin', 52], [3, 'Boston', 27]], keyfield='id')
>>> daf

| id |  city  | age |
| -: | -----: | --: |
|  1 | Denver |  34 |
|  2 | Austin |  52 |
|  3 | Boston |  27 |

%% daf rows=3; cols=3; keyfield='id'; name=''

Read the row at position 1 as a KeyedList, and one value from it by name:

>>> daf.to_klist(1)['city']
'Austin'

CSV, Excel and OpenDocument

from_csv

from_csv(source, **kwargs)

Read a CSV file, a web address or an S3 object into a Daf.

The source may be a path, a Path, an http or https address, or an s3://bucket/key name. The requests and boto3 packages are imported only when they are needed.

Every value is read as text. Call apply_dtypes() on the result to convert them. Giving dtypes here also converts them, but that is deprecated. The other keyword arguments, such as keyfield, are those of from_csv_buff(). The length of each row is not checked. See from_csv_buff().

Parameters:

Name Type Description Default
source str | Path

A file path, an http address or an s3:// name.

required
**kwargs Any

Passed on to from_csv_buff().

{}

Returns:

Type Description
'Daf'

The new Daf.

Raises:

Type Description
RuntimeError

The download fails, a needed package is missing, or the local file cannot be read or parsed. The message says which.

Examples:

Write a CSV file of people, and read it. Every cell is text:

>>> import os, tempfile
>>> path = os.path.join(tempfile.mkdtemp(), 'people.csv')
>>> with open(path, 'w') as f:
...     n = f.write('id,name,age\np1,Ana Lopez,34\n')
>>> Daf.from_csv(path)

| id |   name    | age |
| -: | --------: | --: |
| p1 | Ana Lopez |  34 |

%% daf rows=1; cols=3; keyfield=''; name=''

from_csv_buff

from_csv_buff(
    csv_buff,
    keyfield="",
    dtypes=None,
    noheader=False,
    user_format=False,
    sep=",",
    unflatten=True,
    include_cols=None,
    name="",
)

Make a Daf from CSV text, bytes or an iterator of lines.

The first row holds the column names, unless noheader is True. Quoted fields may hold commas. Empty rows at the end are dropped. Cells are text, unless dtypes is given. Then the cells are converted, and list and dict columns are read from their text unless unflatten is False.

Converting here is deprecated. It does no more than calling apply_dtypes() on the result, which is clearer: a reader only reads, and the types are applied where they are needed. Pass no dtypes, and call apply_dtypes() afterwards. The conversion still works, for now.

Empty text, or a source with no rows, gives an empty Daf with no columns, as from_md() does. Check len() of the result if an empty source would be an error.

A keyfield that is not a column of the file raises KeyError.

The length of each row is not checked, so that a read costs no more than it must. A row with a missing cell, or with an extra one from an unquoted comma, is kept as it is, and a later call may fail with an error that does not mention it. If you do not trust the source, call is_rectangular(). It looks at every row. To pad short rows, call force_rectangular(). It raises ValueError for a row that is too long.

Parameters:

Name Type Description Default
csv_buff bytes | str | Iterator[str]

The CSV, as text, bytes or an iterator of lines.

required
keyfield str

Column, or tuple or list of columns, whose values identify rows.

''
dtypes T_dtype_dict | None

Type for each column. Deprecated: call apply_dtypes() on the result instead.

None
noheader bool

If True, the first row is data, and the Daf has no column names.

False
user_format bool

If True, skip comment lines and blank lines.

False
sep str

The character that separates fields.

','
unflatten bool

If True, read list and dict columns from their text.

True
include_cols T_ls | None

Keep only these columns, in this order. The cells of the other columns are never kept, so a wide file costs little memory.

None
name str

Name of the new Daf.

''

Returns:

Type Description
'Daf'

The new Daf.

Raises:

Type Description
KeyError

A name in include_cols, or the keyfield, is not in the header of the file.

ValueError

include_cols is given with noheader=True, because the names of the columns come from the header.

Examples:

Read CSV text. A quoted cell may hold a comma:

>>> daf = Daf.from_csv_buff('id,name\np1,Ana Lopez\np2,"Okafor, Ben"\n')
>>> daf

| id |    name     |
| -: | ----------: |
| p1 |   Ana Lopez |
| p2 | Okafor, Ben |

%% daf rows=2; cols=2; keyfield=''; name=''

The cells are text. Convert them with apply_dtypes():

>>> Daf.from_csv_buff('id,age\n1,34\n').apply_dtypes(dtypes={'id': int, 'age': int})

| id | age |
| -: | --: |
|  1 |  34 |

%% daf rows=1; cols=2; keyfield=''; name=''

Keep only some columns, in a new order:

>>> daf = Daf.from_csv_buff('id,name,age\np1,Ana Lopez,34\np2,Ben Okafor,52\n', include_cols=['age', 'id'])
>>> daf

| age | id |
| --: | -: |
|  34 | p1 |
|  52 | p2 |

%% daf rows=2; cols=2; keyfield=''; name=''

A short line makes a short row, so the Daf is ragged until it is made rectangular:

>>> daf = Daf.from_csv_buff('id,name\np1,Ana Lopez\np2\n')
>>> daf

| id |   name    |
| -: | --------: |
| p1 | Ana Lopez |
| p2 |           |

%% daf rows=2; cols=2; keyfield=''; name=''; ragged rows detected

>>> daf.is_rectangular()
False
>>> daf.force_rectangular()

| id |   name    |
| -: | --------: |
| p1 | Ana Lopez |
| p2 |           |

%% daf rows=2; cols=2; keyfield=''; name=''

With user_format=True, comment lines, which start with #, and blank lines are skipped:

>>> Daf.from_csv_buff('# stock count\nid,qty\n\na1,3\nb2,10\n', user_format=True).lol
[['a1', '3'], ['b2', '10']]

Another separator, and no header line:

>>> Daf.from_csv_buff('a1;3\nb2;10\n', sep=';', noheader=True).lol
[['a1', '3'], ['b2', '10']]

Keep only some columns, in a new order, with a keyfield and a name:

>>> picked = Daf.from_csv_buff('id,qty,note\na1,3,x\n', include_cols=['qty', 'id'], keyfield='id', name='picked')
>>> picked

| qty | id |
| --: | -: |
|   3 | a1 |

%% daf rows=1; cols=2; keyfield='id'; name='picked'

A list column is read from its text, for its dtype. With unflatten=False it stays text:

>>> print(Daf.from_csv_buff('id,tags\na1,"[1, 2]"\n', dtypes={'id': str, 'tags': list}).to_md(show=['dtypes', 'quotes']))
| id: str | tags: list |
| ------: | ---------: |
|    'a1' |     [1, 2] |

>>> print(Daf.from_csv_buff('id,tags\na1,"[1, 2]"\n', dtypes={'id': str, 'tags': list}, unflatten=False).to_md(show=['dtypes', 'quotes']))
| id: str | tags: list (str) |
| ------: | ---------------: |
|    'a1' |         '[1, 2]' |

from_csv_file

from_csv_file(
    filepath,
    keyfield="",
    dtypes=None,
    noheader=False,
    user_format=False,
    sep=",",
    unflatten=True,
    include_cols=None,
    name="",
)

Read a CSV file into a Daf. Deprecated, use from_csv().

It calls from_csv(), so it reads the file as UTF-8, and a file that cannot be read raises RuntimeError.

Parameters:

Name Type Description Default
filepath str

Path of the file.

required
keyfield str

Column, or tuple or list of columns, whose values identify rows.

''
dtypes T_dtype_dict | None

Type for each column. Deprecated: call apply_dtypes() on the result instead.

None
noheader bool

If True, the first row is data, and the Daf has no column names.

False
user_format bool

If True, skip comment lines and blank lines.

False
sep str

The character that separates fields.

','
unflatten bool

If True, read list and dict columns from their text.

True
include_cols T_ls | None

Keep only these columns, in this order. The cells of the other columns are never kept, so a wide file costs little memory.

None
name str

Name of the new Daf.

''

Returns:

Type Description
'Daf'

The new Daf.

Raises:

Type Description
RuntimeError

The file cannot be read or parsed.

Examples:

Write a CSV file of people, and read it:

>>> import os, tempfile
>>> path = os.path.join(tempfile.mkdtemp(), 'people.csv')
>>> with open(path, 'w') as f:
...     n = f.write('id,name,age\np1,Ana Lopez,34\n')
>>> Daf.from_csv_file(path)

| id |   name    | age |
| -: | --------: | --: |
| p1 | Ana Lopez |  34 |

%% daf rows=1; cols=3; keyfield=''; name=''

The options are those of from_csv_buff(), which has an example of each. Here a file with a comment line, read with a keyfield, one column and a name:

>>> import os, tempfile
>>> path = os.path.join(tempfile.mkdtemp(), 'stock.csv')
>>> with open(path, 'w') as csv_file:
...     n = csv_file.write('# stock count\nid;qty;note\na1;3;x\nb2;10;y\n')
>>> Daf.from_csv_file(path, sep=';', user_format=True, keyfield='id', include_cols=['id', 'qty'], name='stock')

| id | qty |
| -: | --: |
| a1 |   3 |
| b2 |  10 |

%% daf rows=2; cols=2; keyfield='id'; name='stock'

to_csv_file

to_csv_file(
    file_path="", line_terminator=None, include_header=True
)

Write the Daf to a CSV file.

The file has a header row of column names unless include_header is False. Each cell is written as text, so lists and dicts are written in their str() form. A NULL cell is written as nothing. The default line ending is \r\n.

Parameters:

Name Type Description Default
file_path str | Path

Where to write the file.

''
line_terminator str | None

The line ending. If None, \r\n is used.

None
include_header bool

If True, write the column names first.

True

Returns:

Type Description
str

The path that was written.

Examples:

Write a small Daf of people to a CSV file:

>>> import os, tempfile
>>> path = os.path.join(tempfile.mkdtemp(), 'people.csv')
>>> daf = Daf(cols=['id', 'name'], lol=[['p1', 'Ana Lopez']])
>>> daf

| id |   name    |
| -: | --------: |
| p1 | Ana Lopez |

%% daf rows=1; cols=2; keyfield=''; name=''

>>> daf.to_csv_file(path) == path
True

Without the header line, and with another line ending:

>>> import os, tempfile
>>> path = os.path.join(tempfile.mkdtemp(), 'stock.csv')
>>> stock = Daf(cols=['id', 'qty'], lol=[['a1', 3], ['b2', 10]])
>>> stock.to_csv_file(path, line_terminator='\n', include_header=False) == path
True
>>> open(path).read()
'a1,3\nb2,10\n'

to_csv_buff

to_csv_buff(line_terminator=None, include_header=True)

Make CSV text from the Daf.

The text can be saved to a file or uploaded. There is no need to call flatten() first. Each cell is written with str(), so a list or dict is written as its Python text, with single quotes. The bool True is written as True. A NULL cell is written as nothing. The text is not JSON.

A column converted by apply_dtypes() to bool holds 0 and 1, so it is written as 0 and 1. A column of Python bools is written as True and False. Both read back as 0 and 1 when the column has dtype bool. The cells are written as they are held, with no conversion, since any pass over them would slow every save.

A dict whose keys are not text, such as {1: 'a'}, is written with its keys as they are.

Parameters:

Name Type Description Default
line_terminator str | None

The line ending. If None, \r\n is used.

None
include_header bool

If True, write the column names first.

True

Returns:

Type Description
str

The CSV text.

Examples:

Make CSV text from a small Daf of people:

>>> daf = Daf(cols=['id', 'name'], lol=[['p1', 'Ana Lopez']])
>>> daf

| id |   name    |
| -: | --------: |
| p1 | Ana Lopez |

%% daf rows=1; cols=2; keyfield=''; name=''

>>> daf.to_csv_buff(line_terminator='\n')
'id,name\np1,Ana Lopez\n'

A dict in a cell is written as its text:

>>> Daf(cols=['prefs'], lol=[[{'news': 1}]]).to_csv_buff(line_terminator='\n')
"prefs\n{'news': 1}\n"

Without the header line, and with another line ending:

>>> stock = Daf(cols=['id', 'qty'], lol=[['a1', 3], ['b2', 10]])
>>> stock.to_csv_buff(include_header=False)
'a1,3\r\nb2,10\r\n'
>>> stock.to_csv_buff(line_terminator='\n')
'id,qty\na1,3\nb2,10\n'

buff_to_file

buff_to_file(buff, file_path, fmt='.csv')

Write text or bytes to a file.

This is a static method, so call it as Daf.buff_to_file(buff, path). It is the helper that to_csv_file() uses.

Parameters:

Name Type Description Default
buff T_buff

The text or bytes to write.

required
file_path str | Path

Where to write.

required
fmt str

Not used.

'.csv'

Returns:

Type Description
str

The path that was written.

Examples:

Write text to a file, and read it back:

>>> import os, tempfile
>>> path = os.path.join(tempfile.mkdtemp(), 'people.csv')
>>> Daf.buff_to_file('id,name\np1,Ana Lopez\n', path) == path
True
>>> open(path).read()
'id,name\np1,Ana Lopez\n'

from_excel_buff

from_excel_buff(
    excel_buff,
    keyfield="",
    dtypes=None,
    noheader=False,
    user_format=False,
    unflatten=True,
)

Make a Daf from the bytes of an xlsx file.

The first sheet is turned into CSV by the xlsx2csv package. The CSV is then read like any CSV text. All values start as text, so give dtypes or call apply_dtypes() to convert them.

Short rows are not padded here. xlsx2csv pads them. See xlsx_to_csv().

Parameters:

Name Type Description Default
excel_buff bytes

The bytes of the xlsx file.

required
keyfield str

Column whose values identify rows.

''
dtypes T_dtype_dict | None

Type for each column. Deprecated: call apply_dtypes() on the result instead.

None
noheader bool

If True, the first row is data, not column names.

False
user_format bool

If True, skip comment lines and blank lines.

False
unflatten bool

If True, read list and dict columns from their text.

True

Returns:

Type Description
'Daf'

The new Daf.

Examples:

A small Excel file, made here with xlsxwriter for the example:

>>> import io, xlsxwriter
>>> def excel_bytes(rows):
...     buff = io.BytesIO()
...     workbook = xlsxwriter.Workbook(buff, {'in_memory': True})
...     sheet = workbook.add_worksheet()
...     for irow, row in enumerate(rows):
...         sheet.write_row(irow, 0, row)
...     workbook.close()
...     return buff.getvalue()
>>> xlsx = excel_bytes([['id', 'name'], [1, 'Ana Lopez'], [2, 'Ben Okafor']])
>>> daf = Daf.from_excel_buff(xlsx, keyfield='id')
>>> daf

| id |    name    |
| -: | ---------: |
|  1 |  Ana Lopez |
|  2 | Ben Okafor |

%% daf rows=2; cols=2; keyfield='id'; name=''

>>> Daf.from_excel_buff(xlsx, dtypes={'id': int, 'name': str})

| id |    name    |
| -: | ---------: |
|  1 |  Ana Lopez |
|  2 | Ben Okafor |

%% daf rows=2; cols=2; keyfield=''; name=''

With noheader=True, the first row is data, and the Daf has no column names:

>>> Daf.from_excel_buff(excel_bytes([['a1', 3], ['b2', 10]]), noheader=True).lol
[['a1', '3'], ['b2', '10']]

With user_format=True, comment lines, which start with #, and blank lines are skipped:

>>> Daf.from_excel_buff(excel_bytes([['# stock count'], ['id', 'qty'], [], ['a1', 3]]), user_format=True).lol
[['a1', '3']]

A list or dict column is read from its text, for its dtype. With unflatten=False it stays text:

>>> tags = excel_bytes([['id', 'tags'], ['a1', '[1, 2]']])
>>> print(Daf.from_excel_buff(tags, dtypes={'id': str, 'tags': list}).to_md(show=['dtypes', 'quotes']))
| id: str | tags: list |
| ------: | ---------: |
|    'a1' |     [1, 2] |

>>> print(Daf.from_excel_buff(tags, dtypes={'id': str, 'tags': list}, unflatten=False).to_md(show=['dtypes', 'quotes']))
| id: str | tags: list (str) |
| ------: | ---------------: |
|    'a1' |         '[1, 2]' |

from_ods

from_ods(
    source,
    sheet=0,
    keyfield="",
    dtypes=None,
    noheader=False,
    unflatten=True,
    name="",
)

Make a Daf from one sheet of an OpenDocument spreadsheet, an .ods file.

Use it for a table kept in LibreOffice Calc. Only the first sheet is read, unless you give sheet. Only the content of the cells is read: styles, comments, links and formulas are left out. No package is needed, since the file is read with the standard library. See lib/daf_ods.py.

All values start as text, as from a CSV file, so give dtypes or call apply_dtypes() to convert them. A number, date or boolean cell gives its stored value, such as 168.5 or 2026-09-01, not the text shown, such as $168.50. A cell with several lines gives them joined with a newline.

Rows are padded with NULL to the width of the widest row. A blank row inside the table is kept as a row of NULL, so the rows line up with the spreadsheet. Drop such rows yourself if they must not be used. The blank rows at the end are left out.

Parameters:

Name Type Description Default
source str | Path | bytes

The path of the file, or its bytes.

required
sheet int | str

The position of the sheet, from 0, or its name.

0
keyfield str

Column whose values identify rows.

''
dtypes T_dtype_dict | None

Type for each column. Deprecated: call apply_dtypes() on the result instead.

None
noheader bool

If True, the first row is data, not column names.

False
unflatten bool

If True, read list and dict columns from their text.

True
name str

The name of the new Daf.

''

Returns:

Type Description
'Daf'

The new Daf.

Raises:

Type Description
IndexError

There is no sheet at that position.

KeyError

There is no sheet with that name.

Examples:

A small .ods file, made here for the example. A real one comes from a file or a download:

>>> import io, zipfile
>>> def ods_bytes(sheets):
...     ns = ('xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" '
...           'xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" '
...           'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"')
...     body = ''
...     for name, rows in sheets.items():
...         body += f'<table:table table:name="{name}">'
...         for row in rows:
...             cells = ''.join(f'<table:table-cell><text:p>{v}</text:p></table:table-cell>' for v in row)
...             body += f'<table:table-row>{cells}</table:table-row>'
...         body += '</table:table>'
...     content = (f'<office:document-content {ns}><office:body><office:spreadsheet>'
...                f'{body}</office:spreadsheet></office:body></office:document-content>')
...     buff = io.BytesIO()
...     with zipfile.ZipFile(buff, 'w') as odsfile:
...         odsfile.writestr('mimetype', 'application/vnd.oasis.opendocument.spreadsheet')
...         odsfile.writestr('content.xml', content)
...     return buff.getvalue()
>>> ods = ods_bytes({'stock': [['id', 'qty'], ['a1', '3'], ['b2', '10']], 'notes': [['a1', 'new']]})

The first sheet, with a keyfield and a name. The cells are read as text:

>>> stock = Daf.from_ods(ods, keyfield='id', name='stock')
>>> stock

| id | qty |
| -: | --: |
| a1 |   3 |
| b2 |  10 |

%% daf rows=2; cols=2; keyfield='id'; name='stock'

Another sheet, by name or by position, whose first row is data:

>>> Daf.from_ods(ods, sheet='notes', noheader=True).lol
[['a1', 'new']]
>>> Daf.from_ods(ods, sheet=1, noheader=True).lol
[['a1', 'new']]

JSON

to_json

to_json(concise=True)

Make JSON text that holds the whole Daf.

The text holds the rows, the column names, the dtypes, the keyfield, the name, the attrs and the display columns. With concise=True, the parts that are empty are left out. Use from_json() to read it back.

Types are written by name. Only int, float, str, bool, list and dict are read back as types. Other names, such as date, come back as text.

Cells must be JSON values. A tuple comes back as a list, and a set raises a TypeError. A NaN is written as NaN, which some JSON readers reject.

Parameters:

Name Type Description Default
concise bool

If True, leave out the parts that are empty.

True

Returns:

Type Description
str

The JSON text.

Examples:

Make JSON text of a small Daf, leaving out the parts that are empty:

>>> Daf(cols=['age'], lol=[[34]]).to_json()
'{"lol": [[34]], "hd": {"age": 0}}'

With concise=False, the empty parts are written too:

>>> Daf(cols=['age'], lol=[[34]]).to_json(concise=False)
'{"name": "", "lol": [[34]], "hd": {"age": 0}, "dtypes": {}, "keyfield": "", "attrs": {}, "disp_cols": []}'

from_json

from_json(json_str)

Make a Daf from JSON text made by to_json().

The name, the keyfield and the dtypes come back with the rows. The dtypes are stored by the name of each type, and turned back into types. A dict or a list in a cell comes back as a dict or a list, since JSON holds them as they are. Use it to send a Daf to another process, or to keep one in a text file.

Parameters:

Name Type Description Default
json_str str

The JSON text.

required

Returns:

Type Description
'Daf'

The new Daf.

Examples:

Create a small Daf of people:

>>> daf = Daf(cols=['id', 'name'], lol=[['p1', 'Ana Lopez']], keyfield='id')
>>> daf

| id |   name    |
| -: | --------: |
| p1 | Ana Lopez |

%% daf rows=1; cols=2; keyfield='id'; name=''

Write it as JSON, and read it back as an equal Daf:

>>> Daf.from_json(daf.to_json()) == daf
True

Markdown

from_md classmethod

from_md(md_str)

Make a Daf from a Markdown table. This reads what to_md() writes.

Only the first table in the text is read. Text before it, such as headings and prose, is skipped. A table row starts and ends with |. The table must have a header row and then a separator row such as | -: | -: |. The columns are the header cells. A table with no header row is not read, and raises RuntimeError. The table ends at the first line that is not a row, such as a blank line. A row with more or fewer cells than the header raises RuntimeError, which names the line, so that no row is lost without notice.

A Daf with rows and no column names is written by to_md() with the spreadsheet names A, B and so on in the header. Reading that text gives a Daf with those names, so the round trip adds column names. The text of a table cannot tell these names from names that you chose.

A footer line that starts with %% daf, or %% sdaf from a SqlDaf, and follows the table may give the keyfield and name, as to_md(include_summary=True) writes. Every cell is read as text. Convert them with apply_dtypes(). Text with no table, or empty text, gives an empty Daf.

Markdown is a report format, so a round trip through to_md() is not exact:

  • Text that to_md() shortened, rows and columns it left out, and numbers that smart_fmt rounded stay that way.
  • A newline in a cell is written as <br>, and is read back as <br>.

A | in a cell is written as \|, and is read back as |. A composite keyfield in the footer is read back as the tuple or list it was.

A header that to_md(show='dtypes') wrote, as age: int, gives the column name and its dtype. A note in parentheses after the dtype is not read. The header is split only if every other header cell has no : in it. The cells are not converted. A column with no dtype gets str. A summary line with show='quotes', as to_md(show='quotes', include_summary=True) writes, has the quotes taken off each quoted cell.

Parameters:

Name Type Description Default
md_str str

The Markdown text.

required

Returns:

Type Description
'Daf'

The new Daf.

Raises:

Type Description
RuntimeError

A table is found but it has no header row and separator row, or a row has a different number of cells than the header.

Examples:

Read a Markdown table, with its summary line, back into a Daf:

>>> from daffodil.daf import Daf
>>> text = "| id | name |\n| -: | -: |\n|  1 | Ana Lopez |\n|  2 | Ben Okafor |\n\n%% daf rows=2; cols=2; keyfield='id'; name='people'\n"
>>> daf = Daf.from_md(text)
>>> daf

| id |    name    |
| -: | ---------: |
|  1 |  Ana Lopez |
|  2 | Ben Okafor |

%% daf rows=2; cols=2; keyfield='id'; name='people'

A Daf with no column names is written with A, B and so on, which come back as its names:

>>> Daf.from_md(Daf(lol=[['p1', 'Ana Lopez']]).to_md()).columns()
['A', 'B']

Read the dtypes from the header. The cells stay text until apply_dtypes() converts them:

>>> typed = Daf.from_md("| id: str | age: int |\n| -: | -: |\n| p1 | 34 |\n")
>>> typed.dtypes
{'id': <class 'str'>, 'age': <class 'int'>}
>>> typed.lol
[['p1', '34']]
>>> print(typed.apply_dtypes().to_md(show='dtypes'))
| id: str | age: int |
| ------: | -------: |
|      p1 |       34 |

dodaf_to_md staticmethod

dodaf_to_md(dodaf, report_header='', **to_md_kwargs)

Make one Markdown report from a dict of Daf instances.

This is a static method. The report has an optional first level heading, then a second level section for each key, in the order of the dict. Each section holds that Daf as a table, from its own to_md(). A Daf with no rows still gets its section, with a line *(no rows)*, so you can see which sections were empty. Use dodaf_from_md() to read the report back.

Parameters:

Name Type Description Default
dodaf Dict[str, Any]

A dict that maps a section name to a Daf.

required
report_header str

The title of the report. Left out if empty.

''
**to_md_kwargs Any

Passed on to the to_md() of each Daf, such as max_rows.

{}

Returns:

Type Description
str

The Markdown text.

Examples:

Make one Markdown report of several Daf tables, each under its own heading:

>>> from daffodil.daf import Daf
>>> print(Daf.dodaf_to_md({'people': Daf(cols=['age'], lol=[[34]])}, report_header='Report'))
# Report


## people


| age |
| --: |
|  34 |

dodaf_from_md classmethod

dodaf_from_md(md_str, header_level=2)

Read a report made by dodaf_to_md() back into a dict of Daf instances.

A section starts at a line that begins with the marker of header_level, which is ## by default. Text before the first section, such as the report title, is skipped. Each section becomes a Daf, read with from_md(), so the cells are text. The section title is the dict key and the name of the Daf, unless the table's footer gives a name. The footer wins for the name, never for the key. Empty text gives an empty dict.

Parameters:

Name Type Description Default
md_str str

The Markdown text.

required
header_level int

The number of # that start a section heading.

2

Returns:

Type Description
Dict[str, Any]

A dict that maps each section title to a Daf.

Examples:

Make a report of one table, and read it back into a dict of Daf tables:

>>> from daffodil.daf import Daf
>>> report = Daf.dodaf_to_md({'people': Daf(cols=['age'], lol=[[34]])})
>>> back = Daf.dodaf_from_md(report)
>>> back['people']

| age |
| --: |
|  34 |

%% daf rows=1; cols=1; keyfield=''; name='people'

Pandas and NumPy

from_pandas_df classmethod

from_pandas_df(
    df, keyfield="", name="", use_csv=False, dtypes=None
)

Make a Daf from a Pandas DataFrame or Series.

The values become plain Python values. The column names come from the DataFrame. The index is not kept. The dtypes are worked out from the Pandas dtypes, but the values are not converted to them. A Series becomes one row, with the index labels as column names. Its dtypes are the Python types of its values, one for each label.

With use_csv=True the DataFrame is turned into CSV text first, and that is read back. This can be faster for some frames.

Parameters:

Name Type Description Default
df T_df

The DataFrame or Series.

required
keyfield str

Column, or tuple or list of columns, whose values identify rows.

''
name str

Name of the new Daf.

''
use_csv bool

If True, convert by way of CSV text.

False
dtypes T_dtype_dict | None

Deprecated, and it is not used. The dtypes come from df. It will be removed. A DeprecationWarning is given if it is passed.

None

Returns:

Type Description
Daf

The new Daf.

Examples:

Make a Daf from a pandas DataFrame, with a keyfield:

>>> import pandas as pd
>>> from daffodil.daf import Daf
>>> df = pd.DataFrame({'id': [1, 2], 'name': ['Ana Lopez', 'Ben Okafor']})
>>> daf = Daf.from_pandas_df(df, keyfield='id')
>>> daf

| id |    name    |
| -: | ---------: |
|  1 |  Ana Lopez |
|  2 | Ben Okafor |

%% daf rows=2; cols=2; keyfield='id'; name=''

to_pandas_df

to_pandas_df(
    cols=None,
    *,
    use_csv=False,
    use_donpa=False,
    default=_MISSING,
    defaulting_cols=None,
)

Make a Pandas DataFrame from the Daf.

Daffodil stores a missing value as an empty string. Pandas treats that as text, so a column with empty cells is of type object, even if the rest are numbers. Text that looks like a number stays text. Convert columns afterwards if you want other types.

Giving default replaces empty and None cells in the Daf itself, before the conversion. That changes the Daf you called it on. Copy it first if you need it unchanged. default cannot be used with use_csv. With use_donpa, the default replaces the cells in the arrays only. The Daf is not changed, and it goes to every included column, as defaulting_cols is not used on that route.

Parameters:

Name Type Description Default
cols T_ls | None

Names, or positions, of the columns to include. If None, all columns.

None
use_csv bool

If True, convert by way of CSV text.

False
use_donpa bool

If True, convert by way of to_donpa(). This suits numeric columns.

False
default Any

Value that replaces empty and None cells. This changes the Daf, except with use_donpa.

_MISSING
defaulting_cols T_ls | None

The columns that default applies to. If None, all included columns.

None

Returns:

Type Description
Any

The DataFrame.

Raises:

Type Description
NotImplementedError

default is given with use_csv=True.

Examples:

Make a pandas DataFrame from a small Daf of people:

>>> from daffodil.daf import Daf
>>> daf = Daf(cols=['id', 'name'], lol=[[1, 'Ana Lopez'], [2, 'Ben Okafor']])
>>> daf

| id |    name    |
| -: | ---------: |
|  1 |  Ana Lopez |
|  2 | Ben Okafor |

%% daf rows=2; cols=2; keyfield=''; name=''

Make the DataFrame, and read its values as lists:

>>> daf.to_pandas_df().values.tolist()
[[1, 'Ana Lopez'], [2, 'Ben Okafor']]

from_numpy

from_numpy(npa, keyfield='', cols=None, name='')

Make a Daf from a NumPy array.

The values become plain Python values. A two dimensional array gives one row for each row of the array. A one dimensional array gives a single row.

An array has one dtype. np.array([[1, 'x']]) makes every value text, before this method sees it, so the Daf gets text too. An array with dtype=object keeps each value as the Python object it was, such as an int, a str or None.

Parameters:

Name Type Description Default
npa Any

The NumPy array.

required
keyfield str

Column, or tuple or list of columns, whose values identify rows.

''
cols T_la | None

Column names.

None
name str

Name of the new Daf.

''

Returns:

Type Description
'Daf'

The new Daf.

Examples:

Make a Daf from a numpy array, with column names:

>>> import numpy as np
>>> Daf.from_numpy(np.array([[1042, 34], [2071, 52]]), cols=['emp_no', 'age'])

| emp_no | age |
| -----: | --: |
|   1042 |  34 |
|   2071 |  52 |

%% daf rows=2; cols=2; keyfield=''; name=''

Give the keyfield and a name:

>>> import numpy as np
>>> table = Daf.from_numpy(np.array([[1042, 34], [2071, 52]]), cols=['emp_no', 'age'], keyfield='emp_no', name='staff')
>>> table.name, table.select_record(2071)
('staff', {'emp_no': 2071, 'age': 52})

to_numpy

to_numpy()

Make a NumPy array of the rows.

Column names, the keyfield and any dtypes are not kept. NumPy picks one type for the whole array. If the Daf mixes numbers and text, every value becomes text.

Returns:

Type Description
T_npa

The NumPy array.

Examples:

Make a numpy array from a small Daf of orders:

>>> Daf(cols=['qty', 'amount'], lol=[[2, 25.0], [6, 24.5]]).to_numpy().tolist()
[[2.0, 25.0], [6.0, 24.5]]

to_donpa

to_donpa(colnames=None, default=_MISSING)

Make a dict of NumPy arrays, one array for each column.

This is a light form of a DataFrame. The arrays can be used in vector operations, such as donpa['margin'] = donpa['profit'] / donpa['cost']. Each column gets its own type, so numbers and text can be mixed.

Parameters:

Name Type Description Default
colnames T_ls | None

The columns to include. If None, all columns.

None
default Any

A value that replaces each NULL, None and NaN cell, in the arrays only. The Daf is not changed. It applies to every column in colnames, so give only the numeric columns, or a text column gets it too.

_MISSING

Returns:

Type Description
T_donpa

A dict that maps each column name to an array.

Notes

A column that mixes numbers and NULL becomes a text array, because NumPy has one type for an array. Give a numeric default to keep it numeric. A column with a default is also faster to make than one with blanks and no default, because the array is built from numbers.

Raises:

Type Description
ColumnNotFoundError

A name in colnames is not a column. This is a KeyError, and also a RuntimeError.

Examples:

Make a dict of numpy arrays, one for each column named:

>>> Daf(cols=['id', 'qty'], lol=[['o1', 2], ['o2', 6]]).to_donpa(['qty'])['qty'].tolist()
[2, 6]

A column with an empty cell becomes a text array. A numeric default replaces each empty or None cell, so the column stays numeric. The Daf is not changed:

>>> daf = Daf(cols=['qty', 'amount'], lol=[[2, 25.0], [6, ''], [None, 30.5]])
>>> daf

| qty  | amount |
| ---: | -----: |
|    2 |   25.0 |
|    6 |        |
| None |   30.5 |

%% daf rows=3; cols=2; keyfield=''; name=''

>>> daf.to_donpa(['amount'])['amount'].tolist()
['25.0', '', '30.5']
>>> daf.to_donpa(['qty', 'amount'], default=0)['amount'].tolist()
[25.0, 0.0, 30.5]
>>> daf

| qty  | amount |
| ---: | -----: |
|    2 |   25.0 |
|    6 |        |
| None |   30.5 |

%% daf rows=3; cols=2; keyfield=''; name=''

Other sources

from_directory

from_directory(
    source,
    schema=None,
    recursive=True,
    file_pat=None,
    include_dirs=False,
)

Make a Daf that lists the files in a folder.

Each row describes one file. The columns are the path, the folder, the name, the name without its extension, the extension, the size in bytes, and the modified and changed times. On Linux and macOS the ctime is the time of the last change to the file's metadata, such as its permissions, not the time it was created. Paths use / on every system.

With include_dirs=True, the folders are listed too, each before the files in the same folder. A folder has an is_dir of 1, a size of 0 and no extension. Without it, no folder is listed and is_dir is always 0.

The rows are in the order in which the file system lists the files. That order is not sorted, and it differs between machines. Sort the result if the order matters.

The schema chooses the columns of the result. A @schemaclass that lists only some of the fields above keeps only those. Other columns of the schema get their defaults. The new Daf has no keyfield. Files that cannot be read are skipped.

Only local folders are supported.

Parameters:

Name Type Description Default
source str | Path

The folder to list.

required
schema type | None

A @schemaclass that chooses the columns. If None, the built in one is used.

None
recursive bool

If True, list files in all sub folders. Otherwise only the folder itself.

True
file_pat str | None

A regular expression. Only names that match it are listed. Case is ignored.

None
include_dirs bool

If True, list the folders as well as the files.

False

Returns:

Type Description
'Daf'

The new Daf.

Examples:

Make a folder with three files, one in a subfolder:

>>> import os, tempfile
>>> folder = tempfile.mkdtemp()
>>> os.mkdir(os.path.join(folder, 'sub'))
>>> for name in ('b.txt', 'a.csv', os.path.join('sub', 'c.csv')):
...     with open(os.path.join(folder, name), 'w') as f:
...         n = f.write('hello')

List every file, with its name and size, and then only some of them:

>>> daf = Daf.from_directory(folder)
>>> sorted(daf.col('basename')), daf.col('size')
(['a.csv', 'b.txt', 'c.csv'], [5, 5, 5])
>>> sorted(Daf.from_directory(folder, recursive=False).col('basename'))
['a.csv', 'b.txt']
>>> sorted(Daf.from_directory(folder, file_pat=r'\.csv$').col('basename'))
['a.csv', 'c.csv']
>>> sorted(Daf.from_directory(folder, include_dirs=True).col('basename'))
['a.csv', 'b.txt', 'c.csv', 'sub']

A @schemaclass that names only some of the columns keeps only those:

>>> from daffodil.lib.schemaclass import schemaclass
>>> @schemaclass
... class FileRow:
...     basename: str = ''
...     size: int = 0
>>> listed = Daf.from_directory(folder, schema=FileRow)
>>> listed.columns()
['basename', 'size']

from_pdf class-attribute instance-attribute

from_pdf = from_pdf

from_pdf() reads one kind of county report, of election participation by precinct, from a PDF file, with pdfplumber. It is experimental and written for that report. Its interface may change, so it is not documented here.

from_googlesheet

from_googlesheet(
    spreadsheet_id,
    sheetname="Sheet1",
    *,
    service_account_file,
)

Read a Google Sheet into a Daf. This is not implemented.

It always raises NotImplementedError. It holds the place of the interface, which takes the path of a service account file.

Parameters:

Name Type Description Default
spreadsheet_id str

The ID of the Google Sheet.

required
sheetname str

The name of the sheet.

'Sheet1'
service_account_file str

The path of the Google service account file with the credentials.

required

Raises:

Type Description
NotImplementedError

Always.

Examples:

Read a Google Sheet. This needs the network and a key file, so it is not run here:

>>> Daf.from_googlesheet('some-id', service_account_file='key.json')
Traceback (most recent call last):
    ...
NotImplementedError: from_googlesheet() is not implemented yet.

to_googlesheet

to_googlesheet(
    spreadsheet_id,
    sheetname="Sheet1",
    *,
    service_account_file,
)

Write the Daf to a Google Sheet. This is not implemented.

It always raises NotImplementedError. It holds the place of the interface, which takes the path of a service account file.

Parameters:

Name Type Description Default
spreadsheet_id str

The ID of the Google Sheet.

required
sheetname str

The name of the sheet.

'Sheet1'
service_account_file str

The path of the Google service account file with the credentials.

required

Raises:

Type Description
NotImplementedError

Always.

Examples:

Write to a Google Sheet. This needs the network and a key file, so it is not run here:

>>> Daf(cols=['x'], lol=[[1]]).to_googlesheet('some-id', service_account_file='key.json')
Traceback (most recent call last):
    ...
NotImplementedError: to_googlesheet() is not implemented yet.