Skip to content

Construct Array from DataFrame with meta - #7486

Closed
jakirkham wants to merge 1 commit into
dask:mainfrom
jakirkham:fix_meta_values
Closed

jakirkham wants to merge 1 commit into
dask:mainfrom
jakirkham:fix_meta_values

Conversation

@jakirkham

@jakirkham jakirkham commented Mar 30, 2021 •

Copy link
Copy Markdown
Member

Currently the _meta of values doesn't get correctly updated when using cuDF DataFrames instead of Pandas ones. Thus the _meta winds up being a NumPy array instead of a CuPy array.

It seems the meta handling code in Dask DataFrame hadn't been updated since Dask Array got its own meta. So simply update the Array construction code to use meta.

cc @rjzamora @kkraus14

  • Closes #xxxx
  • Tests added / passed
  • Passes black dask / flake8 dask

Comment thread dask/dataframe/core.py Resolved
@jakirkham
jakirkham force-pushed the fix_meta_values branch 2 times, most recently from 1690d01 to 040dbda Compare March 30, 2021 18:01
Comment thread dask/dataframe/core.py Resolved
Currently the `_meta` of `values` doesn't get correctly updated when
using cuDF DataFrames instead of Pandas ones. Thus the `_meta`
winds up being a NumPy array instead of a CuPy array.

It seems the `meta` handling code in Dask DataFrame hadn't been updated
since Dask Array got its own `meta`. So simply update the `Array`
construction code to use `meta`.
@jakirkham jakirkham changed the title Patch the _meta of values in Dask Dataframe Construct Array from DataFrame with meta Mar 30, 2021
@jakirkham

Copy link
Copy Markdown
Member Author

Interesting looks like this causes some issues with record arrays

_______________________________ test_to_records ________________________________

    def test_to_records():
        pytest.importorskip("dask.array")
        from dask.array.utils import assert_eq
    
        df = pd.DataFrame(
            {"x": ["a", "b", "c", "d"], "y": [2, 3, 4, 5]},
            index=pd.Index([1.0, 2.0, 3.0, 4.0], name="ind"),
        )
        ddf = dd.from_pandas(df, 2)
    
>       assert_eq(df.to_records(), ddf.to_records())

dask/dataframe/io/tests/test_io.py:532: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

a = rec.array([(1., 'a', 2), (2., 'b', 3), (3., 'c', 4), (4., 'd', 5)],
          dtype=[('ind', '<f8'), ('x', 'O'), ('y', '<i8')])
b = array([(1., 'a', 2), (2., 'b', 3), (3., 'c', 4), (4., 'd', 5)],
      dtype=(numpy.record, [('ind', '<f8'), ('x', 'O'), ('y', '<i8')]))
check_shape = True, check_graph = True, check_meta = True, check_chunks = True
kwargs = {}
a_original = rec.array([(1., 'a', 2), (2., 'b', 3), (3., 'c', 4), (4., 'd', 5)],
          dtype=[('ind', '<f8'), ('x', 'O'), ('y', '<i8')])
b_original = dask.array<to_records, shape=(nan,), dtype=(numpy.record, [('ind', '<f8'), ('x', 'O'), ('y', '<i8')]),

https://github.com/dask/dask/pull/7486/checks?check_run_id=2230484933

@jakirkham

Copy link
Copy Markdown
Member Author

Actually am a little surprised that test passes on main. These already generate different types. Here's an example

In [1]: import numpy as np
   ...: import pandas as pd
   ...: import dask.dataframe as dd

In [2]: df = pd.DataFrame(
   ...:     {"x": ["a", "b", "c", "d"], "y": [2, 3, 4, 5]},
   ...:     index=pd.Index([1.0, 2.0, 3.0, 4.0], name="ind"),
   ...: )
   ...: ddf = dd.from_pandas(df, 2)

In [3]: df.to_records()
Out[3]: 
rec.array([(1., 'a', 2), (2., 'b', 3), (3., 'c', 4), (4., 'd', 5)],
          dtype=[('ind', '<f8'), ('x', 'O'), ('y', '<i8')])

In [4]: ddf.to_records().compute()
Out[4]: 
array([(1., 'a', 2), (2., 'b', 3), (3., 'c', 4), (4., 'd', 5)],
      dtype=(numpy.record, [('ind', '<f8'), ('x', 'O'), ('y', '<i8')]))

@jsignell

Copy link
Copy Markdown
Member

I think assert_eq for array is missing an overall computed type check. I will open a separate PR to see what that would look like.

@gjoseph92

Copy link
Copy Markdown
Collaborator

@jakirkham you might be interested in #7399, where I tried the same thing. As I mentioned there, there are some other issues that emerge if you allow any array-like object to be the _meta in an Array.

@jakirkham

Copy link
Copy Markdown
Member Author

Oops totally missed that 😅

Thanks for working on this Gabe! Should we close this one out to consolidate work in that PR or vice versa? No strong feelings either way 🙂

@gjoseph92

Copy link
Copy Markdown
Collaborator

I don't have a preference either. If we want to take the finalize-preserves-subclasses approach I used to get tests passing, let's consolidate on #7399. But if we want to take the check_type=False approach in #7491, then your PR here has a smaller diff.

Worth noting that either way, this comment applies: #7399 (comment). Once we merge this, users doing .values on a DataFrame containing ExtensionArrays are not gonna have a good time. ExtensionArrays are basically nonfunctional right now as Array chunks; you can't do any reductions or elementwise operations on them. Whereas right now, .vaules just fails, so it's only one possible error instead of many 😉 .

@jsignell

jsignell commented Apr 1, 2021

Copy link
Copy Markdown
Member

@jakirkham @gjoseph92 and I are planning to meet up to talk through a plan tomorrow at 1pm eastern. Please feel free to join or suggest a different time if you want to be in on that :)

@jakirkham

Copy link
Copy Markdown
Member Author

Sounds good. Feel free to send me an invite 🙂

@jakirkham

Copy link
Copy Markdown
Member Author

cc @douglasdavis (in case this discussion is of interest to you as well 🙂)

@jakirkham

Copy link
Copy Markdown
Member Author

Closing so we can consolidate work in PR ( #7399 )

@jakirkham jakirkham closed this Apr 2, 2021
@jakirkham
jakirkham deleted the fix_meta_values branch April 2, 2021 18:50
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.

5 participants