-
Notifications
You must be signed in to change notification settings - Fork 6
Add ability to read CSV without header row #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ def load(self, path: Union[_typing.PathLike, Dict[str, str]], options: SchemaDic | |
| - ``columns`` key specifies the data type of each column. Each data type corresponds to a Pandas' | ||
| supported dtype. If unspecified, then it is default. | ||
| - ``delimiter`` key specifies the delimiter of the input CSV file. | ||
| - ``header`` key specifies if the first row of the CSV file contains the headers. Defaults to True | ||
| - ``encoding`` key specifies the encoding of the CSV file. Defaults to UTF-8. | ||
| :raises TypeError: ``path`` is not a path object. | ||
| """ | ||
|
|
@@ -55,9 +56,15 @@ def load(self, path: Union[_typing.PathLike, Dict[str, str]], options: SchemaDic | |
| else: | ||
| dtypes[column] = type_ | ||
|
|
||
| names = None | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if options.get('header', True) is False: | ||
| # If no header use the columns provided in schema | ||
| names = [*options.get('columns', {})] | ||
|
|
||
| return pd.read_csv(path, dtype=dtypes, | ||
| # The following line after "if" is for circumventing | ||
| # https://github.com/pandas-dev/pandas/issues/38489 | ||
| parse_dates=parse_dates if len(parse_dates) > 0 else False, | ||
| names=names, | ||
| encoding=options.get('encoding', 'utf-8'), | ||
| delimiter=options.get('delimiter', ',')) | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -243,3 +243,13 @@ def test_csv_pandas_loader_no_encoding(self, tmp_path, noaa_jfk_schema): | |||||||||
|
|
||||||||||
| del noaa_jfk_schema['subdatasets']['jfk_weather_cleaned']['format']['options']['encoding'] | ||||||||||
| self.test_csv_pandas_loader(tmp_path, noaa_jfk_schema) | ||||||||||
|
|
||||||||||
| def test_csv_pandas_header(self, tmp_path, noaa_jfk_schema): | ||||||||||
| "Test CSVPandasLoader header options" | ||||||||||
|
|
||||||||||
| noaa_jfk_schema['subdatasets']['jfk_weather_cleaned']['format']['options']['header'] = True | ||||||||||
| self.test_csv_pandas_loader(tmp_path, noaa_jfk_schema) | ||||||||||
|
|
||||||||||
| with pytest.raises(ValueError): # Pandas should error from trying to read string as another dtype | ||||||||||
| noaa_jfk_schema['subdatasets']['jfk_weather_cleaned']['format']['options']['header'] = False | ||||||||||
|
Comment on lines
+253
to
+254
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also assert a couple of keyword in the exception message? |
||||||||||
| Dataset(noaa_jfk_schema, tmp_path, mode=Dataset.InitializationMode.DOWNLOAD_AND_LOAD) | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like as long as
headeris notFalse, it is treated asTrue(even for empty strings, empty lists, which are usually evaluated to False in Python). Could you make this point clear in this document?