Skip to content
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

convert_excel_date, convert_matlab_date for Polars #1365

Merged
merged 110 commits into from
Jul 3, 2024
Merged
Changes from 1 commit
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
862c7dd
add make_clean_names function that can be applied to polars
Apr 19, 2024
01531cc
add examples for make_clean_names
Apr 20, 2024
0fb440e
changelog
Apr 20, 2024
5e944b2
limit import location for polars
Apr 20, 2024
501d9c6
limit import location for polars
Apr 20, 2024
9506832
fix polars in environment-dev.yml
Apr 20, 2024
1ae8edd
install polars in doctest
Apr 20, 2024
3b1829b
limit polars imports - user should have polars already installed
Apr 20, 2024
52fd80c
use subprocess.run
Apr 20, 2024
2dce78b
add subprocess.devnull to docstrings
Apr 20, 2024
37b3feb
add subprocess.devnull to docstrings
Apr 20, 2024
0953f2d
add subprocess.devnull to docstrings
Apr 20, 2024
d7c71b6
add subprocess.devnull to docstrings
Apr 20, 2024
40b8502
add os.devnull
Apr 20, 2024
4f11d09
add polars as requirement for docs
Apr 20, 2024
54b179c
add polars to tests requirements
Apr 20, 2024
25b39b9
delete irrelevant folder
Apr 20, 2024
a09f34b
changelog
Apr 20, 2024
1b375f8
create submodule for polars
Apr 21, 2024
799532f
fix doctests
Apr 21, 2024
dbce4b9
fix tests; add polars to documentation
Apr 21, 2024
1c642e6
fix tests; add polars to documentation
Apr 21, 2024
407d21b
import janitor.polars
Apr 21, 2024
aedfc65
control docs output for polars submodule
Apr 21, 2024
db9b486
exclude functions in docs rendering
Apr 21, 2024
6a91e67
exclude functions in docs rendering
Apr 21, 2024
7a88078
show_submodules=true
Apr 21, 2024
6d7885e
fix docstring rendering for polars
Apr 21, 2024
944fa02
Expression -> expression
Apr 21, 2024
b9aefaa
Merge dev into samukweku/polars_clean_names
ericmjl Apr 23, 2024
e9c370a
rename functions.py
Apr 23, 2024
ee66d2a
pivot_longer implemented for polars
Apr 29, 2024
959b082
changelog
Apr 30, 2024
3177503
keep changes related only to pivot_longer
Apr 30, 2024
ee899b2
pd -> pl
Apr 30, 2024
8ea9b71
pd -> pl
Apr 30, 2024
d12ae1a
df.pivot_longer -> df.janitor.pivot_longer
Apr 30, 2024
652f3e3
df.pivot_longer -> df.janitor.pivot_longer
Apr 30, 2024
9b9c1a9
pd -> pl
Apr 30, 2024
69c273f
pd -> pl
Apr 30, 2024
b3391e8
add >>> df
Apr 30, 2024
4ffaac5
add >>> df
Apr 30, 2024
1de57bb
keep changes related only to polars pivot_longer
Apr 30, 2024
8097bb4
code update
May 3, 2024
1bcd1b4
names_transform if there is not dot value
May 4, 2024
a492b85
add support for lazyframe
May 4, 2024
193fdc7
fix column order in docs
May 4, 2024
cd5baeb
fix column order in docs
May 4, 2024
1ba2650
fix column order in docs
May 4, 2024
908d71d
fix column order in docs
May 4, 2024
0cc6a93
separate namespaces for lazyframe and eager dataframe
May 5, 2024
0d41284
separate namespaces for lazyframe and eager dataframe
May 5, 2024
e8e6baf
improve perf if no dot_value and values_to is a str
May 5, 2024
2886ea1
fix docs
May 6, 2024
beec9e2
handle deprecation warning for pd.unique
May 6, 2024
a8d97ff
Merge dev into samukweku/polars_pivot_longer
ericmjl May 6, 2024
302a39b
Merge branch 'dev' into samukweku/polars_pivot_longer
samukweku May 10, 2024
7e4cc00
added pivot_longer_spec
May 12, 2024
a2df26f
update docs
May 12, 2024
3635639
update docs
May 12, 2024
d799ef5
update docs
May 12, 2024
198886d
update docs
May 12, 2024
c8a89eb
update docs
May 12, 2024
f733cfd
update docs
May 12, 2024
765837f
update docs
May 12, 2024
0fbe05a
update docs
May 12, 2024
b51f803
reduce repetitiveness in function calls
May 12, 2024
4798b09
update docs
May 12, 2024
30a290b
update docs
May 12, 2024
0755e91
update docs
May 13, 2024
2eeba86
update docs
May 13, 2024
1efdc96
refactor code - create spec then unpivot
May 13, 2024
41f450d
refactor code - create spec then unpivot
May 13, 2024
e03c11a
update docs
May 13, 2024
ba6df7d
update docs
May 13, 2024
0aac3cb
update docs
May 13, 2024
7f689a7
update docs
May 13, 2024
56bca98
add check for spec dataframe
May 17, 2024
65ee3b8
fix doc fail
May 17, 2024
7a69b54
fix doc fail
May 17, 2024
3365f01
exclude non polars code
May 17, 2024
04a8444
Merge dev into samukweku/polars_pivot_longer
ericmjl May 19, 2024
2e67269
convert_excel_date and convert_matlab_date for polars
May 21, 2024
4ac9f7e
convert_excel_date and convert_matlab_date for polars
May 21, 2024
a0994b9
update docs
May 21, 2024
850d41f
update docs - convert_matlab_date
May 21, 2024
41bcbc7
update docs - convert_matlab_date
May 21, 2024
a00a5ac
Merge dev into samukweku/convert_excel_date_polars
ericmjl May 23, 2024
63a623b
Merge dev into samukweku/convert_excel_date_polars
ericmjl May 27, 2024
e9284f6
Merge branch 'dev' into samukweku/convert_excel_date_polars
samukweku Jun 3, 2024
3b63a9c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2024
43d742e
fix conflicts
Jun 3, 2024
24dcc36
Merge dev into samukweku/convert_excel_date_polars
ericmjl Jun 4, 2024
f36fe60
Merge branch 'dev' into samukweku/convert_excel_date_polars
samukweku Jun 11, 2024
d233c3d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 11, 2024
98f530e
fix docs
Jun 11, 2024
ee38161
fix docs
Jun 11, 2024
5fda208
Merge dev into samukweku/convert_excel_date_polars
ericmjl Jun 12, 2024
7f4de97
Merge branch 'dev' into samukweku/convert_excel_date_polars
samukweku Jun 14, 2024
77601a2
Merge dev into samukweku/convert_excel_date_polars
ericmjl Jun 18, 2024
01954f7
Merge dev into samukweku/convert_excel_date_polars
ericmjl Jun 18, 2024
7048457
Merge dev into samukweku/convert_excel_date_polars
ericmjl Jun 18, 2024
94c90bf
Merge dev into samukweku/convert_excel_date_polars
ericmjl Jun 19, 2024
aa873e2
fix tests
Jun 21, 2024
fed8bb0
fix tests
Jun 21, 2024
f141632
fix tests
Jun 21, 2024
1e28e22
fix docs
Jun 21, 2024
7b941b3
changelog
Jun 21, 2024
f5f219e
Merge dev into samukweku/convert_excel_date_polars
ericmjl Jun 28, 2024
ce32626
Merge dev into samukweku/convert_excel_date_polars
ericmjl Jun 28, 2024
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
Prev Previous commit
Next Next commit
create submodule for polars
samuel.oranyeli committed Apr 21, 2024
commit 1b375f84e4d5cba9e5955d6ade247f767c10c4d3
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

## [Unreleased]
- [ENH] Add `make_clean_names` function which works on a pandas Index/Series, a Polars Expression, or a python string/list. Issue #1343
- [ENH] Added a `clean_names` method for polars - it can be used to clean the column names, or clean column values . Issue #1343

## [v0.27.0] - 2024-03-21

2 changes: 0 additions & 2 deletions janitor/functions/__init__.py
Original file line number Diff line number Diff line change
@@ -81,7 +81,6 @@
col,
get_columns,
get_index_labels,
make_clean_names,
patterns,
unionize_dataframe_categories,
)
@@ -130,7 +129,6 @@
"join_apply",
"label_encode",
"limit_column_characters",
"make_clean_names",
"min_max_scale",
"move",
"pivot_longer",
125 changes: 115 additions & 10 deletions janitor/functions/clean_names.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""Functions for cleaning columns/index names and/or column values."""

from __future__ import annotations

import unicodedata
from typing import Optional, Union

import pandas as pd
import pandas_flavor as pf
from pandas.api.types import is_scalar

from janitor.functions.utils import (
get_index_labels,
make_clean_names,
)
from janitor.errors import JanitorError
from janitor.functions.utils import _is_str_or_cat, get_index_labels
from janitor.utils import deprecated_alias


@@ -120,15 +121,14 @@ def clean_names(
column_names = [column_names]
df = df.copy()
for column_name in column_names:
df[column_name] = make_clean_names(
df[column_name] = _clean_names(
obj=df[column_name],
enforce_string=enforce_string,
case_type=case_type,
remove_special=remove_special,
strip_accents=strip_accents,
strip_underscores=strip_underscores,
truncate_limit=truncate_limit,
object_type="pandas",
)
return df

@@ -141,31 +141,136 @@ def clean_names(
for number in range(target_axis.nlevels)
]
target_axis = [
make_clean_names(
_clean_names(
obj=obj,
enforce_string=enforce_string,
case_type=case_type,
remove_special=remove_special,
strip_accents=strip_accents,
strip_underscores=strip_underscores,
truncate_limit=truncate_limit,
object_type="pandas",
)
for obj in target_axis
]
else:
target_axis = make_clean_names(
target_axis = _clean_names(
obj=target_axis,
enforce_string=enforce_string,
case_type=case_type,
remove_special=remove_special,
strip_accents=strip_accents,
strip_underscores=strip_underscores,
truncate_limit=truncate_limit,
object_type="pandas",
)
# Store the original column names, if enabled by user
if preserve_original_labels:
df.__dict__["original_labels"] = getattr(df, axis)
setattr(df, axis, target_axis)
return df


def _clean_names(
obj: Union[pd.Index, pd.Series],
strip_underscores: Optional[Union[str, bool]] = None,
case_type: str = "lower",
remove_special: bool = False,
strip_accents: bool = False,
enforce_string: bool = False,
truncate_limit: int = None,
) -> Union[pd.Index, pd.Series]:
"""
Generic function to clean labels in a pandas object.
"""
if enforce_string and not (_is_str_or_cat(obj)):
obj = obj.astype(str)
obj = _change_case(obj=obj, case_type=case_type)
obj = _normalize_1(obj=obj)
if remove_special:
obj = obj.str.replace(
pat="[^A-Za-z_\\d]", repl="", regex=True
).str.strip()
if strip_accents:
obj = _strip_accents(obj=obj)
obj = obj.str.replace(pat="_+", repl="_", regex=True)
obj = _strip_underscores_func(
obj,
strip_underscores=strip_underscores,
)
if truncate_limit:
obj = obj.str[:truncate_limit]
return obj


def _change_case(
obj: Union[pd.Index, pd.Series],
case_type: str,
) -> Union[pd.Index, pd.Series]:
"""Change case of labels in obj."""
case_types = {"preserve", "upper", "lower", "snake"}
case_type = case_type.lower()
if case_type not in case_types:
raise JanitorError(f"case_type must be one of: {case_types}")

if case_type == "preserve":
return obj
if case_type == "upper":
return obj.str.upper()
if case_type == "lower":
return obj.str.lower()
# Implementation taken from: https://gist.github.com/jaytaylor/3660565
# by @jtaylor
return (
obj.str.replace(pat=r"(.)([A-Z][a-z]+)", repl=r"\1_\2", regex=True)
.str.replace(pat=r"([a-z0-9])([A-Z])", repl=r"\1_\2", regex=True)
.str.lower()
)


def _normalize_1(
obj: Union[pd.Index, pd.Series]
) -> Union[pd.Index, pd.Series]:
"""Perform normalization of labels in obj."""
FIXES = [(r"[ /:,?()\.-]", "_"), (r"['’]", ""), (r"[\xa0]", "_")]
for search, replace in FIXES:
obj = obj.str.replace(pat=search, repl=replace, regex=True)

return obj


def _strip_accents(
obj: Union[pd.Index, pd.Series],
) -> Union[pd.Index, pd.Series]:
"""Remove accents from a label.

Inspired from [StackOverflow][so].

[so]: https://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-in-a-python-unicode-strin
""" # noqa: E501
return obj.map(
lambda f: "".join(
[
letter
for letter in unicodedata.normalize("NFD", str(f))
if not unicodedata.combining(letter)
]
)
)


def _strip_underscores_func(
obj: Union[pd.Index, pd.Series],
strip_underscores: Union[str, bool] = None,
) -> Union[pd.Index, pd.Series]:
"""Strip underscores."""
underscore_options = {None, "left", "right", "both", "l", "r", True}
if strip_underscores not in underscore_options:
raise JanitorError(
f"strip_underscores must be one of: {underscore_options}"
)
if strip_underscores in {"left", "l"}:
return obj.str.lstrip("_")
if strip_underscores in {"right", "r"}:
return obj.str.rstrip("_")
if strip_underscores in {True, "both"}:
return obj.str.strip("_")
return obj
Loading