You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is especially important to me because in this case one row group is 10GB when stored naively. The data is somewhat unreadable in its current state on modest machines. I also suspect that I'm spending almost all of my time just creating and then destroying Python objects.
Thanks @mrocklin. This sounds sensible. Something like use_nullable_dtypes=True + telling pandas to use string[pyarrow] as the default string type should do the trick.
Was the dataset originally written with extension string dtypes? Ideally I'd like to see us respect roundtripping
Was the dataset originally written with extension string dtypes
This one was not. This one is maintained by nyc tlc and I don't think that they (yet!) use Dask for data prep.
Something like use_nullable_dtypes=True + telling pandas to use string[pyarrow] as the default string type should do the trick
I would hope so, but I'm not sure that that's the case. In this case Dask/Pandas/Arrow aren't sure that the object dtypes are strings, they might be some other serialized object. In this case I'd like to explicitly tell the system "nope, this is definitely text data, please act accordingly".
Was the dataset originally written with extension string dtypes
This one was not.
If I remember correctly (which probably don't), this may mean there is no way to do anything more efficient than using astype("string[pyarrow]") after the read. I was looking into this for #9476, but got distracted by other things.
I've got to believe that when Arrow reads this dataset they are able to see "yup, this is text" and read it in appropriately. Spark also. @jorisvandenbossche any suggestions?
@mrocklin are you able to set pd.option_context("string_storage", "pyarrow") on the workers before reading the data? And maybe doing it just on the client would be sufficient for checking the meta.
I'm not sure if that has a trip from arrow -> python objects -> arrow, but hopefully it's zero-copy from arrow memory to pandas.
I didn't test in combination with dask, but just using pandas, the following seems to work:
import pyarrow as pa
import pyarrow.parquet as pq
pq.write_table(pa.table({"col": ['a', 'b', 'c']}), "test_string.parquet")
In [5]: pd.read_parquet("test_string.parquet").dtypes
Out[5]:
col object
dtype: object
In [6]: pd.read_parquet("test_string.parquet", use_nullable_dtypes=True).dtypes
Out[6]:
col string
dtype: object
In [7]: pd.options.mode.string_storage = "pyarrow"
In [8]: pd.read_parquet("test_string.parquet", use_nullable_dtypes=True).dtypes
Out[8]:
col string
dtype: object
In [9]: pd.read_parquet("test_string.parquet", use_nullable_dtypes=True).dtypes[0]
Out[9]: string[pyarrow]
So it might be some issue in how dask is propagating those keywords to pandas?
I was suspicious that this might be due to PyArrow writing metadata differently than however this data in the wild was written so I read the dataset with Pandas. I received the following:
My current rough understanding is, in general, string[pyarrow] performance is greater than, or equal to, string[python] -- does that seem like a good rule of thumb?
Yeah definitely
# pyarrow.__version__ == 8.0.1
In [1]: ser = pd.Series(["a"] * 1_000_000, dtype="string[pyarrow]")
In [2]: ser_pyarrow = pd.Series(["a"] * 1_000_000, dtype="string[pyarrow]")
In [4]: ser_python = pd.Series(["a"] * 1_000_000, dtype="string[python]")
In [5]: %timeit ser_pyarrow.str.replace("a", "A")
16.8 ms ± 264 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [6]: %timeit ser_python.str.replace("a", "A")
236 ms ± 1.51 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
And if pyarrow backed data needs to fallback on a slower path, the user should be warned
In [8]: ser_pyarrow.str.replace("a", "A", case=False)
<ipython-input-8-9fcf10dba08a>:1: PerformanceWarning: Falling back on a non-pyarrow code path which may decrease performance.
ser_pyarrow.str.replace("a", "A", case=False)
Out[8]:
0 A
1 A
2 A
3 A
4 A
..
999995 A
999996 A
999997 A
999998 A
999999 A
Length: 1000000, dtype: string
FYI for folks here, in #9719 I'm suggesting we extend use_nullable_dtypes to support "pandas" and "pyarrow" (similar-ish to pandasnullable_backend config option) to allow for easy reading of parquet datasets into pyarrow-backed extension dtypes.
With that PR, @mrocklin's original example looks like:
Note that string in the above are actually still pyarrow-backed
In [4]: df.wav_match_flag.dtypeOut[4]: string[pyarrow]
As a side not, @mroeschke I've been confused a few times where pyarrow-backed strings sometimes appear as "string[pyarrow]" and sometimes just "string". I definitely could be missing some context, but why not just always have "string[pyarrow]" displayed?
The name makes sense from a pandas internals perspective, but doesn't make much sense from a naive user perspective. Putting on my naive user hat, nullable dtypes are different from pyarrow strings.
Is it possible to make this default in some way with configuration? Or would the plan be for us to try this manually for a while, and then switch the default in the read_parquet definition sometime?
The name makes sense from a pandas internals perspective, but doesn't make much sense from a naive user perspective. Putting on my naive user hat, nullable dtypes are different from pyarrow strings.
Hm I'm not sure I agree. I view the pyarrow-backed dtypes (e.g. int64[pyarrow]) as nullable in the sense that they have a notion of missing data and work with pd.NA. Totally agree they're different than the numpy-backed nullable dtypes in pandas (e.g. Int64) but I think "nullable" still applies to both. IIUC pandas is also using the term nullable in this context in user-facing APIs.
Is it possible to make this default in some way with configuration? Or would the plan be for us to try this manually for a while, and then switch the default in the read_parquet definition sometime?
I think both. I like the manual keyword here while we try things out, but long-term I think we'll want to add a dask.dataframe.nullable_backend (or some other name) config option that puts Dask into "pyarrow mode". I think there's a smooth path here for (at some point) making pyarrow mode configurable / default.
The term nullable to me means "a data type that supports an NA or missing value". I agree that that's consistent with pyarrow strings and other pyarrow types, but it's not the first thing that comes to mind.
If I'm a user and I'm saying "gosh, I want those fast arrow data types. How do I get those?" If I see "nullable" I'm not going to immediately say "Yes! that's clearly the fast arrow thing that I want"
It feels a bit like putting bananas in the dairy section of a super market because they're white. Yes, they do fit. But no, it's not the term that I would normally search for them by.
Maybe that doesn't make sense though. I don't know.
Again, I care less if we're going to move this to be default.
Putting on my naive user hat, nullable dtypes are different from pyarrow strings.
This is really interesting feedback coming from the pandas side and would help market "why should I use pyarrow" in pandas too.
pandas (I) originally grouped the arrow types with the pandas (numpy based) nullable dtypes since they share the common and original motivation for the nullable dtypes - a dtype independent way to represent missing values. Hence why in pandas we also want to let to user choose arrow via nullable-related keywords e.g. use_nullable_dtypes
The arrow types also happen to generally be more performant too so maybe they should be associated with more performance based keywords? e.g. engine
As a side not, @mroeschke I've been confused a few times where pyarrow-backed strings sometimes appear as "string[pyarrow]" and sometimes just "string". I definitely could be missing some context, but why not just always have "string[pyarrow]" displayed?
@jrbourbeau I would consider this a bug. If it's easy to get a simple reproducer, it would be great to have as a pandas issue
If pandas is using the term nulalble for this kind of thing then I'm happy to back off from a Dask dataframe perspective. We generally just follow whatever the upstream library does. Please ignore my feedback within the context of this PR.
As a pandas user though, this does feel strange to me though. I understand how we got to "nullable" as a term here, but it doesn't feel right to me as someone who has just arrived at this situation.
It feels a bit like putting bananas in the dairy section of a super market because they're white. Yes, they do fit. But no, it's not the term that I would normally search for them by.
I feel like there's a banana split joke to be had here, I just can find it... That said, it's a fair point.
The arrow types also happen to generally be more performant too so maybe they should be associated with more performance based keywords? e.g. engine
@mroeschke how hardened is the relationship between "nullable" and the numpy- and pyarrow-backed extension dtypes in pandas? I know use_nullable_dtypes has been out since pandas=1.2.0, but that's the only user-facing API I'm aware of. Given the nullable_backend option hasn't been released yet (and is also going through a name change at the moment anyways), do you think there's another term that would fit user expectations more? engine, dtype_backend, and dtype_engine come to mind for me -- @mrocklin do you have a term that you like for this concept?
dtypes="arrow"? I don't really know enough context to have a good idea here.
Also, if we're ok moving the default around in the future then I care much less. It seems like arrow support is pretty good these days. I'm hopeful that the developers and experts will use it, see that it works well, and then we'll switch the defaults around. The devs and experts won't care much about the name.
how hardened is the relationship between "nullable" and the numpy- and pyarrow-backed extension dtypes in pandas?
As of 1.5 not really hardened. Their only association is just documenting that they both use pd.NA to (externally) represent missing values. I'll bring this topic up on our next community dev meeting on Wednesday
Activity
jrbourbeau commentedon Nov 7, 2022
Thanks @mrocklin. This sounds sensible. Something like
use_nullable_dtypes=True+ tellingpandasto usestring[pyarrow]as the default string type should do the trick.Was the dataset originally written with extension string dtypes? Ideally I'd like to see us respect roundtripping
mrocklin commentedon Nov 7, 2022
This one was not. This one is maintained by nyc tlc and I don't think that they (yet!) use Dask for data prep.
I would hope so, but I'm not sure that that's the case. In this case Dask/Pandas/Arrow aren't sure that the object dtypes are strings, they might be some other serialized object. In this case I'd like to explicitly tell the system "nope, this is definitely text data, please act accordingly".
ncclementi commentedon Nov 7, 2022
@jrbourbeau We have data written with pyarrow strings, python strings and categorical (3 different sets), that we can use to test this if needed.
rjzamora commentedon Nov 7, 2022
If I remember correctly (which probably don't), this may mean there is no way to do anything more efficient than using
astype("string[pyarrow]")after the read. I was looking into this for #9476, but got distracted by other things.mrocklin commentedon Nov 7, 2022
I've got to believe that when Arrow reads this dataset they are able to see "yup, this is text" and read it in appropriately. Spark also. @jorisvandenbossche any suggestions?
TomAugspurger commentedon Nov 7, 2022
I think that's right. See pandas-dev/pandas#42664 (comment)
@mrocklin are you able to set
pd.option_context("string_storage", "pyarrow")on the workers before reading the data? And maybe doing it just on the client would be sufficient for checking themeta.I'm not sure if that has a trip from arrow -> python objects -> arrow, but hopefully it's zero-copy from arrow memory to pandas.
mrocklin commentedon Nov 7, 2022
Still no dice on the problem above:
I think that the problem here is less about the
string[pyarrow]vsstring[python]distinction as it is about thestringvsobjectdistinction.jorisvandenbossche commentedon Nov 7, 2022
I didn't test in combination with dask, but just using pandas, the following seems to work:
So it might be some issue in how dask is propagating those keywords to pandas?
mrocklin commentedon Nov 7, 2022
I was suspicious that this might be due to PyArrow writing metadata differently than however this data in the wild was written so I read the dataset with Pandas. I received the following:
It looks like Pandas is properly getting the string dtype (🎉).
It's not clear to me that it's using pyarrow here for storage. Those storage sizes seem large to me given the values.
8 remaining items
mroeschke commentedon Dec 5, 2022
Yeah definitely
And if pyarrow backed data needs to fallback on a slower path, the user should be warned
jrbourbeau commentedon Dec 6, 2022
FYI for folks here, in #9719 I'm suggesting we extend
use_nullable_dtypesto support"pandas"and"pyarrow"(similar-ish topandasnullable_backendconfig option) to allow for easy reading of parquet datasets intopyarrow-backed extension dtypes.With that PR, @mrocklin's original example looks like:
Note that
stringin the above are actually stillpyarrow-backedAs a side not, @mroeschke I've been confused a few times where
pyarrow-backed strings sometimes appear as "string[pyarrow]" and sometimes just "string". I definitely could be missing some context, but why not just always have "string[pyarrow]" displayed?mrocklin commentedon Dec 6, 2022
I'm excited about this. Two questions:
mrocklin commentedon Dec 6, 2022
I care less about the name if there is a good path towards making this default
jrbourbeau commentedon Dec 7, 2022
Nice, glad to hear it
Hm I'm not sure I agree. I view the
pyarrow-backed dtypes (e.g.int64[pyarrow]) as nullable in the sense that they have a notion of missing data and work withpd.NA. Totally agree they're different than thenumpy-backed nullable dtypes inpandas(e.g.Int64) but I think "nullable" still applies to both. IIUCpandasis also using the term nullable in this context in user-facing APIs.I think both. I like the manual keyword here while we try things out, but long-term I think we'll want to add a
dask.dataframe.nullable_backend(or some other name) config option that puts Dask into "pyarrow mode". I think there's a smooth path here for (at some point) making pyarrow mode configurable / default.mrocklin commentedon Dec 7, 2022
The term nullable to me means "a data type that supports an NA or missing value". I agree that that's consistent with pyarrow strings and other pyarrow types, but it's not the first thing that comes to mind.
If I'm a user and I'm saying "gosh, I want those fast arrow data types. How do I get those?" If I see "nullable" I'm not going to immediately say "Yes! that's clearly the fast arrow thing that I want"
It feels a bit like putting bananas in the dairy section of a super market because they're white. Yes, they do fit. But no, it's not the term that I would normally search for them by.
Maybe that doesn't make sense though. I don't know.
Again, I care less if we're going to move this to be default.
mroeschke commentedon Dec 7, 2022
This is really interesting feedback coming from the pandas side and would help market "why should I use pyarrow" in pandas too.
pandas (I) originally grouped the arrow types with the pandas (numpy based) nullable dtypes since they share the common and original motivation for the nullable dtypes - a dtype independent way to represent missing values. Hence why in pandas we also want to let to user choose arrow via nullable-related keywords e.g.
use_nullable_dtypesThe arrow types also happen to generally be more performant too so maybe they should be associated with more performance based keywords? e.g.
enginemroeschke commentedon Dec 7, 2022
@jrbourbeau I would consider this a bug. If it's easy to get a simple reproducer, it would be great to have as a pandas issue
mrocklin commentedon Dec 7, 2022
If pandas is using the term
nulalblefor this kind of thing then I'm happy to back off from a Dask dataframe perspective. We generally just follow whatever the upstream library does. Please ignore my feedback within the context of this PR.As a pandas user though, this does feel strange to me though. I understand how we got to "nullable" as a term here, but it doesn't feel right to me as someone who has just arrived at this situation.
jrbourbeau commentedon Dec 7, 2022
Opened pandas-dev/pandas#50099
I feel like there's a banana split joke to be had here, I just can find it... That said, it's a fair point.
@mroeschke how hardened is the relationship between "nullable" and the
numpy- andpyarrow-backed extension dtypes inpandas? I knowuse_nullable_dtypeshas been out sincepandas=1.2.0, but that's the only user-facing API I'm aware of. Given thenullable_backendoption hasn't been released yet (and is also going through a name change at the moment anyways), do you think there's another term that would fit user expectations more?engine,dtype_backend, anddtype_enginecome to mind for me -- @mrocklin do you have a term that you like for this concept?mrocklin commentedon Dec 7, 2022
dtypes="arrow"? I don't really know enough context to have a good idea here.Also, if we're ok moving the default around in the future then I care much less. It seems like arrow support is pretty good these days. I'm hopeful that the developers and experts will use it, see that it works well, and then we'll switch the defaults around. The devs and experts won't care much about the name.
mroeschke commentedon Dec 12, 2022
As of 1.5 not really hardened. Their only association is just documenting that they both use
pd.NAto (externally) represent missing values. I'll bring this topic up on our next community dev meeting on Wednesdayjrbourbeau commentedon Dec 12, 2022
Thanks @mroeschke. I saw online that meeting is open to the public, so I might drop by to say hi and represent Dask folks there
mroeschke commentedon Dec 13, 2022
Awesome! I put it on the meeting agenda so would be great to hear a representative pitch from the Dask side