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

enh: add prefix to generate_temporary_column_names #1294

Open
MarcoGorelli opened this issue Oct 31, 2024 · 1 comment
Open

enh: add prefix to generate_temporary_column_names #1294

MarcoGorelli opened this issue Oct 31, 2024 · 1 comment
Labels
enhancement New feature or request needs discussion

Comments

@MarcoGorelli
Copy link
Member

This should generate a unique token which is not present in any of [f'{prefix}{col}' for col in df.columns]

@MarcoGorelli MarcoGorelli added enhancement New feature or request high priority labels Oct 31, 2024
@FBruzzesi
Copy link
Member

I think the plotly use case is slightly different. I currently wrote the following helper function:

def _generate_temporary_column_name(n_bytes: int, columns: list[str]) -> str:
    """Wraps of Narwhals generate_temporary_column_name to generate a token
    which is guaranteed to not be in columns, nor in [col + token for col in columns]
    """
    counter = 0
    while True:
        # This is guaranteed to not be in columns by Narwhals
        token = nw.generate_temporary_column_name(n_bytes, columns=columns)
        if token not in {f"{c}{token}" for c in columns}:
            return token

        counter += 1
        if counter > 100:
            msg = (
                "Internal Error: Narwhals was not able to generate a column name with "
                f"{n_bytes=} and not in {columns}"
            )
            raise AssertionError(msg)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs discussion
Projects
None yet
Development

No branches or pull requests

2 participants