Skip to content

Better support for reading files without using any column as index, or any row as column names #1102

@gdementen

Description

@gdementen

i.e. use synthetic 0-N labels in anonymous axes.

This is necessary to support database-like files and this is the default behaviour in Pandas.

First create our test case:

>>> from pathlib import Path
>>> arr = ndtest('a=a0,a1;column=b,c')
>>> arr.to_csv('test.csv', dialect='classic')
>>> print(Path('test.csv').read_text())
a,b,c
a0,0,1
a1,2,3
>>> arr.to_excel('test.xlsx')

I want to be able achieve this:

>>> Array([['a0', 0, 1],
...        ['a1', 2, 3]], axes=(2, 'a,b,c'))
{0}*\{1}   a  b  c
       0  a0  0  1
       1  a1  2  3

This mostly works for CSV files but this is not documented and we need a different API anyway (using nb_axes=0 feels just wrong) and we need unit tests too (assuming there are none already)

>>> read_csv('test.csv', nb_axes=0)
{0}\{1}   a  b  c
      0  a0  0  1
      1  a1  2  3

But it does not work for read_excel...

>>> read_excel('classic.xlsx', nb_axes=0)
ValueError: Must pass non-zero number of levels/codes
>>> # ... unless we use engine='openpyxl'
>>> read_excel('classic.xlsx', engine='openpyxl', nb_axes=0)
{0}\{1}  a\column  b  c
      0        a0  0  1
      1        a1  2  3

I would also like to be able to achieve this:

>>> Array([[ 'a', 'b', 'c'],
...        ['a0',   0,   1],
...        ['a1',   2,   3]])
{0}*\{1}*   0  1  2
        0   a  b  c
        1  a0  0  1
        2  a1  2  3

It is possible using open_excel:

>>> with open_excel('test.xlsx') as wb:
...     print(wb[0][:])
{0}*\{1}*         0  1  2
        0  a\column  b  c
        1        a0  0  1
        2        a1  2  3      

but I don't think it is currently possible using read_csv nor read_excel(engine='xlwings'). It might be possible using read_excel(engine='openpyxl') by using undocumented (in larray) pandas arguments.

My old fix_nb_axes_eq_1 branch fixes a related problem and discuss a bit about this.

I think in the end we will need arguments to specify number of (or indices of) vertical axes and horizontal axes AND support those arguments to be 0.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions