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

sparse=True option for from_dataframe and from_series #3210

Merged
merged 5 commits into from
Aug 27, 2019

Conversation

shoyer
Copy link
Member

@shoyer shoyer commented Aug 13, 2019

Fixes #3206

Example usage:

In [3]: import pandas as pd
   ...: import numpy as np
   ...: import xarray
   ...: df = pd.DataFrame({
   ...:     'w': range(10),
   ...:     'x': list('abcdefghij'),
   ...:     'y': np.arange(0, 100, 10),
   ...:     'z': np.ones(10),
   ...: }).set_index(['w', 'x', 'y'])
   ...:

In [4]: ds = xarray.Dataset.from_dataframe(df, sparse=True)

In [5]: ds.z.data
Out[5]: <COO: shape=(10, 10, 10), dtype=float64, nnz=10, fill_value=nan>

Fixes pydata#3206

Example usage:

    In [3]: import pandas as pd
       ...: import numpy as np
       ...: import xarray
       ...: df = pd.DataFrame({
       ...:     'w': range(10),
       ...:     'x': list('abcdefghij'),
       ...:     'y': np.arange(0, 100, 10),
       ...:     'z': np.ones(10),
       ...: }).set_index(['w', 'x', 'y'])
       ...:

    In [4]: ds = xarray.Dataset.from_dataframe(df, sparse=True)

    In [5]: ds.z.data
    Out[5]: <COO: shape=(10, 10, 10), dtype=float64, nnz=10, fill_value=nan>
@max-sixty
Copy link
Collaborator

Amazing! This is going to allow way more usage of xarray for sparse representations of data currently confined to pandas

@shoyer
Copy link
Member Author

shoyer commented Aug 15, 2019

It would be great if someone could review this. Maybe @crusaderky ?

xarray/core/dataarray.py Show resolved Hide resolved
xarray/core/dataarray.py Outdated Show resolved Hide resolved
xarray/core/dataset.py Outdated Show resolved Hide resolved
xarray/core/dataset.py Show resolved Hide resolved
xarray/core/dataset.py Show resolved Hide resolved
xarray/tests/test_dataarray.py Outdated Show resolved Hide resolved
xarray/tests/test_dataarray.py Outdated Show resolved Hide resolved
xarray/tests/test_dataset.py Outdated Show resolved Hide resolved
Copy link
Member Author

@shoyer shoyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the review!

xarray/core/dataarray.py Outdated Show resolved Hide resolved
xarray/core/dataset.py Outdated Show resolved Hide resolved
xarray/core/dataset.py Show resolved Hide resolved
xarray/core/dataset.py Show resolved Hide resolved
xarray/tests/test_dataarray.py Outdated Show resolved Hide resolved
xarray/tests/test_dataarray.py Outdated Show resolved Hide resolved
@shoyer
Copy link
Member Author

shoyer commented Aug 27, 2019

@crusaderky any other comments?

temp_name = "__temporary_name"
df = pd.DataFrame({temp_name: series})
ds = Dataset.from_dataframe(df, sparse=sparse)
result = cast(DataArray, ds[temp_name])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I can get rid of the cast by changing Dataset.__getitem__. Let's merge this one first though.

Copy link
Contributor

@crusaderky crusaderky Aug 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... or not. Unlike @functools.single_dispatch, @typing.overload does not allow for a more specific signature followed by a more generic one :(

    @overload
    def __getitem__(self, key: Mapping) -> "Dataset":
        ...

    @overload
    def __getitem__(self, key: Hashable) -> "DataArray":
        ...

    @overload
    def __getitem__(self, key: Any) -> "Dataset":
        ...

    def __getitem__(self, key):

mypy complains:


xarray/core/dataset.py:1218: error: Overloaded function signatures 1 and 2 overlap with incompatible return types
xarray/core/dataset.py:1222: error: Overloaded function signatures 2 and 3 overlap with incompatible return types

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I tried something very similar. This is why I wrote the "TODO" note above mentioning python/mypy#7328.

@crusaderky
Copy link
Contributor

Looks good to me

@crusaderky crusaderky merged commit 3f4b025 into pydata:master Aug 27, 2019
@max-sixty
Copy link
Collaborator

Great!

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.

DataFrame with MultiIndex -> xarray with sparse array From pandas to xarray without blowing up memory
3 participants