Skip to content

Fix reading of a simple 1x1 dt with NA value - #2517

Merged
mattdowle merged 4 commits into
masterfrom
issue2516
Jan 3, 2018
Merged

mattdowle merged 4 commits into
masterfrom
issue2516

Conversation

@st-pasha

Copy link
Copy Markdown
Contributor

Closes #2516

@st-pasha st-pasha added the fread label Dec 13, 2017
@st-pasha st-pasha added this to the v1.10.6 milestone Dec 13, 2017
@st-pasha st-pasha self-assigned this Dec 13, 2017
@st-pasha
st-pasha requested a review from mattdowle December 13, 2017 01:25
@codecov-io

codecov-io commented Dec 13, 2017 •

Copy link
Copy Markdown

Codecov Report

❗ No coverage uploaded for pull request base (master@58c753e). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #2517   +/-   ##
=========================================
  Coverage          ?   91.44%           
=========================================
  Files             ?       63           
  Lines             ?    12067           
  Branches          ?        0           
=========================================
  Hits              ?    11035           
  Misses            ?     1032           
  Partials          ?        0
Impacted Files Coverage Δ
src/fread.c 95.96% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 58c753e...67ae0ac. Read the comment docs.

@mattdowle

Copy link
Copy Markdown
Member

Not sure about this.
This PR has this behaviour :

> fread("A\n")
Empty data.table (0 rows) of 1 col: A
> fread("A\n\n")
    A
1: NA
> fread("A\n\n\n")
    A
1: NA
2: NA
> fread("A,B\n")
Empty data.table (0 rows) of 2 cols: A,B
> fread("A,B\n\n")
Empty data.table (0 rows) of 2 cols: A,B
> fread("A,B\n\n\n")
Empty data.table (0 rows) of 2 cols: A,B
>

which would be a change from CRAN version which does this :

> fread("A\n")
Empty data.table (0 rows) of 1 col: A
> fread("A\n\n")
Empty data.table (0 rows) of 1 col: A
> fread("A\n\n\n")
Empty data.table (0 rows) of 1 col: A
> fread("A,B\n")
Empty data.table (0 rows) of 2 cols: A,B
> fread("A,B\n\n")
Empty data.table (0 rows) of 2 cols: A,B
> fread("A,B\n\n\n")
Empty data.table (0 rows) of 2 cols: A,B

Thoughts?

@st-pasha

Copy link
Copy Markdown
Contributor Author

Consider this file:

Aa,Bb
10,20

30,40

Technically, this is not a valid CSV -- there shouldn't be a blank line there, but still, if this is the data, what do you do? You could choose a different coping strategy: (1) set fill=TRUE and fill the second line with NAs; (2) set blank.lines.skip=TRUE and ignore the second line; (3) the default action is to stop reading at the first blank line:

> fread("Aa,Bb\n10,20\n\n30,40\n")
    Aa Bb
1: 10 20
Warning message:
In fread("Aa,Bb\n10,20\n\n30,40\n") :
  Found the last consistent line but text exists afterwards. Consider fill=TRUE and/or blank.lines.skip=TRUE. First 200 characters of discarded line: <<30,40>>

Similarly, if the blank line occurs at the end, only this time fread doesn't display any warning:

> fread("Aa,Bb\n10,20\n30,40\n\n")
    Aa Bb
1: 10 20
2: 30 40

In both cases the CSV is still invalid, and fread deals with it in a way that it thinks is "most reasonable".


Now let's go back to the case in question. Consider the following file:

Aa
10

30

Unlike the first case, this is already a valid CSV: there is only one way to read it:

> fread("Aa\n10\n\n20\n")
   Aa
1: 10
2: NA
3: 20

Similarly, if the empty line occurs at the end of the file, it still remains a valid CSV file, and it should be read as such:

> fread("Aa\n10\n20\n\n")  # expected output:
   Aa
1: 10
2: 20
3: NA

The fact that it doesn't I view as a bug:

> fread("Aa\n10\n20\n\n")  # actual output
   Aa
1: 10
2: 20

To recap, in the first case (2+ -column file) blank lines make it an invalid CSV file, and there could be difference in opinion as to what coping strategy is best. At the same time, in the second case (1-column file) blank lines are allowed in a valid CSV file, and therefore they should be treated accordingly.

@mattdowle mattdowle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok good points. I'll add a few more tests, news item and entry to manual page, and then merge.

@mattdowle
mattdowle merged commit 1c4af71 into master Jan 3, 2018
@mattdowle
mattdowle deleted the issue2516 branch January 3, 2018 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants