Skip to content

Commit cc07c77

Browse files
committed
Lint and parameter description
1 parent a012f40 commit cc07c77

File tree

4 files changed

+43
-33
lines changed

4 files changed

+43
-33
lines changed

pandas/core/generic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3859,6 +3859,11 @@ def to_csv(
38593859
38603860
{storage_options}
38613861
3862+
preserve_complex : bool, default False
3863+
If True, arrays (e.g. NumPy arrays) or complex data are serialized and
3864+
reconstructed in a custom manner. If False (default), standard CSV
3865+
behavior is used.
3866+
38623867
Returns
38633868
-------
38643869
None or str

pandas/io/parsers/readers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,11 @@ class _read_shared(TypedDict, Generic[HashableT], total=False):
455455
456456
{storage_options}
457457
458+
preserve_complex : bool, default False
459+
If True, arrays (e.g. NumPy arrays) or complex data are serialized and
460+
reconstructed in a custom manner. If False (default), standard CSV
461+
behavior is used.
462+
458463
dtype_backend : {{'numpy_nullable', 'pyarrow'}}
459464
Back-end data type applied to the resultant :class:`DataFrame`
460465
(still experimental). If not specified, the default behavior

pandas/tests/io/formats/test_to_csv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,4 +740,4 @@ def test_to_csv_iterative_compression_buffer(compression):
740740
tm.assert_frame_equal(
741741
pd.read_csv(buffer, compression=compression, index_col=0), df
742742
)
743-
assert not buffer.closed
743+
assert not buffer.closed

scripts/tests/test_csv.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import os
21
import tempfile
32

43
import numpy as np
4+
55
import pandas as pd
66

77

@@ -11,42 +11,39 @@ def test_preserve_numpy_arrays_in_csv():
1111
"id": [1, 2],
1212
"embedding": [
1313
np.array([0.1, 0.2, 0.3]),
14-
np.array([0.4, 0.5, 0.6])
14+
np.array([0.4, 0.5, 0.6]),
1515
],
1616
})
1717

18-
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp:
18+
with tempfile.NamedTemporaryFile(suffix=".csv") as tmp:
1919
path = tmp.name
20-
21-
try:
2220
df.to_csv(path, index=False, preserve_complex=True)
2321
df_loaded = pd.read_csv(path, preserve_complex=True)
24-
assert isinstance(df_loaded["embedding"][0], np.ndarray), (
25-
"Test Failed: The CSV did not preserve embeddings as NumPy arrays!"
26-
)
27-
print("PASS: test_preserve_numpy_arrays_in_csv")
28-
finally:
29-
os.remove(path)
22+
23+
assert isinstance(
24+
df_loaded["embedding"][0], np.ndarray
25+
), "Test Failed: The CSV did not preserve embeddings as NumPy arrays!"
26+
27+
print("PASS: test_preserve_numpy_arrays_in_csv")
3028

3129

3230
def test_preserve_numpy_arrays_in_csv_empty_dataframe():
3331
print("\nRunning: test_preserve_numpy_arrays_in_csv_empty_dataframe")
3432
df = pd.DataFrame({"embedding": []})
3533
expected = "embedding\n"
3634

37-
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp:
35+
with tempfile.NamedTemporaryFile(suffix=".csv") as tmp:
3836
path = tmp.name
39-
40-
try:
4137
df.to_csv(path, index=False, preserve_complex=True)
4238
with open(path, encoding="utf-8") as f:
4339
result = f.read()
44-
assert result == expected, (
45-
f"CSV output mismatch for empty DataFrame.\nGot:\n{result}\nExpected:\n{expected}"
46-
)
47-
print("PASS: test_preserve_numpy_arrays_in_csv_empty_dataframe")
48-
finally:
49-
os.remove(path)
40+
41+
msg = (
42+
f"CSV output mismatch for empty DataFrame.\n"
43+
f"Got:\n{result}\nExpected:\n{expected}"
44+
)
45+
assert result == expected, msg
46+
print("PASS: test_preserve_numpy_arrays_in_csv_empty_dataframe")
5047

5148

5249
def test_preserve_numpy_arrays_in_csv_mixed_dtypes():
@@ -56,30 +53,33 @@ def test_preserve_numpy_arrays_in_csv_mixed_dtypes():
5653
"name": ["alice", "bob"],
5754
"scores": [
5855
np.array([95.5, 88.0]),
59-
np.array([76.0, 90.5])
56+
np.array([76.0, 90.5]),
6057
],
6158
"age": [25, 30],
6259
})
6360

64-
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp:
61+
with tempfile.NamedTemporaryFile(suffix=".csv") as tmp:
6562
path = tmp.name
66-
67-
try:
6863
df.to_csv(path, index=False, preserve_complex=True)
6964
df_loaded = pd.read_csv(path, preserve_complex=True)
70-
assert isinstance(df_loaded["scores"][0], np.ndarray), (
71-
"Failed: 'scores' column not deserialized as np.ndarray."
65+
66+
err_scores = "Failed: 'scores' column not deserialized as np.ndarray."
67+
assert isinstance(df_loaded["scores"][0], np.ndarray), err_scores
68+
assert df_loaded["id"].dtype == np.int64, (
69+
"Failed: 'id' should still be int."
70+
)
71+
assert df_loaded["name"].dtype == object, (
72+
"Failed: 'name' should still be object/string."
73+
)
74+
assert df_loaded["age"].dtype == np.int64, (
75+
"Failed: 'age' should still be int."
7276
)
73-
assert df_loaded["id"].dtype == np.int64, "Failed: 'id' should still be int."
74-
assert df_loaded["name"].dtype == object, "Failed: 'name' should still be object/string."
75-
assert df_loaded["age"].dtype == np.int64, "Failed: 'age' should still be int."
7677

77-
print("PASS: test_preserve_numpy_arrays_in_csv_mixed_dtypes")
78-
finally:
79-
os.remove(path)
78+
print("PASS: test_preserve_numpy_arrays_in_csv_mixed_dtypes")
8079

8180

8281
if __name__ == "__main__":
8382
test_preserve_numpy_arrays_in_csv()
8483
test_preserve_numpy_arrays_in_csv_empty_dataframe()
8584
test_preserve_numpy_arrays_in_csv_mixed_dtypes()
85+
print("\nDone.")

0 commit comments

Comments
 (0)