-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
PDEP-14: Dedicated string data type for pandas 3.0 #58551
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
Changes from 2 commits
fbeb69d
f03f54d
561de87
86f4e51
30c7b43
54a43b3
5b5835b
9ede2e6
f5faf4e
f554909
ac2d21a
82027d2
5b24c24
f9c55f4
2c58c4c
0a68504
8974c5b
cca3a7f
d24a80a
9c5342a
b5663cc
1c4c2d9
c44bfb5
af5ad3c
bd52f39
f8fbc61
d78462d
4de20d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,247 @@ | ||||||||||||||
# PDEP-XX: Dedicated string data type for pandas 3.0 | ||||||||||||||
|
||||||||||||||
- Created: May 3, 2024 | ||||||||||||||
- Status: Under discussion | ||||||||||||||
- Discussion: | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see no reason not to use #57073 as the discussion issue as any further discussion will be here and #57073 can now focus on whether to reject PDEP-10 and what to do about the planned improvements to other dtypes. My assumption is that approval of this PDEP should not, in itself, be a justification to overturn the PDEP-10 decision even though they are very much related and the implementation of the fallback option is only applicable if PDEP-10 is formally rejected. |
||||||||||||||
- Author: [Joris Van den Bossche](https://github.com/jorisvandenbossche) | ||||||||||||||
- Revision: 1 | ||||||||||||||
|
||||||||||||||
## Abstract | ||||||||||||||
|
||||||||||||||
This PDEP proposes to introduce a dedicated string dtype that will be used by | ||||||||||||||
default in pandas 3.0: | ||||||||||||||
|
||||||||||||||
* In pandas 3.0, enable a "string" dtype by default, using PyArrow if available | ||||||||||||||
or otherwise the numpy object-dtype alternative. | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should you allow the possability of a NumPy 2 improved type for pandas 3? With a heirarchy arrow -> np 2 -> np object? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This proposal does not preclude any further improvements for the numpy-based string dtype using numpy 2.0. A few lines below I explicitly mention it as a future improvement and in the "Object-dtype "fallback" implementation" section as well. I just don't want to explicitly commit to anything for pandas 3.0 related to that, given it is hard to judge right now how well it will work / how much work it is to get it ready (not only our own implementation, but also support in the rest of the ecosystem). If it is ready by 3.0, then we can evaluate that separately, but this proposal doesn't stand or fall with it. Regardless of whether to also use numpy 2.0, we have to agree on 1) making a "string" dtype the default for 3.0, 2) the missing value behaviour to use for this dtype, and 3) whether to provide an alternative for PyArrow (in which case we need the object-dtype version anyway since we also can't require numpy 2.0). I would like the proposal to focus on those aspects. |
||||||||||||||
* The default string dtype will use missing value semantics (using NaN) consistent | ||||||||||||||
with the other default data types. | ||||||||||||||
|
||||||||||||||
This will give users a long-awaited proper string dtype for 3.0, while 1) not | ||||||||||||||
(yet) making PyArrow a _hard_ dependency, but only a dependency used by default, | ||||||||||||||
and 2) leaving room for future improvements (different missing value semantics, | ||||||||||||||
using NumPy 2.0, etc). | ||||||||||||||
|
||||||||||||||
# Dedicated string data type for pandas 3.0 | ||||||||||||||
|
||||||||||||||
## Background | ||||||||||||||
|
||||||||||||||
Currently, pandas by default stores text data in an `object`-dtype NumPy array. | ||||||||||||||
The current implementation has two primary drawbacks. First, `object` dtype is | ||||||||||||||
not specific to strings: any Python object can be stored in an `object`-dtype | ||||||||||||||
array, not just strings, and seeing `object` as the dtype for a column with | ||||||||||||||
strings is confusing for users. Second: this is not efficient (all string | ||||||||||||||
methods on a Series are eventually calling Python methods on the individual | ||||||||||||||
string objects). | ||||||||||||||
|
||||||||||||||
To solve the first issue, a dedicated extension dtype for string data has | ||||||||||||||
already been | ||||||||||||||
[added in pandas 1.0](https://pandas.pydata.org/docs/whatsnew/v1.0.0.html#dedicated-string-data-type). | ||||||||||||||
This has always been opt-in for now, requiring users to explicitly request the | ||||||||||||||
dtype (with `dtype="string"` or `dtype=pd.StringDtype()`). The array backing | ||||||||||||||
this string dtype was initially almost the same as the default implementation, | ||||||||||||||
i.e. an `object`-dtype NumPy array of Python strings. | ||||||||||||||
|
||||||||||||||
To solve the second issue (performance), pandas contributed to the development | ||||||||||||||
of string kernels in the PyArrow package, and a variant of the string dtype | ||||||||||||||
backed by PyArrow was | ||||||||||||||
[added in pandas 1.3](https://pandas.pydata.org/docs/whatsnew/v1.3.0.html#pyarrow-backed-string-data-type). | ||||||||||||||
This could be specified with the `storage` keyword in the opt-in string dtype | ||||||||||||||
(`pd.StringDtype(storage="pyarrow")`). | ||||||||||||||
|
||||||||||||||
Since its introduction, the `StringDtype` has always been opt-in, and has used | ||||||||||||||
the experimental `pd.NA` sentinel for missing values (which was also [introduced | ||||||||||||||
in pandas 1.0](https://pandas.pydata.org/docs/whatsnew/v1.0.0.html#experimental-na-scalar-to-denote-missing-values)). | ||||||||||||||
However, up to this date, pandas has not yet taken the step to use `pd.NA` by | ||||||||||||||
default, and thus the `StringDtype` deviates in missing value behaviour compared | ||||||||||||||
to the default data types. | ||||||||||||||
|
||||||||||||||
In 2023, [PDEP-10](https://pandas.pydata.org/pdeps/0010-required-pyarrow-dependency.html) | ||||||||||||||
proposed to start using a PyArrow-backed string dtype by default in pandas 3.0 | ||||||||||||||
(i.e. infer this type for string data instead of object dtype). To ensure we | ||||||||||||||
could use the variant of `StringDtype` backed by PyArrow instead of Python | ||||||||||||||
objects (for better performance), it proposed to make `pyarrow` a new required | ||||||||||||||
runtime dependency of pandas. | ||||||||||||||
|
||||||||||||||
In the meantime, NumPy has also been working on a native variable-width string | ||||||||||||||
data type, which will be available [starting with NumPy | ||||||||||||||
2.0](https://numpy.org/devdocs/release/2.0.0-notes.html#stringdtype-has-been-added-to-numpy). | ||||||||||||||
This can provide a potential alternative to PyArrow for implementing a string | ||||||||||||||
data type in pandas that is not backed by Python objects. | ||||||||||||||
|
||||||||||||||
After acceptance of PDEP-10, two aspects of the proposal have been under | ||||||||||||||
reconsideration: | ||||||||||||||
|
||||||||||||||
- Based on user feedback, it has been considered to relax the new `pyarrow` | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth mentioning why this has been objected to? As far as I am aware virtually all objections are due to the installation size effect, and not performance or compatibility. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can certainly mention something, but would prefer to keep that brief to focus here on the strings context and not trigger discussion here about the merits of those objections. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added "(mostly around installation complexity and size)" |
||||||||||||||
requirement to not be a _hard_ runtime dependency. In addition, NumPy 2.0 can | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think NumPy 2.0 will reduce the need to make pyarrow a dependency for strings; as far as I am aware it is not natively returned by any I/O operation and it has a completely different string architecture than pyarrow, so there is no zero-copy capability. Those seem like they either will require a large amount of string copying or a hefty amount of updates to make it natively work with our I/O, as well as with the larger Arrow ecosystem. That's a huge amount of things to gloss over There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think it can do that if your motivation for wanting pyarrow is the better performance compared to object-dtype. In that case, numpy 2.0's StringDType can give you a part of the speedup, without requiring pyarrow. But you are completely right that there are a lot of things that would need to be implemented to make it fully usable for us. That's also the reason that this PDEP does not say to use numpy 2.0, but defers that as a possible future enhancement, to discuss later. And you are also right that it has drawbacks compared to a Arrow based solution (using Arrow memory layout, but not necessary using pyarrow the package), another reason for me personally to again defer that to a separate discussion. I just wanted to mention it for the complete context of the string dtype history and discussion. Now, I already mention its existence in the previous paragraph, so could keep it shorter here. |
||||||||||||||
potentially reduce the need to make PyArrow a required dependency specifically | ||||||||||||||
for a dedicated pandas string dtype. | ||||||||||||||
- The PDEP did not consider the usage of the experimental `pd.NA` as a | ||||||||||||||
consequence of adopting one of the existing implementations of the | ||||||||||||||
`StringDtype`. | ||||||||||||||
|
||||||||||||||
For the second aspect, another variant of the `StringDtype` was | ||||||||||||||
[introduced in pandas 2.1](https://pandas.pydata.org/docs/whatsnew/v2.1.0.html#whatsnew-210-enhancements-infer-strings) | ||||||||||||||
that is still backed by PyArrow but follows the default missing values semantics | ||||||||||||||
pandas uses for all other default data types (and using `NaN` as the missing | ||||||||||||||
value sentinel) ([GH-54792](https://github.com/pandas-dev/pandas/issues/54792)). | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The I think this is also important to mention. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point, I haven't yet mentioned that the original StringDtype returns masked arrays from operations (only that it uses |
||||||||||||||
At the time, the `storage` option for this new variant was called | ||||||||||||||
`"pyarrow_numpy"` to disambiguate from the existing `"pyarrow"` option using `pd.NA`. | ||||||||||||||
|
||||||||||||||
This last dtype variant is what you currently (pandas 2.2) get for string data | ||||||||||||||
when enabling the ``future.infer_string`` option (to enable the behaviour which | ||||||||||||||
is intended to become the default in pandas 3.0). | ||||||||||||||
|
||||||||||||||
## Proposal | ||||||||||||||
|
||||||||||||||
To be able to move forward with a string data type in pandas 3.0, this PDEP proposes: | ||||||||||||||
|
||||||||||||||
1. For pandas 3.0, we enable a "string" dtype by default, which will use PyArrow | ||||||||||||||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
if installed, and otherwise falls back to an in-house functionally-equivalent | ||||||||||||||
mroeschke marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
(but slower) version. | ||||||||||||||
Dr-Irv marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
2. This default "string" dtype will follow the same behaviour for missing values | ||||||||||||||
as our other default data types, and use `NaN` as the missing value sentinel. | ||||||||||||||
3. The version that is not backed by PyArrow can reuse the existing numpy | ||||||||||||||
object-dtype backed StringArray for its implementation. | ||||||||||||||
simonjayhawkins marked this conversation as resolved.
Show resolved
Hide resolved
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
4. We update installation guidelines to clearly encourage users to install | ||||||||||||||
pyarrow for the default user experience. | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and do we consider adding a performance warning to the fallback also? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I personally wouldn't do that always / for each method, because that would be super noisy (and in some cases, like smallish data, it doesn't matter that much, so getting those warnings would be annoying). If we wanted to warn users to gently push them towards installing pyarrow, I think we could do a warning but only 1) raise it once, and 2) only when doing one of the string operations on a big enough dataset (with some threshold). Now, your question reminds me that the current pyarrow-backed string dtype has those fallback warnings for very specific cases, which I personally think we should stop doing when it becomes the default dtype. Given this is already for the existing implementation (and to keep the many discussion lines here a bit more limited), I opened a separate issue for this: #58581. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair point. from the recent user feedback of adding the deprecation warning for the PyArrow requirement, then maybe not having any warnings is wise.
+1 |
||||||||||||||
|
||||||||||||||
### Default inference of a string dtype | ||||||||||||||
|
||||||||||||||
By default, pandas will infer this new string dtype for string data (when | ||||||||||||||
creating pandas objects, such as in constructors or IO functions). | ||||||||||||||
|
||||||||||||||
The existing `future.infer_string` option can be used to opt-in to the future | ||||||||||||||
default behaviour: | ||||||||||||||
|
||||||||||||||
```python | ||||||||||||||
>>> pd.options.future.infer_string = True | ||||||||||||||
>>> pd.Series(["a", "b", None]) | ||||||||||||||
0 a | ||||||||||||||
1 b | ||||||||||||||
2 NaN | ||||||||||||||
dtype: string | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
This option will be expanded to also work when PyArrow is not installed. | ||||||||||||||
|
||||||||||||||
### Missing value semantics | ||||||||||||||
|
||||||||||||||
Given that all other default data types use NaN semantics for missing values, | ||||||||||||||
this proposal says that a new default string dtype should still use the same | ||||||||||||||
default semantics. Further, it should result in default data types when doing | ||||||||||||||
operations on the string column that result in a boolean or numeric data type | ||||||||||||||
(e.g., methods like `.str.startswith(..)` or `.str.len(..)`, or comparison | ||||||||||||||
operators like `==`, should result in default `int64` and `bool` data types). | ||||||||||||||
|
||||||||||||||
Because the original `StringDtype` implementations already use `pd.NA` and | ||||||||||||||
return masked integer and boolean arrays in operations, a new variant of the | ||||||||||||||
existing dtypes that uses `NaN` and default data types is needed. | ||||||||||||||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
### Object-dtype "fallback" implementation | ||||||||||||||
|
||||||||||||||
To avoid a hard dependency on PyArrow for pandas 3.0, this PDEP proposes to keep | ||||||||||||||
a "fallback" option in case PyArrow is not installed. The original `StringDtype` | ||||||||||||||
backed by a numpy object-dtype array of Python strings can be used for this, and | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to clarify that this is a separate dtype from the original There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to clarify in the test that it is indeed a new variant of the string dtype and uses a subclass to reuse most code |
||||||||||||||
only need minor updates to follow the above-mentioned missing value semantics | ||||||||||||||
([GH-58451](https://github.com/pandas-dev/pandas/pull/58451)). | ||||||||||||||
|
||||||||||||||
For pandas 3.0, this is the most realistic option given this implementation is | ||||||||||||||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
already available for a long time. Beyond 3.0, we can still explore further | ||||||||||||||
improvements such as using nanoarrow or NumPy 2.0, but at that point that is an | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would drop this bit about nanoarrow (given it is not explained/introduced in the paragraphs beforehand). If you want to add an explanation above, that's also fine with me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a link to the discussion issues for both numpy 2.0 and nanoarrow, so people can find more explanation there if they want. |
||||||||||||||
implementation detail that should not have a direct impact on users (except for | ||||||||||||||
performance). | ||||||||||||||
|
||||||||||||||
### Naming | ||||||||||||||
|
||||||||||||||
Given the long history of this topic, the naming of the dtypes is a difficult | ||||||||||||||
topic. | ||||||||||||||
|
||||||||||||||
In the first place, we need to acknowledge that most users should not need to | ||||||||||||||
use storage-specific options. Users are expected to specify `pd.StringDtype()` | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we are reusing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, and that is what already happens since pandas 2.1 with
Yes, I mentioned that in the "Backwards compatibility" section There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah thanks - sorry for overlooking that. So I think it goes without saying then that if we go this route we no longer will declare There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yep, given the proposal is to enable this by default, I think that is indeed saying to remove the experimental label (I can mention that somewhere explicitly if that helps)
Once we have a |
||||||||||||||
or `"string"`, and that will give them their default string dtype (which | ||||||||||||||
depends on whether PyArrow is installed or not). | ||||||||||||||
|
||||||||||||||
But for testing purposes and advanced use cases that want control over this, we | ||||||||||||||
need some way to specify this and distinguish them from the other string dtypes. | ||||||||||||||
Currently, the `StringDtype(storage="pyarrow_numpy")` is used, where | ||||||||||||||
"pyarrow_numpy" is a rather confusing option. | ||||||||||||||
|
||||||||||||||
TODO see if we can come up with a better naming scheme | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If i'm understanding correctly about the motivation for the change in dtype (improved overall user experience), then moving forward I suspect that when we can have improved/native dtypes for other data types (nested, date, etc) that the same logic would need to apply, i.e. we would need to have a variants of these with NumPy semantics. Now this probably falls under PDEP-13 but if we have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
or maybe "nullable=[True|False]" However, at the moment, we distinguish the nullable data types for the other dtypes (int, float, etc) with capitalization and so for consistency could also consider string/String as the dtypes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PDEP-13 proposes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@jbrockmendel good point that we can also use other keywords than just
Only if users explicitly specify a non-default value for this, and never by default. This is the same with whatever option we come up with (eg also when using
Yeah, only unfortunately to be consistent with the other dtypes where we use capitalization, it would need to be To keep the sub-discussions manageable, I moved this specific topic out of this inline comment thread, and into it's own issue: #58613 |
||||||||||||||
|
||||||||||||||
## Alternatives | ||||||||||||||
|
||||||||||||||
### Why not delay introducing a default string dtype? | ||||||||||||||
|
||||||||||||||
To avoid introducing a new string dtype while other discussions and changes are | ||||||||||||||
in flux (eventually making pyarrow a required dependency? adopting `pd.NA` as | ||||||||||||||
the default missing value sentinel? using the new NumPy 2.0 capabilities?), we | ||||||||||||||
could also delay introducing a default string dtype until there is more clarity | ||||||||||||||
in those other discussions. | ||||||||||||||
|
||||||||||||||
However: | ||||||||||||||
|
||||||||||||||
1. Delaying has a cost: it further postpones introducing a dedicated string | ||||||||||||||
dtype that has massive benefits for our users, both in usability as (for the | ||||||||||||||
significant part of the user base that has PyArrow installed) in performance. | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we can just claim this. I don't disagree, but this should be backed up more. At least from the feedback received from #57073 and the other issue, there's at least a significant part of the user base that doesn't use strings. There's also a significant chunk of the population that can't install pyarrow (due to size requirements or exotic platforms or whatever). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure this argument is that convincing either, although for slightly different reasons. I don't think we need to feel rushed for the next release There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@lithomas1 can you clarify which part of the paragraph you think requires more backing up? I don't think we can ever know exact numbers for this, but one data point is that pandas currently has 210M monthly downloads and pyarrow has 120M monthly downloads. Of course not all of those pyarrow users are also using pandas, but let's just assume that half of those pyarrow downloads come from people using pandas, that would mean that around 30% for our users already have pyarrow installed, which I would consider as a "significant part". But anyway, we are never going to know this exact number, but IMO we do know that a significant part of our userbase has pyarrow and will benefit from using that by default.
Yes, and then this PDEP is not relevant for them. But it's not because some users don't use strings, that we shouldn't improve the life of those users that do use strings? (so just not really understanding how this is a relevant argument)
Yes, and this PDEP addresses that by allowing a fallback when pyarrow is not installed.
@WillAyd can you then clarify which other reasons? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My other reason is that I don't think there is ever a rush to get a release out; we have historically never operated that way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For the last six years, we have roughly released a new feature release every six months. We indeed never rush a specific release if there is something holding it up for a bit, but historically we have been releasing somewhat regularly. At this point, a next feature release will be 3.0 given the amount of changes we already made on the main branch that require the next release cut from main to be 3.0 and not 2.3 (enforced deprecations etc). So I would say there is not necessarily a rush to do a release with a default "string" dtype (that is up for debate, i.e. this PDEP), but there is some rush to get a 3.0 release out. In the meaning that I think we don't want to delay 3.0 for like half a year or longer. So for me delaying the string dtype, essentially means not including it in 3.0 but postponing it to pandas 4.0 (I should maybe be clearer in the paragraph above about that). And then I try to argue in the text here that postponing it for 4.0 has a cost (or, missed benefit), because we have an implementation we could use for a default string dtype in pandas 3.0, and postponing introducing it makes that users will use the sub-optimal object dtype for longer, for (IMO) no good reason. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It'd be nice to add how much perf benefits Arrow strings are expected to bring (e.g. 20%? 2x? 10x?). It'd also be good to elaborate on the usability part. IIUC, the main benefit here is not having to manually check element to see whether your object dtype'd column contains strings (since I think all the string methods work on object dtype'd columns). I think it's also fair to amend this part to say "massive benefits to users that use strings" (instead of in general). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Benchmarks are going to be highly dependent on usage and context. If working in an Arrow native ecosystem, the speedup of strings may be a factor over 100x. If working in a space where you have to copy back and forth a lot with NumPy, that number goes way down. I think trying to set expectations on one number / benchmark for performance is futile, but generally Arrow only helps, and makes it so that we as developers don't need to write custom I/O solutions (eg: ADBC Drivers, parquet, read_csv with pyarrow all work with Arrow natively with no extra pandas dev effort) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Indeed, for single operations you can easily get a >10x speedup, but of course a typical workflow does not consist of just string operations, and the overall speedup depends a lot (see this slide for one small example comparison (https://phofl.github.io/pydata-berlin/pydata-berlin-2023/intro.html#74) and this blogpost from Patrick showing the benefit in a dask example workflow (https://towardsdatascience.com/utilizing-pyarrow-to-improve-pandas-and-dask-workflows-2891d3d96d2b).
That is often true, but except for strings ;). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
2. In case we eventually transition to use `pd.NA` as the default missing value | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I might be missing the intent but I don't understand why the larger issue of NA handling means we should be faster to implement this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's not a reason to do it "faster", but I meant to say that the discussion regarding NA is not a reason to do it "slower" (to delay introducing a dedicated string dtype) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the flip side is that if we aren't careful about the NA handling we can introduce some new keywords / terminology that makes it very confusing in the long run (which is essentially one of the problems with our strings naming conventions) As a practical example, if we decided we wanted (not saying the above is necessarily the truth, just cherry picking from conversation so far) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's one reason that I personally would prefer not introducing a keyword specifically for the missing value semantics, for now (just for this PDEP / the string dtype). I just listed some options in #58613, and I think we can do without it. |
||||||||||||||
sentinel, we will need a migration path for _all_ our data types, and thus | ||||||||||||||
the challenges around this will not be unique to the string dtype and | ||||||||||||||
therefore not a reason to delay this. | ||||||||||||||
simonjayhawkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
### Why not use the existing StringDtype with `pd.NA`? | ||||||||||||||
|
||||||||||||||
Wouldn't adding even more variants of the string dtype will make things only more | ||||||||||||||
confusing? Indeed, this proposal unfortunately introduces more variants of the | ||||||||||||||
string dtype. However, the reason for this is to ensure the actual default user | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This just retroactively clarifies the reasoning for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's indeed explaining why we did this, which is of course "retroactively" given I was asked to write this PDEP partly for changes that have already been released. So a big part of the PDEP is retroactively in that sense (which it not necessarily helping to write it clearly ..). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
however, more importantly, the PDEP makes this (the already added dtype) the default in 3.0. It would remain behind the future flag for the next release if enough people feel we are not ready. |
||||||||||||||
experience is _less_ confusing, and the new string dtype fits better with the | ||||||||||||||
other default data types. | ||||||||||||||
|
||||||||||||||
If the new default string data type would use `pd.NA`, then after some | ||||||||||||||
operations, a user can easily end up with a DataFrame that mixes columns using | ||||||||||||||
`NaN` semantics and columns using `NA` semantics (and thus a DataFrame that | ||||||||||||||
could have columns with two different int64, two different float64, two different | ||||||||||||||
bool, etc dtypes). This would lead to a very confusing default experience. | ||||||||||||||
|
||||||||||||||
With the proposed new variant of the StringDtype, this will ensure that for the | ||||||||||||||
_default_ experience, a user will only see only 1 kind of integer dtype, only | ||||||||||||||
kind of 1 bool dtype, etc. For now, a user should only get columns with an | ||||||||||||||
`ArrowDtype` and/or using `pd.NA` when explicitly opting into this. | ||||||||||||||
|
||||||||||||||
## Backward compatibility | ||||||||||||||
|
||||||||||||||
The most visible backwards incompatible change will be that columns with string | ||||||||||||||
data will no longer have an `object` dtype. Therefore, code that assumes | ||||||||||||||
`object` dtype (such as `ser.dtype == object`) will need to be updated. | ||||||||||||||
|
||||||||||||||
To allow testing your code in advance, the | ||||||||||||||
`pd.options.future.infer_string = True` option is available. | ||||||||||||||
|
||||||||||||||
Otherwise, the actual string-specific functionality (such as the `.str` accessor | ||||||||||||||
methods) should all keep working as is. By preserving the current missing value | ||||||||||||||
semantics, this proposal is also backwards compatible on this aspect. | ||||||||||||||
|
||||||||||||||
One other backwards incompatible change is present for early adopters of the | ||||||||||||||
existing `StringDtype`. In pandas 3.0, calling `pd.StringDtype()` will start | ||||||||||||||
returning the new default string dtype, while up to now this returned the | ||||||||||||||
experimental string dtype using `pd.NA` introduced in pandas 1.0. Those users | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Historically you would get this by using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has been available (as pyarrow backed) since 1.3, so almost three years (July 2, 2021). Even though considered experimental, if the new string dtype is not accepted for 3.0, then maybe a deprecation warning should be added? (We could also do this if decided a 2.3 release is needed?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A deprecation warning about what exactly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The scope of changing NaN to NA for all users is much bigger though (essentially what was decided in PDEP-10 if we would follow it strictly to the letter). I know that this is not necessarily a good argument to justify this breaking change (because we certainly should be wary of the scope of those breaking changes), but I do want to point out again that the choice in this PDEP to use NaN semantics is to reduce the scope of the breaking changes for most users (at the expense of increasing the scope of breaking changes for the smaller subset of users that was already using If we don't want to make And personally I think "string" is by far the best name (and I find the small breakage worth it for being able to use that name), and as I argued elsewhere (and in the Why not delay introducing a default string dtype? section in the PDEP text), I think it is valuable for our users to not wait with adding a dedicated string dtype until we are ready with the NA discussion and implementation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is where I am a little uncomfortable - I don't know how to measure the size of that, but I am wary of assuming it is not a signifcant number of users. The fact that "string" returns NA as a missing value is a documented difference in our code base: https://pandas.pydata.org/docs/dev/user_guide/text.html#behavior-differences And its usage has been promoted for quite some time: https://stackoverflow.com/a/60553529/621736
Yea none of these options are great...but out of them I still would probably prefer waiting. I think right now we are marching down a path of "string" missing values:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think we have to carefully specify what the user specifies in a So we could have a mapping that says
The first row depends on whether Separately, we can then debate what the values in the second column should look like in #58613 . I personally am not a fan of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah OK - I didn't realize you were proposing that change be a part of this PDEP, just thought it was an idea you had for the future. But that's a completely new behavior...and then begs the question of do we go back and change dtype=object to have that same behavior or just have dtype="string" exclusively have it. Ultimately we end up with the same issue There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I also agree with Will that it's not fair to change this without warning for people already using "string". Maybe a good compromise would be to use If we were to move ahead with the move to nullable dtypes in general, I worry that this changing of the na value for If we were to do 2.3 (like I suggested below), this might be addressable there (with a deprecation). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still adding some deprecation warnings in 2.x for current users of StringDtype is something we certainly could do. I am personally ambivalent about it, but fine with adding it if others think that is better (I do think it might become quite noisy, and it also does not change the fact that 3.0 would switch from NA to NaN) The warning message could then point people to enable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I created a variant of that table #58613 (comment) with a concrete proposal
(for clarity, this "second" row referred to specifying a dtype with |
||||||||||||||
will need to start specifying a keyword in the dtype constructor if they want to | ||||||||||||||
keep using `pd.NA` (but if they just want to have a dedicated string dtype, they | ||||||||||||||
don't need to change their code). | ||||||||||||||
|
||||||||||||||
## Timeline | ||||||||||||||
|
||||||||||||||
The future PyArrow-backed string dtype was already made available behind a feature | ||||||||||||||
flag in pandas 2.1 (by `pd.options.future.infer_string = True`). | ||||||||||||||
|
||||||||||||||
Some small enhancements or fixes (or naming changes) might still be needed and | ||||||||||||||
can be backported to pandas 2.2.x. | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part of the plan worries me a little. Maybe it would be better to cut off a 2.3 from 2.2.x. I think there's a significant proportion of the downloads for 2.2 that aren't on the latest patch release. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a 2.3 release (maybe around the same time as 3.0rc) sounds reasonable. If the features/bugfixes added to 2.3 are limited to the string dtype then we shouldn't need many patch releases. We may not need to fix any string dtype related issues that are fixed for 3.0 as these will be behind a flag in 2.3 and so shouldn't break existing code. On the other hand, as these features are behind a flag, maybe releasing a 2.3 would not gain the field testing we hope for. And therefore, instead of doing a 2.3, planning for at least a couple of release candidates for 3.0 would better achieve this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thoughts on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, if we still plan to add a deprecation warning and change the naming scheme in |
||||||||||||||
|
||||||||||||||
The variant using numpy object-dtype could potentially also be backported to | ||||||||||||||
2.2.x to allow easier testing. | ||||||||||||||
Dr-Irv marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
For pandas 3.0, this flag becomes enabled by default. | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
## PDEP-XX History | ||||||||||||||
|
||||||||||||||
- 3 May 2024: Initial version |
Uh oh!
There was an error while loading. Please reload this page.