|
1 | 1 | import io
|
2 | 2 | import os
|
| 3 | +from pathlib import Path |
3 | 4 | import sys
|
| 5 | +from zipfile import ZipFile |
4 | 6 |
|
5 | 7 | import numpy as np
|
6 | 8 | import pytest
|
@@ -541,23 +543,38 @@ def test_to_csv_compression_dict_no_method_raises(self):
|
541 | 543 | df.to_csv(path, compression=compression)
|
542 | 544 |
|
543 | 545 | @pytest.mark.parametrize("compression", ["zip", "infer"])
|
544 |
| - @pytest.mark.parametrize( |
545 |
| - "archive_name", [None, "test_to_csv.csv", "test_to_csv.zip"] |
546 |
| - ) |
| 546 | + @pytest.mark.parametrize("archive_name", ["test_to_csv.csv", "test_to_csv.zip"]) |
547 | 547 | def test_to_csv_zip_arguments(self, compression, archive_name):
|
548 | 548 | # GH 26023
|
549 |
| - from zipfile import ZipFile |
550 |
| - |
551 | 549 | df = DataFrame({"ABC": [1]})
|
552 | 550 | with tm.ensure_clean("to_csv_archive_name.zip") as path:
|
553 | 551 | df.to_csv(
|
554 | 552 | path, compression={"method": compression, "archive_name": archive_name}
|
555 | 553 | )
|
556 | 554 | with ZipFile(path) as zp:
|
557 |
| - expected_arcname = path if archive_name is None else archive_name |
558 |
| - expected_arcname = os.path.basename(expected_arcname) |
559 | 555 | assert len(zp.filelist) == 1
|
560 |
| - archived_file = os.path.basename(zp.filelist[0].filename) |
| 556 | + archived_file = zp.filelist[0].filename |
| 557 | + assert archived_file == archive_name |
| 558 | + |
| 559 | + @pytest.mark.parametrize( |
| 560 | + "filename,expected_arcname", |
| 561 | + [ |
| 562 | + ("archive.csv", "archive.csv"), |
| 563 | + ("archive.tsv", "archive.tsv"), |
| 564 | + ("archive.csv.zip", "archive.csv"), |
| 565 | + ("archive.tsv.zip", "archive.tsv"), |
| 566 | + ("archive.zip", "archive"), |
| 567 | + ], |
| 568 | + ) |
| 569 | + def test_to_csv_zip_infer_name(self, filename, expected_arcname): |
| 570 | + # GH 39465 |
| 571 | + df = DataFrame({"ABC": [1]}) |
| 572 | + with tm.ensure_clean_dir() as dir: |
| 573 | + path = Path(dir, filename) |
| 574 | + df.to_csv(path, compression="zip") |
| 575 | + with ZipFile(path) as zp: |
| 576 | + assert len(zp.filelist) == 1 |
| 577 | + archived_file = zp.filelist[0].filename |
561 | 578 | assert archived_file == expected_arcname
|
562 | 579 |
|
563 | 580 | @pytest.mark.parametrize("df_new_type", ["Int64"])
|
|
0 commit comments