Closed davidorme closed 1 month ago
Happy to fix. Can you provide a reproducible example? So a very small .csv file and the arguments you used?
Then I'll write a unit test to ensure it doesn't come back ever :)
Ahh great! So using the following contents
A,B,C 1,2,3 4,5,6
calling {{ read_csv('test.csv', header = None) }}
generates the above error, as does {{ read_excel('Example.xlsx', sheet_name = 'test', header = None) }}
.
The current work around we are using is calling {{ read_csv('test.csv', header = None, names = ["a", "b", "c"]) }}
, which ensures that valid header names are provided.
Just to add to that. If the data below is in a CSV file then {{ read_csv('test.csv') }}
works. The same data in an Excel file ({{ read_excel('tmp.xlsx', tablefmt='github') }}
) fails with the error above when it tries to sanitise the headers. I assume that this is because pandas.read_csv
automatically constrains header entries to be string, but the dataframe object from an Excel file retains the cell format and so the 3
comes through as an int
.
A,B,3
1,2,3
4,5,6
I implemented your suggestion to support non-string headers (in https://github.com/timvink/mkdocs-table-reader-plugin/commit/0b6201de52d5fcc96cce96386bfe936f90fdba44).
Indeed specifying header=None
will leave you with an integer heading:
If you need more control, I suggest writing a mkdocs hook to write the table to a markdown file, and insert it using the snippets extension. See https://timvink.github.io/mkdocs-table-reader-plugin/howto/alternatives/#write-tables-to-markdown-files
Fantastic. Thanks, @timvink - we'd adopted a hook to solve a more complex problem, but this really simplifies most of our use cases.
If you set
header=None
in the arguments toread_excel
then the resulting dataframe comes in with integer column names. That then breaks on this line:https://github.com/timvink/mkdocs-table-reader-plugin/blob/9f5c1aae8c7445bec311f672ecde6b1c2da5d97c/mkdocs_table_reader_plugin/markdown.py#L27
with the exceptions:
The reason we're trying to do this is we want to display data that repeats a value in the first row and need to avoid
pandas
duplicated name mangling. However, the same bug occurs in normal use if a worksheet has a non-string entry in the first row.The code below fixes the problem but feels a little bit hacky because the
thead
element of the rendered table is still there, containing those integer codes. Also, if the values are dates, then the user unavoidably gets the default string representation of thedatetime
object.