3
3
from __future__ import annotations
4
4
5
5
import unicodedata
6
- from typing import Optional , Union
7
6
8
7
import pandas as pd
9
8
import pandas_flavor as pf
18
17
@deprecated_alias (preserve_original_columns = "preserve_original_labels" )
19
18
def clean_names (
20
19
df : pd .DataFrame ,
21
- axis : Union [ str , None ] = "columns" ,
22
- column_names : Union [ str , list ] = None ,
23
- strip_underscores : Optional [ Union [ str , bool ]] = None ,
20
+ axis : str = "columns" ,
21
+ column_names : str | list = None ,
22
+ strip_underscores : str | bool = None ,
24
23
case_type : str = "lower" ,
25
24
remove_special : bool = False ,
26
25
strip_accents : bool = True ,
@@ -170,14 +169,14 @@ def clean_names(
170
169
171
170
172
171
def _clean_names (
173
- obj : Union [ pd .Index , pd .Series ] ,
174
- strip_underscores : Optional [ Union [ str , bool ]] = None ,
175
- case_type : str = "lower" ,
176
- remove_special : bool = False ,
177
- strip_accents : bool = False ,
178
- enforce_string : bool = False ,
179
- truncate_limit : int = None ,
180
- ) -> Union [ pd .Index , pd .Series ] :
172
+ obj : pd .Index | pd .Series ,
173
+ strip_underscores : str | bool ,
174
+ case_type : str ,
175
+ remove_special : bool ,
176
+ strip_accents : bool ,
177
+ enforce_string : bool ,
178
+ truncate_limit : int ,
179
+ ) -> pd .Index | pd .Series :
181
180
"""
182
181
Generic function to clean labels in a pandas object.
183
182
"""
@@ -202,9 +201,9 @@ def _clean_names(
202
201
203
202
204
203
def _change_case (
205
- obj : Union [ pd .Index , pd .Series ] ,
204
+ obj : pd .Index | pd .Series ,
206
205
case_type : str ,
207
- ) -> Union [ pd .Index , pd .Series ] :
206
+ ) -> pd .Index | pd .Series :
208
207
"""Change case of labels in obj."""
209
208
case_types = {"preserve" , "upper" , "lower" , "snake" }
210
209
case_type = case_type .lower ()
@@ -226,9 +225,7 @@ def _change_case(
226
225
)
227
226
228
227
229
- def _normalize_1 (
230
- obj : Union [pd .Index , pd .Series ]
231
- ) -> Union [pd .Index , pd .Series ]:
228
+ def _normalize_1 (obj : pd .Index | pd .Series ) -> pd .Index | pd .Series :
232
229
"""Perform normalization of labels in obj."""
233
230
FIXES = [(r"[ /:,?()\.-]" , "_" ), (r"['’]" , "" ), (r"[\xa0]" , "_" )]
234
231
for search , replace in FIXES :
@@ -238,8 +235,8 @@ def _normalize_1(
238
235
239
236
240
237
def _strip_accents (
241
- obj : Union [ pd .Index , pd .Series ] ,
242
- ) -> Union [ pd .Index , pd .Series ] :
238
+ obj : pd .Index | pd .Series ,
239
+ ) -> pd .Index | pd .Series :
243
240
"""Remove accents from a label.
244
241
245
242
Inspired from [StackOverflow][so].
@@ -258,9 +255,9 @@ def _strip_accents(
258
255
259
256
260
257
def _strip_underscores_func (
261
- obj : Union [ pd .Index , pd .Series ] ,
262
- strip_underscores : Union [ str , bool ] = None ,
263
- ) -> Union [ pd .Index , pd .Series ] :
258
+ obj : pd .Index | pd .Series ,
259
+ strip_underscores : str | bool = None ,
260
+ ) -> pd .Index | pd .Series :
264
261
"""Strip underscores."""
265
262
underscore_options = {None , "left" , "right" , "both" , "l" , "r" , True }
266
263
if strip_underscores not in underscore_options :
0 commit comments