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

[dtype] Make learn_rounding_digits() work with new pandas dtypes #859

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 1 addition & 2 deletions rdt/transformers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ def learn_rounding_digits(data):
"""
# check if data has any decimals
name = data.name
data = np.array(data)
roundable_data = data[~(np.isinf(data) | pd.isna(data))]
roundable_data = data[~(np.isinf(data.astype(float)) | pd.isna(data))]

# Doesn't contain numbers
if len(roundable_data) == 0:
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/transformers/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,29 @@ def test_learn_rounding_digits_all_missing_value_replacements():
output = learn_rounding_digits(data)

assert output is None


def test_learn_rounding_digits_new_pandas_dtypes():
"""Test that ``learn_rounding_digits`` supports the new pandas dtypes."""
# Setup
data = pd.DataFrame({
'Int8': pd.Series([1, 2, -3, pd.NA, None, pd.NA], dtype='Int8'),
'Int16': pd.Series([1, 2, -3, pd.NA, None, pd.NA], dtype='Int16'),
'Int32': pd.Series([1, 2, -3, pd.NA, None, pd.NA], dtype='Int32'),
'Int64': pd.Series([1, 2, -3, pd.NA, None, pd.NA], dtype='Int64'),
'Float32': pd.Series([1.12, 2.23, 3.33, pd.NA, None, pd.NA], dtype='Float32'),
'Float64': pd.Series([1.1234, 2.2345, 3.323, pd.NA, None, pd.NA], dtype='Float64'),
})
expected_output = {
'Int8': 0,
'Int16': 0,
'Int32': 0,
'Int64': 0,
'Float32': 2,
'Float64': 4,
}

# Run and Assert
for column in data.columns:
output = learn_rounding_digits(data[column])
assert output == expected_output[column]
Loading