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

0.1.1 release #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Fix issue when splitting and combining datasets with mixed or non-str…
…ing column and index names
ejolly committed May 2, 2022
commit c5eb978d359ebc3b4a835c40a81fb7eef7d363af
6 changes: 6 additions & 0 deletions neighbors/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -129,17 +129,23 @@ def test_create_sparse_mask(simulate_wide_data):
expected_items = int(simulate_wide_data.shape[1] * (1 - 0.10))
assert mask.shape == simulate_wide_data.shape
assert all(mask.sum(1) == expected_items)
assert mask.index.name == simulate_wide_data.index.name
assert mask.columns.name == simulate_wide_data.columns.name

mask = create_sparse_mask(simulate_wide_data, n_mask_items=19)
assert mask.shape == simulate_wide_data.shape
expected_items = int(simulate_wide_data.shape[1] - 19)
assert all(mask.sum(1) == expected_items)
assert mask.index.name == simulate_wide_data.index.name
assert mask.columns.name == simulate_wide_data.columns.name

masked_data = simulate_wide_data[mask]
assert isinstance(masked_data, pd.DataFrame)
assert masked_data.shape == simulate_wide_data.shape
assert ~simulate_wide_data.isnull().any().any()
assert masked_data.isnull().any().any()
assert mask.index.name == simulate_wide_data.index.name
assert mask.columns.name == simulate_wide_data.columns.name


def test_flatten_dataframe(simulate_wide_data):
14 changes: 12 additions & 2 deletions neighbors/utils.py
Original file line number Diff line number Diff line change
@@ -206,7 +206,10 @@ def create_sparse_mask(data, n_mask_items=0.2, random_state=None):
for _ in range(data.shape[0])
]
)
return pd.DataFrame(mask, index=data.index, columns=data.columns)
out = pd.DataFrame(mask, index=data.index, columns=data.columns)
out.index.name = data.index.name
out.columns.name = data.columns.name
return out


def flatten_dataframe(data: pd.DataFrame) -> list:
@@ -223,7 +226,14 @@ def flatten_dataframe(data: pd.DataFrame) -> list:
if not isinstance(data, pd.DataFrame):
raise TypeError("input must be a pandas dataframe")

out = zip(product(data.index, data.columns), data.to_numpy().ravel())
# Force index and columns names to be strings so that unflatten can more reliably
# auto-infer index and column names when they aren't passed, i.e doesn't add
# additional columns or rows with numeric versions of the same name
out = data.copy()
out.index = list(map(str, out.index))
out.columns = list(map(str, out.columns))

out = zip(product(out.index, out.columns), out.to_numpy().ravel())
return np.array([(elem[0][0], elem[0][1], elem[1]) for elem in out])


6 changes: 5 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -3,4 +3,8 @@ black
mkdocs
mkdocs-material
mkdocstrings
mkdocs-jupyter
mkdocs-jupyter
pycodestyle
pytest
pytest-sugar
pytest-xdist