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

[Coming soon] Support Polars objects for cache hashing #10347

Open
2 tasks done
BartSchuurmans opened this issue Feb 5, 2025 · 2 comments · May be fixed by #10408
Open
2 tasks done

[Coming soon] Support Polars objects for cache hashing #10347

BartSchuurmans opened this issue Feb 5, 2025 · 2 comments · May be fixed by #10408
Labels
feature:cache Related to st.cache_data and st.cache_resource feature:cache-hash-func type:enhancement Requests for feature enhancements or new features

Comments

@BartSchuurmans
Copy link

Checklist

  • I have searched the existing issues for similar feature requests.
  • I added a descriptive title and summary to this issue.

Summary

When passing a Polars dataframe to a function that is decorated with @st.cache_data, you get the following error:

UnhashableParamError: Cannot hash argument 'my_df' (of type polars.dataframe.frame.DataFrame) in 'my_func'.

Follow-up of #5088 (comment)

Why?

I want to be able to cache functions that have a Polars dataframe as input.

How?

There is already support for Pandas dataframes:

elif type_util.is_type(obj, "pandas.core.frame.DataFrame"):
import pandas as pd
obj = cast(pd.DataFrame, obj)
self.update(h, obj.shape)
if len(obj) >= _PANDAS_ROWS_LARGE:
obj = obj.sample(n=_PANDAS_SAMPLE_SIZE, random_state=0)
try:
column_hash_bytes = self.to_bytes(
pd.util.hash_pandas_object(obj.dtypes)
)
self.update(h, column_hash_bytes)
values_hash_bytes = self.to_bytes(pd.util.hash_pandas_object(obj))
self.update(h, values_hash_bytes)
return h.digest()
except TypeError:
# Use pickle if pandas cannot hash the object for example if
# it contains unhashable objects.
return b"%s" % pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)

Support for Polars dataframes could be implemented the same way.
Alternative: Use https://github.com/narwhals-dev/narwhals for a dataframe-agnostic implementation.

Additional Context

No response

@BartSchuurmans BartSchuurmans added the type:enhancement Requests for feature enhancements or new features label Feb 5, 2025
Copy link

github-actions bot commented Feb 5, 2025

To help Streamlit prioritize this feature, react with a 👍 (thumbs up emoji) to the initial post.

Your vote helps us identify which enhancements matter most to our users.

Visits

@lukasmasuch lukasmasuch added the feature:cache Related to st.cache_data and st.cache_resource label Feb 5, 2025
@MarcoGorelli
Copy link

Hey

Alternative: Use https://github.com/narwhals-dev/narwhals for a dataframe-agnostic implementation.

I just wanted to note that Streamlit depends on Altair, which in turn already depends on Narwhals. So, Streamlit would be able to use Narwhals functionality with zero cost to its users as they all already have it installed because of Altair

@kajarenc kajarenc linked a pull request Feb 21, 2025 that will close this issue
@sfc-gh-lmasuch sfc-gh-lmasuch changed the title Support Polars objects for cache hashing [Coming soon] Support Polars objects for cache hashing Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:cache Related to st.cache_data and st.cache_resource feature:cache-hash-func type:enhancement Requests for feature enhancements or new features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants