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

Faster categorical column names selection #1

Merged
merged 2 commits into from
Nov 9, 2021

Conversation

Neronuser
Copy link
Owner

Change slow and redundant dataframe query by select_dtypes into a dataframe.dtypes list comprehension

import pandas as pd
import random
temp_df = pd.DataFrame({str(a):[random.random() for _ in range(100)] for a in range(200)})
temp_df[["100", "121", "115"]] = temp_df[["100", "121", "115"]].astype("category")

%timeit temp_df.select_dtypes("category").columns->23.1 ms ± 1.04 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

VS

%timeit [col for col, t in zip(temp_df.columns, temp_df.dtypes) if isinstance(t, CategoricalDtype)] ->167 µs ± 16.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Change slow and redundant dataframe query by select_dtypes into a dataframe.dtypes list comprehension
@Neronuser Neronuser merged commit 20ee631 into master Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant