Skip to content

Add to_snake_case and to_camel_case for index label conversion using … #61259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7190,6 +7190,63 @@ def __invert__(self) -> Index:
# GH#8875
return self._unary_method(operator.inv)

# --------------------------------------------------------------------
# String Case Conversion Methods

def to_snake_case(self):
"""
Convert index labels to snake_case.

Returns
-------
Index
A new Index with labels converted to snake_case.

Notes
-----
This method uses the `inflection.underscore` function to perform the
conversion. Non-string values in the index are left unchanged.

Examples
--------
>>> idx = pd.Index(["ColumnName", "AnotherColumn", 123])
>>> idx.to_snake_case()
Index(['column_name', 'another_column', 123], dtype='object')
"""
from inflection import underscore

return self.map(
lambda x: underscore(x.replace(" ", "_")) if isinstance(x, str) else x
)

def to_camel_case(self):
"""
Convert index labels to camelCase.

Returns
-------
Index
A new Index with labels converted to camelCase.

Notes
-----
This method uses the `inflection.camelize` function to perform the
conversion. Non-string values in the index are left unchanged.

Examples
--------
>>> idx = pd.Index(["column_name", "another_column", 123])
>>> idx.to_camel_case()
Index(['columnName', 'anotherColumn', 123], dtype='object')
"""
from inflection import camelize

return self.map(
lambda x: camelize(x.replace(" ", "_"), uppercase_first_letter=False)
if isinstance(x, str)
else x
)

# --------------------------------------------------------------------
# Reductions

Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,38 @@ def test_index_round(self, decimals, expected_results):

tm.assert_index_equal(result, expected)

@pytest.mark.parametrize(
"input_labels, expected_snake_case",
[
(
["ColumnName", "AnotherColumn", 123],
["column_name", "another_column", 123],
),
(["MixedCase", "with spaces", None], ["mixed_case", "with_spaces", None]),
],
)
def test_to_snake_case(self, input_labels, expected_snake_case):
idx = Index(input_labels)
result = idx.to_snake_case()
expected = Index(expected_snake_case)
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize(
"input_labels, expected_camel_case",
[
(
["column_name", "another_column", 123],
["columnName", "anotherColumn", 123],
),
(["with_spaces", "Mixed_Case", None], ["withSpaces", "mixedCase", None]),
],
)
def test_to_camel_case(self, input_labels, expected_camel_case):
idx = Index(input_labels)
result = idx.to_camel_case()
expected = Index(expected_camel_case)
tm.assert_index_equal(result, expected)


class TestMixedIntIndex:
# Mostly the tests from common.py for which the results differ
Expand Down
Loading