|
2 | 2 | # functions, not the general printing of pandas objects.
|
3 | 3 | from collections.abc import Mapping
|
4 | 4 | import string
|
5 |
| -import ast |
| 5 | +import pytest |
6 | 6 | import pandas._config.config as cf
|
7 | 7 | import pandas as pd
|
8 | 8 | from pandas.io.formats import printing
|
9 | 9 |
|
10 | 10 |
|
11 |
| -def test_formatted_index_names(): |
12 |
| - # Test cases: (input index names, expected formatted index names as lists) |
13 |
| - test_cases = [ |
14 |
| - (["'a", "b"], ['a', 'b']), # Remove leading quote |
15 |
| - (["test's", "b"], ['tests', 'b']), # Remove apostrophe |
16 |
| - (["'test'", "b"], ['test', 'b']), # Remove surrounding quotes |
17 |
| - (["test","'b"], ["test",'b']), # Remove single quote |
18 |
| - (["'test\n'", "b"], ['test\n', 'b']) # Remove quotes, preserve newline |
19 |
| - ] |
20 |
| - |
21 |
| - for input_names, expected_names in test_cases: |
22 |
| - # Create DataFrame with specified index names |
23 |
| - df = pd.DataFrame( |
24 |
| - {name: [1, 2, 3] for name in input_names} |
| 11 | +@pytest.mark.parametrize("input_names, expected_names", [ |
| 12 | + (["'a", "b"], ["\'a", "b"]), # Escape leading quote |
| 13 | + (["test's", "b"], ["test\'s", "b"]), # Escape apostrophe |
| 14 | + (["'test'", "b"], ["\'test\'", "b"]), # Escape surrounding quotes |
| 15 | + (["test","b'"], ["test","b\'"]), # Escape single quote |
| 16 | + (["'test\n'", "b"], ["\'test\n\'", "b"]) # Escape and preserve newline |
| 17 | +]) |
| 18 | +def test_formatted_index_names(input_names, expected_names): |
| 19 | + # Create DataFrame with specified index names |
| 20 | + df = pd.DataFrame( |
| 21 | + {name: [1, 2, 3] for name in input_names} |
25 | 22 | ).set_index(input_names)
|
26 |
| - index_names_str = df.index.names.__str__() |
27 |
| - |
28 |
| - formatted_names = ast.literal_eval(index_names_str) |
29 |
| - |
30 |
| - # Compare the formatted names with the expected names |
31 |
| - assert formatted_names == expected_names |
| 23 | + formatted_names = df.index.names |
| 24 | + |
| 25 | + assert formatted_names == expected_names |
32 | 26 |
|
33 | 27 |
|
34 | 28 | def test_adjoin():
|
|
0 commit comments