Skip to content

Commit 9b99775

Browse files
ek-okIkkei
authored andcommitted
Add to_snake_case and to_camel_case for index label conversion using inflection
1 parent cc4f585 commit 9b99775

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

pandas/core/indexes/base.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7190,6 +7190,63 @@ def __invert__(self) -> Index:
71907190
# GH#8875
71917191
return self._unary_method(operator.inv)
71927192

7193+
# --------------------------------------------------------------------
7194+
# String Case Conversion Methods
7195+
7196+
def to_snake_case(self):
7197+
"""
7198+
Convert index labels to snake_case.
7199+
7200+
Returns
7201+
-------
7202+
Index
7203+
A new Index with labels converted to snake_case.
7204+
7205+
Notes
7206+
-----
7207+
This method uses the `inflection.underscore` function to perform the
7208+
conversion. Non-string values in the index are left unchanged.
7209+
7210+
Examples
7211+
--------
7212+
>>> idx = pd.Index(["ColumnName", "AnotherColumn", 123])
7213+
>>> idx.to_snake_case()
7214+
Index(['column_name', 'another_column', 123], dtype='object')
7215+
"""
7216+
from inflection import underscore
7217+
7218+
return self.map(
7219+
lambda x: underscore(x.replace(" ", "_")) if isinstance(x, str) else x
7220+
)
7221+
7222+
def to_camel_case(self):
7223+
"""
7224+
Convert index labels to camelCase.
7225+
7226+
Returns
7227+
-------
7228+
Index
7229+
A new Index with labels converted to camelCase.
7230+
7231+
Notes
7232+
-----
7233+
This method uses the `inflection.camelize` function to perform the
7234+
conversion. Non-string values in the index are left unchanged.
7235+
7236+
Examples
7237+
--------
7238+
>>> idx = pd.Index(["column_name", "another_column", 123])
7239+
>>> idx.to_camel_case()
7240+
Index(['columnName', 'anotherColumn', 123], dtype='object')
7241+
"""
7242+
from inflection import camelize
7243+
7244+
return self.map(
7245+
lambda x: camelize(x.replace(" ", "_"), uppercase_first_letter=False)
7246+
if isinstance(x, str)
7247+
else x
7248+
)
7249+
71937250
# --------------------------------------------------------------------
71947251
# Reductions
71957252

pandas/tests/indexes/test_base.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,38 @@ def test_index_round(self, decimals, expected_results):
13501350

13511351
tm.assert_index_equal(result, expected)
13521352

1353+
@pytest.mark.parametrize(
1354+
"input_labels, expected_snake_case",
1355+
[
1356+
(
1357+
["ColumnName", "AnotherColumn", 123],
1358+
["column_name", "another_column", 123],
1359+
),
1360+
(["MixedCase", "with spaces", None], ["mixed_case", "with_spaces", None]),
1361+
],
1362+
)
1363+
def test_to_snake_case(self, input_labels, expected_snake_case):
1364+
idx = Index(input_labels)
1365+
result = idx.to_snake_case()
1366+
expected = Index(expected_snake_case)
1367+
tm.assert_index_equal(result, expected)
1368+
1369+
@pytest.mark.parametrize(
1370+
"input_labels, expected_camel_case",
1371+
[
1372+
(
1373+
["column_name", "another_column", 123],
1374+
["columnName", "anotherColumn", 123],
1375+
),
1376+
(["with_spaces", "Mixed_Case", None], ["withSpaces", "mixedCase", None]),
1377+
],
1378+
)
1379+
def test_to_camel_case(self, input_labels, expected_camel_case):
1380+
idx = Index(input_labels)
1381+
result = idx.to_camel_case()
1382+
expected = Index(expected_camel_case)
1383+
tm.assert_index_equal(result, expected)
1384+
13531385

13541386
class TestMixedIntIndex:
13551387
# Mostly the tests from common.py for which the results differ

0 commit comments

Comments
 (0)