Repository: pyjanitor-devs/pyjanitor

Issue:
[ENH] Add optional removal of accents on functions.clean_names, enabled by default.
# Brief Description

<!-- Please provide a brief description of what you'd like to propose. -->

I'd like to suggest an option to remove accents from column names on the clean_names function.
It could be implemented using the normalize function of the standard library unicodedata:

[What is the best way to remove accents in a Python unicode string?](https://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-in-a-python-unicode-string)

I have created a branch called strip_accents and checked that the code addition does not break any tests.
[mralbu/pyjanitor/tree/strip_accents](https://github.com/mralbu/pyjanitor/tree/strip_accents)

# Example API

<!-- One of the selling points of pyjanitor is the API. Hence, we guard the API very carefully, and want to
make sure that it is accessible and understandable to many people. Please provide a few examples of what the API
of the new function you're proposing will look like. We have provided an example that you should modify. -->

```python
# create test DataFrame
df = pd.DataFrame({"João": [1, 2], "Лука́ся": [1, 2], "Käfer": [1, 2]})

# remove column name accents
df = df.clean_names(strip_accents=True)
expected_columns = ["joao", "лукася", "kafer"]
assert set(df.columns) == set(expected_columns)
```



Discussion:
Thanks for pinging in, @mralbu!

Yes, I think this would be a great addition to the library. Would you like to submit in a PR?
Thank your for the great library! I use rstats janitor, and I did miss some of its functionality in python. (particularly cleaning column names and removing accents, which the R package does by default)
I'd be happy to submit a pull request, thanks!

Resolving patch:
diff --git a/janitor/functions.py b/janitor/functions.py
--- a/janitor/functions.py
+++ b/janitor/functions.py
@@ -2,6 +2,7 @@
 
 import datetime as dt
 import re
+import unicodedata
 import warnings
 from fnmatch import translate
 from functools import partial, reduce
@@ -121,6 +122,7 @@ def clean_names(
     strip_underscores: str = None,
     case_type: str = "lower",
     remove_special: bool = False,
+    strip_accents: bool = True,
     preserve_original_columns: bool = True,
 ) -> pd.DataFrame:
     """
@@ -189,6 +191,9 @@ def _remove_special(col):
     if remove_special:
         df = df.rename(columns=_remove_special)
 
+    if strip_accents:
+        df = df.rename(columns=_strip_accents)
+
     df = df.rename(columns=lambda x: re.sub("_+", "_", x))
     df = _strip_underscores(df, strip_underscores)
 
@@ -208,6 +213,18 @@ def _normalize_1(col_name: str) -> str:
     return result
 
 
+def _strip_accents(col_name: str) -> str:
+    """
+    Removes accents from a DataFrame column name.
+    .. _StackOverflow: https://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-in-a-python-unicode-string # noqa: E501
+    """
+    return "".join(
+        l
+        for l in unicodedata.normalize("NFD", col_name)
+        if not unicodedata.combining(l)
+    )
+
+
 @pf.register_dataframe_method
 def remove_empty(df: pd.DataFrame) -> pd.DataFrame:
     """


Test patch:
diff --git a/tests/functions/test_clean_names.py b/tests/functions/test_clean_names.py
--- a/tests/functions/test_clean_names.py
+++ b/tests/functions/test_clean_names.py
@@ -104,6 +104,14 @@ def test_clean_names_strip_underscores(
     assert set(df.columns) == set(expected_columns)
 
 
+@pytest.mark.functions
+def test_clean_names_strip_accents():
+    df = pd.DataFrame({"João": [1, 2], "Лука́ся": [1, 2], "Käfer": [1, 2]})
+    df = df.clean_names(strip_accents=True)
+    expected_columns = ["joao", "лукася", "kafer"]
+    assert set(df.columns) == set(expected_columns)
+
+
 @pytest.mark.functions
 def test_incorrect_strip_underscores(multiindex_dataframe):
     with pytest.raises(JanitorError):