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

Conversation

ek-ok
Copy link

@ek-ok ek-ok commented Apr 9, 2025

Add to_snake_case and to_camel_case methods for Index label conversion

This PR adds two new string transformation methods to the pandas.Index class:

🚀 New methods

  • to_snake_case(): Converts string index labels to snake_case using inflection.underscore
  • to_camel_case(): Converts string index labels to camelCase using inflection.camelize

Both methods:

  • Leave non-string values (e.g. integers, None) unchanged
  • Are chainable and return a new Index instance

🧪 Tests

Added unit tests for both methods in test_base.py:

  • Covers strings with mixed case and spaces
  • Verifies behavior with mixed-type labels

📌 Example

import pandas as pd

df = pd.DataFrame({"first name": [1], "another_column": [1]})
df.columns = df.columns.to_camel_case()
print(df.columns)
# Index(['firstName', 'anotherColumn'], dtype='object')

df.columns = df.columns.to_snake_case()
print(df.columns)
# Index(['first_name', 'another_column'], dtype='object')

@mroeschke
Copy link
Member

Thanks for the PR, but it appears this feature is not tied to an open issue with core developer support. For new APIs, we would need core developer support before adding new API. (Note given the simplicity of this feature, it's unlikely that this would be added to pandas.)

Thanks for the suggestion but closing

@mroeschke mroeschke closed this Apr 9, 2025
@ek-ok
Copy link
Author

ek-ok commented Apr 10, 2025

Thanks for your comment and understand your point. This is actually something I've always wanted when dealing with messy column names. Without modifying pandas itself, the code below is the alternative I can think of. Do you have any recommendations?

import pandas as pd
import inflection

@pd.api.extensions.register_index_accessor("clean")
class ColumnIndexAccessor:
    def __init__(self, pandas_obj):
        self._obj = pandas_obj

    def to_snake_case(self):
        return self._obj.to_series().apply(
            lambda x: inflection.underscore(x).replace(" ", "_")
        ).values

    def to_camel_case(self):
        return self._obj.to_series().apply(
            lambda x: inflection.camelize(x, uppercase_first_letter=False)
        ).values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants