Repository: dask/dask

Issue:
Wrong path when reading empty csv file
```python

import dask.dataframe as dd

for k, content in enumerate(["0, 1, 2", "3, 4, 5", "6, 7, 8"]):
    with open(str(k) + ".csv", "w") as file:
        file.write(content)

print(dd.read_csv("*.csv",  include_path_column=True, names=["A", "B", "C"],).compute())
print()

# The second file is empty
for k, content in enumerate(["0, 1, 2", "", "6, 7, 8"]):
    with open(str(k) + ".csv", "w") as file:
        file.write(content)

print(dd.read_csv("*.csv",  include_path_column=True, names=["A", "B", "C"],).compute())

```

returns:

```
   A  B  C    path
0  0  1  2    0.csv
0  3  4  5    1.csv
0  6  7  8    2.csv

   A  B  C    path
0  0  1  2    0.csv
0  6  7  8    1.csv  # <- This is wrong and should be "2.csv"

```


**Environment**:

- Dask version: 2.19.0
- Python version: 3.8.3
- Operating System: Windows 10
- Install method (conda, pip, source): Anaconda



Discussion:
Thanks for raising an issue @JulianWgs, I'm able to reproduce on the current development version of Dask and can confirm this is a bug. Is this something you're interested in looking into more? (no obligation though)
I think what is happening here is that blocks will look like the following:

> [[Delayed('read-block-0-217e6e7550017caad19382e07bcec157')], [], [Delayed('read-block-0-3df2b8124f02524ab7b4eb76b9822d31')]]

And eventually the empty list is dropped: https://github.com/dask/dask/blob/51509339c1cc476f5f96ae78e61daf125037ca20/dask/dataframe/io/csv.py#L325

Previously when building up the list of paths: https://github.com/dask/dask/blob/51509339c1cc476f5f96ae78e61daf125037ca20/dask/dataframe/io/csv.py#L482-L487 we don't update the paths for the upcoming flattening step
Great, that you could reproduce it. Sorry, I don't have the time right now :/

>> Am 26.08.2020 um 19:27 schrieb Benjamin Zaitlen <notifications@github.com>:
> ﻿
> I think what is happening here is that blocks will look like the following:
> 
> [[Delayed('read-block-0-217e6e7550017caad19382e07bcec157')], [], [Delayed('read-block-0-3df2b8124f02524ab7b4eb76b9822d31')]]
> 
> And eventually the empty list is dropped: https://github.com/dask/dask/blob/51509339c1cc476f5f96ae78e61daf125037ca20/dask/dataframe/io/csv.py#L325
> 
> Previously when building up the list of paths: https://github.com/dask/dask/blob/51509339c1cc476f5f96ae78e61daf125037ca20/dask/dataframe/io/csv.py#L482-L487 we don't update the paths for the upcoming flattening step
> 
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub, or unsubscribe.

No worries! @quasiben has done a great job tracking down the root cause of this issue, which will help whenever someone picks this up 

Resolving patch:
diff --git a/dask/dataframe/io/csv.py b/dask/dataframe/io/csv.py
--- a/dask/dataframe/io/csv.py
+++ b/dask/dataframe/io/csv.py
@@ -1,3 +1,4 @@
+from os.path import basename
 from collections.abc import Mapping
 from io import BytesIO
 from warnings import warn, catch_warnings, simplefilter
@@ -329,6 +330,12 @@ def text_blocks_to_pandas(
     name = "read-csv-" + tokenize(reader, columns, enforce, head)
 
     if path:
+        block_file_names = [basename(b[1].path) for b in blocks]
+        path = (
+            path[0],
+            [p for p in path[1] if basename(p) in block_file_names],
+        )
+
         colname, paths = path
         head = head.assign(
             **{


Test patch:
diff --git a/dask/dataframe/io/tests/test_csv.py b/dask/dataframe/io/tests/test_csv.py
--- a/dask/dataframe/io/tests/test_csv.py
+++ b/dask/dataframe/io/tests/test_csv.py
@@ -1564,3 +1564,26 @@ def test_to_csv_line_ending():
 def test_block_mask(block_lists):
     mask = list(block_mask(block_lists))
     assert len(mask) == len(list(flatten(block_lists)))
+
+
+def test_reading_empty_csv_files_with_path():
+    with tmpdir() as tdir:
+        for k, content in enumerate(["0, 1, 2", "", "6, 7, 8"]):
+            with open(os.path.join(tdir, str(k) + ".csv"), "w") as file:
+                file.write(content)
+        result = dd.read_csv(
+            os.path.join(tdir, "*.csv"),
+            include_path_column=True,
+            converters={"path": parse_filename},
+            names=["A", "B", "C"],
+        ).compute()
+        df = pd.DataFrame(
+            {
+                "A": [0, 6],
+                "B": [1, 7],
+                "C": [2, 8],
+                "path": ["0.csv", "2.csv"],
+            }
+        )
+        df["path"] = df["path"].astype("category")
+        assert_eq(result, df, check_index=False)