|
1 |
| -# import pandas as pd |
2 |
| -# import numpy as np |
| 1 | +import pandas as pd |
| 2 | +print(pd.__file__) |
| 3 | +print(pd.__version__) |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import os |
3 | 7 |
|
4 | 8 | # # Create a DataFrame with NumPy arrays
|
5 | 9 | # df = pd.DataFrame({
|
6 | 10 | # 'id': [1, 2],
|
7 | 11 | # 'embedding': [np.array([0.1, 0.2, 0.3]), np.array([0.4, 0.5, 0.6])]
|
8 | 12 | # })
|
9 | 13 |
|
10 |
| -# # Save to CSV |
| 14 | +# # Save to CSV (where your custom preserve_complex logic resides) |
11 | 15 | # csv_file = "test_numpy_array.csv"
|
12 | 16 | # df.to_csv(csv_file, index=False, preserve_complex=True)
|
13 |
| -# print(f"Saved CSV:\n{open(csv_file).read()}") |
14 | 17 |
|
15 |
| -# # Read back the CSV |
16 |
| -# df_loaded = pd.read_csv(csv_file) |
| 18 | +# # Read back the raw CSV content (as text only) |
| 19 | +# with open(csv_file, "r") as f: |
| 20 | +# csv_content = f.read() |
17 | 21 |
|
18 |
| -# # Print results |
19 |
| -# print("\nLoaded DataFrame:") |
20 |
| -# print(df_loaded) |
| 22 | +# print(f"Saved CSV:\n{csv_content}") |
21 | 23 |
|
22 |
| -# # ✅ **Make the test fail by checking if we correctly load NumPy arrays** |
| 24 | +# # Simple test: check that our JSON-ified arrays are present in the CSV text |
23 | 25 | # try:
|
24 |
| -# assert isinstance(df_loaded["embedding"][0], np.ndarray), "Test Failed: Embeddings were not preserved as NumPy arrays!" |
25 |
| -# print("\nTest Passed: Embeddings were correctly preserved as NumPy arrays") |
26 |
| -# except AssertionError as e: |
27 |
| -# print("\nTest Failed: Pandas does not preserve NumPy arrays in CSV, needs improvement!") |
28 |
| -# raise e |
| 26 | +# assert "[0.1, 0.2, 0.3]" in csv_content |
| 27 | +# assert "[0.4, 0.5, 0.6]" in csv_content |
| 28 | +# print("\nTest Passed: The CSV output includes JSON-serialized arrays for 'embedding'.") |
| 29 | +# except AssertionError: |
| 30 | +# print("\nTest Failed: The CSV does not appear to have JSON-serialized arrays as expected!") |
| 31 | +# raise |
29 | 32 |
|
30 |
| -import pandas as pd |
31 |
| -print(pd.__file__) |
32 |
| -print(pd.__version__) |
33 | 33 |
|
34 |
| -import numpy as np |
35 |
| -import os |
36 | 34 |
|
| 35 | +# TEST2 |
37 | 36 | # Create a DataFrame with NumPy arrays
|
38 | 37 | df = pd.DataFrame({
|
39 | 38 | 'id': [1, 2],
|
40 | 39 | 'embedding': [np.array([0.1, 0.2, 0.3]), np.array([0.4, 0.5, 0.6])]
|
41 | 40 | })
|
42 | 41 |
|
43 |
| -# Save to CSV (where your custom preserve_complex logic resides) |
| 42 | +# Save to CSV |
44 | 43 | csv_file = "test_numpy_array.csv"
|
45 | 44 | df.to_csv(csv_file, index=False, preserve_complex=True)
|
| 45 | +print(f"Saved CSV:\n{open(csv_file).read()}") |
46 | 46 |
|
47 |
| -# Read back the raw CSV content (as text only) |
48 |
| -with open(csv_file, "r") as f: |
49 |
| - csv_content = f.read() |
| 47 | +# Read back the CSV |
| 48 | +df_loaded = pd.read_csv(csv_file, preserve_complex=True) |
50 | 49 |
|
51 |
| -print(f"Saved CSV:\n{csv_content}") |
| 50 | +# Print results |
| 51 | +print("\nLoaded DataFrame:") |
| 52 | +print(df_loaded) |
52 | 53 |
|
53 |
| -# Simple test: check that our JSON-ified arrays are present in the CSV text |
| 54 | +# ✅ **Make the test fail by checking if we correctly load NumPy arrays** |
54 | 55 | try:
|
55 |
| - assert "[0.1, 0.2, 0.3]" in csv_content |
56 |
| - assert "[0.4, 0.5, 0.6]" in csv_content |
57 |
| - print("\nTest Passed: The CSV output includes JSON-serialized arrays for 'embedding'.") |
58 |
| -except AssertionError: |
59 |
| - print("\nTest Failed: The CSV does not appear to have JSON-serialized arrays as expected!") |
60 |
| - raise |
| 56 | + assert isinstance(df_loaded["embedding"][0], np.ndarray), "Test Failed: Embeddings were not preserved as NumPy arrays!" |
| 57 | + print("\nTest Passed: Embeddings were correctly preserved as NumPy arrays") |
| 58 | +except AssertionError as e: |
| 59 | + print("\nTest Failed: Pandas does not preserve NumPy arrays in CSV, needs improvement!") |
| 60 | + raise e |
0 commit comments